mpd: catch errors when writing command.
[nephilim.git] / nephilim / plugin.py
blob560081bd555f0e9a57292b6db7a628fbbc92d4e1
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
21 import logging
23 import plugins
25 class Plugin(QtCore.QObject):
26 # public, const
27 name = None
28 info = None
29 logger = None
30 mpclient = None
31 settings = None
33 # public, read-only
34 loaded = None
36 _dock_widget = None
37 _parent = None
39 o = None
40 DEFAULTS = {}
42 def __init__(self, parent, mpclient, name):
43 QtCore.QObject.__init__(self)
44 self.name = name
45 self.mpclient = mpclient
46 self._parent = parent
47 self.loaded = False
48 self.settings = QtCore.QSettings()
50 self.logger = logging.getLogger(self.name)
52 #init settings
53 self.logger.debug('Initializing default settings.')
54 self.settings.beginGroup(name)
55 for key in self.DEFAULTS:
56 if not self.settings.contains(key):
57 self.settings.setValue(key, QVariant(self.DEFAULTS[key]))
58 self.settings.endGroup()
60 def parent(self):
61 return self._parent
62 def set_status(self, status):
63 self._parent.setStatus(status)
65 def load(self):
66 self.logger.info('loading')
67 self._load()
68 opts = QtGui.QDockWidget.DockWidgetClosable|QtGui.QDockWidget.DockWidgetMovable
69 QtGui.QApplication.instance().main_win.add_dock(self.get_dock_widget(opts))
70 QtGui.QApplication.instance().main_win.restore_layout()
71 self.mpclient.connect_changed.connect(self.set_enabled)
72 self.loaded = True
73 def unload(self):
74 if not self.loaded:
75 return
76 self.logger.info("unloading")
77 self._unload()
78 dock_widget = self.get_dock_widget()
79 if dock_widget:
80 QtGui.QApplication.instance().main_win.remove_dock(dock_widget)
81 self._dock_widget = None
82 self.settingsWidget = None
83 self.mpclient.connect_changed.disconnect(self.set_enabled)
84 self.loaded = False
85 def set_enabled(self, val):
86 if self.o:
87 self.o.setEnabled(val)
89 def get_dock_widget(self, opts = None):
90 self._dock_widget = self._get_dock_widget()
91 if self._dock_widget and opts:
92 self._dock_widget.setFeatures(opts)
93 self._dock_widget.setAllowedAreas(QtCore.Qt.AllDockWidgetAreas)
94 return self._dock_widget
96 class SettingsWidget(QtGui.QWidget):
97 """ plugins should subclass this"""
98 plugin = None
99 settings = None
101 def __init__(self, plugin):
102 QtGui.QWidget.__init__(self)
103 self.plugin = plugin
104 self.settings = QtCore.QSettings()
106 def settings(self):
107 return self.settings
109 def savesettings(self):
110 """ reimplement this"""
111 pass
113 def _add_widget(self, widget, label = '', tooltip = ''):
114 """adds a widget with label"""
115 if not self.layout():
116 self.plugin.logger.error('Attempted to call add_widget with no layout set.')
117 widget.setToolTip(tooltip)
118 layout = QtGui.QHBoxLayout()
119 layout.addWidget(QtGui.QLabel(label))
120 layout.addWidget(widget)
121 self.layout().addLayout(layout)
123 def get_settings_widget(self):
124 """Should return subclassed SettingsWidget."""
125 return
127 def _get_dock_widget(self):
128 """Override this one."""
129 return None
130 def _create_dock(self, widget):
131 """Creates a QDockWidget with _parent $_parent containing widget $widget."""
132 dock=QtGui.QDockWidget(self.name, QtGui.QApplication.instance().main_win)
133 dock.setObjectName(self.name)
134 dock.setWidget(widget)
136 return dock
137 def _load(self):
138 """Override this one."""
139 return
140 def _unload(self):
141 """Override this one."""
142 return