Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(chatform): Highlight chat history
Browse files Browse the repository at this point in the history
Calendar dates with chat history highlighted with bold
Fixes #2296
  • Loading branch information
tWido committed Jun 25, 2017
1 parent 0b5b3fc commit 3257770
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,47 @@ QList<History::HistMessage> History::getChatHistory(const QString& friendPk, con
return messages;
}

/**
* @brief Fetches chat messages counts for each day from the database.
* @param friendPk Friend public key to fetch.
* @param from Start of period to fetch.
* @param to End of period to fetch.
* @return List of structs containing days offset and message count for that day.
*/
QList<History::DateMessages> History::getChatHistoryCounts(const ToxPk& friendPk, const QDate& from,
const QDate& to)
{
if (!isValid()) {
return {};
}
QDateTime fromTime(from);
QDateTime toTime(to);

QList<DateMessages> counts;

auto rowCallback = [&counts](const QVector<QVariant>& row) {
DateMessages app;
app.count = row[0].toUInt();
app.offsetDays = row[1].toUInt();
counts.append(app);
};

QString queryText =
QString("SELECT COUNT(history.id), ((timestamp / 1000 / 60 / 60 / 24) - %4 ) AS day "
"FROM history "
"JOIN peers chat ON chat_id = chat.id "
"WHERE timestamp BETWEEN %1 AND %2 AND chat.public_key='%3'"
"GROUP BY day;")
.arg(fromTime.toMSecsSinceEpoch())
.arg(toTime.toMSecsSinceEpoch())
.arg(friendPk.toString())
.arg(QDateTime::fromMSecsSinceEpoch(0).daysTo(fromTime));

db->execNow({queryText, rowCallback});

return counts;
}

/**
* @brief Marks a message as sent.
* Removing message from the faux-offline pending messages list.
Expand Down
10 changes: 10 additions & 0 deletions src/persistence/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cstdint>
#include <tox/toxencryptsave.h>

#include "src/core/toxpk.h"
#include "src/persistence/db/rawdatabase.h"

class Profile;
Expand Down Expand Up @@ -58,6 +59,12 @@ class History
bool isSent;
};

struct DateMessages
{
uint offsetDays;
uint count;
};

public:
explicit History(std::shared_ptr<RawDatabase> db);
~History();
Expand All @@ -73,6 +80,9 @@ class History

QList<HistMessage> getChatHistory(const QString& friendPk, const QDateTime& from,
const QDateTime& to);

QList<DateMessages> getChatHistoryCounts(const ToxPk& friendPk, const QDate& from, const QDate& to);

void markAsSent(qint64 messageId);

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ void ChatForm::onLoadHistory()
return;
}

LoadHistoryDialog dlg;
LoadHistoryDialog dlg(f->getPublicKey());
if (dlg.exec()) {
QDateTime fromTime = dlg.getFromDate();
loadHistory(fromTime);
Expand Down
28 changes: 27 additions & 1 deletion src/widget/form/loadhistorydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@

#include "loadhistorydialog.h"
#include "ui_loadhistorydialog.h"
#include "src/nexus.h"
#include "src/persistence/history.h"
#include "src/persistence/profile.h"
#include <QDate>
#include <QTextCharFormat>

LoadHistoryDialog::LoadHistoryDialog(QWidget* parent)
LoadHistoryDialog::LoadHistoryDialog(const ToxPk& friendPk, QWidget* parent)
: QDialog(parent)
, ui(new Ui::LoadHistoryDialog)
, friendPk(friendPk)
{
ui->setupUi(this);
highlightDates(QDate::currentDate().year(), QDate::currentDate().month());
connect(ui->fromDate, &QCalendarWidget::currentPageChanged, this,
&LoadHistoryDialog::highlightDates);
}

LoadHistoryDialog::~LoadHistoryDialog()
Expand All @@ -43,3 +52,20 @@ QDateTime LoadHistoryDialog::getFromDate()

return res;
}

void LoadHistoryDialog::highlightDates(int year, int month)
{
History* history = Nexus::getProfile()->getHistory();
QDate monthStart(year, month, 1);
QDate monthEnd(year, month + 1, 1);
QList<History::DateMessages> counts =
history->getChatHistoryCounts(this->friendPk, monthStart, monthEnd);

QTextCharFormat bold;
bold.setFontWeight(QFont::Bold);

QCalendarWidget* calendar = ui->fromDate;
for (History::DateMessages p : counts) {
calendar->setDateTextFormat(monthStart.addDays(p.offsetDays), bold);
}
}
7 changes: 6 additions & 1 deletion src/widget/form/loadhistorydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef LOADHISTORYDIALOG_H
#define LOADHISTORYDIALOG_H

#include "src/core/toxpk.h"
#include <QDateTime>
#include <QDialog>

Expand All @@ -32,13 +33,17 @@ class LoadHistoryDialog : public QDialog
Q_OBJECT

public:
explicit LoadHistoryDialog(QWidget* parent = 0);
explicit LoadHistoryDialog(const ToxPk& friendPk, QWidget* parent = 0);
~LoadHistoryDialog();

QDateTime getFromDate();

public slots:
void highlightDates(int year, int month);

private:
Ui::LoadHistoryDialog* ui;
const ToxPk friendPk;
};

#endif // LOADHISTORYDIALOG_H

0 comments on commit 3257770

Please sign in to comment.