add licence headers to all files.
[nephilim.git] / nephilim / plugins / Systray.py
blob169579c4fecfa1e1b7214a87592bb59c53fbd450
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.o.show()
52 def _unload(self):
53 self.disconnect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.update)
54 self.disconnect(self.mpclient(), QtCore.SIGNAL('disconnected'), self.update)
55 self.disconnect(self.mpclient(), QtCore.SIGNAL('time_changed'), self.update)
56 self.o.hide()
57 self.o.setIcon(QtGui.QIcon(None))
58 self.o = None
59 self.parent()._wheelEvent = None
60 def getInfo(self):
61 return "Display the mpclientpc icon in the systray."
63 def update(self):
64 status = self.mpclient().status()
65 if not status:
66 return
68 values = {'state':''}
69 values['state']={'play':'playing', 'stop':'stopped', 'pause':'paused'}[status['state']]
70 if 'time' in status:
71 values['length'] = sec2min(status['length'])
72 values['time'] = sec2min(status['time'])
74 song = self.mpclient().current_song()
75 if song:
76 self.o.setToolTip(expand_tags(self.format, (song,)))
77 else:
78 self.o.setToolTip('%s not playing'%APPNAME)
80 def onSysTrayClick(self, reason):
81 if reason == QtGui.QSystemTrayIcon.Trigger or\
82 reason == QtGui.QSystemTrayIcon.Context:
83 w = self.parent()
84 if w.isVisible():
85 w.setVisible(False)
86 else:
87 w.setVisible(True)
88 elif reason == QtGui.QSystemTrayIcon.MiddleClick:
89 if self.mpclient().is_playing():
90 self.mpclient().pause()
91 else:
92 self.mpclient().resume()
94 class SettingsWidgetSystray(Plugin.SettingsWidget):
95 format = None
97 def __init__(self, plugin):
98 Plugin.SettingsWidget.__init__(self, plugin)
100 self.format = QtGui.QLineEdit(self.settings().value(self.plugin.name() + '/format').toString())
102 self.setLayout(QtGui.QVBoxLayout())
103 self._add_widget(self.format, 'Tooltip format')
105 def save_settings(self):
106 self.settings().beginGroup(self.plugin.name())
107 self.settings().setValue('format', QVariant(self.format.text()))
108 self.settings().endGroup()
110 def get_settings_widget(self):
111 return self.SettingsWidgetSystray(self)