| 1 |
import os |
|---|
| 2 |
import gtk |
|---|
| 3 |
|
|---|
| 4 |
import shutil |
|---|
| 5 |
|
|---|
| 6 |
from plugins.songsmenu import SongsMenuPlugin |
|---|
| 7 |
|
|---|
| 8 |
class CopyFiles(SongsMenuPlugin): |
|---|
| 9 |
PLUGIN_ID = "Copy Files" |
|---|
| 10 |
PLUGIN_NAME = _("Copy Files") |
|---|
| 11 |
PLUGIN_DESC = _("Copy the selected song list to a directory") |
|---|
| 12 |
PLUGIN_ICON = gtk.STOCK_CONVERT |
|---|
| 13 |
PLUGIN_VERSION = "0.01" |
|---|
| 14 |
|
|---|
| 15 |
def plugin_songs(self, songs): |
|---|
| 16 |
if not songs: return |
|---|
| 17 |
|
|---|
| 18 |
chooser = gtk.FileChooserDialog( |
|---|
| 19 |
title="Copy to directory", |
|---|
| 20 |
action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER, |
|---|
| 21 |
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, |
|---|
| 22 |
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) |
|---|
| 23 |
chooser.set_default_response(gtk.RESPONSE_ACCEPT) |
|---|
| 24 |
resp = chooser.run() |
|---|
| 25 |
|
|---|
| 26 |
if resp != gtk.RESPONSE_ACCEPT: |
|---|
| 27 |
chooser.destroy() |
|---|
| 28 |
return |
|---|
| 29 |
|
|---|
| 30 |
folder = chooser.get_filename() |
|---|
| 31 |
chooser.destroy() |
|---|
| 32 |
|
|---|
| 33 |
msgbox = gtk.MessageDialog( |
|---|
| 34 |
parent=None, |
|---|
| 35 |
flags=0, |
|---|
| 36 |
type=gtk.MESSAGE_INFO, |
|---|
| 37 |
buttons=gtk.BUTTONS_OK, |
|---|
| 38 |
message_format=None) |
|---|
| 39 |
|
|---|
| 40 |
try: |
|---|
| 41 |
for song in songs: |
|---|
| 42 |
self.__copy(song, folder) |
|---|
| 43 |
except Exception, e: |
|---|
| 44 |
msgbox.set_markup("An error occurred: %s" % e) |
|---|
| 45 |
else: |
|---|
| 46 |
msgbox.set_markup("Copy successful") |
|---|
| 47 |
msgbox.run() |
|---|
| 48 |
msgbox.destroy() |
|---|
| 49 |
return |
|---|
| 50 |
|
|---|
| 51 |
def __copy(self, song, folder): |
|---|
| 52 |
source = song['~filename'] |
|---|
| 53 |
filename = song('~basename') |
|---|
| 54 |
try: |
|---|
| 55 |
artist = song['artist'] |
|---|
| 56 |
except: |
|---|
| 57 |
artist = "Unknown" |
|---|
| 58 |
target = os.path.join(folder,artist,filename) |
|---|
| 59 |
try: |
|---|
| 60 |
os.makedirs(os.path.dirname(target)) |
|---|
| 61 |
except: pass |
|---|
| 62 |
try: |
|---|
| 63 |
shutil.copyfile(source,target) |
|---|
| 64 |
except: raise |
|---|
| 65 |
return True |
|---|