mpclient: change info from function to var.
[nephilim.git] / nephilim / settings_wg.py
blob145114a5f4735a1ac258b0429d03d33f57920d57
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 common 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 = None
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.update.clicked.connect(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.stateChanged.connect(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.xfade.valueChanged.connect(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 = []
132 self.settings_wg.append(self.SettingsWidgetMPD(mpclient))
133 tab_wg.addTab(self.settings_wg[-1], 'MPD settings')
135 self.pluginlist = QtGui.QListWidget(self)
136 self.fill_pluginlist()
137 tab_wg.addTab(self.pluginlist, 'Plugins')
139 for plugin in self.plugins.loaded_plugins():
140 wg = plugin.get_settings_widget()
141 if wg:
142 self.settings_wg.append(wg)
143 tab_wg.addTab(self.settings_wg[-1], plugin.name)
145 self.setLayout(QtGui.QGridLayout())
146 self.layout().addWidget(tab_wg, 0, 0, 1, 2)
147 self.layout().addWidget(self.save_btn, 1, 0)
148 self.layout().addWidget(self.close_btn, 1, 1)
150 self.pluginlist.itemChanged.connect(self.plugin_checked)
152 self.setWindowTitle('Settings')
153 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
154 self.center()
155 self.resize(800,400)
157 def fill_pluginlist(self):
158 self.pluginlist.clear()
159 for plugin in self.plugins.plugins():
160 item = QtGui.QListWidgetItem("%s\t%s"%(plugin.name, plugin.info))
161 if plugin.loaded:
162 item.setCheckState(QtCore.Qt.Checked)
163 else:
164 item.setCheckState(QtCore.Qt.Unchecked)
166 self.pluginlist.addItem(item)
168 def center(self):
169 screen = QtGui.QDesktopWidget().screenGeometry()
170 size = self.geometry()
171 self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)
173 def save_clicked(self):
174 for wg in self.settings_wg:
175 wg.save_settings()
177 def close_clicked(self):
178 self.close()
180 def plugin_checked(self, item):
181 toload = (item.checkState() == QtCore.Qt.Checked)
182 name = str(item.text()[0:str(item.text()).find('\t')])
183 if toload:
184 # refresh the plugin file
185 self.plugins.load(name)
186 self.fill_pluginlist()
187 else:
188 self.plugins.unload(name)
189 self.settings.setValue(name + '/load', QtCore.QVariant(toload))
191 def closeEvent(self, event):
192 self.settings_wg = None