plugin: make settings a var, not a function.
[nephilim.git] / nephilim / plugins / Systray.py
blob558fc24f4417502bd1317e61192feaf6a69a5dc4
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
20 from PyQt4.QtCore import QVariant
22 from ..plugin import Plugin
23 from ..misc import sec2min, APPNAME, appIcon, expand_tags
25 class Systray(Plugin):
26 o = None
27 format = None
28 eventObj = None
29 DEFAULTS = {'format': '$track - $title by $artist on $album ($length)'}
31 def _load(self):
32 self.format = self.settings.value(self.name + '/format').toString()
33 class SystrayWheelEventObject(QtCore.QObject):
34 """This class listens for systray-wheel events"""
35 def eventFilter(self, object, event):
36 if type(event)==QtGui.QWheelEvent:
37 numDegrees=event.delta() / 8
38 numSteps=5*numDegrees/15
39 self.plugin.mpclient.set_volume(self.plugin.mpclient.volume() + numSteps)
40 event.accept()
41 return True
42 return False
44 self.o = QtGui.QSystemTrayIcon(QtGui.QIcon(appIcon), self.parent())
45 self.eventObj=SystrayWheelEventObject()
46 self.eventObj.plugin = self
47 self.o.installEventFilter(self.eventObj)
48 self.parent().connect(self.o, QtCore.SIGNAL('activated (QSystemTrayIcon::ActivationReason)')
49 , self.onSysTrayClick)
51 self.connect(self.mpclient, QtCore.SIGNAL('song_changed'), self.update)
52 self.o.show()
53 def _unload(self):
54 self.disconnect(self.mpclient, QtCore.SIGNAL('song_changed'), self.update)
55 self.disconnect(self.mpclient, QtCore.SIGNAL('disconnected'), self.update)
56 self.disconnect(self.mpclient, QtCore.SIGNAL('time_changed'), self.update)
57 self.o.hide()
58 self.o.setIcon(QtGui.QIcon(None))
59 self.o = None
60 self.parent()._wheelEvent = None
61 def getInfo(self):
62 return "Display the mpclientpc icon in the systray."
64 def update(self):
65 status = self.mpclient.status()
66 if not status:
67 return
69 values = {'state':''}
70 values['state']={'play':'playing', 'stop':'stopped', 'pause':'paused'}[status['state']]
71 if 'time' in status:
72 values['length'] = sec2min(status['length'])
73 values['time'] = sec2min(status['time'])
75 song = self.mpclient.current_song()
76 if song:
77 self.o.setToolTip(expand_tags(self.format, (song,)))
78 else:
79 self.o.setToolTip('%s not playing'%APPNAME)
81 def onSysTrayClick(self, reason):
82 if reason == QtGui.QSystemTrayIcon.Trigger or\
83 reason == QtGui.QSystemTrayIcon.Context:
84 w = self.parent()
85 if w.isVisible():
86 w.setVisible(False)
87 else:
88 w.setVisible(True)
89 elif reason == QtGui.QSystemTrayIcon.MiddleClick:
90 if self.mpclient.is_playing():
91 self.mpclient.pause()
92 else:
93 self.mpclient.resume()
95 class SettingsWidgetSystray(Plugin.SettingsWidget):
96 format = None
98 def __init__(self, plugin):
99 Plugin.SettingsWidget.__init__(self, plugin)
101 self.format = QtGui.QLineEdit(self.settings.value(self.plugin.name + '/format').toString())
103 self.setLayout(QtGui.QVBoxLayout())
104 self._add_widget(self.format, 'Tooltip format')
106 def save_settings(self):
107 self.settings.beginGroup(self.plugin.name)
108 self.settings.setValue('format', QVariant(self.format.text()))
109 self.settings.endGroup()
111 def get_settings_widget(self):
112 return self.SettingsWidgetSystray(self)
114 def set_enabled(self, val):
115 pass