From d55332ee0a0fdda8f63c6ac49f4feb8762bbcc9d Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Thu, 16 May 2019 23:00:15 -0700 Subject: [PATCH] fix(activity): change last activity time from QDate to QDateTime * Allows for strict sorting based on last activity in the future --- src/persistence/ifriendsettings.h | 4 +-- src/persistence/settings.cpp | 8 +++--- src/persistence/settings.h | 8 +++--- src/widget/form/chatform.cpp | 6 ++--- src/widget/form/genericchatform.cpp | 36 ++++++++++++------------- src/widget/form/genericchatform.h | 6 ++--- src/widget/friendlistwidget.cpp | 42 ++++++++++++++--------------- src/widget/friendlistwidget.h | 2 +- src/widget/widget.cpp | 23 +++++++--------- 9 files changed, 66 insertions(+), 69 deletions(-) diff --git a/src/persistence/ifriendsettings.h b/src/persistence/ifriendsettings.h index 857fc3be9..1718936d9 100644 --- a/src/persistence/ifriendsettings.h +++ b/src/persistence/ifriendsettings.h @@ -38,8 +38,8 @@ public: virtual int getFriendCircleID(const ToxPk& pk) const = 0; virtual void setFriendCircleID(const ToxPk& pk, int circleID) = 0; - virtual QDate getFriendActivity(const ToxPk& pk) const = 0; - virtual void setFriendActivity(const ToxPk& pk, const QDate& date) = 0; + virtual QDateTime getFriendActivity(const ToxPk& pk) const = 0; + virtual void setFriendActivity(const ToxPk& pk, const QDateTime& date) = 0; virtual void saveFriendSettings(const ToxPk& pk) = 0; virtual void removeFriendSettings(const ToxPk& pk) = 0; diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index 1f96bb275..706c1a041 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -319,7 +319,7 @@ void Settings::loadPersonal(QString profileName, const ToxEncrypt* passKey) fp.circleID = ps.value("circle", -1).toInt(); if (getEnableLogging()) - fp.activity = ps.value("activity", QDate()).toDate(); + fp.activity = ps.value("activity", QDateTime()).toDateTime(); friendLst.insert(ToxId(fp.addr).getPublicKey().getByteArray(), fp); } ps.endArray(); @@ -2031,17 +2031,17 @@ void Settings::setFriendCircleID(const ToxPk& id, int circleID) frnd.circleID = circleID; } -QDate Settings::getFriendActivity(const ToxPk& id) const +QDateTime Settings::getFriendActivity(const ToxPk& id) const { QMutexLocker locker{&bigLock}; auto it = friendLst.find(id.getByteArray()); if (it != friendLst.end()) return it->activity; - return QDate(); + return QDateTime(); } -void Settings::setFriendActivity(const ToxPk& id, const QDate& activity) +void Settings::setFriendActivity(const ToxPk& id, const QDateTime& activity) { QMutexLocker locker{&bigLock}; auto& frnd = getOrInsertFriendPropRef(id); diff --git a/src/persistence/settings.h b/src/persistence/settings.h index 6c04510a3..d7f2316c0 100644 --- a/src/persistence/settings.h +++ b/src/persistence/settings.h @@ -28,7 +28,7 @@ #include "src/persistence/ifriendsettings.h" #include "src/video/ivideosettings.h" -#include +#include #include #include #include @@ -497,8 +497,8 @@ public: int getFriendCircleID(const ToxPk& id) const override; void setFriendCircleID(const ToxPk& id, int circleID) override; - QDate getFriendActivity(const ToxPk& id) const override; - void setFriendActivity(const ToxPk& id, const QDate& date) override; + QDateTime getFriendActivity(const ToxPk& id) const override; + void setFriendActivity(const ToxPk& id, const QDateTime& date) override; void saveFriendSettings(const ToxPk& id) override; void removeFriendSettings(const ToxPk& id) override; @@ -699,7 +699,7 @@ private: QString autoAcceptDir = ""; QString note = ""; int circleID = -1; - QDate activity = QDate(); + QDateTime activity = QDateTime(); AutoAcceptCallFlags autoAcceptCall; bool autoGroupInvite = false; }; diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 9e7317f80..f2e296c3a 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -536,7 +536,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param const bool isFirst = (parameter.period == PeriodSearch::WithTheFirst); const bool isAfter = (parameter.period == PeriodSearch::AfterDate); if (isFirst || isAfter) { - if (isFirst || (isAfter && parameter.date < getFirstDate())) { + if (isFirst || (isAfter && parameter.date < getFirstTime().date())) { const QString pk = f->getPublicKey().toString(); if ((isFirst || parameter.date >= history->getStartDateChatHistory(pk).date()) && loadHistory(phrase, parameter)) { @@ -547,7 +547,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param onSearchDown(phrase, parameter); } else { - if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstDate()) { + if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstTime().date()) { const QString pk = f->getPublicKey().toString(); if (parameter.date >= history->getStartDateChatHistory(pk).date() && loadHistory(phrase, parameter)) { @@ -1167,7 +1167,7 @@ bool ChatForm::loadHistory(const QString& phrase, const ParameterSearch& paramet const QDateTime newBaseDate = history->getDateWhereFindPhrase(pk, earliestMessage, phrase, parameter); - if (newBaseDate.isValid() && getFirstDate().isValid() && newBaseDate.date() < getFirstDate()) { + if (newBaseDate.isValid() && getFirstTime().isValid() && newBaseDate.date() < getFirstTime().date()) { searchAfterLoadHistory = true; loadHistoryByDateRange(newBaseDate); diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index 19bae9d52..2f195b089 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -285,14 +285,14 @@ void GenericChatForm::hideFileMenu() fileFlyout->animateHide(); } -QDate GenericChatForm::getLatestDate() const +QDateTime GenericChatForm::getLatestTime() const { - return getDate(chatWidget->getLatestLine()); + return getTime(chatWidget->getLatestLine()); } -QDate GenericChatForm::getFirstDate() const +QDateTime GenericChatForm::getFirstTime() const { - return getDate(chatWidget->getFirstLine()); + return getTime(chatWidget->getFirstLine()); } void GenericChatForm::reloadTheme() @@ -401,7 +401,8 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri bool isSelf = author == core->getSelfId().getPublicKey(); QString myNickName = core->getUsername().isEmpty() ? author.toString() : core->getUsername(); QString authorStr = isSelf ? myNickName : resolveToxPk(author); - if (getLatestDate() != QDate::currentDate()) { + const auto now = QDateTime::currentDateTime(); + if (getLatestTime().date() != now.date()) { addSystemDateMessage(); } @@ -411,13 +412,12 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri previousId = ToxPk{}; } else { msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf, QDateTime(), colorizeName); - const QDateTime newMsgDateTime = QDateTime::currentDateTime(); - if (needsToHideName(author, newMsgDateTime)) { + if (needsToHideName(author, now)) { msg->hideSender(); } previousId = author; - prevMsgDateTime = newMsgDateTime; + prevMsgDateTime = now; } if (isSent) { @@ -552,7 +552,7 @@ void GenericChatForm::onChatMessageFontChanged(const QFont& font) void GenericChatForm::addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type, const QDateTime& datetime) { - if (getLatestDate() != QDate::currentDate()) { + if (getLatestTime().date() != QDate::currentDate()) { addSystemDateMessage(); } @@ -569,19 +569,19 @@ void GenericChatForm::addSystemDateMessage() insertChatMessage(ChatMessage::createChatInfoMessage(dateText, ChatMessage::INFO, QDateTime())); } -QDate GenericChatForm::getDate(const ChatLine::Ptr &chatLine) const +QDateTime GenericChatForm::getTime(const ChatLine::Ptr &chatLine) const { if (chatLine) { Timestamp* const timestamp = qobject_cast(chatLine->getContent(2)); if (timestamp) { - return timestamp->getTime().date(); + return timestamp->getTime(); } else { - return QDate::currentDate(); + return QDateTime::currentDateTime(); } } - return QDate(); + return QDateTime(); } void GenericChatForm::disableSearchText() @@ -625,7 +625,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& startLine = 0; } else if (parameter.period == PeriodSearch::AfterDate) { const auto lambda = [=](const ChatLine::Ptr& item) { - const auto d = getDate(item); + const auto d = getTime(item).date(); return d.isValid() && parameter.date <= d; }; @@ -637,7 +637,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& } else if (parameter.period == PeriodSearch::BeforeDate) { #if QT_VERSION > QT_VERSION_CHECK(5, 6, 0) const auto lambda = [=](const ChatLine::Ptr& item) { - const auto d = getDate(item); + const auto d = getTime(item).date(); return d.isValid() && parameter.date >= d; }; @@ -648,7 +648,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& } #else for (int i = lines.size() - 1; i >= 0; --i) { - auto d = getDate(lines[i]); + auto d = getTime(lines[i]).date(); if (d.isValid() && parameter.date >= d) { startLine = i; break; @@ -1001,9 +1001,9 @@ void GenericChatForm::onSearchTriggered() void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line) { - const auto date = getDate(line); + const auto date = getTime(line); - if (date.isValid() && date != QDate::currentDate()) { + if (date.isValid() && date != QDateTime::currentDateTime()) { const auto dateText = QStringLiteral("%1<\b>").arg(date.toString(Settings::getInstance().getDateFormat())); dateInfo->setText(dateText); dateInfo->setVisible(true); diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index 4167a5b96..6edfc5031 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -82,8 +82,8 @@ public: const QDateTime& datetime); void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool colorizeName = false); static QString resolveToxPk(const ToxPk& pk); - QDate getLatestDate() const; - QDate getFirstDate() const; + QDateTime getLatestTime() const; + QDateTime getFirstTime() const; signals: void sendMessage(uint32_t, QString); @@ -125,7 +125,7 @@ protected slots: private: void retranslateUi(); void addSystemDateMessage(); - QDate getDate(const ChatLine::Ptr& chatLine) const; + QDateTime getTime(const ChatLine::Ptr& chatLine) const; protected: ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index a4da86a5b..818ef1fc6 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -52,9 +52,9 @@ enum class Time static const int LAST_TIME = static_cast(Time::Never); -Time getTime(const QDate& date) +Time getTimeBucket(const QDateTime& date) { - if (date == QDate()) { + if (date == QDateTime()) { return Time::Never; } @@ -74,7 +74,7 @@ Time getTime(const QDate& date) // clang-format on for (Time time : dates.keys()) { - if (dates[time] <= date) { + if (dates[time] <= date.date()) { return time; } } @@ -82,7 +82,7 @@ Time getTime(const QDate& date) return Time::LongAgo; } -QDate getDateFriend(const Friend* contact) +QDateTime getActiveTimeFriend(const Friend* contact) { return Settings::getInstance().getFriendActivity(contact->getPublicKey()); } @@ -263,10 +263,10 @@ void FriendListWidget::moveFriends(QLayout* layout) circleWidget->moveFriendWidgets(this); } else if (friendWidget) { const Friend* contact = friendWidget->getFriend(); - QDate activityDate = getDateFriend(contact); - int time = static_cast(getTime(activityDate)); + const auto activityTime = getActiveTimeFriend(contact); + int timeIndex = static_cast(getTimeBucket(activityTime)); - QWidget* w = activityLayout->itemAt(time)->widget(); + QWidget* w = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = qobject_cast(w); categoryWidget->addFriendWidget(friendWidget, contact->getStatus()); } @@ -309,9 +309,9 @@ void FriendListWidget::removeFriendWidget(FriendWidget* w) { const Friend* contact = w->getFriend(); if (mode == Activity) { - QDate activityDate = getDateFriend(contact); - int time = static_cast(getTime(activityDate)); - QWidget* widget = activityLayout->itemAt(time)->widget(); + const auto activityTime = getActiveTimeFriend(contact); + int timeIndex = static_cast(getTimeBucket(activityTime)); + QWidget* widget = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = qobject_cast(widget); categoryWidget->removeFriendWidget(w, contact->getStatus()); categoryWidget->setVisible(categoryWidget->hasChatrooms()); @@ -401,9 +401,9 @@ void FriendListWidget::onFriendWidgetRenamed(FriendWidget* friendWidget) const Friend* contact = friendWidget->getFriend(); auto status = contact->getStatus(); if (mode == Activity) { - QDate activityDate = getDateFriend(contact); - int time = static_cast(getTime(activityDate)); - QWidget* widget = activityLayout->itemAt(time)->widget(); + const auto activityTime = getActiveTimeFriend(contact); + int timeIndex = static_cast(getTimeBucket(activityTime)); + QWidget* widget = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = qobject_cast(widget); categoryWidget->removeFriendWidget(friendWidget, status); categoryWidget->addFriendWidget(friendWidget, status); @@ -452,8 +452,8 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget return; } - QDate activityDate = getDateFriend(friendWidget->getFriend()); - index = static_cast(getTime(activityDate)); + const auto activityTime = getActiveTimeFriend(friendWidget->getFriend()); + index = static_cast(getTimeBucket(activityTime)); QWidget* widget = activityLayout->itemAt(index)->widget(); CategoryWidget* categoryWidget = qobject_cast(widget); @@ -627,22 +627,22 @@ void FriendListWidget::moveWidget(FriendWidget* widget, Status::Status s, bool a circleWidget->addFriendWidget(widget, s); } else { const Friend* contact = widget->getFriend(); - QDate activityDate = getDateFriend(contact); - int time = static_cast(getTime(activityDate)); - QWidget* w = activityLayout->itemAt(time)->widget(); + const auto activityTime = getActiveTimeFriend(contact); + int timeIndex = static_cast(getTimeBucket(activityTime)); + QWidget* w = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = qobject_cast(w); categoryWidget->addFriendWidget(widget, contact->getStatus()); categoryWidget->show(); } } -void FriendListWidget::updateActivityDate(const QDate& date) +void FriendListWidget::updateActivityTime(const QDateTime& time) { if (mode != Activity) return; - int time = static_cast(getTime(date)); - QWidget* widget = activityLayout->itemAt(time)->widget(); + int timeIndex = static_cast(getTimeBucket(time)); + QWidget* widget = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = static_cast(widget); categoryWidget->updateStatus(); diff --git a/src/widget/friendlistwidget.h b/src/widget/friendlistwidget.h index 09279d60d..1f69a7283 100644 --- a/src/widget/friendlistwidget.h +++ b/src/widget/friendlistwidget.h @@ -62,7 +62,7 @@ public: void cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward); - void updateActivityDate(const QDate& date); + void updateActivityTime(const QDateTime& date); void reDraw(); signals: diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 76753d778..a6da70404 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -1001,10 +1001,10 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk) friendWidgets[friendPk] = widget; chatForms[friendPk] = friendForm; - QDate activityDate = settings.getFriendActivity(friendPk); - QDate chatDate = friendForm->getLatestDate(); - if (chatDate > activityDate && chatDate.isValid()) { - settings.setFriendActivity(friendPk, chatDate); + const auto activityTime = settings.getFriendActivity(friendPk); + const auto chatTime = friendForm->getLatestTime(); + if (chatTime > activityTime && chatTime.isValid()) { + settings.setFriendActivity(friendPk, chatTime); } contactListWidget->addFriendWidget(widget, Status::Status::Offline, settings.getFriendCircleID(friendPk)); @@ -1493,15 +1493,12 @@ void Widget::onFriendRequestReceived(const ToxPk& friendPk, const QString& messa void Widget::updateFriendActivity(const Friend* frnd) { const ToxPk& pk = frnd->getPublicKey(); - QDate date = settings.getFriendActivity(pk); - if (date != QDate::currentDate()) { - // Update old activity before after new one. Store old date first. - QDate oldDate = settings.getFriendActivity(pk); - settings.setFriendActivity(pk, QDate::currentDate()); - FriendWidget* widget = friendWidgets[frnd->getPublicKey()]; - contactListWidget->moveWidget(widget, frnd->getStatus()); - contactListWidget->updateActivityDate(oldDate); - } + const auto oldTime = settings.getFriendActivity(pk); + const auto newTime = QDateTime::currentDateTime(); + settings.setFriendActivity(pk, newTime); + FriendWidget* widget = friendWidgets[frnd->getPublicKey()]; + contactListWidget->moveWidget(widget, frnd->getStatus()); + contactListWidget->updateActivityTime(oldTime); // update old category widget } void Widget::removeFriend(Friend* f, bool fake)