| 1 |
import os |
|---|
| 2 |
import gtk |
|---|
| 3 |
from plugins.events import EventPlugin |
|---|
| 4 |
from qltk.entry import ValidatingEntry |
|---|
| 5 |
from qltk import Button, Message, ErrorMessage |
|---|
| 6 |
import util, config, widgets |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
info_use=_(""" |
|---|
| 10 |
<b>Command parameters:</b> |
|---|
| 11 |
The following command parameters will be |
|---|
| 12 |
substituted only when is launched the command |
|---|
| 13 |
of a start/end song: |
|---|
| 14 |
<b>%f</b> the path of the sound file |
|---|
| 15 |
<b>%d</b> directory of the sound file |
|---|
| 16 |
<b>%n</b> filename (without path) |
|---|
| 17 |
|
|---|
| 18 |
<b>Examples:</b> |
|---|
| 19 |
echo starting... %f |
|---|
| 20 |
find %d -iname *.ogg -print |
|---|
| 21 |
find %d -iname *.ogg -fprint ogg_list.txt |
|---|
| 22 |
cp %f /data/listened/%n |
|---|
| 23 |
xterm |
|---|
| 24 |
firefox http://www.python.org |
|---|
| 25 |
sudo halt |
|---|
| 26 |
""") |
|---|
| 27 |
|
|---|
| 28 |
def iscommand(s): |
|---|
| 29 |
"""util function modified""" |
|---|
| 30 |
|
|---|
| 31 |
s=s.strip() |
|---|
| 32 |
if s == "": return False |
|---|
| 33 |
else: |
|---|
| 34 |
s = s.split()[0] |
|---|
| 35 |
if os.path.sep in s: |
|---|
| 36 |
return (os.path.isfile(s) and os.access(s, os.X_OK)) |
|---|
| 37 |
for p in os.defpath.split(os.path.pathsep): |
|---|
| 38 |
p2 = os.path.join(p, s) |
|---|
| 39 |
if (os.path.isfile(p2) and os.access(p2, os.X_OK)): |
|---|
| 40 |
return True |
|---|
| 41 |
else: return False |
|---|
| 42 |
|
|---|
| 43 |
class CommEvent(EventPlugin): |
|---|
| 44 |
PLUGIN_ID = "Command Event" |
|---|
| 45 |
PLUGIN_NAME = _("Command Event") |
|---|
| 46 |
d=_("Execute a command when a song starts or/and ends or/and music stops.") |
|---|
| 47 |
PLUGIN_DESC=(d) |
|---|
| 48 |
PLUGIN_VERSION = "0.1" |
|---|
| 49 |
|
|---|
| 50 |
def __init__(self): |
|---|
| 51 |
self.first_time=True |
|---|
| 52 |
|
|---|
| 53 |
def PluginPreferences(self, parent): |
|---|
| 54 |
self.parent=parent |
|---|
| 55 |
vb = gtk.VBox(spacing=5) |
|---|
| 56 |
|
|---|
| 57 |
text=_("When a song starts:") |
|---|
| 58 |
self._create_preferences(text,vb,"command_event_start") |
|---|
| 59 |
|
|---|
| 60 |
text=_("When a song ends:") |
|---|
| 61 |
self._create_preferences(text,vb,"command_event_end") |
|---|
| 62 |
|
|---|
| 63 |
text=_("When music stops automatically\n (not when paused):") |
|---|
| 64 |
self._create_preferences(text,vb,"command_event_stop_all") |
|---|
| 65 |
info = Button(_("Show Info"), gtk.STOCK_INFO) |
|---|
| 66 |
info.connect('clicked', self._show_info) |
|---|
| 67 |
vb.pack_start(info, expand=False) |
|---|
| 68 |
vb.show_all() |
|---|
| 69 |
return vb |
|---|
| 70 |
|
|---|
| 71 |
def plugin_on_song_ended(self, song, stopped): |
|---|
| 72 |
if stopped==False: self._command("command_event_end",song) |
|---|
| 73 |
return None |
|---|
| 74 |
|
|---|
| 75 |
def plugin_on_song_started(self, song): |
|---|
| 76 |
if self.first_time==True: |
|---|
| 77 |
self.first_time=False |
|---|
| 78 |
return None |
|---|
| 79 |
if song==None: |
|---|
| 80 |
self._command("command_event_stop_all") |
|---|
| 81 |
else: |
|---|
| 82 |
self._command("command_event_start",song) |
|---|
| 83 |
return None |
|---|
| 84 |
|
|---|
| 85 |
def _command(self,id_command,song=None): |
|---|
| 86 |
try: command = config.get("plugins", id_command) |
|---|
| 87 |
except: command="" |
|---|
| 88 |
if command=="": return None |
|---|
| 89 |
command_list=command.split() |
|---|
| 90 |
if song!=None: |
|---|
| 91 |
p=song._song['~filename'] |
|---|
| 92 |
for i in range(len(command_list)): |
|---|
| 93 |
item=command_list[i] |
|---|
| 94 |
item=item.replace("%f",p) |
|---|
| 95 |
item=item.replace("%d",os.path.dirname(p)) |
|---|
| 96 |
item=item.replace("%n",os.path.basename(p)) |
|---|
| 97 |
command_list[i]=item |
|---|
| 98 |
try: |
|---|
| 99 |
|
|---|
| 100 |
util.spawn(command_list) |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
except: |
|---|
| 104 |
|
|---|
| 105 |
c = " ".join(command_list) |
|---|
| 106 |
error_msg = _("<small>Command Event (Quod Libet message):</small>\n\ |
|---|
| 107 |
Some error with the command:\n<b><i>%s</i></b>"%util.escape(c)) |
|---|
| 108 |
dialog = ErrorMessage(widgets.main, error_msg, |
|---|
| 109 |
_("In the Command Event Plugin dialog is:\n\ |
|---|
| 110 |
<i>%s</i>"%util.escape(command))) |
|---|
| 111 |
dialog.connect('response', self._destroy_msg) |
|---|
| 112 |
dialog.show() |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
return None |
|---|
| 117 |
|
|---|
| 118 |
def _create_preferences(self,text,vb,name_id): |
|---|
| 119 |
label_info = gtk.Label(text) |
|---|
| 120 |
label = gtk.Label(" "+_("Command:")) |
|---|
| 121 |
entry = ValidatingEntry(iscommand) |
|---|
| 122 |
try: |
|---|
| 123 |
command = config.get("plugins", name_id) |
|---|
| 124 |
entry.set_text(command) |
|---|
| 125 |
except: |
|---|
| 126 |
pass |
|---|
| 127 |
entry.connect('changed', self._entry_changed, name_id) |
|---|
| 128 |
label.set_mnemonic_widget(entry) |
|---|
| 129 |
hb= gtk.HBox() |
|---|
| 130 |
hb.pack_start(label_info, expand=False) |
|---|
| 131 |
hb.pack_start(gtk.Label(" ")) |
|---|
| 132 |
hb2 = gtk.HBox(spacing=3) |
|---|
| 133 |
hb2.pack_start(label, expand=False) |
|---|
| 134 |
hb2.pack_start(entry) |
|---|
| 135 |
vb.pack_start(hb) |
|---|
| 136 |
vb.pack_start(hb2) |
|---|
| 137 |
return None |
|---|
| 138 |
|
|---|
| 139 |
def _entry_changed(self, entry, name_id): |
|---|
| 140 |
self.first_time=False |
|---|
| 141 |
text=entry.get_text() |
|---|
| 142 |
if text.strip()!="": config.set("plugins", name_id, text) |
|---|
| 143 |
else: config.set("plugins", name_id, "") |
|---|
| 144 |
return None |
|---|
| 145 |
|
|---|
| 146 |
def _show_info(self, button): |
|---|
| 147 |
Message(gtk.MESSAGE_INFO, self.parent, _("Info Command Event"), info_use).run() |
|---|
| 148 |
return None |
|---|
| 149 |
|
|---|
| 150 |
def _destroy_msg(self, dialog, response_id): |
|---|
| 151 |
dialog.destroy() |
|---|
| 152 |
return None |
|---|