Skip to content
Snippets Groups Projects
Commit 10194d68 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

SingleMessage: Refactor code

- Remove ApplicationWindow form glade file, we already inherit form ApplicatonWindow
- Make some methods private
- Fix pylint errors
parent 425f50f8
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -44,42 +44,45 @@ class SingleMessageWindow(Gtk.ApplicationWindow): ...@@ -44,42 +44,45 @@ class SingleMessageWindow(Gtk.ApplicationWindow):
action argument which can be 'send' or 'receive' action argument which can be 'send' or 'receive'
""" """
def __init__(self, account, to='', action='', from_whom='', subject='', def __init__(self, account, to='', action='', from_whom='', subject='',
message='', resource='', session=None): message='', resource='', session=None):
Gtk.ApplicationWindow.__init__(self) Gtk.ApplicationWindow.__init__(self)
self.set_application(app.app) self.set_application(app.app)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_show_menubar(False)
self.set_title(_('Send Single Message')) self.set_title(_('Send Single Message'))
self.set_name('SendSingleMessageWindow') self.set_name('SendSingleMessageWindow')
self.account = account self.account = account
self.action = action self._action = action
self.subject = subject self._subject = subject
self.message = message self._message = message
self.to = to self._to = to
self.from_whom = from_whom self._from_whom = from_whom
self.resource = resource self._resource = resource
self.session = session self._session = session
self._ui = get_builder('single_message_window.ui') self._ui = get_builder('single_message_window.ui')
self.message_tv_buffer = self._ui.message_textview.get_buffer() self._message_tv_buffer = self._ui.message_textview.get_buffer()
self.conversation_textview = ConversationTextview( self._conversation_textview = ConversationTextview(
account, used_in_history_window=True) account, used_in_history_window=True)
self.conversation_textview.tv.show() self._conversation_textview.tv.show()
self.conversation_textview.tv.set_left_margin(6) self._conversation_textview.tv.set_left_margin(6)
self.conversation_textview.tv.set_right_margin(6) self._conversation_textview.tv.set_right_margin(6)
self.conversation_textview.tv.set_top_margin(6) self._conversation_textview.tv.set_top_margin(6)
self.conversation_textview.tv.set_bottom_margin(6) self._conversation_textview.tv.set_bottom_margin(6)
self.conversation_tv_buffer = self.conversation_textview.tv.get_buffer()
self._ui.conversation_scrolledwindow.add( self._ui.conversation_scrolledwindow.add(
self.conversation_textview.tv) self._conversation_textview.tv)
self.message_tv_buffer.connect('changed', self.update_char_counter) self._message_tv_buffer.connect('changed', self._update_char_counter)
if isinstance(to, list): if isinstance(to, list):
jid = ', '.join([i[0].jid for i in to]) jid = ', '.join([i[0].jid for i in to])
self._ui.to_entry.set_text(jid) self._ui.to_entry.set_text(jid)
else: else:
self._ui.to_entry.set_text(to) self._ui.to_entry.set_text(to)
if app.config.get('use_speller') and app.is_installed('GSPELL') and action == 'send': if (app.config.get('use_speller') and
app.is_installed('GSPELL') and
action == 'send'):
lang = app.config.get('speller_language') lang = app.config.get('speller_language')
gspell_lang = Gspell.language_lookup(lang) gspell_lang = Gspell.language_lookup(lang)
if gspell_lang is None: if gspell_lang is None:
...@@ -93,95 +96,64 @@ def __init__(self, account, to='', action='', from_whom='', subject='', ...@@ -93,95 +96,64 @@ def __init__(self, account, to='', action='', from_whom='', subject='',
spell_view.set_inline_spell_checking(True) spell_view.set_inline_spell_checking(True)
spell_view.set_enable_language_menu(True) spell_view.set_enable_language_menu(True)
self.prepare_widgets_for(self.action) self._prepare_widgets_for(self._action)
# set_text(None) raises TypeError exception # set_text(None) raises TypeError exception
if self.subject is None: if self._subject is None:
self.subject = _('(No subject)') self._subject = _('(No subject)')
self._ui.subject_entry.set_text(self.subject) self._ui.subject_entry.set_text(self._subject)
self._ui.subject_from_entry_label.set_text(self.subject) self._ui.subject_from_entry_label.set_text(self._subject)
if to == '': if to == '':
liststore = get_completion_liststore(self._ui.to_entry) liststore = get_completion_liststore(self._ui.to_entry)
self.completion_dict = helpers.get_contact_dict_for_account(account) self._completion_dict = helpers.get_contact_dict_for_account(
keys = sorted(self.completion_dict.keys()) account)
keys = sorted(self._completion_dict.keys())
for jid in keys: for jid in keys:
contact = self.completion_dict[jid] contact = self._completion_dict[jid]
status_icon = get_icon_name(contact.show) status_icon = get_icon_name(contact.show)
liststore.append((status_icon, jid)) liststore.append((status_icon, jid))
else: else:
self.completion_dict = {} self._completion_dict = {}
self._ui.to_entry.connect('changed', self.on_to_entry_changed) self.add(self._ui.box)
self.connect('delete-event', self._on_delete)
self.connect('destroy', self._on_destroy)
self.connect('key-press-event', self._on_key_press_event)
self._ui.to_entry.connect('changed', self._on_to_entry_changed)
self._ui.connect_signals(self) self._ui.connect_signals(self)
# get window position and size from config # get window position and size from config
resize_window(self._ui.single_message_window, resize_window(self,
app.config.get('single-msg-width'), app.config.get('single-msg-width'),
app.config.get('single-msg-height')) app.config.get('single-msg-height'))
move_window(self._ui.single_message_window, move_window(self,
app.config.get('single-msg-x-position'), app.config.get('single-msg-x-position'),
app.config.get('single-msg-y-position')) app.config.get('single-msg-y-position'))
self._ui.single_message_window.show_all() self.show_all()
def on_single_message_window_destroy(self, widget):
c = app.contacts.get_contact_with_highest_priority(self.account,
self.from_whom)
if not c:
# Groupchat is maybe already destroyed
return
if c.is_groupchat and self.from_whom not in \
app.interface.minimized_controls[self.account] and self.action == \
'receive' and app.events.get_nb_roster_events(self.account,
self.from_whom, types=['chat', 'normal']) == 0:
app.interface.roster.remove_groupchat(self.from_whom, self.account)
def set_cursor_to_end(self): def _set_cursor_to_end(self):
end_iter = self.message_tv_buffer.get_end_iter() end_iter = self._message_tv_buffer.get_end_iter()
self.message_tv_buffer.place_cursor(end_iter) self._message_tv_buffer.place_cursor(end_iter)
def save_pos(self): def _save_position(self):
# save the window size and position # save the window size and position
x, y = self._ui.single_message_window.get_position() x_pos, y_pos = self.get_position()
app.config.set('single-msg-x-position', x) app.config.set('single-msg-x-position', x_pos)
app.config.set('single-msg-y-position', y) app.config.set('single-msg-y-position', y_pos)
width, height = self._ui.single_message_window.get_size() width, height = self.get_size()
app.config.set('single-msg-width', width) app.config.set('single-msg-width', width)
app.config.set('single-msg-height', height) app.config.set('single-msg-height', height)
def on_single_message_window_delete_event(self, window, ev): def _on_to_entry_changed(self, _widget):
self.save_pos()
def on_show_contact_info_button_clicked(self, widget):
"""
Ask for vCard
"""
entry = self._ui.to_entry.get_text().strip()
keys = sorted(self.completion_dict.keys())
for key in keys:
contact = self.completion_dict[key]
if entry in key:
entry = contact.jid
break
if entry in app.interface.instances[self.account]['infos']:
app.interface.instances[self.account]['infos'][entry].window.present()
else:
contact = app.contacts.create_contact(jid=entry, account=self.account)
app.interface.instances[self.account]['infos'][entry] = \
vcard.VcardWindow(contact, self.account)
# Remove xmpp page
app.interface.instances[self.account]['infos'][entry].xml.\
get_object('information_notebook').remove_page(0)
def on_to_entry_changed(self, widget):
entry = self._ui.to_entry.get_text() entry = self._ui.to_entry.get_text()
is_empty = bool(not entry == '' and not ',' in entry) is_empty = bool(not entry == '' and not ',' in entry)
self._ui.show_contact_info_button.set_sensitive(is_empty) self._ui.show_contact_info_button.set_sensitive(is_empty)
def prepare_widgets_for(self, action): def _prepare_widgets_for(self, action):
if len(app.connections) > 1: if len(app.connections) > 1:
if action == 'send': if action == 'send':
title = _('Single Message using account %s') % self.account title = _('Single Message using account %s') % self.account
...@@ -200,13 +172,13 @@ def prepare_widgets_for(self, action): ...@@ -200,13 +172,13 @@ def prepare_widgets_for(self, action):
self._ui.send_grid.show() self._ui.send_grid.show()
self._ui.received_grid.hide() self._ui.received_grid.hide()
if self.message: # we come from a reply? if self._message: # we come from a reply?
self._ui.show_contact_info_button.set_sensitive(True) self._ui.show_contact_info_button.set_sensitive(True)
self._ui.message_textview.grab_focus() self._ui.message_textview.grab_focus()
self.message_tv_buffer.set_text(self.message) self._message_tv_buffer.set_text(self._message)
GLib.idle_add(self.set_cursor_to_end) GLib.idle_add(self._set_cursor_to_end)
else: # we write a new message (not from reply) else: # we write a new message (not from reply)
if self.to: # do we already have jid? if self._to: # do we already have jid?
self._ui.subject_entry.grab_focus() self._ui.subject_entry.grab_focus()
elif action == 'receive': # prepare UI for Receiving elif action == 'receive': # prepare UI for Receiving
...@@ -220,54 +192,52 @@ def prepare_widgets_for(self, action): ...@@ -220,54 +192,52 @@ def prepare_widgets_for(self, action):
self._ui.received_grid.show() self._ui.received_grid.show()
self._ui.send_grid.hide() self._ui.send_grid.hide()
if self.message: if self._message:
self.conversation_textview.print_real_text(self.message) self._conversation_textview.print_real_text(self._message)
fjid = self.from_whom fjid = self._from_whom
if self.resource: if self._resource:
fjid += '/' + self.resource # Full jid of sender (with resource) fjid += '/' + self._resource
self._ui.from_entry_label.set_text(fjid) self._ui.from_entry_label.set_text(fjid)
self._ui.single_message_window.set_title(title) self.set_title(title)
def on_close_button_clicked(self, widget):
self.save_pos()
self._ui.single_message_window.destroy()
def update_char_counter(self, widget): def _update_char_counter(self, _widget):
characters_no = self.message_tv_buffer.get_char_count() characters_no = self._message_tv_buffer.get_char_count()
self._ui.count_chars_label.set_text( self._ui.count_chars_label.set_text(
_('Characters typed: %s') % str(characters_no)) _('Characters typed: %s') % str(characters_no))
def send_single_message(self): def _send_single_message(self):
if not app.account_is_connected(self.account): if not app.account_is_connected(self.account):
# if offline or connecting # if offline or connecting
ErrorDialog(_('Connection not available'), ErrorDialog(_('Connection not available'),
_('Please make sure you are connected with "%s".') % self.account) _('Please make sure you are connected with "%s".') %
self.account)
return True return True
if isinstance(self.to, list): if isinstance(self._to, list):
sender_list = [] sender_list = []
for i in self.to: for i in self._to:
if i[0].resource: if i[0].resource:
sender_list.append(i[0].jid + '/' + i[0].resource) sender_list.append(i[0].jid + '/' + i[0].resource)
else: else:
sender_list.append(i[0].jid) sender_list.append(i[0].jid)
else: else:
sender_list = [j.strip() for j in self._ui.to_entry.get_text().split( sender_list = [j.strip() for j
',')] in self._ui.to_entry.get_text().split(',')]
subject = self._ui.subject_entry.get_text() subject = self._ui.subject_entry.get_text()
begin, end = self.message_tv_buffer.get_bounds() begin, end = self._message_tv_buffer.get_bounds()
message = self.message_tv_buffer.get_text(begin, end, True) message = self._message_tv_buffer.get_text(begin, end, True)
recipient_list = [] recipient_list = []
for to_whom_jid in sender_list: for to_whom_jid in sender_list:
if to_whom_jid in self.completion_dict: if to_whom_jid in self._completion_dict:
to_whom_jid = self.completion_dict[to_whom_jid].jid to_whom_jid = self._completion_dict[to_whom_jid].jid
try: try:
to_whom_jid = helpers.parse_jid(to_whom_jid) to_whom_jid = helpers.parse_jid(to_whom_jid)
except helpers.InvalidFormat: except helpers.InvalidFormat:
ErrorDialog(_('Invalid XMPP Address'), ErrorDialog(
_('Invalid XMPP Address'),
_('It is not possible to send a message to %s, this ' _('It is not possible to send a message to %s, this '
'XMPP Address is not valid.') % to_whom_jid) 'XMPP Address is not valid.') % to_whom_jid)
return True return True
...@@ -280,34 +250,91 @@ def send_single_message(self): ...@@ -280,34 +250,91 @@ def send_single_message(self):
recipient_list.append(to_whom_jid) recipient_list.append(to_whom_jid)
app.nec.push_outgoing_event(MessageOutgoingEvent(None, app.nec.push_outgoing_event(MessageOutgoingEvent(
account=self.account, jid=recipient_list, message=message, None,
type_='normal', subject=subject)) account=self.account,
jid=recipient_list,
message=message,
type_='normal',
subject=subject))
self._ui.subject_entry.set_text('') # we sent ok, clear the subject self._ui.subject_entry.set_text('') # we sent ok, clear the subject
self.message_tv_buffer.set_text('') # we sent ok, clear the textview self._message_tv_buffer.set_text('') # we sent ok, clear the textview
return False
def _on_destroy(self, _widget):
contact = app.contacts.get_contact_with_highest_priority(
self.account, self._from_whom)
if not contact:
# Groupchat is maybe already destroyed
return
controls = app.interface.minimized_controls[self.account]
events = app.events.get_nb_roster_events(self.account,
self._from_whom,
types=['chat', 'normal'])
if (contact.is_groupchat and
self._from_whom not in controls and
self._action == 'receive' and
events == 0):
app.interface.roster.remove_groupchat(self._from_whom, self.account)
def _on_delete(self, *args):
self._save_position()
def _on_contact_info_clicked(self, _widget):
"""
Ask for vCard
"""
entry = self._ui.to_entry.get_text().strip()
keys = sorted(self._completion_dict.keys())
for key in keys:
contact = self._completion_dict[key]
if entry in key:
entry = contact.jid
break
if entry in app.interface.instances[self.account]['infos']:
app.interface.instances[self.account]['infos'][entry].\
window.present()
else:
contact = app.contacts.create_contact(jid=entry,
account=self.account)
app.interface.instances[self.account]['infos'][entry] = \
vcard.VcardWindow(contact, self.account)
# Remove xmpp page
app.interface.instances[self.account]['infos'][entry].xml.\
get_object('information_notebook').remove_page(0)
def _on_close_clicked(self, _widget):
self._save_position()
self.destroy()
def on_send_button_clicked(self, widget): def _on_send_clicked(self, _widget):
self.send_single_message() self._send_single_message()
def on_reply_button_clicked(self, widget): def _on_reply_clicked(self, _widget):
# we create a new blank window to send and we preset RE: and to jid # we create a new blank window to send and we preset RE: and to jid
self.subject = _('RE: %s') % self.subject self._subject = _('RE: %s') % self._subject
self.message = _('%s wrote:\n') % self.from_whom + self.message self._message = _('%s wrote:\n') % self._from_whom + self._message
# add > at the begining of each line # add > at the begining of each line
self.message = self.message.replace('\n', '\n> ') + '\n\n' self._message = self._message.replace('\n', '\n> ') + '\n\n'
self._ui.single_message_window.destroy() self.destroy()
SingleMessageWindow(self.account, to=self.from_whom, action='send', SingleMessageWindow(self.account,
from_whom=self.from_whom, subject=self.subject, message=self.message, to=self._from_whom,
session=self.session) action='send',
from_whom=self._from_whom,
def on_send_and_close_button_clicked(self, widget): subject=self._subject,
if self.send_single_message(): message=self._message,
session=self._session)
def _on_send_and_close_clicked(self, _widget):
if self._send_single_message():
return return
self.save_pos() self._save_position()
self._ui.single_message_window.destroy() self.destroy()
def on_single_message_window_key_press_event(self, widget, event): def _on_key_press_event(self, _widget, event):
if event.keyval == Gdk.KEY_Escape: # ESCAPE if event.keyval == Gdk.KEY_Escape: # ESCAPE
self.save_pos() self._save_position()
self._ui.single_message_window.destroy() self.destroy()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment