From 6e07d3e3d4f33faf9d7d641e9a57bc23a610bd24 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 25 Aug 2018 14:23:59 +0300 Subject: [PATCH] contacts menu history fixes --- toxygen/app.py | 1 + toxygen/contacts/contact_menu.py | 42 +++++++++++++++++----------- toxygen/contacts/contacts_manager.py | 4 --- toxygen/history/history.py | 16 ++++++++++- toxygen/ui/main_screen.py | 18 +----------- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index 497a460..e28ccea 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -319,6 +319,7 @@ class App: self._stop_threads(False) data = self._tox.get_savedata() self._save_profile(data) + self._calls_manager.set_toxav(None) del self._tox # create new tox instance self._tox = self._create_tox(data) diff --git a/toxygen/contacts/contact_menu.py b/toxygen/contacts/contact_menu.py index b1c23f5..865e7d5 100644 --- a/toxygen/contacts/contact_menu.py +++ b/toxygen/contacts/contact_menu.py @@ -87,9 +87,13 @@ class BaseContactMenuGenerator: def __init__(self, contact): self._contact = contact - def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service): + def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service, history_loader): return ContactMenuBuilder().build() + # ----------------------------------------------------------------------------------------------------------------- + # Private methods + # ----------------------------------------------------------------------------------------------------------------- + def _generate_copy_menu_builder(self, main_screen): copy_menu_builder = ContactMenuBuilder() (copy_menu_builder @@ -101,11 +105,23 @@ class BaseContactMenuGenerator: return copy_menu_builder + def _generate_history_menu_builder(self, history_loader, main_screen): + history_menu_builder = ContactMenuBuilder() + (history_menu_builder + .with_name(util_ui.tr('Chat history')) + .with_action(util_ui.tr('Clear history'), lambda: history_loader.clear_history(self._contact) + and main_screen.messages.clear()) + .with_action(util_ui.tr('Export as text'), lambda: history_loader.export_history(self._contact)) + .with_action(util_ui.tr('Export as HTML'), lambda: history_loader.export_history(self._contact, False)) + ) + + return history_menu_builder + class FriendMenuGenerator(BaseContactMenuGenerator): - def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service): - history_menu_builder = self._generate_history_menu_builder(main_screen, number) + def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service, history_loader): + history_menu_builder = self._generate_history_menu_builder(history_loader, main_screen) copy_menu_builder = self._generate_copy_menu_builder(main_screen) plugins_menu_builder = self._generate_plugins_menu_builder(plugin_loader, number) groups_menu_builder = self._generate_groups_menu(contacts_manager, groups_service) @@ -132,18 +148,6 @@ class FriendMenuGenerator(BaseContactMenuGenerator): # Private methods # ----------------------------------------------------------------------------------------------------------------- - @staticmethod - def _generate_history_menu_builder(main_screen, number): - history_menu_builder = ContactMenuBuilder() - (history_menu_builder - .with_name(util_ui.tr('Chat history')) - .with_action(util_ui.tr('Clear history'), lambda: main_screen.clear_history(number)) - .with_action(util_ui.tr('Export as text'), lambda: main_screen.export_history(number)) - .with_action(util_ui.tr('Export as HTML'), lambda: main_screen.export_history(number, False)) - ) - - return history_menu_builder - @staticmethod def _generate_plugins_menu_builder(plugin_loader, number): if plugin_loader is None: @@ -174,13 +178,15 @@ class FriendMenuGenerator(BaseContactMenuGenerator): class GroupMenuGenerator(BaseContactMenuGenerator): - def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service): + def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service, history_loader): copy_menu_builder = self._generate_copy_menu_builder(main_screen) + history_menu_builder = self._generate_history_menu_builder(history_loader, main_screen) builder = ContactMenuBuilder() menu = (builder .with_action(util_ui.tr('Set alias'), lambda: main_screen.set_alias(number)) .with_submenu(copy_menu_builder) + .with_submenu(history_menu_builder) .with_optional_action(util_ui.tr('Manage group'), lambda: groups_service.show_group_management_screen(self._contact), self._contact.is_self_founder()) @@ -206,13 +212,15 @@ class GroupMenuGenerator(BaseContactMenuGenerator): class GroupPeerMenuGenerator(BaseContactMenuGenerator): - def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service): + def generate(self, plugin_loader, contacts_manager, main_screen, settings, number, groups_service, history_loader): copy_menu_builder = self._generate_copy_menu_builder(main_screen) + history_menu_builder = self._generate_history_menu_builder(history_loader, main_screen) builder = ContactMenuBuilder() menu = (builder .with_action(util_ui.tr('Set alias'), lambda: main_screen.set_alias(number)) .with_submenu(copy_menu_builder) + .with_submenu(history_menu_builder) .with_action(util_ui.tr('Quit chat'), lambda: contacts_manager.remove_group_peer(self._contact)) .with_action(util_ui.tr('Notes'), lambda: main_screen.show_note(self._contact)) diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 23dc61e..50dbc87 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -290,10 +290,6 @@ class ContactsManager(ToxSave): def friend_public_key(self, num): return self._contacts[num].tox_id - def export_history(self, num, as_text): - contact = self._contacts[num] - return self._history.export_history(contact, as_text) - def delete_friend(self, num): """ Removes friend from contact list diff --git a/toxygen/history/history.py b/toxygen/history/history.py index 4850f7d..f551df5 100644 --- a/toxygen/history/history.py +++ b/toxygen/history/history.py @@ -46,6 +46,20 @@ class History: friend.clear_corr(save_unsent) self._db.delete_friend_from_db(friend.tox_id) + def export_history(self, contact, as_text=True): + extension = 'txt' if as_text else 'html' + file_name, _ = util_ui.save_file_dialog(util_ui.tr('Choose file name'), extension) + + if not file_name: + return + + if not file_name.endswith('.' + extension): + file_name += '.' + extension + + history = self.generate_history(contact, as_text) + with open(file_name, 'wt') as fl: + fl.write(history) + def delete_message(self, message): contact = self._contacts_manager.get_curr_contact() if message.type in (MESSAGE_TYPE['TEXT'], MESSAGE_TYPE['ACTION']): @@ -93,7 +107,7 @@ class History: self._db.add_friend_to_db(tox_id) @staticmethod - def export_history(contact, as_text=True, _range=None): + def generate_history(contact, as_text=True, _range=None): if _range is None: contact.load_all_corr() corr = contact.get_corr() diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index 6217b47..c89f811 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -606,7 +606,7 @@ class MainWindow(QtWidgets.QMainWindow): return generator = contact.get_context_menu_generator() self.listMenu = generator.generate(self._plugins_loader, self._contacts_manager, self, self._settings, number, - self._groups_service) + self._groups_service, self._history_loader) parent_position = self.friends_list.mapToGlobal(QtCore.QPoint(0, 0)) self.listMenu.move(parent_position + pos) self.listMenu.show() @@ -625,19 +625,6 @@ class MainWindow(QtWidgets.QMainWindow): self.note = MultilineEdit(user, note, save_note) self.note.show() - def export_history(self, num, as_text=True): - s = self._contacts_manager.export_history(num, as_text) - extension = 'txt' if as_text else 'html' - file_name, _ = util_ui.save_file_dialog(util_ui.tr('Choose file name'), extension) - - if not file_name: - return - - if not file_name.endswith('.' + extension): - file_name += '.' + extension - with open(file_name, 'wt') as fl: - fl.write(s) - def set_alias(self, num): self._contacts_manager.set_alias(num) @@ -652,9 +639,6 @@ class MainWindow(QtWidgets.QMainWindow): def copy_text(text): util_ui.copy_to_clipboard(text) - def clear_history(self, num): - self._history_loader.clear_history(num) - def auto_accept(self, num, value): tox_id = self._contacts_manager.friend_public_key(num) if value: