]>
jfr.im git - irc/weechat/qweechat.git/blob - qweechat/qweechat.py
1 # -*- coding: utf-8 -*-
3 # qweechat.py - WeeChat remote GUI using Qt toolkit
5 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9 # QWeeChat is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # QWeeChat is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
24 QWeeChat is a WeeChat remote GUI using Qt toolkit.
26 It requires requires WeeChat 0.3.7 or newer, running on local or remote host.
32 # 2011-05-27, Sébastien Helleu <flashcode@flashtux.org>:
38 from pkg_resources
import resource_filename
40 QTCORE
= qt_compat
.import_module('QtCore')
41 QTGUI
= qt_compat
.import_module('QtGui')
43 import weechat
.protocol
as protocol
44 from network
import Network
45 from connection
import ConnectionDialog
46 from buffer import BufferListWidget
, Buffer
47 from debug
import DebugDialog
48 from about
import AboutDialog
49 from version
import qweechat_version
52 AUTHOR
= 'Sébastien Helleu'
53 AUTHOR_MAIL
= 'flashcode@flashtux.org'
54 WEECHAT_SITE
= 'http://weechat.org/'
56 # number of lines in buffer for debug window
60 class MainWindow(QTGUI
.QMainWindow
):
63 def __init__(self
, *args
):
64 QTGUI
.QMainWindow
.__init
__(*(self
,) + args
)
66 self
.config
= config
.read()
68 self
.resize(1000, 600)
69 self
.setWindowTitle(NAME
)
71 self
.debug_dialog
= None
74 self
.about_dialog
= None
75 self
.connection_dialog
= None
78 self
.network
= Network()
79 self
.network
.statusChanged
.connect(self
._network
_status
_changed
)
80 self
.network
.messageFromWeechat
.connect(self
._network
_weechat
_msg
)
83 self
.list_buffers
= BufferListWidget()
84 self
.list_buffers
.currentRowChanged
.connect(self
._buffer
_switch
)
87 self
.buffers
= [Buffer()]
88 self
.stacked_buffers
= QTGUI
.QStackedWidget()
89 self
.stacked_buffers
.addWidget(self
.buffers
[0].widget
)
91 # splitter with buffers + chat/input
92 splitter
= QTGUI
.QSplitter()
93 splitter
.addWidget(self
.list_buffers
)
94 splitter
.addWidget(self
.stacked_buffers
)
96 self
.setCentralWidget(splitter
)
98 if self
.config
.getboolean('look', 'statusbar'):
99 self
.statusBar().visible
= True
101 # actions for menu and toolbar
104 'network-connect.png', 'Connect to WeeChat',
105 'Ctrl+O', self
.open_connection_dialog
],
107 'network-disconnect.png', 'Disconnect from WeeChat',
108 'Ctrl+D', self
.network
.disconnect_weechat
],
110 'edit-find.png', 'Debug console window',
111 'Ctrl+B', self
.open_debug_dialog
],
113 'preferences-other.png', 'Preferences',
114 'Ctrl+P', self
.open_preferences_dialog
],
116 'help-about.png', 'About',
117 'Ctrl+H', self
.open_about_dialog
],
119 'document-save.png', 'Save connection configuration',
120 'Ctrl+S', self
.save_connection
],
122 'application-exit.png', 'Quit application',
123 'Ctrl+Q', self
.close
],
126 for name
, action
in list(actions_def
.items()):
127 self
.actions
[name
] = QTGUI
.QAction(
129 resource_filename(__name__
, 'data/icons/%s' % action
[0])),
130 name
.capitalize(), self
)
131 self
.actions
[name
].setStatusTip(action
[1])
132 self
.actions
[name
].setShortcut(action
[2])
133 self
.actions
[name
].triggered
.connect(action
[3])
136 self
.menu
= self
.menuBar()
137 menu_file
= self
.menu
.addMenu('&File')
138 menu_file
.addActions([self
.actions
['connect'],
139 self
.actions
['disconnect'],
140 self
.actions
['preferences'],
141 self
.actions
['save connection'],
142 self
.actions
['quit']])
143 menu_window
= self
.menu
.addMenu('&Window')
144 menu_window
.addAction(self
.actions
['debug'])
145 menu_help
= self
.menu
.addMenu('&Help')
146 menu_help
.addAction(self
.actions
['about'])
147 self
.network_status
= QTGUI
.QLabel()
148 self
.network_status
.setFixedHeight(20)
149 self
.network_status
.setFixedWidth(200)
150 self
.network_status
.setContentsMargins(0, 0, 10, 0)
151 self
.network_status
.setAlignment(QTCORE
.Qt
.AlignRight
)
152 if hasattr(self
.menu
, 'setCornerWidget'):
153 self
.menu
.setCornerWidget(self
.network_status
,
154 QTCORE
.Qt
.TopRightCorner
)
155 self
.network_status_set(self
.network
.status_disconnected
)
158 toolbar
= self
.addToolBar('toolBar')
159 toolbar
.setToolButtonStyle(QTCORE
.Qt
.ToolButtonTextUnderIcon
)
160 toolbar
.addActions([self
.actions
['connect'],
161 self
.actions
['disconnect'],
162 self
.actions
['debug'],
163 self
.actions
['preferences'],
164 self
.actions
['about'],
165 self
.actions
['quit']])
167 self
.buffers
[0].widget
.input.setFocus()
170 if self
.config
.getboolean('look', 'debug'):
171 self
.open_debug_dialog()
173 # auto-connect to relay
174 if self
.config
.getboolean('relay', 'autoconnect'):
175 self
.network
.connect_weechat(self
.config
.get('relay', 'server'),
176 self
.config
.get('relay', 'port'),
177 self
.config
.getboolean('relay',
179 self
.config
.get('relay', 'password'),
180 self
.config
.get('relay', 'lines'))
184 def _buffer_switch(self
, index
):
185 """Switch to a buffer."""
187 self
.stacked_buffers
.setCurrentIndex(index
)
188 self
.stacked_buffers
.widget(index
).input.setFocus()
190 def buffer_input(self
, full_name
, text
):
191 """Send buffer input to WeeChat."""
192 if self
.network
.is_connected():
193 message
= 'input %s %s\n' % (full_name
, text
)
194 self
.network
.send_to_weechat(message
)
195 self
.debug_display(0, '<==', message
, forcecolor
='#AA0000')
197 def open_preferences_dialog(self
):
198 """Open a dialog with preferences."""
201 def save_connection(self
):
202 """Save connection configuration."""
204 options
= self
.network
.get_options()
205 for option
in options
.keys():
206 self
.config
.set('relay', option
, options
[option
])
208 def debug_display(self
, *args
, **kwargs
):
209 """Display a debug message."""
210 self
.debug_lines
.append((args
, kwargs
))
211 self
.debug_lines
= self
.debug_lines
[-DEBUG_NUM_LINES
:]
212 if self
.debug_dialog
:
213 self
.debug_dialog
.chat
.display(*args
, **kwargs
)
215 def open_debug_dialog(self
):
216 """Open a dialog with debug messages."""
217 if not self
.debug_dialog
:
218 self
.debug_dialog
= DebugDialog(self
)
219 self
.debug_dialog
.input.textSent
.connect(
220 self
.debug_input_text_sent
)
221 self
.debug_dialog
.finished
.connect(self
._debug
_dialog
_closed
)
222 self
.debug_dialog
.display_lines(self
.debug_lines
)
223 self
.debug_dialog
.chat
.scroll_bottom()
225 def debug_input_text_sent(self
, text
):
226 """Send debug buffer input to WeeChat."""
227 if self
.network
.is_connected():
230 if text
.startswith('(') and pos
>= 0:
231 text
= '(debug_%s)%s' % (text
[1:pos
], text
[pos
+1:])
233 text
= '(debug) %s' % text
234 self
.debug_display(0, '<==', text
, forcecolor
='#AA0000')
235 self
.network
.send_to_weechat(text
+ '\n')
237 def _debug_dialog_closed(self
, result
):
238 """Called when debug dialog is closed."""
239 self
.debug_dialog
= None
241 def open_about_dialog(self
):
242 """Open a dialog with info about QWeeChat."""
243 messages
= ['<b>%s</b> %s' % (NAME
, qweechat_version()),
244 '© 2011-2014 %s <<a href="mailto:%s">%s</a>>'
245 % (AUTHOR
, AUTHOR_MAIL
, AUTHOR_MAIL
),
247 'Running with %s' % ('PySide' if qt_compat
.uses_pyside
250 'WeeChat site: <a href="%s">%s</a>'
251 % (WEECHAT_SITE
, WEECHAT_SITE
),
253 self
.about_dialog
= AboutDialog(NAME
, messages
, self
)
255 def open_connection_dialog(self
):
256 """Open a dialog with connection settings."""
258 for option
in ('server', 'port', 'ssl', 'password', 'lines'):
259 values
[option
] = self
.config
.get('relay', option
)
260 self
.connection_dialog
= ConnectionDialog(values
, self
)
261 self
.connection_dialog
.dialog_buttons
.accepted
.connect(
262 self
.connect_weechat
)
264 def connect_weechat(self
):
265 """Connect to WeeChat."""
266 self
.network
.connect_weechat(
267 self
.connection_dialog
.fields
['server'].text(),
268 self
.connection_dialog
.fields
['port'].text(),
269 self
.connection_dialog
.fields
['ssl'].isChecked(),
270 self
.connection_dialog
.fields
['password'].text(),
271 int(self
.connection_dialog
.fields
['lines'].text()))
272 self
.connection_dialog
.close()
274 def _network_status_changed(self
, status
, extra
):
275 """Called when the network status has changed."""
276 if self
.config
.getboolean('look', 'statusbar'):
277 self
.statusBar().showMessage(status
)
278 self
.debug_display(0, '', status
, forcecolor
='#0000AA')
279 self
.network_status_set(status
)
281 def network_status_set(self
, status
):
282 """Set the network status."""
283 pal
= self
.network_status
.palette()
284 if status
== self
.network
.status_connected
:
285 pal
.setColor(self
.network_status
.foregroundRole(),
286 QTGUI
.QColor('green'))
288 pal
.setColor(self
.network_status
.foregroundRole(),
289 QTGUI
.QColor('#aa0000'))
290 ssl
= ' (SSL)' if status
!= self
.network
.status_disconnected \
291 and self
.network
.is_ssl() else ''
292 self
.network_status
.setPalette(pal
)
293 icon
= self
.network
.status_icon(status
)
295 self
.network_status
.setText(
296 '<img src="%s"> %s' %
297 (resource_filename(__name__
, 'data/icons/%s' % icon
),
298 status
.capitalize() + ssl
))
300 self
.network_status
.setText(status
.capitalize())
301 if status
== self
.network
.status_disconnected
:
302 self
.actions
['connect'].setEnabled(True)
303 self
.actions
['disconnect'].setEnabled(False)
305 self
.actions
['connect'].setEnabled(False)
306 self
.actions
['disconnect'].setEnabled(True)
308 def _network_weechat_msg(self
, message
):
309 """Called when a message is received from WeeChat."""
310 self
.debug_display(0, '==>',
311 'message (%d bytes):\n%s'
313 protocol
.hex_and_ascii(message
, 20)),
314 forcecolor
='#008800')
316 proto
= protocol
.Protocol()
317 message
= proto
.decode(str(message
))
318 if message
.uncompressed
:
321 'message uncompressed (%d bytes):\n%s'
322 % (message
.size_uncompressed
,
323 protocol
.hex_and_ascii(message
.uncompressed
, 20)),
324 forcecolor
='#008800')
325 self
.debug_display(0, '', 'Message: %s' % message
)
326 self
.parse_message(message
)
328 print('Error while decoding message from WeeChat:\n%s'
329 % traceback
.format_exc())
330 self
.network
.disconnect_weechat()
332 def _parse_listbuffers(self
, message
):
333 """Parse a WeeChat with list of buffers."""
334 for obj
in message
.objects
:
335 if obj
.objtype
!= 'hda' or obj
.value
['path'][-1] != 'buffer':
337 self
.list_buffers
.clear()
338 while self
.stacked_buffers
.count() > 0:
339 buf
= self
.stacked_buffers
.widget(0)
340 self
.stacked_buffers
.removeWidget(buf
)
342 for item
in obj
.value
['items']:
343 buf
= self
.create_buffer(item
)
344 self
.insert_buffer(len(self
.buffers
), buf
)
345 self
.list_buffers
.setCurrentRow(0)
346 self
.buffers
[0].widget
.input.setFocus()
348 def _parse_line(self
, message
):
349 """Parse a WeeChat message with a buffer line."""
350 for obj
in message
.objects
:
352 if obj
.objtype
!= 'hda' or obj
.value
['path'][-1] != 'line_data':
354 for item
in obj
.value
['items']:
355 if message
.msgid
== 'listlines':
356 ptrbuf
= item
['__path'][0]
358 ptrbuf
= item
['buffer']
359 index
= [i
for i
, b
in enumerate(self
.buffers
)
360 if b
.pointer() == ptrbuf
]
364 (item
['date'], item
['prefix'],
367 if message
.msgid
== 'listlines':
370 self
.buffers
[line
[0]].widget
.chat
.display(*line
[1])
372 def _parse_nicklist(self
, message
):
373 """Parse a WeeChat message with a buffer nicklist."""
375 for obj
in message
.objects
:
376 if obj
.objtype
!= 'hda' or \
377 obj
.value
['path'][-1] != 'nicklist_item':
380 for item
in obj
.value
['items']:
381 index
= [i
for i
, b
in enumerate(self
.buffers
)
382 if b
.pointer() == item
['__path'][0]]
384 if not index
[0] in buffer_refresh
:
385 self
.buffers
[index
[0]].nicklist
= {}
386 buffer_refresh
[index
[0]] = True
389 self
.buffers
[index
[0]].nicklist_add_item(
390 group
, item
['group'], item
['prefix'], item
['name'],
392 for index
in buffer_refresh
:
393 self
.buffers
[index
].nicklist_refresh()
395 def _parse_nicklist_diff(self
, message
):
396 """Parse a WeeChat message with a buffer nicklist diff."""
398 for obj
in message
.objects
:
399 if obj
.objtype
!= 'hda' or \
400 obj
.value
['path'][-1] != 'nicklist_item':
403 for item
in obj
.value
['items']:
404 index
= [i
for i
, b
in enumerate(self
.buffers
)
405 if b
.pointer() == item
['__path'][0]]
408 buffer_refresh
[index
[0]] = True
409 if item
['_diff'] == ord('^'):
411 elif item
['_diff'] == ord('+'):
412 self
.buffers
[index
[0]].nicklist_add_item(
413 group
, item
['group'], item
['prefix'], item
['name'],
415 elif item
['_diff'] == ord('-'):
416 self
.buffers
[index
[0]].nicklist_remove_item(
417 group
, item
['group'], item
['name'])
418 elif item
['_diff'] == ord('*'):
419 self
.buffers
[index
[0]].nicklist_update_item(
420 group
, item
['group'], item
['prefix'], item
['name'],
422 for index
in buffer_refresh
:
423 self
.buffers
[index
].nicklist_refresh()
425 def _parse_buffer_opened(self
, message
):
426 """Parse a WeeChat message with a new buffer (opened)."""
427 for obj
in message
.objects
:
428 if obj
.objtype
!= 'hda' or obj
.value
['path'][-1] != 'buffer':
430 for item
in obj
.value
['items']:
431 buf
= self
.create_buffer(item
)
432 index
= self
.find_buffer_index_for_insert(item
['next_buffer'])
433 self
.insert_buffer(index
, buf
)
435 def _parse_buffer(self
, message
):
436 """Parse a WeeChat message with a buffer event
437 (anything except a new buffer).
439 for obj
in message
.objects
:
440 if obj
.objtype
!= 'hda' or obj
.value
['path'][-1] != 'buffer':
442 for item
in obj
.value
['items']:
443 index
= [i
for i
, b
in enumerate(self
.buffers
)
444 if b
.pointer() == item
['__path'][0]]
448 if message
.msgid
== '_buffer_type_changed':
449 self
.buffers
[index
].data
['type'] = item
['type']
450 elif message
.msgid
in ('_buffer_moved', '_buffer_merged',
452 buf
= self
.buffers
[index
]
453 buf
.data
['number'] = item
['number']
454 self
.remove_buffer(index
)
455 index2
= self
.find_buffer_index_for_insert(
457 self
.insert_buffer(index2
, buf
)
458 elif message
.msgid
== '_buffer_renamed':
459 self
.buffers
[index
].data
['full_name'] = item
['full_name']
460 self
.buffers
[index
].data
['short_name'] = item
['short_name']
461 elif message
.msgid
== '_buffer_title_changed':
462 self
.buffers
[index
].data
['title'] = item
['title']
463 self
.buffers
[index
].update_title()
464 elif message
.msgid
== '_buffer_cleared':
465 self
.buffers
[index
].widget
.chat
.clear()
466 elif message
.msgid
.startswith('_buffer_localvar_'):
467 self
.buffers
[index
].data
['local_variables'] = \
468 item
['local_variables']
469 self
.buffers
[index
].update_prompt()
470 elif message
.msgid
== '_buffer_closing':
471 self
.remove_buffer(index
)
473 def parse_message(self
, message
):
474 """Parse a WeeChat message."""
475 if message
.msgid
.startswith('debug'):
476 self
.debug_display(0, '', '(debug message, ignored)')
477 elif message
.msgid
== 'listbuffers':
478 self
._parse
_listbuffers
(message
)
479 elif message
.msgid
in ('listlines', '_buffer_line_added'):
480 self
._parse
_line
(message
)
481 elif message
.msgid
in ('_nicklist', 'nicklist'):
482 self
._parse
_nicklist
(message
)
483 elif message
.msgid
== '_nicklist_diff':
484 self
._parse
_nicklist
_diff
(message
)
485 elif message
.msgid
== '_buffer_opened':
486 self
._parse
_buffer
_opened
(message
)
487 elif message
.msgid
.startswith('_buffer_'):
488 self
._parse
_buffer
(message
)
489 elif message
.msgid
== '_upgrade':
490 self
.network
.desync_weechat()
491 elif message
.msgid
== '_upgrade_ended':
492 self
.network
.sync_weechat()
494 def create_buffer(self
, item
):
495 """Create a new buffer."""
497 buf
.bufferInput
.connect(self
.buffer_input
)
498 buf
.widget
.input.bufferSwitchPrev
.connect(
499 self
.list_buffers
.switch_prev_buffer
)
500 buf
.widget
.input.bufferSwitchNext
.connect(
501 self
.list_buffers
.switch_next_buffer
)
504 def insert_buffer(self
, index
, buf
):
505 """Insert a buffer in list."""
506 self
.buffers
.insert(index
, buf
)
507 self
.list_buffers
.insertItem(index
, '%d. %s'
508 % (buf
.data
['number'],
509 buf
.data
['full_name'].decode('utf-8')))
510 self
.stacked_buffers
.insertWidget(index
, buf
.widget
)
512 def remove_buffer(self
, index
):
513 """Remove a buffer."""
514 if self
.list_buffers
.currentRow
== index
and index
> 0:
515 self
.list_buffers
.setCurrentRow(index
- 1)
516 self
.list_buffers
.takeItem(index
)
517 self
.stacked_buffers
.removeWidget(self
.stacked_buffers
.widget(index
))
518 self
.buffers
.pop(index
)
520 def find_buffer_index_for_insert(self
, next_buffer
):
521 """Find position to insert a buffer in list."""
523 if next_buffer
== '0x0':
524 index
= len(self
.buffers
)
526 index
= [i
for i
, b
in enumerate(self
.buffers
)
527 if b
.pointer() == next_buffer
]
531 print('Warning: unable to find position for buffer, using end of '
533 index
= len(self
.buffers
)
536 def closeEvent(self
, event
):
537 """Called when QWeeChat window is closed."""
538 self
.network
.disconnect_weechat()
539 if self
.debug_dialog
:
540 self
.debug_dialog
.close()
541 config
.write(self
.config
)
542 QTGUI
.QMainWindow
.closeEvent(self
, event
)
545 app
= QTGUI
.QApplication(sys
.argv
)
546 app
.setStyle(QTGUI
.QStyleFactory
.create('Cleanlooks'))
547 app
.setWindowIcon(QTGUI
.QIcon(
548 resource_filename(__name__
, 'data/icons/weechat_icon_32.png')))
550 sys
.exit(app
.exec_())