From 14a71a0f3fba1437a91d60a5ec5a457beae0696e Mon Sep 17 00:00:00 2001 From: apprb Date: Tue, 14 Oct 2014 14:49:27 +0900 Subject: [PATCH] precise timestamps keeping --- src/historykeeper.cpp | 21 +++++++++++---------- src/historykeeper.h | 6 +++--- src/widget/form/chatform.cpp | 18 +++++++++++------- src/widget/form/genericchatform.cpp | 5 ++--- src/widget/form/genericchatform.h | 11 ++++------- src/widget/widget.cpp | 11 ++++++----- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/historykeeper.cpp b/src/historykeeper.cpp index e7399145b..4fa48f175 100644 --- a/src/historykeeper.cpp +++ b/src/historykeeper.cpp @@ -86,7 +86,7 @@ HistoryKeeper::HistoryKeeper(const QString &path, bool encr) : message */ - db.exec(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, ") + + db.exec(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, ") + QString("profile_id INTEGER NOT NULL, chat_id INTEGER NOT NULL, sender INTERGER NOT NULL, mtype INTEGER NOT NULL, message TEXT NOT NULL);")); db.exec(QString("CREATE TABLE IF NOT EXISTS aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT UNIQUE NOT NULL);")); db.exec(QString("CREATE TABLE IF NOT EXISTS chats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, ctype INTEGER NOT NULL);")); @@ -100,14 +100,14 @@ HistoryKeeper::~HistoryKeeper() db.close(); } -void HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender) +void HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt) { int chat_id = getChatID(chat, ctSingle).first; int sender_id = getAliasID(sender); int profile_id = getCurrentProfileID(); - db.exec(QString("INSERT INTO history (profile_id, chat_id, sender, mtype, message) VALUES (%1, %2, %3, 0, '%4');") - .arg(profile_id).arg(chat_id).arg(sender_id).arg(wrapMessage(message))); + db.exec(QString("INSERT INTO history (profile_id, timestamp, chat_id, sender, mtype, message) VALUES (%1, %2, %3, %4, 0, '%5');") + .arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message))); } QList HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile, @@ -118,8 +118,8 @@ QList HistoryKeeper::getChatHistory(HistoryKeeper::C QList res; - QString timeStr_from = time_from.toString("yyyy-MM-dd hh:mm:ss"); - QString timeStr_to = time_to.toString("yyyy-MM-dd hh:mm:ss"); + qint64 time64_from = time_from.toMSecsSinceEpoch(); + qint64 time64_to = time_to.toMSecsSinceEpoch(); int chat_id = getChatID(chat, ct).first; @@ -127,7 +127,7 @@ QList HistoryKeeper::getChatHistory(HistoryKeeper::C if (ct == ctSingle) { dbAnswer = db.exec(QString("SELECT timestamp, user_id, message FROM history INNER JOIN aliases ON history.sender = aliases.id ") + - QString("AND timestamp BETWEEN '%1' AND '%2' AND chat_id = %3;").arg(timeStr_from).arg(timeStr_to).arg(chat_id)); + QString("AND timestamp BETWEEN %1 AND %2 AND chat_id = %3;").arg(time64_from).arg(time64_to).arg(chat_id)); } else { // no groupchats yet } @@ -136,8 +136,8 @@ QList HistoryKeeper::getChatHistory(HistoryKeeper::C { QString sender = dbAnswer.value(1).toString(); QString message = unWrapMessage(dbAnswer.value(2).toString()); - QDateTime time = dbAnswer.value(0).toDateTime(); - time.setTimeSpec(Qt::UTC); + qint64 timeInt = dbAnswer.value(0).toLongLong(); + QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt); res.push_back({sender,message,time}); } @@ -221,11 +221,12 @@ void HistoryKeeper::resetInstance() historyInstance = nullptr; } -void HistoryKeeper::addGroupChatEntry(const QString &chat, const QString &message, const QString &sender) +void HistoryKeeper::addGroupChatEntry(const QString &chat, const QString &message, const QString &sender, const QDateTime &dt) { Q_UNUSED(chat) Q_UNUSED(message) Q_UNUSED(sender) + Q_UNUSED(dt) // no groupchats yet } diff --git a/src/historykeeper.h b/src/historykeeper.h index ad5092421..cf39d21e6 100644 --- a/src/historykeeper.h +++ b/src/historykeeper.h @@ -37,10 +37,10 @@ public: static void resetInstance(); virtual ~HistoryKeeper(); - void addChatEntry(const QString& chat, const QString& message, const QString& sender); - void addGroupChatEntry(const QString& chat, const QString& message, const QString& sender); + void addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt); + void addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt); QList getChatHistory(ChatType ct, const QString &profile, const QString &chat, - const QDateTime &time_from, const QDateTime &time_to = QDateTime::currentDateTimeUtc()); + const QDateTime &time_from, const QDateTime &time_to); void syncToDisk(); private: diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 4dab89526..6dbd74fdb 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -90,16 +90,17 @@ void ChatForm::onSendTriggered() if (msg.isEmpty()) return; QString name = Widget::getInstance()->getUsername(); - HistoryKeeper::getInstance()->addChatEntry(f->userId, msg, Core::getInstance()->getSelfId().publicKey); + QDateTime timestamp = QDateTime::currentDateTime(); + HistoryKeeper::getInstance()->addChatEntry(f->userId, msg, Core::getInstance()->getSelfId().publicKey, timestamp); if (msg.startsWith("/me ")) { msg = msg.right(msg.length() - 4); - addMessage(name, msg, true); + addMessage(name, msg, true, timestamp); emit sendAction(f->friendId, msg); } else { - addMessage(name, msg, false); + addMessage(name, msg, false, timestamp); emit sendMessage(f->friendId, msg); } msgEdit->clear(); @@ -493,7 +494,7 @@ void ChatForm::onFileSendFailed(int FriendId, const QString &fname) if (FriendId != f->friendId) return; - addSystemInfoMessage("File: \"" + fname + "\" failed to send.", "red"); + addSystemInfoMessage("File: \"" + fname + "\" failed to send.", "red", QDateTime::currentDateTime()); } void ChatForm::onAvatarChange(int FriendId, const QPixmap &pic) @@ -549,12 +550,12 @@ void ChatForm::onLoadHistory() if (*earliestMessage < fromTime) return; if (*earliestMessage < toTime) + { toTime = *earliestMessage; + toTime = toTime.addMSecs(-1); + } } - fromTime = fromTime.toUTC(); - toTime = toTime.toUTC(); - auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, Core::getInstance()->getSelfId().publicKey, f->userId, fromTime, toTime); @@ -578,8 +579,11 @@ void ChatForm::onLoadHistory() historyMessages.append(ca); int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value(); + chatWidget->getMesages().clear(); chatWidget->clear(); + if (earliestMessage != nullptr) + *earliestMessage = fromTime; for (ChatAction *ca : historyMessages) chatWidget->insertMessage(ca); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index a64a72bce..ac17f93c9 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -228,7 +228,7 @@ void GenericChatForm::clearChatArea(bool notinform) previousName = ""; if (!notinform) - addSystemInfoMessage(tr("Cleared"), "white"); + addSystemInfoMessage(tr("Cleared"), "white", QDateTime::currentDateTime()); if (earliestMessage) { @@ -237,8 +237,7 @@ void GenericChatForm::clearChatArea(bool notinform) } } -ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message, - bool isAction, const QDateTime &datetime) +ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime) { if (earliestMessage == nullptr) { diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index fab0d68e7..7a8477cef 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -46,9 +46,8 @@ public: virtual void setName(const QString &newName); virtual void show(Ui::MainWindow &ui); - void addMessage(const QString &author, const QString &message, bool isAction = false, - const QDateTime &datetime=QDateTime::currentDateTime()); - void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime()); + void addMessage(const QString &author, const QString &message, bool isAction, const QDateTime &datetime); + void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime); signals: void sendMessage(int, QString); @@ -66,10 +65,8 @@ protected slots: protected: QString getElidedName(const QString& name); - ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction = false, - const QDateTime &datetime=QDateTime::currentDateTime()); - ChatAction* genSystemInfoAction(const QString &message, const QString &type, - const QDateTime &datetime=QDateTime::currentDateTime()); + ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime); + ChatAction* genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime); QMenu menu; CroppingLabel *nameLabel; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 1c8e16eb3..33faca9a5 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -499,8 +499,9 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool if (!f) return; - f->chatForm->addMessage(f->getName(), message, isAction); - HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId); + QDateTime timestamp = QDateTime::currentDateTime(); + f->chatForm->addMessage(f->getName(), message, isAction, timestamp); + HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId, timestamp); if (activeChatroomWidget != nullptr) { @@ -606,7 +607,7 @@ void Widget::onGroupMessageReceived(int groupnumber, const QString& message, con if (!g) return; - g->chatForm->addMessage(author, message); + g->chatForm->addMessage(author, message, false, QDateTime::currentDateTime()); if ((static_cast(g->widget) != activeChatroomWidget) || isMinimized() || !isActiveWindow()) { @@ -736,7 +737,7 @@ void Widget::onMessageSendResult(int friendId, const QString& message, int messa return; if (!messageId) - f->chatForm->addSystemInfoMessage("Message failed to send", "red"); + f->chatForm->addSystemInfoMessage("Message failed to send", "red", QDateTime::currentDateTime()); } void Widget::onGroupSendResult(int groupId, const QString& message, int result) @@ -747,5 +748,5 @@ void Widget::onGroupSendResult(int groupId, const QString& message, int result) return; if (result == -1) - g->chatForm->addSystemInfoMessage("Message failed to send", "red"); + g->chatForm->addSystemInfoMessage("Message failed to send", "red", QDateTime::currentDateTime()); }