2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2011 Sebastien Helleu <flashcode@flashtux.org>
6 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
8 # QWeeChat is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # QWeeChat is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
23 # I/O with WeeChat/relay.
28 QtCore
= qt_compat
.import_module('QtCore')
29 QtNetwork
= qt_compat
.import_module('QtNetwork')
31 _PROTO_INIT_CMDS
= ['init password=%(password)s,compression=gzip',
32 '(listbuffers) hdata buffer:gui_buffers(*) number,full_name,nicklist,title',
33 '(listlines) hdata buffer:gui_buffers(*)/own_lines/first_line(*)/data date,displayed,prefix,message',
34 '(nicklist) nicklist',
38 class Network(QtCore
.QObject
):
39 """I/O with WeeChat/relay."""
41 statusChanged
= qt_compat
.Signal(str, str)
42 messageFromWeechat
= qt_compat
.Signal(QtCore
.QByteArray
)
44 def __init__(self
, *args
):
45 apply(QtCore
.QObject
.__init
__, (self
,) + args
)
46 self
.status_disconnected
= 'disconnected'
47 self
.status_connecting
= 'connecting...'
48 self
.status_connected
= 'connected'
52 self
._buffer
= QtCore
.QByteArray()
53 self
._socket
= QtNetwork
.QTcpSocket()
54 self
._socket
.connected
.connect(self
._socket
_connected
)
55 self
._socket
.error
.connect(self
._socket
_error
)
56 self
._socket
.readyRead
.connect(self
._socket
_read
)
57 self
._socket
.disconnected
.connect(self
._socket
_disconnected
)
59 def _socket_connected(self
):
60 """Slot: socket connected."""
61 self
.statusChanged
.emit(self
.status_connected
, None)
63 self
._socket
.write('\n'.join(_PROTO_INIT_CMDS
) % {'password': str(self._password)}
)
65 def _socket_error(self
, error
):
66 """Slot: socket error."""
67 self
.statusChanged
.emit(self
.status_disconnected
, 'Failed, error: %s' % self
._socket
.errorString())
69 def _socket_read(self
):
70 """Slot: data available on socket."""
71 avail
= self
._socket
.bytesAvailable()
72 bytes = self
._socket
.read(avail
)
73 self
._buffer
.append(bytes)
74 while len(self
._buffer
) >= 4:
76 length
= struct
.unpack('>i', self
._buffer
[0:4])[0]
77 if len(self
._buffer
) < length
:
78 # partial message, just wait for end of message
80 # more than one message?
81 if length
< len(self
._buffer
):
82 # save beginning of another message
83 remainder
= self
._buffer
[length
:]
84 self
._buffer
= self
._buffer
[0:length
]
85 self
.messageFromWeechat
.emit(self
._buffer
)
88 self
._buffer
.append(remainder
)
90 def _socket_disconnected(self
):
91 """Slot: socket disconnected."""
95 self
.statusChanged
.emit(self
.status_disconnected
, None)
97 def is_connected(self
):
98 return self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
100 def connect_weechat(self
, server
, port
, password
):
101 self
._server
= server
103 self
._port
= int(port
)
106 self
._password
= password
107 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
109 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
111 self
._socket
.connectToHost(self
._server
, self
._port
)
112 self
.statusChanged
.emit(self
.status_connecting
, None)
114 def disconnect_weechat(self
):
115 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
116 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.ConnectedState
:
117 self
.statusChanged
.emit(self
.status_disconnected
, None)
120 def send_to_weechat(self
, message
):
121 self
._socket
.write(str(message
))
123 def status_icon(self
, status
):
124 icon
= {self
.status_disconnected
: 'dialog-close.png',
125 self
.status_connecting
: 'dialog-close.png',
126 self
.status_connected
: 'dialog-ok-apply.png'}
127 return icon
.get(status
, '')