From 4c6205cc39e4bd282ee3eb690fdde2a7914e2638 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 1 Oct 2016 23:06:15 +0300 Subject: [PATCH] chat history fixes --- toxygen/friend.py | 3 ++- toxygen/history.py | 41 ++++++++++++++++++++++++++++++++++------- toxygen/mainscreen.py | 22 +++++++++++++--------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/toxygen/friend.py b/toxygen/friend.py index 4e57f0b..c730176 100644 --- a/toxygen/friend.py +++ b/toxygen/friend.py @@ -116,9 +116,10 @@ class Friend(contact.Contact): def delete_message(self, time): elem = list(filter(lambda x: type(x) is TextMessage and x.get_data()[2] == time, self._corr))[0] tmp = list(filter(lambda x: x.get_type() <= 1, self._corr)) - if elem in tmp[-self._unsaved_messages:]: + if elem in tmp[-self._unsaved_messages:] and self._unsaved_messages: self._unsaved_messages -= 1 self._corr.remove(elem) + self._message_getter.delete_one() def mark_as_sent(self): try: diff --git a/toxygen/history.py b/toxygen/history.py index c73f342..1cfe56d 100644 --- a/toxygen/history.py +++ b/toxygen/history.py @@ -159,20 +159,47 @@ class History: class MessageGetter: def __init__(self, name, tox_id): + self._count = 0 + self._name = name + self._tox_id = tox_id + self._db = self._cursor = None + + def connect(self): chdir(settings.ProfileHelper.get_path()) - self._db = connect(name + '.hstr', timeout=TIMEOUT) + self._db = connect(self._name + '.hstr', timeout=TIMEOUT) self._cursor = self._db.cursor() - self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id + + self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + self._tox_id + ' ORDER BY unix_time DESC;') + def disconnect(self): + self._db.close() + def get_one(self): - return self._cursor.fetchone() + self.connect() + self.skip() + data = self._cursor.fetchone() + self._count += 1 + self.disconnect() + return data def get_all(self): - return self._cursor.fetchall() + self.connect() + data = self._cursor.fetchall() + self.disconnect() + self._count = len(data) + return data def get(self, count): - return self._cursor.fetchmany(count) + self.connect() + self.skip() + data = self._cursor.fetchmany(count) + self.disconnect() + self._count += len(data) + return data - def __del__(self): - self._db.close() + def skip(self): + if self._count: + self._cursor.fetchmany(self._count) + + def delete_one(self): + self._count -= 1 diff --git a/toxygen/mainscreen.py b/toxygen/mainscreen.py index fc285ac..322880a 100644 --- a/toxygen/mainscreen.py +++ b/toxygen/mainscreen.py @@ -18,6 +18,7 @@ class MainWindow(QtGui.QMainWindow, Singleton): self.tray = tray self.setAcceptDrops(True) self.initUI(tox) + self._saved = False if settings.Settings.get_instance()['show_welcome_screen']: self.ws = WelcomeScreen() @@ -262,6 +263,7 @@ class MainWindow(QtGui.QMainWindow, Singleton): self.messages.setGeometry(0, 0, 620, 310) self.messages.setObjectName("messages") self.messages.setSpacing(1) + self.messages.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.messages.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.messages.focusOutEvent = lambda event: self.messages.clearSelection() @@ -332,15 +334,17 @@ class MainWindow(QtGui.QMainWindow, Singleton): def closeEvent(self, event): s = Settings.get_instance() if not s['close_to_tray'] or s.closing: - self.profile.save_history() - self.profile.close() - s['x'] = self.geometry().x() - s['y'] = self.geometry().y() - s['width'] = self.width() - s['height'] = self.height() - s.save() - QtGui.QApplication.closeAllWindows() - event.accept() + if not self._saved: + self._saved = True + self.profile.save_history() + self.profile.close() + s['x'] = self.geometry().x() + s['y'] = self.geometry().y() + s['width'] = self.width() + s['height'] = self.height() + s.save() + QtGui.QApplication.closeAllWindows() + event.accept() elif QtGui.QSystemTrayIcon.isSystemTrayAvailable(): event.ignore() self.hide()