song: make keys case-insensitive
[nephilim.git] / nephilim / settings_wg.py
blobfd7c4435df59c0b1bda17b39a1e486b509407dc0
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 xfade = None
46 def __init__(self, mpclient):
47 plugin.Plugin.SettingsWidget.__init__(self, None)
48 self.mpclient = mpclient
50 self.settings.beginGroup('MPD')
51 self.host_txt = QtGui.QLineEdit(self.settings.value('host', QVariant('localhost')).toString())
52 self.port_txt = QtGui.QLineEdit(self.settings.value('port', QVariant('6600')).toString())
53 self.port_txt.setValidator(QtGui.QIntValidator(0, 65535, self))
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 outputs = QtGui.QGroupBox('Audio outputs')
63 outputs.setLayout(QtGui.QVBoxLayout())
65 for output in self.mpclient.outputs:
66 box = QtGui.QCheckBox(output.name, self)
67 if output.state:
68 box.setChecked(True)
69 else:
70 box.setChecked(False)
71 box.clicked.connect(output.set_state)
72 output.state_changed.connect(box.setChecked)
73 outputs.layout().addWidget(box)
75 self.xfade = QtGui.QSpinBox()
76 self.xfade.setValue(int(self.mpclient.status()['xfade']))
77 self.xfade.valueChanged.connect(self.mpclient.crossfade)
79 self.setLayout(QtGui.QVBoxLayout())
80 self._add_widget(self.host_txt, 'Host', 'Host or socket to connect to')
81 self._add_widget(self.port_txt, 'Port', 'Port to use (empty when using sockets)')
82 self._add_widget(self.pass_txt, 'Password', 'Password')
83 self._add_widget(self.lib_txt, 'Music library', 'Path to music library')
84 self.layout().addWidget(self.update)
85 self.layout().addWidget(outputs)
86 self._add_widget(self.xfade, 'Crossfade', 'Set crossfade between songs (in seconds).')
88 def save_settings(self):
89 reconnect = False
91 self.settings.beginGroup('MPD')
92 if self.host_txt.text() != self.settings.value('host').toString():
93 self.settings.setValue('host', QVariant(self.host_txt.text()))
94 reconnect = True
95 if self.port_txt.text() != self.settings.value('port').toString():
96 self.settings.setValue('port', QVariant(self.port_txt.text()))
97 reconnect = True
98 if self.pass_txt.text() != self.settings.value('password').toString():
99 self.settings.setValue('password', QVariant(self.pass_txt.text()))
100 if self.pass_txt.text():
101 self.mpclient.password(self.pass_txt.text())
102 self.settings.setValue('music_dir', QVariant(self.lib_txt.text()))
103 self.settings.endGroup()
105 if reconnect:
106 self.mpclient.disconnect_mpd()
107 self.mpclient.connect_mpd(self.host_txt.text(), int(self.port_txt.text()), self.pass_txt.text())
109 def update_db(self):
110 self.mpclient.update_db()
112 def __init__(self, mpclient, plugins):
113 QtGui.QWidget.__init__(self, None, QtCore.Qt.Window)
114 self.settings = QtCore.QSettings()
115 self.plugins = plugins
116 self.mpclient = mpclient
118 self.save_btn = Button('save all', self.save_clicked)
119 self.close_btn = Button('close', self.close_clicked)
121 tab_wg = QtGui.QTabWidget(self)
122 self.settings_wg = []
123 self.settings_wg.append(self.SettingsWidgetMPD(mpclient))
124 tab_wg.addTab(self.settings_wg[-1], 'MPD settings')
126 self.pluginlist = QtGui.QListWidget(self)
127 self.fill_pluginlist()
128 tab_wg.addTab(self.pluginlist, 'Plugins')
130 for plugin in self.plugins.loaded_plugins():
131 wg = plugin.get_settings_widget()
132 if wg:
133 self.settings_wg.append(wg)
134 tab_wg.addTab(self.settings_wg[-1], plugin.name)
136 self.setLayout(QtGui.QGridLayout())
137 self.layout().addWidget(tab_wg, 0, 0, 1, 2)
138 self.layout().addWidget(self.save_btn, 1, 0)
139 self.layout().addWidget(self.close_btn, 1, 1)
141 self.pluginlist.itemChanged.connect(self.plugin_checked)
143 self.setWindowTitle('Settings')
144 self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
145 self.center()
146 self.resize(800,400)
148 def fill_pluginlist(self):
149 self.pluginlist.clear()
150 for plugin in self.plugins.plugins():
151 item = QtGui.QListWidgetItem("%s\t%s"%(plugin.name, plugin.info))
152 if plugin.loaded:
153 item.setCheckState(QtCore.Qt.Checked)
154 else:
155 item.setCheckState(QtCore.Qt.Unchecked)
157 self.pluginlist.addItem(item)
159 def center(self):
160 screen = QtGui.QDesktopWidget().screenGeometry()
161 size = self.geometry()
162 self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)
164 def save_clicked(self):
165 for wg in self.settings_wg:
166 wg.save_settings()
168 def close_clicked(self):
169 self.close()
171 def plugin_checked(self, item):
172 toload = (item.checkState() == QtCore.Qt.Checked)
173 name = str(item.text()[0:str(item.text()).find('\t')])
174 if toload:
175 # refresh the plugin file
176 self.plugins.load(name)
177 self.fill_pluginlist()
178 else:
179 self.plugins.unload(name)
180 self.settings.setValue(name + '/load', QtCore.QVariant(toload))
182 def closeEvent(self, event):
183 self.hide()
184 event.ignore()