summaryrefslogtreecommitdiff
path: root/nephilim/winConnect.py
blob: 45789acf64979d6da1396b647fe0470af5fd94da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QVariant
import time
from misc import *
from traceback import print_exc

class winConnect(QtGui.QWidget):
    txtHost=None
    txtPort=None
    lblInfo=None
    _timerID=None
    mpclient = None
    settings = None


    def __init__(self,parent):
        QtGui.QWidget.__init__(self, parent)
        self.settings = QtCore.QSettings(ORGNAME, APPNAME)
        self.txtHost  = QtGui.QLineEdit(self.settings.value('MPD/host', QVariant('localhost')).toString())
        self.txtPort  = QtGui.QLineEdit(self.settings.value('MPD/port', QVariant('6600')).toString())
        self.txtPort.setValidator(QtGui.QIntValidator(1, 65535, self.txtPort))
        self.lblInfo  = QtGui.QLabel("connecting ...")
        self.mpclient = parent.mpclient

        frame=QtGui.QVBoxLayout()
        inputs=QtGui.QHBoxLayout()

        frame.addLayout(inputs)
        frame.addWidget(self.lblInfo)

        inputs.addWidget(self.txtHost)
        inputs.addWidget(self.txtPort)

        self.setWindowIcon(QtGui.QIcon(appIcon))
        self.setWindowTitle('Connect to mpd')
        self.setLayout(frame)
        self.resize(200,80)
        self.center()
        doEvents()

        self.mpclient.add_listener('onReady', self.onReady)
        self.mpclient.add_listener('onConnect', self.onConnect)

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)

    def monitor(self):
        self.txtHost.setEnabled(True)
        self.txtPort.setEnabled(True)
        if self._timerID==None:
            self._timerID=self.startTimer(200)
        if self.isVisible()==False:
            self.show()
        self.activateWindow()
        self.raise_()
        doEvents()


    def onConnect(self, params):
        if self._timerID:
            self.killTimer(self._timerID)
            self._timerID=None
        self.lblInfo.setText('Connected!\nRestoring library and playlist ...')
        doEvents()
        self.settings.setValue('MPD/host', QVariant(self.txtHost.text()))
        self.settings.setValue('MPD/port', QVariant(self.txtPort.text()))
        self.txtHost.setEnabled(False)
        self.txtPort.setEnabled(False)
        doEvents()

    def onReady(self, params):
        self.hide()

    def timerEvent(self, event):
        host = str(self.txtHost.text())
        port = int(self.txtPort.text()) if self.txtPort.text() else None

        self.lblInfo.setText('Trying to connect to '+host+':'+str(port)+' ...')
        doEvents()
        self.mpclient.connect(host, port)
        doEvents()

    def windowActivationChange(self, bool):
        self.activateWindow()
        self.raise_()
        doEvents()