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

precise timestamps keeping

This commit is contained in:
apprb 2014-10-14 14:49:27 +09:00
parent fc9f8a16f3
commit 14a71a0f3f
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
6 changed files with 37 additions and 35 deletions

View File

@ -86,7 +86,7 @@ HistoryKeeper::HistoryKeeper(const QString &path, bool encr) :
message 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);")); 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 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);")); 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(); 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 chat_id = getChatID(chat, ctSingle).first;
int sender_id = getAliasID(sender); int sender_id = getAliasID(sender);
int profile_id = getCurrentProfileID(); int profile_id = getCurrentProfileID();
db.exec(QString("INSERT INTO history (profile_id, chat_id, sender, mtype, message) VALUES (%1, %2, %3, 0, '%4');") 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(chat_id).arg(sender_id).arg(wrapMessage(message))); .arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message)));
} }
QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile, QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile,
@ -118,8 +118,8 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QList<HistMessage> res; QList<HistMessage> res;
QString timeStr_from = time_from.toString("yyyy-MM-dd hh:mm:ss"); qint64 time64_from = time_from.toMSecsSinceEpoch();
QString timeStr_to = time_to.toString("yyyy-MM-dd hh:mm:ss"); qint64 time64_to = time_to.toMSecsSinceEpoch();
int chat_id = getChatID(chat, ct).first; int chat_id = getChatID(chat, ct).first;
@ -127,7 +127,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
if (ct == ctSingle) if (ct == ctSingle)
{ {
dbAnswer = db.exec(QString("SELECT timestamp, user_id, message FROM history INNER JOIN aliases ON history.sender = aliases.id ") + 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 { } else {
// no groupchats yet // no groupchats yet
} }
@ -136,8 +136,8 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
{ {
QString sender = dbAnswer.value(1).toString(); QString sender = dbAnswer.value(1).toString();
QString message = unWrapMessage(dbAnswer.value(2).toString()); QString message = unWrapMessage(dbAnswer.value(2).toString());
QDateTime time = dbAnswer.value(0).toDateTime(); qint64 timeInt = dbAnswer.value(0).toLongLong();
time.setTimeSpec(Qt::UTC); QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt);
res.push_back({sender,message,time}); res.push_back({sender,message,time});
} }
@ -221,11 +221,12 @@ void HistoryKeeper::resetInstance()
historyInstance = nullptr; 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(chat)
Q_UNUSED(message) Q_UNUSED(message)
Q_UNUSED(sender) Q_UNUSED(sender)
Q_UNUSED(dt)
// no groupchats yet // no groupchats yet
} }

View File

@ -37,10 +37,10 @@ public:
static void resetInstance(); static void resetInstance();
virtual ~HistoryKeeper(); virtual ~HistoryKeeper();
void addChatEntry(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); void addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt);
QList<HistMessage> getChatHistory(ChatType ct, const QString &profile, const QString &chat, QList<HistMessage> 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(); void syncToDisk();
private: private:

View File

@ -90,16 +90,17 @@ void ChatForm::onSendTriggered()
if (msg.isEmpty()) if (msg.isEmpty())
return; return;
QString name = Widget::getInstance()->getUsername(); 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 ")) if (msg.startsWith("/me "))
{ {
msg = msg.right(msg.length() - 4); msg = msg.right(msg.length() - 4);
addMessage(name, msg, true); addMessage(name, msg, true, timestamp);
emit sendAction(f->friendId, msg); emit sendAction(f->friendId, msg);
} }
else else
{ {
addMessage(name, msg, false); addMessage(name, msg, false, timestamp);
emit sendMessage(f->friendId, msg); emit sendMessage(f->friendId, msg);
} }
msgEdit->clear(); msgEdit->clear();
@ -493,7 +494,7 @@ void ChatForm::onFileSendFailed(int FriendId, const QString &fname)
if (FriendId != f->friendId) if (FriendId != f->friendId)
return; 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) void ChatForm::onAvatarChange(int FriendId, const QPixmap &pic)
@ -549,12 +550,12 @@ void ChatForm::onLoadHistory()
if (*earliestMessage < fromTime) if (*earliestMessage < fromTime)
return; return;
if (*earliestMessage < toTime) if (*earliestMessage < toTime)
{
toTime = *earliestMessage; toTime = *earliestMessage;
toTime = toTime.addMSecs(-1);
}
} }
fromTime = fromTime.toUTC();
toTime = toTime.toUTC();
auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, Core::getInstance()->getSelfId().publicKey, auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, Core::getInstance()->getSelfId().publicKey,
f->userId, fromTime, toTime); f->userId, fromTime, toTime);
@ -578,8 +579,11 @@ void ChatForm::onLoadHistory()
historyMessages.append(ca); historyMessages.append(ca);
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value(); int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
chatWidget->getMesages().clear(); chatWidget->getMesages().clear();
chatWidget->clear(); chatWidget->clear();
if (earliestMessage != nullptr)
*earliestMessage = fromTime;
for (ChatAction *ca : historyMessages) for (ChatAction *ca : historyMessages)
chatWidget->insertMessage(ca); chatWidget->insertMessage(ca);

View File

@ -228,7 +228,7 @@ void GenericChatForm::clearChatArea(bool notinform)
previousName = ""; previousName = "";
if (!notinform) if (!notinform)
addSystemInfoMessage(tr("Cleared"), "white"); addSystemInfoMessage(tr("Cleared"), "white", QDateTime::currentDateTime());
if (earliestMessage) if (earliestMessage)
{ {
@ -237,8 +237,7 @@ void GenericChatForm::clearChatArea(bool notinform)
} }
} }
ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message, ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime)
bool isAction, const QDateTime &datetime)
{ {
if (earliestMessage == nullptr) if (earliestMessage == nullptr)
{ {

View File

@ -46,9 +46,8 @@ public:
virtual void setName(const QString &newName); virtual void setName(const QString &newName);
virtual void show(Ui::MainWindow &ui); virtual void show(Ui::MainWindow &ui);
void addMessage(const QString &author, const QString &message, bool isAction = false, void addMessage(const QString &author, const QString &message, bool isAction, const QDateTime &datetime);
const QDateTime &datetime=QDateTime::currentDateTime()); void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime);
void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime());
signals: signals:
void sendMessage(int, QString); void sendMessage(int, QString);
@ -66,10 +65,8 @@ protected slots:
protected: protected:
QString getElidedName(const QString& name); QString getElidedName(const QString& name);
ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction = false, ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction, const QDateTime &datetime);
const QDateTime &datetime=QDateTime::currentDateTime()); ChatAction* genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime);
ChatAction* genSystemInfoAction(const QString &message, const QString &type,
const QDateTime &datetime=QDateTime::currentDateTime());
QMenu menu; QMenu menu;
CroppingLabel *nameLabel; CroppingLabel *nameLabel;

View File

@ -499,8 +499,9 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
if (!f) if (!f)
return; return;
f->chatForm->addMessage(f->getName(), message, isAction); QDateTime timestamp = QDateTime::currentDateTime();
HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId); f->chatForm->addMessage(f->getName(), message, isAction, timestamp);
HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId, timestamp);
if (activeChatroomWidget != nullptr) if (activeChatroomWidget != nullptr)
{ {
@ -606,7 +607,7 @@ void Widget::onGroupMessageReceived(int groupnumber, const QString& message, con
if (!g) if (!g)
return; return;
g->chatForm->addMessage(author, message); g->chatForm->addMessage(author, message, false, QDateTime::currentDateTime());
if ((static_cast<GenericChatroomWidget*>(g->widget) != activeChatroomWidget) || isMinimized() || !isActiveWindow()) if ((static_cast<GenericChatroomWidget*>(g->widget) != activeChatroomWidget) || isMinimized() || !isActiveWindow())
{ {
@ -736,7 +737,7 @@ void Widget::onMessageSendResult(int friendId, const QString& message, int messa
return; return;
if (!messageId) 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) 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; return;
if (result == -1) if (result == -1)
g->chatForm->addSystemInfoMessage("Message failed to send", "red"); g->chatForm->addSystemInfoMessage("Message failed to send", "red", QDateTime::currentDateTime());
} }