Commit b3b72117 authored by Linus Jahn's avatar Linus Jahn Committed by Jonah Brüchert
Browse files

MessageHandler: Fix timezone bug of timestamps, Clean up

Finally got fixed! :)

Fixes #159.
parent 4d53d079
......@@ -45,26 +45,21 @@
#include "MessageModel.h"
#include "Notifications.h"
QDateTime glooxStampToQDateTime(std::string stamp_)
QDateTime stringToQDateTime(std::string stamp)
{
QString stamp = QString::fromStdString(stamp_);
QString qStamp = QString::fromStdString(stamp);
QDateTime dateTime;
if (stamp.contains('Z')) {
dateTime = QDateTime::fromString(stamp, "yyyy-MM-ddThh:mm:ss.zzzZ");
} else if (stamp.contains('-') && stamp.contains(':') && stamp.contains('.')) {
dateTime = QDateTime::fromString(stamp, "yyyy-MM-ddThh:mm:ss.zzz");
} else if (stamp.contains('-') && stamp.contains(':')) {
dateTime = QDateTime::fromString(stamp, "yyyy-MM-ddThh:mm:ss");
} else if (stamp.contains(':') && stamp.contains('T')) {
dateTime = QDateTime::fromString(stamp, "yyyyMMddThh:mm:ss");
} else {
dateTime = QDateTime::fromString(stamp, "hh:mm");
}
if (qStamp.contains('.'))
dateTime = QDateTime::fromString(qStamp, Qt::ISODateWithMs);
else
dateTime = QDateTime::fromString(qStamp, Qt::ISODate);
if (!dateTime.isValid())
return QDateTime();
return QDateTime::currentDateTime().toUTC();
// XMPP timestamps are always in UTC
// also read it as such if 'Z' is missing in ISO timestamp
dateTime.setTimeSpec(Qt::UTC);
return dateTime;
}
......@@ -127,12 +122,13 @@ void MessageHandler::handleMessage(const gloox::Message &stanza, gloox::MessageS
const gloox::DelayedDelivery *delayedDelivery = message->when();
if (delayedDelivery)
timestamp = glooxStampToQDateTime(delayedDelivery->stamp())
timestamp = stringToQDateTime(delayedDelivery->stamp())
.toString(Qt::ISODate);
// fallback: use current time from local clock
if (timestamp.isEmpty())
timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
timestamp = QDateTime::currentDateTime().toUTC()
.toString(Qt::ISODate);
// add the message to the database
emit messageModel->addMessageRequested(fromJid, toJid, timestamp,
......@@ -211,7 +207,7 @@ void MessageHandler::sendMessage(QString toJid, QString body)
body.toStdString());
// add the message to the database
const QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
const QString timestamp = QDateTime::currentDateTime().toUTC().toString(Qt::ISODate);
const QString id = QString::fromStdString(message.id());
const QString fromJid = QString::fromStdString(client->jid().bare());
......
......@@ -120,8 +120,8 @@ void MessageModel::addMessage(const QString author, const QString recipient,
record.setValue("timestamp", timestamp);
record.setValue("message", message);
record.setValue("id", msgId);
record.setValue("isSent", sentByMe ? false : true);
record.setValue("isDelivered", sentByMe ? false : true);
record.setValue("isSent", !sentByMe);
record.setValue("isDelivered", !sentByMe);
if (!insertRecord(rowCount(), record)) {
qWarning() << "Failed to add message to DB:" << lastError().text();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment