2 # -*- coding: utf-8 -*-
4 # network.py - I/O with WeeChat/relay
6 # Copyright (C) 2011-2013 Sebastien Helleu <flashcode@flashtux.org>
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
10 # QWeeChat is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # QWeeChat is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
26 QtCore
= qt_compat
.import_module('QtCore')
27 QtNetwork
= qt_compat
.import_module('QtNetwork')
29 _PROTO_INIT_CMD
= ['init password=%(password)s']
30 _PROTO_SYNC_CMDS
= ['(listbuffers) hdata buffer:gui_buffers(*) number,full_name,short_name,type,nicklist,title,local_variables',
31 '(listlines) hdata buffer:gui_buffers(*)/own_lines/first_line(*)/data date,displayed,prefix,message',
32 '(nicklist) nicklist',
37 class Network(QtCore
.QObject
):
38 """I/O with WeeChat/relay."""
40 statusChanged
= qt_compat
.Signal(str, str)
41 messageFromWeechat
= qt_compat
.Signal(QtCore
.QByteArray
)
43 def __init__(self
, *args
):
44 QtCore
.QObject
.__init
__(*(self
,) + args
)
45 self
.status_disconnected
= 'disconnected'
46 self
.status_connecting
= 'connecting...'
47 self
.status_connected
= 'connected'
52 self
._buffer
= QtCore
.QByteArray()
53 self
._socket
= QtNetwork
.QSslSocket()
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
.send_to_weechat('\n'.join(_PROTO_INIT_CMD
+ _PROTO_SYNC_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 bytes = self
._socket
.readAll()
72 self
._buffer
.append(bytes)
73 while len(self
._buffer
) >= 4:
75 length
= struct
.unpack('>i', self
._buffer
[0:4])[0]
76 if len(self
._buffer
) < length
:
77 # partial message, just wait for end of message
79 # more than one message?
80 if length
< len(self
._buffer
):
81 # save beginning of another message
82 remainder
= self
._buffer
[length
:]
83 self
._buffer
= self
._buffer
[0:length
]
84 self
.messageFromWeechat
.emit(self
._buffer
)
85 if not self
.is_connected():
89 self
._buffer
.append(remainder
)
91 def _socket_disconnected(self
):
92 """Slot: socket disconnected."""
97 self
.statusChanged
.emit(self
.status_disconnected
, None)
99 def is_connected(self
):
100 return self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
105 def connect_weechat(self
, server
, port
, ssl
, password
):
106 self
._server
= server
108 self
._port
= int(port
)
112 self
._password
= password
113 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
115 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
117 self
._socket
.connectToHost(self
._server
, self
._port
)
119 self
._socket
.ignoreSslErrors()
120 self
._socket
.startClientEncryption()
121 self
.statusChanged
.emit(self
.status_connecting
, None)
123 def disconnect_weechat(self
):
124 if self
._socket
.state() != QtNetwork
.QAbstractSocket
.UnconnectedState
:
125 if self
._socket
.state() == QtNetwork
.QAbstractSocket
.ConnectedState
:
126 self
.send_to_weechat('quit\n')
127 self
._socket
.waitForBytesWritten(1000)
129 self
.statusChanged
.emit(self
.status_disconnected
, None)
132 def send_to_weechat(self
, message
):
133 self
._socket
.write(message
.encode('utf-8'))
135 def desync_weechat(self
):
136 self
.send_to_weechat('desync\n')
138 def sync_weechat(self
):
139 self
.send_to_weechat('\n'.join(_PROTO_SYNC_CMDS
))
141 def status_icon(self
, status
):
142 icon
= {self
.status_disconnected
: 'dialog-close.png',
143 self
.status_connecting
: 'dialog-close.png',
144 self
.status_connected
: 'dialog-ok-apply.png'}
145 return icon
.get(status
, '')