From 9516723c7fba5aae6747f2248fdd735e05211448 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 28 Jul 2016 23:00:04 +0300 Subject: [PATCH] chat history export --- toxygen/friend.py | 8 ++++++++ toxygen/mainscreen.py | 20 ++++++++++++++------ toxygen/menu.py | 2 +- toxygen/profile.py | 29 ++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/toxygen/friend.py b/toxygen/friend.py index 6d20a65..4e57f0b 100644 --- a/toxygen/friend.py +++ b/toxygen/friend.py @@ -65,6 +65,14 @@ class Friend(contact.Contact): self._corr = data + self._corr self._history_loaded = True + def load_all_corr(self): + data = list(self._message_getter.get_all()) + if data is not None and len(data): + data.reverse() + data = list(map(lambda tupl: TextMessage(*tupl), data)) + self._corr = data + self._corr + self._history_loaded = True + def get_corr_for_saving(self): """ Get data to save in db diff --git a/toxygen/mainscreen.py b/toxygen/mainscreen.py index 0336cee..b65a6ad 100644 --- a/toxygen/mainscreen.py +++ b/toxygen/mainscreen.py @@ -76,7 +76,7 @@ class MainWindow(QtGui.QMainWindow, Singleton): self.actionAbout_program.triggered.connect(self.about_program) self.actionNetwork.triggered.connect(self.network_settings) self.actionAdd_friend.triggered.connect(self.add_contact) - self.actionSettings.triggered.connect(self.profilesettings) + self.actionSettings.triggered.connect(self.profile_settings) self.actionPrivacy_settings.triggered.connect(self.privacy_settings) self.actionInterface_settings.triggered.connect(self.interface_settings) self.actionNotifications.triggered.connect(self.notification_settings) @@ -195,9 +195,9 @@ class MainWindow(QtGui.QMainWindow, Singleton): Form.status_message.setObjectName("status_message") self.connection_status = Form.connection_status = StatusCircle(Form) Form.connection_status.setGeometry(QtCore.QRect(230, 35, 32, 32)) - self.avatar_label.mouseReleaseEvent = self.profilesettings - self.status_message.mouseReleaseEvent = self.profilesettings - self.name.mouseReleaseEvent = self.profilesettings + self.avatar_label.mouseReleaseEvent = self.profile_settings + self.status_message.mouseReleaseEvent = self.profile_settings + self.name.mouseReleaseEvent = self.profile_settings self.connection_status.raise_() Form.connection_status.setObjectName("connection_status") @@ -380,7 +380,7 @@ class MainWindow(QtGui.QMainWindow, Singleton): self.a_c = AddContact(link or '') self.a_c.show() - def profilesettings(self, *args): + def profile_settings(self, *args): self.p_s = ProfileSettings() self.p_s.show() @@ -521,7 +521,12 @@ class MainWindow(QtGui.QMainWindow, Singleton): if item is not None: self.listMenu = QtGui.QMenu() set_alias_item = self.listMenu.addAction(QtGui.QApplication.translate("MainWindow", 'Set alias', None, QtGui.QApplication.UnicodeUTF8)) - clear_history_item = self.listMenu.addAction(QtGui.QApplication.translate("MainWindow", 'Clear history', None, QtGui.QApplication.UnicodeUTF8)) + + history_menu = self.listMenu.addMenu(QtGui.QApplication.translate("MainWindow", 'Chat history', None, QtGui.QApplication.UnicodeUTF8)) + clear_history_item = history_menu.addAction(QtGui.QApplication.translate("MainWindow", 'Clear history', None, QtGui.QApplication.UnicodeUTF8)) + export_to_text_item = history_menu.addAction(QtGui.QApplication.translate("MainWindow", 'Export as text', None, QtGui.QApplication.UnicodeUTF8)) + export_to_html_item = history_menu.addAction(QtGui.QApplication.translate("MainWindow", 'Export as HTML', None, QtGui.QApplication.UnicodeUTF8)) + copy_menu = self.listMenu.addMenu(QtGui.QApplication.translate("MainWindow", 'Copy', None, QtGui.QApplication.UnicodeUTF8)) copy_name_item = copy_menu.addAction(QtGui.QApplication.translate("MainWindow", 'Name', None, QtGui.QApplication.UnicodeUTF8)) copy_status_item = copy_menu.addAction(QtGui.QApplication.translate("MainWindow", 'Status message', None, QtGui.QApplication.UnicodeUTF8)) @@ -543,6 +548,9 @@ class MainWindow(QtGui.QMainWindow, Singleton): self.connect(notes_item, QtCore.SIGNAL("triggered()"), lambda: self.show_note(friend)) self.connect(copy_name_item, QtCore.SIGNAL("triggered()"), lambda: self.copy_name(friend)) self.connect(copy_status_item, QtCore.SIGNAL("triggered()"), lambda: self.copy_status(friend)) + self.connect(export_to_text_item, QtCore.SIGNAL("triggered()"), lambda: self.profile.export_history(num)) + self.connect(export_to_html_item, QtCore.SIGNAL("triggered()"), + lambda: self.profile.export_history(num, False)) parent_position = self.friends_list.mapToGlobal(QtCore.QPoint(0, 0)) self.listMenu.move(parent_position + pos) self.listMenu.show() diff --git a/toxygen/menu.py b/toxygen/menu.py index e6a1156..0d83a2e 100644 --- a/toxygen/menu.py +++ b/toxygen/menu.py @@ -283,7 +283,7 @@ class ProfileSettings(CenteredWidget): settings = Settings.get_instance() settings.export(directory) profile = Profile.get_instance() - profile.export_history(directory) + profile.export_db(directory) ProfileHelper.get_instance().export_profile(directory, reply == QtGui.QMessageBox.Yes) def closeEvent(self, event): diff --git a/toxygen/profile.py b/toxygen/profile.py index 2ab45d9..294427a 100644 --- a/toxygen/profile.py +++ b/toxygen/profile.py @@ -514,9 +514,36 @@ class Profile(contact.Contact, Singleton): False) self._load_history = True - def export_history(self, directory): + def export_db(self, directory): self._history.export(directory) + def export_history(self, num, as_text=True, _range=None): + friend = self._friends[num] + if _range is None: + friend.load_all_corr() + corr = friend.get_corr() if _range is None else friend.get_corr()[_range[0]:_range[1]] + arr = [] + new_line = '\n' if as_text else '
' + for message in corr: + if type(message) is TextMessage: + data = message.get_data() + if as_text: + x = '[{}] {}: {}\n' + else: + x = '[{}] {}: {}
' + arr.append(x.format(convert_time(data[2]) if data[1] != MESSAGE_OWNER['NOT_SENT'] else 'Unsent', + friend.name if data[1] == MESSAGE_OWNER['FRIEND'] else self.name, + data[0])) + s = new_line.join(arr) + directory = QtGui.QFileDialog.getExistingDirectory(None, + QtGui.QApplication.translate("MainWindow", 'Choose folder', None, QtGui.QApplication.UnicodeUTF8), + curr_directory(), + QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontUseNativeDialog) + if directory: + name = 'exported_history_{}.{}'.format(convert_time(time.time()), 'txt' if as_text else 'html') + with open(directory + '/' + name, 'wt') as fl: + fl.write(s) + # ----------------------------------------------------------------------------------------------------------------- # Factories for friend, message and file transfer items # -----------------------------------------------------------------------------------------------------------------