diff --git a/src/authentication.h b/src/authentication.h index dc466620..36320c1a 100644 --- a/src/authentication.h +++ b/src/authentication.h @@ -1,50 +1,62 @@ /* * * 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 AUTHENTICATION_H #define AUTHENTICATION_H #include #include class Authentication { public: Authentication(); + + /** + * @brief Extract info from Google Json API + */ void getDataFromJson(); + + /** + * @brief Call DDPClient's @method method with OAuth params + */ void OAuthLogin(); + + /** + * @brief Make requests to Google on behalf of user using access token + */ void sendApiRequest(); private slots: void onGranted(); private: bool m_authGranted; QString m_client_id; QString m_client_secret; QOAuth2AuthorizationCodeFlow * m_google; }; #endif // AUTHENTICATION_H diff --git a/src/ddpclient.h b/src/ddpclient.h index 67391156..69414999 100644 --- a/src/ddpclient.h +++ b/src/ddpclient.h @@ -1,136 +1,198 @@ /* * * 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) enum MessageType { Persistent, Ephemeral }; DDPClient(const QString &url = QString(), QObject *parent = 0); ~DDPClient(); /** - * @brief Call a method with name @param method and parameters @param params + * @brief Call a method with name @param method and parameters @param params and @param messageType with an empty callback * - * @param method The name of the method + * @param method The name of the method to call Rocket.Chat API for * @param params The parameters - * @return unsigned int, the ID of the called method. Watch for it + * @param messageType The type of message + * @return unsigned int, the ID of the called method */ - unsigned method(const QString &method, const QJsonDocument ¶ms, DDPClient::MessageType messageStatus = DDPClient::Ephemeral); - unsigned method(const QString &method, const QJsonDocument ¶ms, std::function callback, DDPClient::MessageType messageStatus = DDPClient::Ephemeral); + unsigned method(const QString &method, const QJsonDocument ¶ms, DDPClient::MessageType messageType = DDPClient::Ephemeral); + /** + * @brief Send message over network + * + * @param method The name of the method to call Rocket.Chat API for + * @param params The parameters + * @param callback The pointer to callback function + * @param messageType The type of message + * @return unsigned int, the ID of the called method + */ + unsigned method(const QString &method, const QJsonDocument ¶ms, std::function callback, DDPClient::MessageType messageType = DDPClient::Ephemeral); + + + /** + * @brief Subscribes to a collection with name @param collection and parameters @param params + * + * @param collection The name of the collection + * @param params The parameters + */ void subscribe(const QString &collection, const QJsonArray ¶ms); + /** + * @brief Calls method to log in the user with valid username and password + */ Q_INVOKABLE void login(); + + /** + * @brief Closes the websocket connection + */ void logOut(); + /** + * @brief Check whether websocket is connected at url + * + * @return true if connected, else false + */ bool isConnected() const; + + /** + * @brief Check whether user is logged in + * + * @return true if user is logged in, else false + */ bool isLoggedIn() const; + + /** + * @brief Reconnects the websocket to new url + */ void onServerURLChange(); + /** + * @brief Returns the queue used to cache unsent messages + * + *@return QQueue>, The m_messageQueue object + */ QQueue> messageQueue(); + + /** + * @brief Returns standard cache path + * + *@def QString path + */ QString cachePath() const; signals: void connectedChanged(); void loginStatusChanged(); void disconnected(); + void added(QJsonObject item); + void changed(QJsonObject item); /** - * @brief Emitted whenever a result is received. The parameter is the expected ID. + * @brief Emitted whenever a result is received * - * @param id the ID received in the method() call + * @param id The ID received in the method() call + * @param result The response sent by server */ 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; + /** + * @brief Unique message ID for each message sent over network + */ unsigned m_uid; + /** + * @brief Stores callback function associated with each message + * + * @def QHash unsigned messageID and std::function pointer to callback + */ QHash > m_callbackHash; unsigned m_loginJob; LoginStatus m_loginStatus; bool m_connected; bool m_attemptedPasswordLogin; bool m_attemptedTokenLogin; - //Abstract queue for all requests - //QPair- QString method, QJsonDocument params + /** + * @brief Abstract queue for all requests regarding network management + * + * @def QPair QString method and QJsonDocument params + */ QQueue> m_messageQueue; friend class Ruqola; }; // #include "ddpclient.moc" #endif // DDPCLIENT_H diff --git a/src/messagemodel.h b/src/messagemodel.h index 164c8f07..3723eac9 100644 --- a/src/messagemodel.h +++ b/src/messagemodel.h @@ -1,178 +1,207 @@ /* * * 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 QObject { - Q_OBJECT +// Q_OBJECT public: - enum MessageStatus { - Unsent, - Sending, - Sent, - SendFailed - }; - Q_ENUM(MessageStatus) +// enum MessageStatus { +// Unsent, +// Sending, +// Sent, +// SendFailed +// }; +// Q_ENUM(MessageStatus) // 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; } - MessageStatus messageStatus() const; - void setMessageStatus(MessageStatus m); +// MessageStatus messageStatus() const; +// void setMessageStatus(MessageStatus m); //Message Object Fields // _id QString messageID; // rid QString roomID; // msg QString message; // ts qint64 timestamp; // u QString username; QString userID; // _updatedAt qint64 updatedAt; // editedAt qint64 editedAt; // editedBy QString editedByUsername; QString editedByUserID; // urls QString url; QString meta; QString headers; QString parsedUrl; // attachments QString imageUrl; QString color; // alias QString alias; // avatar QString avatar; // groupable bool groupable; // parseUrls bool parseUrls; bool systemMessage = false; QString systemMessageType; - MessageStatus m_messageStatus; +// MessageStatus m_messageStatus; signals: - void messageStatusChanged(); +// void messageStatusChanged(); }; class MessageModel : public QAbstractListModel { Q_OBJECT public: enum MessageRoles { Username = Qt::UserRole + 1, MessageText, Timestamp, UserID, SystemMessage, SystemMessageType, MessageID, RoomID, UpdatedAt, EditedAt, EditedByUserName, EditedByUserID, Url, Meta, Headers, ParsedUrl, ImageUrl, Color, Alias, Avatar, Groupable, ParseUrls }; Q_ENUM(MessageRoles) MessageModel(const QString &roomID = "no_room", QObject *parent = 0); virtual ~MessageModel(); + /** + * @brief Adds a message to QVector m_allMessages + * + * @param message The message to be added + */ void addMessage(const Message& message); + /** + * @brief returns number of messages in QVector m_allMessages + * + * @param parent, it is void + * @return int, The number of messages in QVector m_allMessages + */ virtual int rowCount(const QModelIndex & parent = QModelIndex()) const; + + virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + /** + * @brief Returns last timestamp of last message in QVector m_allMessages + * + * @return qint64 The last timestamp + */ qint64 lastTimestamp() const; + /** + * @brief Constructs Message object from QJsonObject + * + * @param source The Json containing message attributes + * @return Message object, The message constructed from Json + */ static Message fromJSon(const QJsonObject &source); + + /** + * @brief Constructs QBytearray from Message object + * + * @param message The Message object + * @return QByteArray, The Json containing message attributes + */ 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/messagequeue.h b/src/messagequeue.h index d18154cc..110b8105 100644 --- a/src/messagequeue.h +++ b/src/messagequeue.h @@ -1,48 +1,65 @@ /* * * 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 MESSAGEQUEUE_H #define MESSAGEQUEUE_H #include #include class MessageQueue : public QObject { Q_OBJECT public: MessageQueue(); ~MessageQueue(); + /** + * @brief Retry to send unsent messages in DDPClient's abtract message queue + */ void processQueue(); - static QPair fromJson(const QJsonObject &o); + + /** + * @brief Constructs QPair object from QJsonObject + * + * @param object The Json containing message attributes + * @return QPair, The pair containing the method and params + */ + static QPair fromJson(const QJsonObject &object); + + /** + * @brief Constructs QBytearray from QPair object + * + * @param pair The pair containing method and params + * @return QByteArray, The Json containing message attributes + */ static QByteArray serialize(const QPair pair); public slots: void onLoginStatusChanged(); }; #endif // MESSAGEQUEUE_H diff --git a/src/notification.h b/src/notification.h index a4ffefa5..de31c493 100644 --- a/src/notification.h +++ b/src/notification.h @@ -1,46 +1,55 @@ /* * * 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 { Q_OBJECT public: Notification(); private: + + + /** + * @brief Create actions to be displayed in tray icon menu + */ void createActions(); + + /** + * @brief Creates tray icon consisting of actions + */ void createTrayIcon(); QAction *m_quitAction; QMenu *m_trayIconMenu; }; #endif // NOTIFICATION_H diff --git a/src/rocketchatbackend.h b/src/rocketchatbackend.h index 795f7b0c..923da37b 100644 --- a/src/rocketchatbackend.h +++ b/src/rocketchatbackend.h @@ -1,53 +1,58 @@ /* * * 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 ROCKETCHATBACKEND_H #define ROCKETCHATBACKEND_H #include #include #include "roommodel.h" -// #include "ddpclient.h" + class DDPClient; -// class QJsonObject; class RocketChatBackend : public QObject { Q_OBJECT public: RocketChatBackend(QObject *parent = 0); ~RocketChatBackend(); + + /** + * @brief Adds incoming message from server to appropriate room + * + * @param messages The Json containing the message + */ static void processIncomingMessages(QJsonArray messages); private slots: void onAdded(QJsonObject object); void onChanged(QJsonObject object); void onLoggedIn(); void onLoginStatusChanged(); void onUserIDChanged(); private: // RoomModel *m_rooms; }; #endif // ROCKETCHATBACKEND_H diff --git a/src/roommodel.h b/src/roommodel.h index c79f99dd..a212f752 100644 --- a/src/roommodel.h +++ b/src/roommodel.h @@ -1,177 +1,219 @@ /* * * 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: // 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; } + /** + * @brief Return room name + * + * @return QString, The name of the room + */ QString getName() const { return name; } + + /** + * @brief Return topic name + * + * @return QString, The name of the topic of room + */ 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 //Room Object Fields // _id QString id; // t (can take values "d" , "c" or "p") QString type; // name QString name; // u QString userName; QString userID; // topic QString topic; // muted - collection of muted users by its usernames QString mutedUsers; //QStringList // jitsiTimeout qint64 jitsiTimeout; // ro - read-only chat or not bool ro; 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; } 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, RoomType, RoomUserName, //created by UserName RoomUserID, RoomTopic, RoomMuted, RoomJitsiTimeout, RoomRO }; 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; + /** + * @brief Constructs room object from @param roomID and @param roomName and @param selected, then calls @method addRoom + * + * @param roomID The unique room ID + * @param roomName The name of the room + * @param selected True if room if @param roomID is selected, else false + */ Q_INVOKABLE void addRoom(const QString& roomID, const QString& roomName, bool selected = false); + /** + * @brief Adds a room to m_roomsList with @param room + * + * @param room The room to be added + */ void addRoom(const Room& room); - RoomWrapper* findRoom(const QString &roomID) const; + /** + * @brief Finds a room with @param roomID in m_roomsList + * + * @param roomID The ID of the room to find + * @return RoomWrapper Pointer, The pointer to room with @param roomID in m_roomsList, if exists. Else return a new RoomWrapper object + */ + RoomWrapper* findRoom(const QString &roomID) const; + /** + * @brief Constructs Message object from QJsonObject + * + * @param source The Json containing room attributes + * @return Room object, The room constructed from Json + */ static Room fromJSon(const QJsonObject &source); + + /** + * @brief Constructs QBytearray from Message object + * + * @param message The Room object + * @return QByteArray, The Json containing room attributes + */ static QByteArray serialize(const Room &r); // void setActiveRoom(const QString &activeRoom); - +//Clear data and refill it with data in the cache, if there is 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 0fa7fe7f..c7d9c753 100644 --- a/src/ruqola.h +++ b/src/ruqola.h @@ -1,126 +1,165 @@ /* * * 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" #include "authentication.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: + + /** + * @brief Singleton provider + * + * @return Returns the singleton object m_self + */ 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(); Authentication *authentication(); Q_INVOKABLE RoomModel *roomModel(); + + + /** + * @brief Constructs a Json with @param roomID and @param message and @param type, then calls DDPClient's method to send text message over the network + */ Q_INVOKABLE void sendMessage(const QString &roomID, const QString &message, const QString &type); + + /** + * @brief Returns a model for room with ID @param roomID + * + * @return MessageModel Pointer, model for room + */ Q_INVOKABLE MessageModel* getModelForRoom(const QString &roomID); + /** + * @brief Reset models, load cache and call DDPClient's object to automatically try to connect and log in via username and password + */ Q_INVOKABLE void tryLogin(); + + /** + * @brief Clear models, stores cache and logs out the user + */ Q_INVOKABLE void logOut(); + + /** + * @brief Reset models, load cache and call DDPClient's object to automatically try to connect and log in via Google account + */ Q_INVOKABLE void tryOAuthLogin(); + + /** + * @brief Finds room with @param roomID + * + * @return RoomWrapper Pointer, The room model for @param roomID + */ Q_INVOKABLE RoomWrapper* getRoom(const QString &roomID); Q_INVOKABLE void attachmentButtonClicked(); + /** + * @brief Returns standard cache path + * + * @return QString, The standard cache path + */ 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; Authentication *m_authentication; 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