Commit 45e512f0 authored by Linus Jahn's avatar Linus Jahn Committed by GitHub

Count unread messages and save them in the db (#92)

Clean up all includes and don't use "Swiften/Swiften.h" anymore.
This will speed up builds much.
parent a0059808
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<name>GlobalDrawer</name> <name>GlobalDrawer</name>
<message> <message>
<source>Add Contact</source> <source>Add Contact</source>
<translation>Kontakt Hinzufügen</translation> <translation type="vanished">Kontakt Hinzufügen</translation>
</message> </message>
<message> <message>
<source>Logout</source> <source>Logout</source>
...@@ -49,6 +49,10 @@ ...@@ -49,6 +49,10 @@
<source>About</source> <source>About</source>
<translation>Über</translation> <translation>Über</translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>LoginPage</name> <name>LoginPage</name>
...@@ -101,7 +105,7 @@ ...@@ -101,7 +105,7 @@
<name>RosterAddContactSheet</name> <name>RosterAddContactSheet</name>
<message> <message>
<source>Add Contact</source> <source>Add Contact</source>
<translation>Kontakt Hinzufügen</translation> <translation type="vanished">Kontakt Hinzufügen</translation>
</message> </message>
<message> <message>
<source>Nickname:</source> <source>Nickname:</source>
...@@ -123,6 +127,10 @@ ...@@ -123,6 +127,10 @@
<source>Add</source> <source>Add</source>
<translation>Hinzufügen</translation> <translation>Hinzufügen</translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>RosterPage</name> <name>RosterPage</name>
...@@ -131,6 +139,25 @@ ...@@ -131,6 +139,25 @@
<translation>Kontakte</translation> <translation>Kontakte</translation>
</message> </message>
</context> </context>
<context>
<name>RosterRemoveContactSheet</name>
<message>
<source>Do you really want to delete the contact &quot;%1&quot; from your roster?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete contact</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished">Abbrechen</translation>
</message>
<message>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>main</name> <name>main</name>
<message> <message>
......
...@@ -38,15 +38,15 @@ ...@@ -38,15 +38,15 @@
<context> <context>
<name>GlobalDrawer</name> <name>GlobalDrawer</name>
<message> <message>
<source>Add Contact</source> <source>Logout</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>Logout</source> <source>About</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>About</source> <source>Add new contact</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>
...@@ -95,10 +95,6 @@ ...@@ -95,10 +95,6 @@
</context> </context>
<context> <context>
<name>RosterAddContactSheet</name> <name>RosterAddContactSheet</name>
<message>
<source>Add Contact</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>Nickname:</source> <source>Nickname:</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
...@@ -119,6 +115,10 @@ ...@@ -119,6 +115,10 @@
<source>Add</source> <source>Add</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>RosterPage</name> <name>RosterPage</name>
...@@ -127,4 +127,23 @@ ...@@ -127,4 +127,23 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>
<context>
<name>RosterRemoveContactSheet</name>
<message>
<source>Do you really want to delete the contact &quot;%1&quot; from your roster?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete contact</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS> </TS>
...@@ -38,15 +38,15 @@ ...@@ -38,15 +38,15 @@
<context> <context>
<name>GlobalDrawer</name> <name>GlobalDrawer</name>
<message> <message>
<source>Add Contact</source> <source>Logout</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>Logout</source> <source>About</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<source>About</source> <source>Add new contact</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>
...@@ -99,10 +99,6 @@ ...@@ -99,10 +99,6 @@
</context> </context>
<context> <context>
<name>RosterAddContactSheet</name> <name>RosterAddContactSheet</name>
<message>
<source>Add Contact</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>Nickname:</source> <source>Nickname:</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
...@@ -123,6 +119,10 @@ ...@@ -123,6 +119,10 @@
<source>Add</source> <source>Add</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>RosterPage</name> <name>RosterPage</name>
...@@ -131,4 +131,23 @@ ...@@ -131,4 +131,23 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>
<context>
<name>RosterRemoveContactSheet</name>
<message>
<source>Do you really want to delete the contact &quot;%1&quot; from your roster?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete contact</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS> </TS>
...@@ -37,10 +37,6 @@ ...@@ -37,10 +37,6 @@
</context> </context>
<context> <context>
<name>GlobalDrawer</name> <name>GlobalDrawer</name>
<message>
<source>Add Contact</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>Logout</source> <source>Logout</source>
<translation type="unfinished">Выйти</translation> <translation type="unfinished">Выйти</translation>
...@@ -49,6 +45,10 @@ ...@@ -49,6 +45,10 @@
<source>About</source> <source>About</source>
<translation type="unfinished">О программе</translation> <translation type="unfinished">О программе</translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>LoginPage</name> <name>LoginPage</name>
...@@ -99,10 +99,6 @@ ...@@ -99,10 +99,6 @@
</context> </context>
<context> <context>
<name>RosterAddContactSheet</name> <name>RosterAddContactSheet</name>
<message>
<source>Add Contact</source>
<translation type="unfinished"></translation>
</message>
<message> <message>
<source>Nickname:</source> <source>Nickname:</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
...@@ -123,6 +119,10 @@ ...@@ -123,6 +119,10 @@
<source>Add</source> <source>Add</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Add new contact</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>RosterPage</name> <name>RosterPage</name>
...@@ -131,6 +131,25 @@ ...@@ -131,6 +131,25 @@
<translation>Контакты</translation> <translation>Контакты</translation>
</message> </message>
</context> </context>
<context>
<name>RosterRemoveContactSheet</name>
<message>
<source>Do you really want to delete the contact &quot;%1&quot; from your roster?</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete contact</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cancel</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>main</name> <name>main</name>
<message> <message>
......
...@@ -31,12 +31,15 @@ ...@@ -31,12 +31,15 @@
// Boost // Boost
#include <boost/bind.hpp> #include <boost/bind.hpp>
// Swiften // Swiften
#include <Swiften/Swiften.h> #include <Swiften/Client/Client.h>
#include <Swiften/Client/MemoryStorages.h>
#include <Swiften/Crypto/PlatformCryptoProvider.h> #include <Swiften/Crypto/PlatformCryptoProvider.h>
#include <Swiften/Queries/Responders/SoftwareVersionResponder.h>
// Kaidan // Kaidan
#include "RosterController.h" #include "RosterController.h"
#include "PresenceController.h" #include "PresenceController.h"
#include "MessageController.h" #include "MessageController.h"
#include "MessageModel.h"
#include "VCardController.h" #include "VCardController.h"
#include "ServiceDiscoveryManager.h" #include "ServiceDiscoveryManager.h"
...@@ -69,7 +72,7 @@ Kaidan::Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent) : QOb ...@@ -69,7 +72,7 @@ Kaidan::Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent) : QOb
// create/update the full jid // create/update the full jid
updateFullJid(); updateFullJid();
// load controlers // load controllers
messageController = new MessageController(&jid); messageController = new MessageController(&jid);
rosterController = new RosterController(); rosterController = new RosterController();
presenceController = new PresenceController(); presenceController = new PresenceController();
...@@ -130,7 +133,7 @@ void Kaidan::mainConnect() ...@@ -130,7 +133,7 @@ void Kaidan::mainConnect()
Swift::ClientOptions options; Swift::ClientOptions options;
options.useStreamCompression = false; options.useStreamCompression = false;
// .. and connect! // .. and connect!
client->connect(options); client->connect(options);
} }
......
...@@ -28,7 +28,8 @@ ...@@ -28,7 +28,8 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
// Swiften // Swiften
#include <Swiften/Swiften.h> #include <Swiften/Client/Client.h>
#include <Swiften/Client/ClientXMLTracer.h>
// Kaidan // Kaidan
#include "RosterController.h" #include "RosterController.h"
#include "MessageController.h" #include "MessageController.h"
......
...@@ -32,7 +32,11 @@ ...@@ -32,7 +32,11 @@
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/smart_ptr/make_shared.hpp> #include <boost/smart_ptr/make_shared.hpp>
// Swiften // Swiften
#include <Swiften/Swiften.h> #include <Swiften/Client/Client.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/DeliveryReceipt.h>
#include <Swiften/Elements/DeliveryReceiptRequest.h>
#include <Swiften/Base/IDGenerator.h>
// Kaidan // Kaidan
#include "MessageModel.h" #include "MessageModel.h"
#include "Notifications.h" #include "Notifications.h"
...@@ -67,6 +71,7 @@ MessageModel* MessageController::getMessageModel() ...@@ -67,6 +71,7 @@ MessageModel* MessageController::getMessageModel()
void MessageController::setRecipient(QString recipient_) void MessageController::setRecipient(QString recipient_)
{ {
// update the recipient
if (recipient == recipient_) if (recipient == recipient_)
return; return;
...@@ -77,6 +82,9 @@ void MessageController::setRecipient(QString recipient_) ...@@ -77,6 +82,9 @@ void MessageController::setRecipient(QString recipient_)
// we're offline or we haven't connected already. // we're offline or we haven't connected already.
messageModel->applyRecipientFilter(recipient, *ownJid); messageModel->applyRecipientFilter(recipient, *ownJid);
emit messageModelChanged(); emit messageModelChanged();
// reset the unread message counter for this contact
rosterController->resetUnreadMessagesForJid(recipient_);
} }
QString MessageController::getRecipient() QString MessageController::getRecipient()
...@@ -84,11 +92,6 @@ QString MessageController::getRecipient() ...@@ -84,11 +92,6 @@ QString MessageController::getRecipient()
return recipient; return recipient;
} }
void MessageController::setMessageAsRead(const QString msgId)
{
messageModel->setMessageAsRead(msgId);
}
void MessageController::handleMessageReceived(Swift::Message::ref message_) void MessageController::handleMessageReceived(Swift::Message::ref message_)
{ {
boost::optional<std::string> bodyOpt = message_->getBody(); boost::optional<std::string> bodyOpt = message_->getBody();
...@@ -100,7 +103,7 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_) ...@@ -100,7 +103,7 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
// //
// author is only the 'bare' JID: e.g. 'albert@einstein.ch' // author is only the 'bare' JID: e.g. 'albert@einstein.ch'
const QString author = QString(message_->getFrom().toBare().toString().c_str()); const QString author = QString::fromStdString(message_->getFrom().toBare().toString());
const QString author_resource = QString(message_->getFrom().getResource().c_str()); const QString author_resource = QString(message_->getFrom().getResource().c_str());
const QString recipient_resource = QString::fromStdString(client->getJID().getResource()); const QString recipient_resource = QString::fromStdString(client->getJID().getResource());
QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate); // fallback timestamp QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate); // fallback timestamp
...@@ -162,11 +165,20 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_) ...@@ -162,11 +165,20 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
} }
// //
// Update lastExchanged in roster controller // Update last exchanged, unread message count in roster controller
// //
rosterController->updateLastExchangedOfJid(QString::fromStdString( if (bodyOpt)
message_->getFrom().toBare().toString())); {
const QString msgAuthor = QString::fromStdString(message_->getFrom().toBare().toString());
rosterController->updateLastExchangedOfJid(msgAuthor);
if (msgAuthor != this->recipient)
{
rosterController->newUnreadMessageForJid(msgAuthor);
}
}
} }
void MessageController::sendMessage(const QString recipient_, const QString message_) void MessageController::sendMessage(const QString recipient_, const QString message_)
......
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
#include <QObject> #include <QObject>
#include <QSqlTableModel> #include <QSqlTableModel>
// Swiften // Swiften
#include <Swiften/Swiften.h> #include <Swiften/Client/Client.h>
#include <Swiften/Elements/Message.h>
// Kaidan // Kaidan
#include "MessageModel.h" #include "MessageModel.h"
#include "RosterController.h" #include "RosterController.h"
...@@ -47,7 +48,6 @@ public: ...@@ -47,7 +48,6 @@ public:
QString getRecipient(); QString getRecipient();
Q_INVOKABLE void sendMessage(const QString recipient_, const QString message_); Q_INVOKABLE void sendMessage(const QString recipient_, const QString message_);
Q_INVOKABLE void setMessageAsRead(const QString msgId);
signals: signals:
void messageModelChanged(); void messageModelChanged();
......
...@@ -47,20 +47,17 @@ static void createTable() ...@@ -47,20 +47,17 @@ static void createTable()
"'timestamp' TEXT NOT NULL," "'timestamp' TEXT NOT NULL,"
"'message' TEXT NOT NULL," "'message' TEXT NOT NULL,"
"'id' TEXT NOT NULL," "'id' TEXT NOT NULL,"
"'isSent' BOOL," "'isSent' BOOL," // is sent to server
"'isDelivered' BOOL," "'isDelivered' BOOL," // message has arrived at other client
"'isRead' BOOL," "FOREIGN KEY('author') REFERENCES Roster ('jid'),"
"'meta' TEXT," // placeholder field for later additons "FOREIGN KEY('recipient') REFERENCES Roster ('jid')"
"FOREIGN KEY('author') REFERENCES Contacts ('jid'),"
"FOREIGN KEY('recipient') REFERENCES Contacts ('jid')"
")")) ")"))
{ {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text())); qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
} }
} }
MessageModel::MessageModel(QObject *parent) : MessageModel::MessageModel(QObject *parent) : QSqlTableModel(parent)
QSqlTableModel(parent)
{ {
createTable(); createTable();
setTable(conversationsTableName); setTable(conversationsTableName);
...@@ -92,19 +89,12 @@ QVariant MessageModel::data(const QModelIndex &index, int role) const ...@@ -92,19 +89,12 @@ QVariant MessageModel::data(const QModelIndex &index, int role) const
QHash<int, QByteArray> MessageModel::roleNames() const QHash<int, QByteArray> MessageModel::roleNames() const
{ {
QHash<int, QByteArray> names; QHash<int, QByteArray> roles;
names[Qt::UserRole] = "author"; // record() returns an empty QSqlRecord
names[Qt::UserRole + 1] = "author_resource"; for (int i = 0; i < this->record().count(); i++) {
names[Qt::UserRole + 2] = "recipient"; roles.insert(Qt::UserRole + i, record().fieldName(i).toUtf8());
names[Qt::UserRole + 3] = "recipient_resource"; }
names[Qt::UserRole + 4] = "timestamp"; return roles;
names[Qt::UserRole + 5] = "message";
names[Qt::UserRole + 6] = "id";
names[Qt::UserRole + 7] = "isSent"; // sent to server
names[Qt::UserRole + 8] = "isDelivered"; // message has arrived on a client
names[Qt::UserRole + 9] = "isRead"; // message has been read from the recipient
names[Qt::UserRole + 10] = "meta";
return names;
} }
void MessageModel::setMessageAsSent(const QString msgId) void MessageModel::setMessageAsSent(const QString msgId)
...@@ -121,13 +111,6 @@ void MessageModel::setMessageAsDelivered(const QString msgId) ...@@ -121,13 +111,6 @@ void MessageModel::setMessageAsDelivered(const QString msgId)
submitAll(); submitAll();
} }
void MessageModel::setMessageAsRead(const QString msgId)
{
QSqlQuery newQuery;
newQuery.exec(QString("UPDATE 'Messages' SET 'isRead' = 1 WHERE id = '%1'").arg(msgId));
submitAll();
}
void MessageModel::addMessage(const QString* author, const QString* author_resource, void MessageModel::addMessage(const QString* author, const QString* author_resource,
const QString* recipient, const QString* recipient_resource, const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message, const QString* msgId, const QString* timestamp, const QString* message, const QString* msgId,
...@@ -137,19 +120,20 @@ void MessageModel::addMessage(const QString* author, const QString* author_resou ...@@ -137,19 +120,20 @@ void MessageModel::addMessage(const QString* author, const QString* author_resou
// add the new message // add the new message