common: split MetadataFetcher into its own file
[nephilim.git] / nephilim / plugins / Systray.py
blob2411309a5eb0a52f53754a04694d10a43ab002f5
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtGui, QtCore
21 from ..plugin import Plugin
22 from ..common import sec2min, APPNAME, appIcon, expand_tags
23 from .. import icons
25 class Systray(Plugin):
26 # public, const
27 info = 'Provides the systray icon.'
29 # public, read-only
30 o = None
32 # private
33 format = None
34 eventObj = None
35 DEFAULTS = {'format': '${track} - ${title} by ${artist} on ${album} (${length})'}
37 def _load(self):
38 self.format = self.settings.value(self.name + '/format')
39 class SystrayWheelEventObject(QtCore.QObject):
40 """This class listens for systray-wheel events"""
41 def eventFilter(self, object, event):
42 if type(event)==QtGui.QWheelEvent:
43 numDegrees=event.delta() / 8
44 numSteps=5*numDegrees/15
45 self.plugin.mpclient.set_volume(self.plugin.mpclient.status['volume'] + numSteps)
46 event.accept()
47 return True
48 return False
50 self.o = QtGui.QSystemTrayIcon(QtGui.QIcon(appIcon), self.parent())
51 self.eventObj=SystrayWheelEventObject()
52 self.eventObj.plugin = self
53 self.o.installEventFilter(self.eventObj)
54 self.o.activated.connect(self.onSysTrayClick)
56 self.mpclient.song_changed.connect(self.update)
57 self.o.show()
58 def _unload(self):
59 self.mpclient.song_changed.disconnect(self.update)
60 self.o.hide()
61 self.o.setIcon(QtGui.QIcon(None))
62 self.o = None
63 self.parent()._wheelEvent = None
65 def update(self):
66 status = self.mpclient.status
67 if not status:
68 return
70 values = {'state':''}
71 values['state']={'play':'playing', 'stop':'stopped', 'pause':'paused'}[status['state']]
72 if 'time' in status:
73 values['length'] = sec2min(status['time'][0])
74 values['time'] = sec2min(status['time'][1])
76 song = self.mpclient.cur_song
77 if song:
78 self.o.setToolTip(expand_tags(self.format, (song,)))
79 else:
80 self.o.setToolTip('%s not playing'%APPNAME)
82 def onSysTrayClick(self, reason):
83 if reason == QtGui.QSystemTrayIcon.Trigger or\
84 reason == QtGui.QSystemTrayIcon.Context:
85 w = self.parent()
86 if w.isVisible():
87 w.setVisible(False)
88 else:
89 w.setVisible(True)
90 elif reason == QtGui.QSystemTrayIcon.MiddleClick:
91 if self.mpclient.is_playing():
92 self.mpclient.pause()
93 else:
94 self.mpclient.resume()
96 class SettingsWidgetSystray(Plugin.SettingsWidget):
97 format = None
99 def __init__(self, plugin):
100 Plugin.SettingsWidget.__init__(self, plugin)
102 self.format = QtGui.QLineEdit(self.settings.value(self.plugin.name + '/format'))
104 self.setLayout(QtGui.QVBoxLayout())
105 self._add_widget(self.format, 'Tooltip format')
107 def save_settings(self):
108 self.settings.beginGroup(self.plugin.name)
109 self.settings.setValue('format', self.format.text())
110 self.settings.endGroup()
112 def get_settings_widget(self):
113 return self.SettingsWidgetSystray(self)
115 def set_enabled(self, val):
116 pass