diff --git a/main.cpp b/main.cpp index 5398f34f..993371e8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,44 +1,43 @@ #include #include // only if desktop #include "src/roommodel.h" #include "src/rocketchatbackend.h" #include "src/ruqola.h" #include "src/notification.h" #include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setWindowIcon(QIcon(":/systray.png")); QCoreApplication::setOrganizationName("KDE"); QCoreApplication::setOrganizationDomain("kde.org"); QCoreApplication::setApplicationName("Ruqola"); qmlRegisterSingletonType("KDE.Ruqola.Ruqola", 1, 0, "Ruqola", ruqola_singletontype_provider); qmlRegisterType("KDE.Ruqola.MessageModel", 1, 0, "MessageModel"); qmlRegisterType("KDE.Ruqola.DDPClient", 1, 0, "DDPClient"); -// qmlRegisterType("KDE.Ruqola.DDPClient", 1, 0, "MessageQueue"); qmlRegisterType("KDE.Ruqola.RoomModel", 1, 0, "RoomModel"); qmlRegisterType("KDE.Ruqola.RoomWrapper", 1, 0, "RoomWrapper"); qmlRegisterType("KDE.Ruqola.Notification", 1, 0, "Notification"); RocketChatBackend c; QQmlApplicationEngine engine; QQmlContext *ctxt = engine.rootContext(); ctxt->setContextProperty("systrayIcon", Ruqola::self()->notification()); // ctxt->setContextProperty("attachmentButton", Ruqola::self()->attachmentButtonClicked()); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } diff --git a/src/ruqola.cpp b/src/ruqola.cpp index af4b5cf8..1200b78b 100644 --- a/src/ruqola.cpp +++ b/src/ruqola.cpp @@ -1,283 +1,284 @@ /* * * Copyright 2016 Riccardo Iaconelli * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "ruqola.h" #include "roommodel.h" #include "ddpclient.h" #include "notification.h" +#include "messagequeue.h" #include #include #include #include Ruqola *Ruqola::m_self = 0; QString Ruqola::authToken() const { return m_authToken; } QString Ruqola::userName() const { return m_userName; } QString Ruqola::userID() const { return m_userID; } QString Ruqola::password() const { return m_password; } void Ruqola::setAuthToken(const QString& token) { qDebug() << "Setting token to" << token; QSettings s; m_authToken = token; s.setValue("authToken", token); } void Ruqola::setPassword(const QString& password) { m_password = password; } void Ruqola::setUserName(const QString& username) { m_userName = username; QSettings s; s.setValue("username", username); emit userNameChanged(); } void Ruqola::setUserID(const QString& userID) { m_userName = userID; QSettings s; s.setValue("userID", userID); emit userIDChanged(); } RoomModel * Ruqola::roomModel() { if (!m_roomModel) { qDebug() << "creating new RoomModel"; m_roomModel = new RoomModel(this); qDebug() << m_roomModel; } return m_roomModel; } DDPClient * Ruqola::ddp() { if (!m_ddp) { m_ddp = new DDPClient(serverURL()); connect(m_ddp, &DDPClient::loginStatusChanged, this, &Ruqola::loginStatusChanged); // connect(m_ddp, &DDPClient::loginStatusChanged, this, [=](){qDebug() << "Signal received";}); } return m_ddp; } MessageQueue * Ruqola::messageQueue() { if (!m_messageQueue) { m_messageQueue = new MessageQueue(); // retry to send any unsent messages Ruqola::self()->messageQueue()->retry(); // connect(m_messageQueue, &DDPClient::loginStatusChanged, this, &MessageQueue::loginStatusChanged); } return m_messageQueue; } Notification * Ruqola::notification() { if (m_notification == NULL) { m_notification = new Notification(); m_notification->show(); } return m_notification; } void Ruqola::attachmentButtonClicked() { QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, "Select one or more files to open", QDir::homePath(), "Images (*.png *.jpeg *.jpg)"); qDebug() << "Selected Image " << fileName; QFile file(fileName); if (!file.open(QFile::ReadOnly)) { qDebug() << "Cannot open the selected file"; return; } const QString message = QString::fromLatin1(file.readAll().toBase64()); const QString roomID("3cGRyFLWgnPL7B79n"); //hard code roomID for now const QString type("image"); sendMessage(roomID, message, type); } void Ruqola::sendMessage(const QString &roomID, const QString &message, const QString &type) { QJsonObject json; json["rid"] = roomID; json["msg"] = message; json["type"] = type; ddp()->method("sendMessage", QJsonDocument(json)); } MessageModel * Ruqola::getModelForRoom(const QString& roomID) { if (m_messageModels.contains(roomID)) { // qDebug() << "Returning old model for " << roomID; return m_messageModels.value(roomID); } else { // qDebug() << "Creating a new model"; m_messageModels[roomID] = new MessageModel(roomID, this); return m_messageModels[roomID]; } } QString Ruqola::serverURL() const { return m_serverURL; } void Ruqola::setServerURL(const QString& serverURL) { if (m_serverURL == serverURL) { return; } QSettings s; s.setValue("serverURL", serverURL); m_serverURL = serverURL; // m_roomModel->reset(); emit serverURLChanged(); } DDPClient::LoginStatus Ruqola::loginStatus() { if (m_ddp) { return ddp()->loginStatus(); } else { return DDPClient::LoggedOut; } } void Ruqola::tryLogin() { qDebug() << "Attempting login" << userName() << "on" << serverURL(); // Reset model views foreach (const QString key, m_messageModels.keys()) { MessageModel *m = m_messageModels.take(key); delete m; } delete m_ddp; m_ddp = 0; // In the meantime, load cache... m_roomModel->reset(); // This creates a new ddp() object. // DDP will automatically try to connect and login. ddp(); } void Ruqola::logOut() { setAuthToken(QString()); setPassword(QString()); foreach (const QString key, m_messageModels.keys()) { MessageModel *m = m_messageModels.take(key); delete m; } delete m_ddp; m_ddp = 0; emit loginStatusChanged(); m_roomModel->clear(); } QString Ruqola::cacheBasePath() const { if (m_serverURL.isEmpty()) { return QString(); } return QStandardPaths::writableLocation(QStandardPaths::CacheLocation)+'/'+m_serverURL; } // QString Ruqola::activeRoom() const // { // return m_activeRoom; // } // void Ruqola::setActiveRoom(const QString& activeRoom) // { // m_activeRoom = activeRoom; // // roomModel()->setActiveRoom(activeRoom); // emit activeRoomChanged(); // } RoomWrapper * Ruqola::getRoom(const QString& roomID) { return roomModel()->findRoom(roomID); } Ruqola::Ruqola(QObject* parent): QObject(parent), m_ddp(0), m_messageQueue(0), m_roomModel(0), m_notification(0) { QSettings s; m_serverURL = s.value("serverURL", "demo.rocket.chat").toString(); m_userName = s.value("username").toString(); m_userID = s.value("userID").toString(); m_authToken = s.value("authToken").toString(); } Ruqola * Ruqola::self() { if (!m_self) { m_self = new Ruqola; // Create DDP object so we try to connect at startup m_self->ddp(); // Clear rooms data and refill it with data in the cache, if there is m_self->roomModel()->reset(); // Create systray to show notifications m_self->notification(); //Initialize the messageQueue object m_self->messageQueue(); } return m_self; } diff --git a/src/ruqola.h b/src/ruqola.h index 0f83e242..8e888956 100644 --- a/src/ruqola.h +++ b/src/ruqola.h @@ -1,123 +1,124 @@ /* * * Copyright 2016 Riccardo Iaconelli * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #ifndef USERDATA_H #define USERDATA_H #include #include #include #include "ddpclient.h" #include "roommodel.h" #include "messagemodel.h" #include "notification.h" +#include "messagequeue.h" class QString; class Ruqola: public QObject { Q_OBJECT Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged) Q_PROPERTY(QString userID READ userID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QString serverURL READ serverURL WRITE setServerURL NOTIFY serverURLChanged) Q_PROPERTY(QString password WRITE setPassword) // Q_PROPERTY (bool connected READ connected NOTIFY connectedChanged) Q_PROPERTY(DDPClient::LoginStatus loginStatus READ loginStatus NOTIFY loginStatusChanged) // Q_PROPERTY(QString activeRoom READ activeRoom WRITE setActiveRoom NOTIFY activeRoomChanged) public: static Ruqola* self(); void setUserName(const QString &username); QString userName() const; void setUserID(const QString &userID); QString userID() const; void setPassword(const QString &password); QString password() const; void setAuthToken(const QString &token); QString authToken() const; bool connected(); DDPClient::LoginStatus loginStatus(); QString serverURL() const; void setServerURL(const QString &serverURL); // QString activeRoom() const; // void setActiveRoom(const QString &activeRoom); DDPClient *ddp(); Notification *notification(); MessageQueue *messageQueue(); Q_INVOKABLE RoomModel *roomModel(); Q_INVOKABLE void sendMessage(const QString &roomID, const QString &message, const QString &type); Q_INVOKABLE MessageModel* getModelForRoom(const QString &roomID); Q_INVOKABLE void tryLogin(); Q_INVOKABLE void logOut(); Q_INVOKABLE RoomWrapper* getRoom(const QString &roomID); Q_INVOKABLE void attachmentButtonClicked(); QString cacheBasePath() const; signals: void userNameChanged(); void userIDChanged(); void serverURLChanged(); void loginStatusChanged(); private: Ruqola(QObject *parent = 0); static Ruqola *m_self; QString m_password; QString m_userName; QString m_userID; QString m_authToken; QString m_serverURL; DDPClient *m_ddp; MessageQueue *m_messageQueue; RoomModel *m_roomModel; Notification *m_notification; QHash< QString, MessageModel * > m_messageModels; }; inline static QObject *ruqola_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine) Q_UNUSED(scriptEngine) Ruqola *userData = Ruqola::self(); return userData; } #endif // USERDATA_H