#!/usr/bin/env python """This module adds a menu item to the nautilus right-click menu which allows to add all the selected files to the Rhythmbox Play Queue. """ # add-to-rhythmbox.py version 0.2 (Enqueues folders # recursively) # # Copyright 2010 Seemanta Dutta # # Inspired by the original enqueue plugin written for Audacious by # Giuseppe Penone # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import pygtk pygtk.require('2.0') import gtk import nautilus, gconf, urllib, os, sys, subprocess AUDIO_TYPES = ['.mp3','.mpa','.flac','.ogg','.m3u','.wav','.wma','.MP3','.MPA','.FLAC','.OGG','.M3U','.WAV','.WMA'] NAUTILUS_MENU_LABEL = 'Add To rhythmbox Play Queue' NAUTILUS_MENU_HELP = 'Add the selected Audio file(s) to the Rhythmbox Play Queue ' def error_dialog(message, dialog_title = "Error..."): """The extension's error dialog""" dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, message_format=message) dialog.set_title(dialog_title) dialog.run() dialog.destroy() class AddToRhythmBoxPlaylist(nautilus.MenuProvider): """Implements the 'Add To Rhythmbox Playlist' extension to the nautilus right-click menu""" def __init__(self): """Creates a GConfClient (Gnome Configuration Client)""" self.client = gconf.client_get_default() def run(self, menu, source_path_list): """Runs the Adding of selected Audio file(s) to the Rhythmbox Playlist""" try: # First, we need to replace any ' with \' new_source_path_list = [] for str in source_path_list: new_source_path_list.append(str.replace('\"', '\\\"')) # Then just import the file if it is not a part of the library yet bash_string = "rhythmbox-client \"" + "\" \"".join(new_source_path_list) + "\" &" subprocess.call(bash_string, shell=True) # Now, enqueue it into the Play-Queue of rhythmbox bash_string = "rhythmbox-client --enqueue \"" + "\" \"".join(new_source_path_list) + "\" &" subprocess.call(bash_string, shell=True) except: error_dialog("Exception Error:\n%s" % sys.exc_info()) def listFiles(self, dir, all_files): basedir = dir subdirlist = [] for item in os.listdir(dir): if os.path.isfile(os.path.join(basedir,item)): if os.path.splitext(item)[1] in AUDIO_TYPES: all_files.append(os.path.join(basedir,item)) else: subdirlist.append(os.path.join(basedir, item)) for subdir in subdirlist: self.listFiles(subdir, all_files) def get_file_items(self, window, sel_items): """Adds the 'Add To Rhythmbox Playlist' menu item to the Nautilus right-click menu, connects its 'activate' signal to the 'run' method passing the list of selected Audio items""" source_path_list = [] files = [] if sel_items: for sel_item in sel_items: if sel_item.is_directory(): dir_name = urllib.unquote(sel_item.get_uri()[7:]) self.listFiles(dir_name, source_path_list) else: source_path = urllib.unquote(sel_item.get_uri()[7:]) if os.path.splitext(source_path)[1] in AUDIO_TYPES: source_path_list.append(source_path) if source_path_list: item = nautilus.MenuItem('NautilusPython::rhythmbox', NAUTILUS_MENU_LABEL, NAUTILUS_MENU_HELP) item.set_property('icon', 'rhythmbox') item.connect('activate', self.run, source_path_list) return item,