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
*/
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::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile,
@ -118,8 +118,8 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QList<HistMessage> 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::HistMessage> 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::HistMessage> 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
}

View File

@ -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<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();
private:

View File

@ -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);

View File

@ -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)
{

View File

@ -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;

View File

@ -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<GenericChatroomWidget*>(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());
}