From e28e625b42a835149a7baa47262ae9f6d5888ba2 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 16 Mar 2016 18:15:55 +0300 Subject: [PATCH] message splitting and some bug fixes --- src/callbacks.py | 2 +- src/main.py | 17 ++++++++++++----- src/profile.py | 26 ++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index d435a03..3b8d960 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -96,7 +96,6 @@ def friend_message(window, tray): :return: function for tox.callback_friend_message. Adds new message to list """ def wrapped(tox, friend_number, message_type, message, size, user_data): - print 'Message: ', message.decode('utf8') profile = Profile.get_instance() settings = Settings.get_instance() invoke_in_main_thread(profile.new_message, friend_number, message_type, message) @@ -123,6 +122,7 @@ def init_callbacks(tox, window, tray): Initialization of all callbacks. :param tox: tox instance :param window: main window + :param tray: tray (for notifications) """ tox.callback_friend_status(friend_status, 0) tox.callback_friend_message(friend_message(window, tray), 0) diff --git a/src/main.py b/src/main.py index aee38af..823fddf 100644 --- a/src/main.py +++ b/src/main.py @@ -119,13 +119,20 @@ class Toxygen(object): # initializing callbacks init_callbacks(self.tox, self.ms, self.tray) # bootstrap - for data in node_generator(): - self.tox.bootstrap(*data) - self.msleep(10000) - while not self.tox.self_get_connection_status() and not self.stop: + try: for data in node_generator(): self.tox.bootstrap(*data) - self.msleep(5000) + except: + pass + self.msleep(10000) + while not self.tox.self_get_connection_status() and not self.stop: + try: + for data in node_generator(): + self.tox.bootstrap(*data) + except: + pass + finally: + self.msleep(5000) class ToxIterateThread(QtCore.QThread): diff --git a/src/profile.py b/src/profile.py index 891d472..80146b5 100644 --- a/src/profile.py +++ b/src/profile.py @@ -447,6 +447,29 @@ class Profile(Contact, Singleton): # Private messages # ----------------------------------------------------------------------------------------------------------------- + def split_and_send(self, number, message_type, message): + """ + Message splitting + :param number: friend's number + :param message_type: type of message + :param message: message text + """ + while len(message) > TOX_MAX_MESSAGE_LENGTH: + size = TOX_MAX_MESSAGE_LENGTH * 4 / 5 + last_part = message[size:] + if ' ' in last_part: + index = last_part.index(' ') + elif ',' in last_part: + index = last_part.index(',') + elif '.' in last_part: + index = last_part.index('.') + else: + index = TOX_MAX_MESSAGE_LENGTH - size + index += size + self._tox.friend_send_message(number, message_type, message[:index]) + message = message[index:] + self._tox.friend_send_message(number, message_type, message) + def new_message(self, friend_num, message_type, message): """ Current user gets new message @@ -482,8 +505,7 @@ class Profile(Contact, Singleton): else: message_type = TOX_MESSAGE_TYPE['NORMAL'] friend = self._friends[self._active_friend] - # TODO: add message splitting - self._tox.friend_send_message(friend.number, message_type, text.encode('utf-8')) + self.split_and_send(friend.number, message_type, text.encode('utf-8')) self.create_message_item(text, curr_time(), self._name, message_type) self._screen.messageEdit.clear() self._messages.scrollToBottom()