common: split MetadataFetcher into its own file
[nephilim.git] / nephilim / plugins / Notify.py
blobbb01aabc388bf64b1fb163bf1d77d13db19ac0d5
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 ..common import sec2min, APPNAME, expand_tags
22 from ..plugin import Plugin
23 from .. import plugins
25 NOTIFY_PRIORITY_SONG = 1
26 NOTIFY_PRIORITY_VOLUME = 2
28 class winNotify(QtGui.QWidget):
29 parent = None
30 p = None
32 _current_priority = 0
34 timer = None
35 cover_label = None
36 text_label = None
38 def __init__(self, p):
39 QtGui.QWidget.__init__(self, p.parent())
40 self.p = p
41 self.parent = p.parent()
43 self.timer = QtCore.QTimer(self)
44 self.timer.setSingleShot(True)
45 self.timer.timeout.connect(self._hide)
47 layout = QtGui.QHBoxLayout()
48 self.cover_label = QtGui.QLabel()
49 self.text_label = QtGui.QLabel()
50 self.text_label.setWordWrap(True)
52 self.setLayout(QtGui.QHBoxLayout())
53 self.layout().addWidget(self.cover_label)
54 self.layout().addWidget(self.text_label)
56 self.setWindowFlags(QtCore.Qt.ToolTip | QtCore.Qt.WindowStaysOnTopHint)
57 self.setWindowOpacity(0.7)
59 font = QtGui.QFont()
60 font.setPixelSize(20)
61 self.setFont(font)
63 ac = QtGui.QApplication.instance().plugins.plugin('AlbumCover')
64 if ac:
65 ac.cover_changed.connect(self.on_cover_changed)
67 def on_cover_changed(self, cover):
68 if cover.isNull():
69 self.cover_label.clear()
70 return
72 self.cover_label.setPixmap(cover.scaledToHeight(self.fontInfo().pixelSize()*4))
73 self.resize(self.layout().sizeHint())
75 def mousePressEvent(self, event):
76 self.hide()
78 def show(self, text, time = 3000, priority = 0):
79 if not priority >= self._current_priority:
80 return
81 self._current_priority = priority
83 self.text_label.setText(text)
84 self.resize(self.layout().sizeHint())
85 self.centerH()
86 self.setVisible(True)
87 self.timer.start(time)
89 def _hide(self):
90 self.setHidden(True)
91 self._current_priority = -1
93 def centerH(self):
94 screen = QtGui.QDesktopWidget().screenGeometry()
95 size = self.geometry()
96 self.move((screen.width()-size.width())/2, 100)
98 class Notify(Plugin):
99 # public, const
100 info = 'Show notifications in a popup window.'
102 # public, read-only
103 o = None
105 DEFAULTS = {'songformat' : '${track} - ${artist} - ${title} (${album}) [${length}]',
106 'timer' : 3000}
108 def _load(self):
109 self.o = winNotify(self)
110 self.mpclient.song_changed.connect(self.onSongChange)
111 self.mpclient.connect_changed.connect(self.on_connect_changed)
112 self.mpclient.state_changed.connect(self.onStateChange)
113 self.mpclient.volume_changed.connect (self.onVolumeChange)
114 def _unload(self):
115 self.o=None
116 self.mpclient.song_changed.disconnect(self.onSongChange)
117 self.mpclient.connect_changed.disconnect(self.on_connect_changed)
118 self.mpclient.state_changed.disconnect(self.onStateChange)
119 self.mpclient.volume_changed.disconnect(self.onVolumeChange)
120 def onSongChange(self):
121 song = self.mpclient.cur_song
122 if not song:
123 return
124 self.settings.beginGroup(self.name)
125 self.o.show(expand_tags(self.settings.value('songformat'), (song,)), int(self.settings.value('timer')), NOTIFY_PRIORITY_SONG)
126 self.settings.endGroup()
128 def on_connect_changed(self, val):
129 self.o.show('%s.'%('Connected' if val else 'Disconnected'), int(self.settings.value(self.name + '/timer')))
131 def onStateChange(self, new_state):
132 self.o.show(new_state, int(self.settings.value(self.name + '/timer')))
134 def onVolumeChange(self, new_vol):
135 self.o.show('Volume: %i%%'%(new_vol), int(self.settings.value(self.name + '/timer')), priority = NOTIFY_PRIORITY_VOLUME)
137 class SettingsWidgetNotify(Plugin.SettingsWidget):
138 format = None
139 timer = None
141 def __init__(self, plugin):
142 Plugin.SettingsWidget.__init__(self, plugin)
143 self.settings.beginGroup(self.plugin.name)
145 self.format = QtGui.QLineEdit(self.settings.value('songformat'))
147 self.timer = QtGui.QLineEdit(self.settings.value('timer'))
148 self.timer.setValidator(QtGui.QIntValidator(self.timer))
150 self.setLayout(QtGui.QVBoxLayout())
151 self._add_widget(self.format, 'Format', 'Format of notifications. All tags supported by MPD\n'
152 'will be expanded to their values for current song,\n'
153 'e.g. ${track}, ${title}, ${artist}, ${album}, ${length}, ${date}, etc.')
154 self._add_widget(self.timer, 'Duration', 'How long should notifications be displayed in ms.')
155 self.settings.endGroup()
157 def save_settings(self):
158 self.settings.beginGroup(self.plugin.name)
159 self.settings.setValue('songformat', self.format.text())
160 self.settings.setValue('timer', int(self.timer.text()))
161 self.settings.endGroup()
162 self.plugin.onSongChange()
164 def get_settings_widget(self):
165 return self.SettingsWidgetNotify(self)