diff --git a/src/ddpclient.h b/src/ddpclient.h index 9ba5625a..2b818167 100644 --- a/src/ddpclient.h +++ b/src/ddpclient.h @@ -1,129 +1,129 @@ /* * * 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 DDPCLIENT_H #define DDPCLIENT_H // #include // #include // #include #include #include #include class QJsonObject; class QJsonDocument; class QUrl; class QWebSocket; class DDPClient : public QObject { Q_OBJECT public: enum LoginStatus { NotConnected, LoggingIn, LoggedIn, LoginFailed, LoggedOut }; Q_ENUM(LoginStatus) - + DDPClient(const QString &url = QString(), QObject *parent = 0); ~DDPClient(); - + /** * @brief Call a method with name @param method and parameters @param params - * + * * @param method The name of the method * @param params The parameters * @return unsigned int, the ID of the called method. Watch for it */ unsigned method(const QString &method, const QJsonDocument ¶ms); unsigned method(const QString &method, const QJsonDocument ¶ms, std::function callback); // unsigned method(const QString &method, const QJsonObject ¶ms); - + void subscribe(const QString &collection, const QJsonArray ¶ms); - + Q_INVOKABLE void login(); void logOut(); // Q_INVOKABLE void loginWithPassword(); bool isConnected() const; bool isLoggedIn() const; - + void onServerURLChange(); - + signals: // void connected(); void connectedChanged(); - + void loginStatusChanged(); // void loggedInChanged(); void disconnected(); /** * @brief Emitted whenever a result is received. The parameter is the expected ID. - * + * * @param id the ID received in the method() call */ void result(unsigned id, QJsonDocument result); - + void added(QJsonObject item); void changed(QJsonObject item); - + private slots: void onWSConnected(); void onTextMessageReceived(QString message); void WSclosed(); - + private: - + LoginStatus loginStatus() const; void setLoginStatus(LoginStatus l); - + void resume_login_callback(QJsonDocument doc); - + QString m_url; QWebSocket m_webSocket; unsigned m_uid; - + QHash > m_callbackHash; - + unsigned m_loginJob; LoginStatus m_loginStatus; - + bool m_connected; - + bool m_attemptedPasswordLogin; bool m_attemptedTokenLogin; - + friend class Ruqola; }; // #include "ddpclient.moc" #endif // DDPCLIENT_H diff --git a/src/messagemodel.h b/src/messagemodel.h index b28afcd1..f53490ba 100644 --- a/src/messagemodel.h +++ b/src/messagemodel.h @@ -1,97 +1,98 @@ /* * * 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 MESSAGEMODEL_H #define MESSAGEMODEL_H #include #include #include #include #include #include -class Message { +class Message +{ public: // To be used in ID find: message ID inline bool operator==(const Message &other) const { return other.messageID == messageID; } // To be used in sorted insert: timestamp inline bool operator<(const Message &other) const { return timestamp < other.timestamp; } - + QString messageID; - + QString username; QString userID; QString message; qint64 timestamp; bool systemMessage = false; QString roomID; QString systemMessageType; }; - + class MessageModel : public QAbstractListModel { Q_OBJECT public: enum MessageRoles { Username = Qt::UserRole + 1, MessageText, Timestamp, UserID, SystemMessage, SystemMessageType }; MessageModel(const QString &roomID = "no_room", QObject *parent = 0); virtual ~MessageModel(); void addMessage(const Message& message); virtual int rowCount(const QModelIndex & parent = QModelIndex()) const; virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; qint64 lastTimestamp() const; - + static Message fromJSon(const QJsonObject &source); static QByteArray serialize(const Message &message); protected: virtual QHash roleNames() const; private: const QString m_roomID; - + QVector m_allMessages; - + // QMap m_allMessages; // QMap m_allMessages; QString m_writableLocation; - + QFile *cacheWriter; }; #endif diff --git a/src/notification.h b/src/notification.h index 946eb1bf..e148033a 100644 --- a/src/notification.h +++ b/src/notification.h @@ -1,65 +1,66 @@ /* * * 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 NOTIFICATION_H #define NOTIFICATION_H #include #include #include -class Notification: public QSystemTrayIcon{ +class Notification: public QSystemTrayIcon +{ Q_OBJECT - Q_PROPERTY (bool windowVisible READ windowVisible WRITE setWindowVisible NOTIFY windowVisibleChanged) + Q_PROPERTY(bool windowVisible READ windowVisible WRITE setWindowVisible NOTIFY windowVisibleChanged) Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged) public: void setWindowVisible(bool val); bool windowVisible() const; void setMessage(const QString &message); QString message() const; Notification(); signals: void windowVisibleChanged(); void messageChanged(); private slots: void updateDesktopNotification(); private: void createActions(); void createTrayIcon(); QAction *m_quitAction; QMenu *m_trayIconMenu; bool m_windowVisible; //Notification message QString m_message; }; #endif // NOTIFICATION_H diff --git a/src/roommodel.h b/src/roommodel.h index 8d912eed..3e928269 100644 --- a/src/roommodel.h +++ b/src/roommodel.h @@ -1,130 +1,143 @@ /* * * 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 ROOMMODEL_H #define ROOMMODEL_H #include #include -class Room { -public: +class Room +{ +public: // Room(const Room &room) // { // // this->parent = room.parent(); // } - + // To be used in ID find: message ID inline bool operator==(const Room &other) const { return other.id == id; } // To be used in sorted insert: timestamp inline bool operator<(const Room &other) const { return name < other.name; } - - QString getName() const {return name;} - QString getTopic() const {return topic;} - + + QString getName() const + { + return name; + } + QString getTopic() const + { + return topic; + } + // private: // friend class RoomModel; // friend class RoomWrapper; - - + + // When you add a field, please remember to also add relevant code // to the enum declaration, roleNames, fromJSon and serialize QString name, topic, id; int unread; bool selected = false; }; class RoomWrapper : public QObject { Q_PROPERTY(QString name READ getName NOTIFY nameChanged) Q_PROPERTY(QString topic READ getTopic NOTIFY topicChanged) Q_OBJECT - + public: RoomWrapper(QObject *parent = 0); RoomWrapper(const Room &r, QObject *parent = 0); - - QString getName() {return m_name;} - QString getTopic() {return m_topic;} - + + QString getName() + { + return m_name; + } + QString getTopic() + { + return m_topic; + } + signals: void nameChanged(); void topicChanged(); - + private: QString m_name, m_topic, m_id; int m_unread; bool m_selected; }; - + class RoomModel : public QAbstractListModel { Q_OBJECT public: enum RoomRoles { RoomName = Qt::UserRole + 1, RoomSelected, RoomID, RoomUnread }; - - RoomModel (QObject *parent = 0); + + RoomModel(QObject *parent = 0); virtual ~RoomModel(); - + virtual int rowCount(const QModelIndex & parent = QModelIndex()) const; virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; // void setCurrentRoom(const QString &newRoom); // QString getCurrentRoom() const; - + Q_INVOKABLE void addRoom(const QString& roomID, const QString& roomName, bool selected = false); - + void addRoom(const Room& room); RoomWrapper* findRoom(const QString &roomID) const; - - + + static Room fromJSon(const QJsonObject &source); static QByteArray serialize(const Room &r); - + // void setActiveRoom(const QString &activeRoom); - + void reset(); void clear(); protected: virtual QHash roleNames() const; - + private: - + QVector m_roomsList; // QHash< QString, Room > m_roomsHash; }; #endif // ROOMMODEL_H diff --git a/src/ruqola.h b/src/ruqola.h index 29127870..55695dd8 100644 --- a/src/ruqola.h +++ b/src/ruqola.h @@ -1,118 +1,118 @@ /* * * 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" 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(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(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(); Q_INVOKABLE RoomModel *roomModel(); Q_INVOKABLE void sendMessage(const QString &roomID, const QString &message); Q_INVOKABLE MessageModel* getModelForRoom(const QString &roomID); Q_INVOKABLE void tryLogin(); Q_INVOKABLE void logOut(); Q_INVOKABLE RoomWrapper* getRoom(const QString &roomID); // void setRoomModel(); 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; 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