1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(activity): change last activity time from QDate to QDateTime

* Allows for strict sorting based on last activity in the future
This commit is contained in:
Anthony Bilinski 2019-05-16 23:00:15 -07:00
parent ca397aebbf
commit d55332ee0a
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
9 changed files with 66 additions and 69 deletions

View File

@ -38,8 +38,8 @@ public:
virtual int getFriendCircleID(const ToxPk& pk) const = 0; virtual int getFriendCircleID(const ToxPk& pk) const = 0;
virtual void setFriendCircleID(const ToxPk& pk, int circleID) = 0; virtual void setFriendCircleID(const ToxPk& pk, int circleID) = 0;
virtual QDate getFriendActivity(const ToxPk& pk) const = 0; virtual QDateTime getFriendActivity(const ToxPk& pk) const = 0;
virtual void setFriendActivity(const ToxPk& pk, const QDate& date) = 0; virtual void setFriendActivity(const ToxPk& pk, const QDateTime& date) = 0;
virtual void saveFriendSettings(const ToxPk& pk) = 0; virtual void saveFriendSettings(const ToxPk& pk) = 0;
virtual void removeFriendSettings(const ToxPk& pk) = 0; virtual void removeFriendSettings(const ToxPk& pk) = 0;

View File

@ -319,7 +319,7 @@ void Settings::loadPersonal(QString profileName, const ToxEncrypt* passKey)
fp.circleID = ps.value("circle", -1).toInt(); fp.circleID = ps.value("circle", -1).toInt();
if (getEnableLogging()) if (getEnableLogging())
fp.activity = ps.value("activity", QDate()).toDate(); fp.activity = ps.value("activity", QDateTime()).toDateTime();
friendLst.insert(ToxId(fp.addr).getPublicKey().getByteArray(), fp); friendLst.insert(ToxId(fp.addr).getPublicKey().getByteArray(), fp);
} }
ps.endArray(); ps.endArray();
@ -2031,17 +2031,17 @@ void Settings::setFriendCircleID(const ToxPk& id, int circleID)
frnd.circleID = circleID; frnd.circleID = circleID;
} }
QDate Settings::getFriendActivity(const ToxPk& id) const QDateTime Settings::getFriendActivity(const ToxPk& id) const
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getByteArray()); auto it = friendLst.find(id.getByteArray());
if (it != friendLst.end()) if (it != friendLst.end())
return it->activity; 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}; QMutexLocker locker{&bigLock};
auto& frnd = getOrInsertFriendPropRef(id); auto& frnd = getOrInsertFriendPropRef(id);

View File

@ -28,7 +28,7 @@
#include "src/persistence/ifriendsettings.h" #include "src/persistence/ifriendsettings.h"
#include "src/video/ivideosettings.h" #include "src/video/ivideosettings.h"
#include <QDate> #include <QDateTime>
#include <QFlags> #include <QFlags>
#include <QFont> #include <QFont>
#include <QHash> #include <QHash>
@ -497,8 +497,8 @@ public:
int getFriendCircleID(const ToxPk& id) const override; int getFriendCircleID(const ToxPk& id) const override;
void setFriendCircleID(const ToxPk& id, int circleID) override; void setFriendCircleID(const ToxPk& id, int circleID) override;
QDate getFriendActivity(const ToxPk& id) const override; QDateTime getFriendActivity(const ToxPk& id) const override;
void setFriendActivity(const ToxPk& id, const QDate& date) override; void setFriendActivity(const ToxPk& id, const QDateTime& date) override;
void saveFriendSettings(const ToxPk& id) override; void saveFriendSettings(const ToxPk& id) override;
void removeFriendSettings(const ToxPk& id) override; void removeFriendSettings(const ToxPk& id) override;
@ -699,7 +699,7 @@ private:
QString autoAcceptDir = ""; QString autoAcceptDir = "";
QString note = ""; QString note = "";
int circleID = -1; int circleID = -1;
QDate activity = QDate(); QDateTime activity = QDateTime();
AutoAcceptCallFlags autoAcceptCall; AutoAcceptCallFlags autoAcceptCall;
bool autoGroupInvite = false; bool autoGroupInvite = false;
}; };

View File

@ -536,7 +536,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param
const bool isFirst = (parameter.period == PeriodSearch::WithTheFirst); const bool isFirst = (parameter.period == PeriodSearch::WithTheFirst);
const bool isAfter = (parameter.period == PeriodSearch::AfterDate); const bool isAfter = (parameter.period == PeriodSearch::AfterDate);
if (isFirst || isAfter) { if (isFirst || isAfter) {
if (isFirst || (isAfter && parameter.date < getFirstDate())) { if (isFirst || (isAfter && parameter.date < getFirstTime().date())) {
const QString pk = f->getPublicKey().toString(); const QString pk = f->getPublicKey().toString();
if ((isFirst || parameter.date >= history->getStartDateChatHistory(pk).date()) if ((isFirst || parameter.date >= history->getStartDateChatHistory(pk).date())
&& loadHistory(phrase, parameter)) { && loadHistory(phrase, parameter)) {
@ -547,7 +547,7 @@ void ChatForm::searchInBegin(const QString& phrase, const ParameterSearch& param
onSearchDown(phrase, parameter); onSearchDown(phrase, parameter);
} else { } else {
if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstDate()) { if (parameter.period == PeriodSearch::BeforeDate && parameter.date < getFirstTime().date()) {
const QString pk = f->getPublicKey().toString(); const QString pk = f->getPublicKey().toString();
if (parameter.date >= history->getStartDateChatHistory(pk).date() if (parameter.date >= history->getStartDateChatHistory(pk).date()
&& loadHistory(phrase, parameter)) { && loadHistory(phrase, parameter)) {
@ -1167,7 +1167,7 @@ bool ChatForm::loadHistory(const QString& phrase, const ParameterSearch& paramet
const QDateTime newBaseDate = const QDateTime newBaseDate =
history->getDateWhereFindPhrase(pk, earliestMessage, phrase, parameter); 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; searchAfterLoadHistory = true;
loadHistoryByDateRange(newBaseDate); loadHistoryByDateRange(newBaseDate);

View File

@ -285,14 +285,14 @@ void GenericChatForm::hideFileMenu()
fileFlyout->animateHide(); 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() void GenericChatForm::reloadTheme()
@ -401,7 +401,8 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri
bool isSelf = author == core->getSelfId().getPublicKey(); bool isSelf = author == core->getSelfId().getPublicKey();
QString myNickName = core->getUsername().isEmpty() ? author.toString() : core->getUsername(); QString myNickName = core->getUsername().isEmpty() ? author.toString() : core->getUsername();
QString authorStr = isSelf ? myNickName : resolveToxPk(author); QString authorStr = isSelf ? myNickName : resolveToxPk(author);
if (getLatestDate() != QDate::currentDate()) { const auto now = QDateTime::currentDateTime();
if (getLatestTime().date() != now.date()) {
addSystemDateMessage(); addSystemDateMessage();
} }
@ -411,13 +412,12 @@ ChatMessage::Ptr GenericChatForm::createMessage(const ToxPk& author, const QStri
previousId = ToxPk{}; previousId = ToxPk{};
} else { } else {
msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf, QDateTime(), colorizeName); msg = ChatMessage::createChatMessage(authorStr, message, ChatMessage::NORMAL, isSelf, QDateTime(), colorizeName);
const QDateTime newMsgDateTime = QDateTime::currentDateTime(); if (needsToHideName(author, now)) {
if (needsToHideName(author, newMsgDateTime)) {
msg->hideSender(); msg->hideSender();
} }
previousId = author; previousId = author;
prevMsgDateTime = newMsgDateTime; prevMsgDateTime = now;
} }
if (isSent) { if (isSent) {
@ -552,7 +552,7 @@ void GenericChatForm::onChatMessageFontChanged(const QFont& font)
void GenericChatForm::addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type, void GenericChatForm::addSystemInfoMessage(const QString& message, ChatMessage::SystemMessageType type,
const QDateTime& datetime) const QDateTime& datetime)
{ {
if (getLatestDate() != QDate::currentDate()) { if (getLatestTime().date() != QDate::currentDate()) {
addSystemDateMessage(); addSystemDateMessage();
} }
@ -569,19 +569,19 @@ void GenericChatForm::addSystemDateMessage()
insertChatMessage(ChatMessage::createChatInfoMessage(dateText, ChatMessage::INFO, QDateTime())); 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) { if (chatLine) {
Timestamp* const timestamp = qobject_cast<Timestamp*>(chatLine->getContent(2)); Timestamp* const timestamp = qobject_cast<Timestamp*>(chatLine->getContent(2));
if (timestamp) { if (timestamp) {
return timestamp->getTime().date(); return timestamp->getTime();
} else { } else {
return QDate::currentDate(); return QDateTime::currentDateTime();
} }
} }
return QDate(); return QDateTime();
} }
void GenericChatForm::disableSearchText() void GenericChatForm::disableSearchText()
@ -625,7 +625,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
startLine = 0; startLine = 0;
} else if (parameter.period == PeriodSearch::AfterDate) { } else if (parameter.period == PeriodSearch::AfterDate) {
const auto lambda = [=](const ChatLine::Ptr& item) { const auto lambda = [=](const ChatLine::Ptr& item) {
const auto d = getDate(item); const auto d = getTime(item).date();
return d.isValid() && parameter.date <= d; return d.isValid() && parameter.date <= d;
}; };
@ -637,7 +637,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
} else if (parameter.period == PeriodSearch::BeforeDate) { } else if (parameter.period == PeriodSearch::BeforeDate) {
#if QT_VERSION > QT_VERSION_CHECK(5, 6, 0) #if QT_VERSION > QT_VERSION_CHECK(5, 6, 0)
const auto lambda = [=](const ChatLine::Ptr& item) { const auto lambda = [=](const ChatLine::Ptr& item) {
const auto d = getDate(item); const auto d = getTime(item).date();
return d.isValid() && parameter.date >= d; return d.isValid() && parameter.date >= d;
}; };
@ -648,7 +648,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch&
} }
#else #else
for (int i = lines.size() - 1; i >= 0; --i) { 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) { if (d.isValid() && parameter.date >= d) {
startLine = i; startLine = i;
break; break;
@ -1001,9 +1001,9 @@ void GenericChatForm::onSearchTriggered()
void GenericChatForm::updateShowDateInfo(const ChatLine::Ptr& line) 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("<b>%1<\b>").arg(date.toString(Settings::getInstance().getDateFormat())); const auto dateText = QStringLiteral("<b>%1<\b>").arg(date.toString(Settings::getInstance().getDateFormat()));
dateInfo->setText(dateText); dateInfo->setText(dateText);
dateInfo->setVisible(true); dateInfo->setVisible(true);

View File

@ -82,8 +82,8 @@ public:
const QDateTime& datetime); const QDateTime& datetime);
void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool colorizeName = false); void addAlertMessage(const ToxPk& author, const QString& message, const QDateTime& datetime, bool colorizeName = false);
static QString resolveToxPk(const ToxPk& pk); static QString resolveToxPk(const ToxPk& pk);
QDate getLatestDate() const; QDateTime getLatestTime() const;
QDate getFirstDate() const; QDateTime getFirstTime() const;
signals: signals:
void sendMessage(uint32_t, QString); void sendMessage(uint32_t, QString);
@ -125,7 +125,7 @@ protected slots:
private: private:
void retranslateUi(); void retranslateUi();
void addSystemDateMessage(); void addSystemDateMessage();
QDate getDate(const ChatLine::Ptr& chatLine) const; QDateTime getTime(const ChatLine::Ptr& chatLine) const;
protected: protected:
ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message, ChatMessage::Ptr createMessage(const ToxPk& author, const QString& message,

View File

@ -52,9 +52,9 @@ enum class Time
static const int LAST_TIME = static_cast<int>(Time::Never); static const int LAST_TIME = static_cast<int>(Time::Never);
Time getTime(const QDate& date) Time getTimeBucket(const QDateTime& date)
{ {
if (date == QDate()) { if (date == QDateTime()) {
return Time::Never; return Time::Never;
} }
@ -74,7 +74,7 @@ Time getTime(const QDate& date)
// clang-format on // clang-format on
for (Time time : dates.keys()) { for (Time time : dates.keys()) {
if (dates[time] <= date) { if (dates[time] <= date.date()) {
return time; return time;
} }
} }
@ -82,7 +82,7 @@ Time getTime(const QDate& date)
return Time::LongAgo; return Time::LongAgo;
} }
QDate getDateFriend(const Friend* contact) QDateTime getActiveTimeFriend(const Friend* contact)
{ {
return Settings::getInstance().getFriendActivity(contact->getPublicKey()); return Settings::getInstance().getFriendActivity(contact->getPublicKey());
} }
@ -263,10 +263,10 @@ void FriendListWidget::moveFriends(QLayout* layout)
circleWidget->moveFriendWidgets(this); circleWidget->moveFriendWidgets(this);
} else if (friendWidget) { } else if (friendWidget) {
const Friend* contact = friendWidget->getFriend(); const Friend* contact = friendWidget->getFriend();
QDate activityDate = getDateFriend(contact); const auto activityTime = getActiveTimeFriend(contact);
int time = static_cast<int>(getTime(activityDate)); int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* w = activityLayout->itemAt(time)->widget(); QWidget* w = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w);
categoryWidget->addFriendWidget(friendWidget, contact->getStatus()); categoryWidget->addFriendWidget(friendWidget, contact->getStatus());
} }
@ -309,9 +309,9 @@ void FriendListWidget::removeFriendWidget(FriendWidget* w)
{ {
const Friend* contact = w->getFriend(); const Friend* contact = w->getFriend();
if (mode == Activity) { if (mode == Activity) {
QDate activityDate = getDateFriend(contact); const auto activityTime = getActiveTimeFriend(contact);
int time = static_cast<int>(getTime(activityDate)); int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(time)->widget(); QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
categoryWidget->removeFriendWidget(w, contact->getStatus()); categoryWidget->removeFriendWidget(w, contact->getStatus());
categoryWidget->setVisible(categoryWidget->hasChatrooms()); categoryWidget->setVisible(categoryWidget->hasChatrooms());
@ -401,9 +401,9 @@ void FriendListWidget::onFriendWidgetRenamed(FriendWidget* friendWidget)
const Friend* contact = friendWidget->getFriend(); const Friend* contact = friendWidget->getFriend();
auto status = contact->getStatus(); auto status = contact->getStatus();
if (mode == Activity) { if (mode == Activity) {
QDate activityDate = getDateFriend(contact); const auto activityTime = getActiveTimeFriend(contact);
int time = static_cast<int>(getTime(activityDate)); int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(time)->widget(); QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
categoryWidget->removeFriendWidget(friendWidget, status); categoryWidget->removeFriendWidget(friendWidget, status);
categoryWidget->addFriendWidget(friendWidget, status); categoryWidget->addFriendWidget(friendWidget, status);
@ -452,8 +452,8 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget
return; return;
} }
QDate activityDate = getDateFriend(friendWidget->getFriend()); const auto activityTime = getActiveTimeFriend(friendWidget->getFriend());
index = static_cast<int>(getTime(activityDate)); index = static_cast<int>(getTimeBucket(activityTime));
QWidget* widget = activityLayout->itemAt(index)->widget(); QWidget* widget = activityLayout->itemAt(index)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
@ -627,22 +627,22 @@ void FriendListWidget::moveWidget(FriendWidget* widget, Status::Status s, bool a
circleWidget->addFriendWidget(widget, s); circleWidget->addFriendWidget(widget, s);
} else { } else {
const Friend* contact = widget->getFriend(); const Friend* contact = widget->getFriend();
QDate activityDate = getDateFriend(contact); const auto activityTime = getActiveTimeFriend(contact);
int time = static_cast<int>(getTime(activityDate)); int timeIndex = static_cast<int>(getTimeBucket(activityTime));
QWidget* w = activityLayout->itemAt(time)->widget(); QWidget* w = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w);
categoryWidget->addFriendWidget(widget, contact->getStatus()); categoryWidget->addFriendWidget(widget, contact->getStatus());
categoryWidget->show(); categoryWidget->show();
} }
} }
void FriendListWidget::updateActivityDate(const QDate& date) void FriendListWidget::updateActivityTime(const QDateTime& time)
{ {
if (mode != Activity) if (mode != Activity)
return; return;
int time = static_cast<int>(getTime(date)); int timeIndex = static_cast<int>(getTimeBucket(time));
QWidget* widget = activityLayout->itemAt(time)->widget(); QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = static_cast<CategoryWidget*>(widget); CategoryWidget* categoryWidget = static_cast<CategoryWidget*>(widget);
categoryWidget->updateStatus(); categoryWidget->updateStatus();

View File

@ -62,7 +62,7 @@ public:
void cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward); void cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward);
void updateActivityDate(const QDate& date); void updateActivityTime(const QDateTime& date);
void reDraw(); void reDraw();
signals: signals:

View File

@ -1001,10 +1001,10 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
friendWidgets[friendPk] = widget; friendWidgets[friendPk] = widget;
chatForms[friendPk] = friendForm; chatForms[friendPk] = friendForm;
QDate activityDate = settings.getFriendActivity(friendPk); const auto activityTime = settings.getFriendActivity(friendPk);
QDate chatDate = friendForm->getLatestDate(); const auto chatTime = friendForm->getLatestTime();
if (chatDate > activityDate && chatDate.isValid()) { if (chatTime > activityTime && chatTime.isValid()) {
settings.setFriendActivity(friendPk, chatDate); settings.setFriendActivity(friendPk, chatTime);
} }
contactListWidget->addFriendWidget(widget, Status::Status::Offline, settings.getFriendCircleID(friendPk)); 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) void Widget::updateFriendActivity(const Friend* frnd)
{ {
const ToxPk& pk = frnd->getPublicKey(); const ToxPk& pk = frnd->getPublicKey();
QDate date = settings.getFriendActivity(pk); const auto oldTime = settings.getFriendActivity(pk);
if (date != QDate::currentDate()) { const auto newTime = QDateTime::currentDateTime();
// Update old activity before after new one. Store old date first. settings.setFriendActivity(pk, newTime);
QDate oldDate = settings.getFriendActivity(pk); FriendWidget* widget = friendWidgets[frnd->getPublicKey()];
settings.setFriendActivity(pk, QDate::currentDate()); contactListWidget->moveWidget(widget, frnd->getStatus());
FriendWidget* widget = friendWidgets[frnd->getPublicKey()]; contactListWidget->updateActivityTime(oldTime); // update old category widget
contactListWidget->moveWidget(widget, frnd->getStatus());
contactListWidget->updateActivityDate(oldDate);
}
} }
void Widget::removeFriend(Friend* f, bool fake) void Widget::removeFriend(Friend* f, bool fake)