plugin: make settings a var, not a function.
[nephilim.git] / nephilim / settings_wg.py
blobd8c1177dfd967e5e843a09199abafe701b895969
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 os
23 from misc import Button
24 import plugin
27 class SettingsWidget(QtGui.QWidget):
28 save_btn = None
29 close_btn = None
30 pluginlist = None
31 settings = None
32 settings_wg = []
34 mpclient = None
35 plugins = None
37 class SettingsWidgetMPD(plugin.Plugin.SettingsWidget):
38 mpclient = None
39 host_txt = None
40 port_txt = None
41 pass_txt = None
42 lib_txt = None
43 update = None
44 outputs = None
45 xfade = None
47 def __init__(self, mpclient):
48 plugin.Plugin.SettingsWidget.__init__(self, None)
49 self.mpclient = mpclient
51 self.settings.beginGroup('MPD')
52 self.host_txt = QtGui.QLineEdit(self.settings.value('host', QVariant('localhost')).toString())
53 self.port_txt = QtGui.QLineEdit(self.settings.value('port', QVariant('6600')).toString())
54 self.pass_txt = QtGui.QLineEdit(self.settings.value('password').toString())
55 self.pass_txt.setEchoMode(QtGui.QLineEdit.Password)
56 self.lib_txt = QtGui.QLineEdit(self.settings.value('music_dir', QVariant(os.path.expanduser('~/music/'))).toString())
57 self.settings.endGroup()
59 self.update = QtGui.QPushButton('Update MPD database')
60 self.connect(self.update, QtCore.SIGNAL('clicked()'), self.update_db)
62 self.outputs = QtGui.QGroupBox('Audio outputs')
63 self.outputs.setLayout(QtGui.QVBoxLayout())
64 class Output(QtGui.QCheckBox):
65 id = None
66 mpclient = None
67 def __init__(self, text, mpclient, id):
68 QtGui.QCheckBox.__init__(self, text)
69 self.mpclient = mpclient
70 self.id = id
71 self.connect(self, QtCore.SIGNAL('stateChanged(int)'), self.change_state)
73 def change_state(self, state):
74 self.mpclient.set_output(self.id, state)
76 for output in self.mpclient.outputs():
77 box = Output(output['outputname'], self.mpclient, output['outputid'])
78 if output['outputenabled'] == '1':
79 box.setChecked(True)
80 else:
81 box.setChecked(False)
82 self.outputs.layout().addWidget(box)
84 self.xfade = QtGui.QSpinBox()
85 self.xfade.setValue(int(self.mpclient.status()['xfade']))
86 self.connect(self.xfade, QtCore.SIGNAL('valueChanged(int)'), self.mpclient.crossfade)
88 self.setLayout(QtGui.QVBoxLayout())
89 self._add_widget(self.host_txt, 'Host', 'Host or socket to connect to')
90 self._add_widget(self.port_txt, 'Port', 'Port to use (empty when using sockets)')
91 self._add_widget(self.pass_txt, 'Password', 'Password')
92 self._add_widget(self.lib_txt, 'Music library', 'Path to music library')
93 self.layout().addWidget(self.update)
94 self.layout().addWidget(self.outputs)
95 self._add_widget(self.xfade, 'Crossfade', 'Set crossfade between songs (in seconds).')
97 def save_settings(self):
98 reconnect = False
100 self.settings.beginGroup('MPD')
101 if self.host_txt.text() != self.settings.value('host').toString():
102 self.settings.setValue('host', QVariant(self.host_txt.text()))
103 reconnect = True
104 if self.port_txt.text() != self.settings.value('port').toString():
105 self.settings.setValue('port', QVariant(self.port_txt.text()))
106 reconnect = True
107 if self.pass_txt.text() != self.settings.value('password').toString():
108 self.settings.setValue('password', QVariant(self.pass_txt.text()))
109 if self.pass_txt.text():
110 self.mpclient.password(self.pass_txt.text())
111 self.settings.setValue('music_dir', QVariant(self.lib_txt.text()))
112 self.settings.endGroup()
114 if reconnect:
115 self.mpclient.disconnect_mpd()
116 self.mpclient.connect_mpd(str(self.host_txt.text()), str(self.port_txt.text()), str(self.pass_txt.text()))
118 def update_db(self):
119 self.mpclient.update_db()
121 def __init__(self, mpclient, plugins):
122 QtGui.QWidget.__init__(self, None, QtCore.Qt.Window)
123 self.settings = QtCore.QSettings()
124 self.plugins = plugins
125 self.mpclient = mpclient
127 self.save_btn = Button('save all', self.save_clicked)
128 self.close_btn = Button('close', self.close_clicked)
130 tab_wg = QtGui.QTabWidget(self)
131 self.settings_wg.append(self.SettingsWidgetMPD(mpclient))
132 tab_wg.addTab(self.settings_wg[-1], 'MPD settings')
134 self.pluginlist = QtGui.QListWidget(self)
135 self.fill_pluginlist()
136 tab_wg.addTab(self.pluginlist, 'Plugins')
138 for plugin in self.plugins.loaded_plugins():
139 wg = plugin.get_settings_widget()
140 if wg:
141 self.settings_wg.append(wg)
142 tab_wg.addTab(self.settings_wg[-1], plugin.name)
144 self.setLayout(QtGui.QGridLayout())
145 self.layout().addWidget(tab_wg, 0, 0, 1, 2)
146 self.layout().addWidget(self.save_btn, 1, 0)
147 self.layout().addWidget(self.close_btn, 1, 1)
149 self.connect(self.pluginlist, QtCore.SIGNAL('itemChanged(QListWidgetItem*)'), self.plugin_checked)
151 self.setWindowTitle('Settings')
152 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
153 self.center()
154 self.resize(800,400)
156 def fill_pluginlist(self):
157 self.pluginlist.clear()
158 for plugin in self.plugins.plugins():
159 item = QtGui.QListWidgetItem("%s\t%s"%(plugin.name, plugin.info()))
160 if plugin.loaded:
161 item.setCheckState(QtCore.Qt.Checked)
162 else:
163 item.setCheckState(QtCore.Qt.Unchecked)
165 self.pluginlist.addItem(item)
167 def center(self):
168 screen = QtGui.QDesktopWidget().screenGeometry()
169 size = self.geometry()
170 self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)
172 def save_clicked(self):
173 for wg in self.settings_wg:
174 wg.save_settings
176 def close_clicked(self):
177 self.close()
179 def plugin_checked(self, item):
180 toload = (item.checkState() == QtCore.Qt.Checked)
181 name = str(item.text()[0:str(item.text()).find('\t')])
182 if toload:
183 # refresh the plugin file
184 self.plugins.load(name)
185 self.fill_pluginlist()
186 else:
187 self.plugins.unload(name)
188 self.settings.setValue(name + '/load', QtCore.QVariant(toload))
190 def closeEvent(self, event):
191 self.settings_wg = None