From ab419edddab95d3db6156184d9b1a0479a16933f Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Tue, 15 Mar 2016 22:12:37 +0300 Subject: [PATCH] bug fixes --- src/callbacks.py | 9 +++++---- src/main.py | 21 ++++++++++++--------- src/notifications.py | 5 +---- src/profile.py | 6 +++--- tests/tests.py | 4 +--- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index f67fb5e..d435a03 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -89,9 +89,10 @@ def friend_status_message(tox, friend_num, status_message, size, user_data): invoke_in_main_thread(profile.set_active) -def friend_message(window): +def friend_message(window, tray): """ :param window: main window + :param tray: tray :return: function for tox.callback_friend_message. Adds new message to list """ def wrapped(tox, friend_number, message_type, message, size, user_data): @@ -102,7 +103,7 @@ def friend_message(window): if not window.isActiveWindow(): friend = profile.get_friend_by_number(friend_number) if settings['notifications']: - invoke_in_main_thread(tray_notification, friend.name, message.decode('utf8')) + invoke_in_main_thread(tray_notification, friend.name, message.decode('utf8'), tray) if settings['sound_notifications']: sound_notification(SOUND_NOTIFICATION['MESSAGE']) return wrapped @@ -117,14 +118,14 @@ def friend_request(tox, public_key, message, message_size, user_data): invoke_in_main_thread(profile.process_friend_request, tox_id, message.decode('utf-8')) -def init_callbacks(tox, window): +def init_callbacks(tox, window, tray): """ Initialization of all callbacks. :param tox: tox instance :param window: main window """ tox.callback_friend_status(friend_status, 0) - tox.callback_friend_message(friend_message(window), 0) + tox.callback_friend_message(friend_message(window, tray), 0) tox.callback_self_connection_status(self_connection_status(tox), 0) tox.callback_friend_connection_status(friend_connection_status, 0) tox.callback_friend_name(friend_name, 0) diff --git a/src/main.py b/src/main.py index 01d582f..270e73e 100644 --- a/src/main.py +++ b/src/main.py @@ -55,12 +55,17 @@ class Toxygen(object): data = ProfileHelper.open_profile(path, name) self.tox = tox_factory(data, settings) + # tray icon + self.tray = QtGui.QSystemTrayIcon(QtGui.QIcon(curr_directory() + '/images/icon.png')) + self.tray.setContextMenu(QtGui.QMenu()) + self.tray.show() + self.ms = MainWindow(self.tox, self.reset) self.ms.show() QtGui.QApplication.setStyle(get_style(settings['theme'])) # set application style # init thread - self.init = self.InitThread(self.tox, self.ms) + self.init = self.InitThread(self.tox, self.ms, self.tray) self.init.start() # starting thread for tox iterate @@ -83,14 +88,12 @@ class Toxygen(object): self.mainloop.wait() self.init.terminate() data = self.tox.get_savedata() - length = self.tox.get_savedata_size() - savedata = ''.join('{}'.format(data[i]) for i in xrange(length)) - ProfileHelper.save_profile(savedata) + ProfileHelper.save_profile(data) del self.tox # create new tox instance - self.tox = tox_factory(savedata, Settings.get_instance()) + self.tox = tox_factory(data, Settings.get_instance()) # init thread - self.init = self.InitThread(self.tox, self.ms) + self.init = self.InitThread(self.tox, self.ms, self.tray) self.init.start() # starting thread for tox iterate @@ -104,13 +107,13 @@ class Toxygen(object): class InitThread(QtCore.QThread): - def __init__(self, tox, ms): + def __init__(self, tox, ms, tray): QtCore.QThread.__init__(self) - self.tox, self.ms = tox, ms + self.tox, self.ms, self.tray = tox, ms, tray def run(self): # initializing callbacks - init_callbacks(self.tox, self.ms) + init_callbacks(self.tox, self.ms, self.tray) # bootstrap for data in node_generator(): self.tox.bootstrap(*data) diff --git a/src/notifications.py b/src/notifications.py index b6e9ee4..d090afd 100644 --- a/src/notifications.py +++ b/src/notifications.py @@ -11,11 +11,8 @@ SOUND_NOTIFICATION = { } -def tray_notification(title, text): +def tray_notification(title, text, tray): if QtGui.QSystemTrayIcon.isSystemTrayAvailable(): - tray = QtGui.QSystemTrayIcon(QtGui.QIcon(curr_directory() + '/images/icon.png')) - tray.setContextMenu(QtGui.QMenu()) - tray.show() if len(text) > 30: text = text[:27] + '...' tray.showMessage(title, text, QtGui.QSystemTrayIcon.NoIcon, 3000) diff --git a/src/profile.py b/src/profile.py index 70cd6cb..36b2417 100644 --- a/src/profile.py +++ b/src/profile.py @@ -428,10 +428,10 @@ class Profile(Contact, Singleton): active_friend = property(get_active, set_active) def get_active_number(self): - return self._friends[self._active_friend].number + return self._friends[self._active_friend].number if self._active_friend + 1 else -1 def get_active_name(self): - return self._friends[self._active_friend].name + return self._friends[self._active_friend].name if self._active_friend + 1 else '' def is_active_online(self): return self._active_friend + 1 and self._friends[self._active_friend].status is not None @@ -451,7 +451,7 @@ class Profile(Contact, Singleton): user_name = Profile.get_instance().get_active_name() self.create_message_item(message.decode('utf-8'), curr_time(), user_name, message_type) self._messages.scrollToBottom() - self._friends[self._active_friend].append_message((message, + self._friends[self._active_friend].append_message((message.decode('utf-8'), MESSAGE_OWNER['FRIEND'], time.time(), message_type)) diff --git a/tests/tests.py b/tests/tests.py index 9d04b7f..b14bd26 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -56,10 +56,8 @@ class TestTox(): tox.self_set_name(name) tox.self_set_status_message(status_message) data = tox.get_savedata() - length = tox.get_savedata_size() - savedata = ''.join('{}'.format(data[i]) for i in xrange(length)) del tox - tox = tox_factory(savedata) + tox = tox_factory(data) assert tox.self_get_name() == name assert tox.self_get_status_message() == status_message