diff options
author | Anton Khirnov <wyskas@gmail.com> | 2009-03-14 20:44:52 +0100 |
---|---|---|
committer | Anton Khirnov <wyskas@gmail.com> | 2009-03-14 20:44:52 +0100 |
commit | 5fcc05296c289c458f95815ea3eba3a48ee2938b (patch) | |
tree | b3ce34e21ad4dde89a01fe084541f71fb60c3eef /nephilim | |
parent | 647ca2cb8e69beb309cfe3babd9f56a994a7669b (diff) |
Add basic support for password authentication.
Diffstat (limited to 'nephilim')
-rw-r--r-- | nephilim/connect_wg.py | 14 | ||||
-rw-r--r-- | nephilim/mpclient.py | 17 | ||||
-rw-r--r-- | nephilim/settings_win.py | 20 |
3 files changed, 43 insertions, 8 deletions
diff --git a/nephilim/connect_wg.py b/nephilim/connect_wg.py index 373b612..a04e53b 100644 --- a/nephilim/connect_wg.py +++ b/nephilim/connect_wg.py @@ -5,6 +5,7 @@ import time class ConnectWidget(QtGui.QWidget): host_txt = None port_txt = None + pass_txt = None info_lbl = None connect_btn = None mpclient = None @@ -18,6 +19,8 @@ class ConnectWidget(QtGui.QWidget): self.host_txt = QtGui.QLineEdit(self.settings.value('MPD/host', QVariant('localhost')).toString()) self.port_txt = QtGui.QLineEdit(self.settings.value('MPD/port', QVariant('6600')).toString()) self.port_txt.setValidator(QtGui.QIntValidator(1, 65535, self.port_txt)) + self.pass_txt = QtGui.QLineEdit(self.settings.value('MPD/password').toString()) + self.pass_txt.setEchoMode(QtGui.QLineEdit.Password) self.info_lbl = QtGui.QLabel("connecting...") self.connect_btn = QtGui.QPushButton('Connect') @@ -27,7 +30,8 @@ class ConnectWidget(QtGui.QWidget): self.layout().addWidget(self.info_lbl, 0, 0, 1, 2) self.layout().addWidget(self.host_txt, 1, 0) self.layout().addWidget(self.port_txt, 1, 1) - self.layout().addWidget(self.connect_btn, 2, 0) + self.layout().addWidget(self.port_txt, 2, 0) + self.layout().addWidget(self.connect_btn, 2, 1) self.setWindowTitle('Connect to MPD') self.center() @@ -47,16 +51,18 @@ class ConnectWidget(QtGui.QWidget): self.connect_mpd() def connect_mpd(self): - host = str(self.host_txt.text()) - port = int(self.port_txt.text()) if self.port_txt.text() else None + host = str(self.host_txt.text()) + port = int(self.port_txt.text()) if self.port_txt.text() else None + passw = str(self.pass_txt.text()) self.info_lbl.setText('Trying to connect to %s' + (':%d'%port if port else '')) - if not self.mpclient.connect_mpd(host, port): + if not self.mpclient.connect_mpd(host, port, passw): self.info_lbl.setText('Connection failed') def on_connected(self): self.info_lbl.setText('Connected') self.settings.setValue('MPD/host', QVariant(self.host_txt.text())) self.settings.setValue('MPD/port', QVariant(self.port_txt.text())) + self.settings.setValue('MPD/password', QVariant(self.pass_txt.text())) self.hide() diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index c367204..0635a16 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -1,6 +1,7 @@ from PyQt4 import QtCore import mpd import socket +import logging from song import Song @@ -24,8 +25,9 @@ class MPClient(QtCore.QObject): self._cur_lib = [] self._cur_playlist = [] - def connect_mpd(self, host, port): - """Connect to MPD@host:port. Returns Tue at success, False otherwise.""" + def connect_mpd(self, host, port, password = None): + """Connect to MPD@host:port, optionally using password. + Returns Tue at success, False otherwise.""" if self._client: return True try: @@ -35,6 +37,9 @@ class MPClient(QtCore.QObject): self._client = None return False + if password: + self.password(password) + self._status = MPClient._status self._update_lib() self._update_playlist() @@ -52,6 +57,14 @@ class MPClient(QtCore.QObject): self._client.disconnect() self._client = None + def password(self, password): + """Use the password to authenticate with MPD.""" + try: + self._client.password(password) + logging.info('Successfully authenticated') + except mpd.CommandError: + logging.error('Incorrect password.') + def is_connected(self): """Returns True if connected to MPD, False otherwise.""" return self._client != None diff --git a/nephilim/settings_win.py b/nephilim/settings_win.py index df822da..59955e4 100644 --- a/nephilim/settings_win.py +++ b/nephilim/settings_win.py @@ -18,6 +18,7 @@ class SettingsWindow(QtGui.QWidget): mpclient = None host_txt = None port_txt = None + pass_txt = None lib_txt = None update = None outputs = None @@ -29,6 +30,7 @@ class SettingsWindow(QtGui.QWidget): self.settings().beginGroup('MPD') self.host_txt = QtGui.QLineEdit(self.settings().value('host', QVariant('localhost')).toString()) self.port_txt = QtGui.QLineEdit(self.settings().value('port', QVariant('6600')).toString()) + self.pass_txt = QtGui.QLineEdit(self.settings().value('password').toString()) self.lib_txt = QtGui.QLineEdit(self.settings().value('music_dir', QVariant(os.path.expanduser('~/music/'))).toString()) self.settings().endGroup() @@ -60,17 +62,31 @@ class SettingsWindow(QtGui.QWidget): self.setLayout(QtGui.QVBoxLayout()) self._add_widget(self.host_txt, 'Host', 'Host or socket to connect to') self._add_widget(self.port_txt, 'Port', 'Port to use (empty when using sockets)') + self._add_widget(self.pass_txt, 'Password', 'Password') self._add_widget(self.lib_txt, 'Music library', 'Path to music library') self.layout().addWidget(self.update) self.layout().addWidget(self.outputs) def save_settings(self): + reconnect = False + self.settings().beginGroup('MPD') - self.settings().setValue('host', QVariant(self.host_txt.text())) - self.settings().setValue('port', QVariant(self.port_txt.text())) + if self.host_txt.text() != self.settings().value('host').toString(): + self.settings().setValue('host', QVariant(self.host_txt.text())) + reconnect = True + if self.port_txt.text() != self.settings().value('port').toString(): + self.settings().setValue('port', QVariant(self.port_txt.text())) + reconnect = True + if self.pass_txt.text() != self.settings().value('password').toString(): + self.settings().setValue('port', QVariant(self.pass_txt.text())) + self.mpclient.password(self.pass_txt.text()) self.settings().setValue('music_dir', QVariant(self.lib_txt.text())) self.settings().endGroup() + if reconnect: + self.mpclient.disconnect_mpd() + self.mpclient.connect_mpd(self.host_txt.text(), self.port_txt.text()) + def update_db(self): self.mpclient.update_db() |