diff --git a/Android b/Android new file mode 100644 index 00000000..e69de29b diff --git a/src/notification.cpp b/src/notification.cpp index 67793552..1a7606d0 100644 --- a/src/notification.cpp +++ b/src/notification.cpp @@ -1,106 +1,116 @@ /* * * 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 "userdata.h" -#include "ddpclient.h" #include "notification.h" +#include #include #include #include #include bool Notification::windowClosed() const { return m_windowClosed; } void Notification::setWindowClosed(bool val){ + qDebug() << "set window closed is called"; m_windowClosed = val; + emit windowClosedChanged(); } //Opens the room having new message //void Notification::notificationClicked(){ /* * 1. Maximize systray * 2. switch to unread room * */ //} void Notification::createActions(){ qDebug() << "i m in create action"; m_quitAction = new QAction(tr("&Quit"), this); connect(m_quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); // QWindow window; // m_restore = new QAction(tr("&Restore"),this); // connect(m_restore, &QAction::triggered, window, &QWindow::showMaximized ); } void Notification::createTrayIcon(){ qDebug() << "i m in create tray"; if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("Cannot detect SystemTray on this system.")); return; } m_trayIconMenu = new QMenu(); m_trayIconMenu->addAction(m_quitAction); m_trayIconMenu->addSeparator(); this->setContextMenu(m_trayIconMenu); this->setToolTip("Ruqola"); this->setIcon(QIcon(":/systray.png")); this->setVisible(true); } -Notification::Notification(): m_windowClosed(false) -{ - +Notification::Notification(): m_windowClosed(false){ qDebug() << "i m in constructor"; // m_systrayIcon = new QSystemTrayIcon(); createActions(); createTrayIcon(); connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); } +Notification::~Notification(){ + qDebug() << "notification descructor called for " << this; + +} + + void Notification::iconActivated(QSystemTrayIcon::ActivationReason reason){ qDebug() << "Icon activated"; - if (reason == QSystemTrayIcon::Trigger) { + if (reason == QSystemTrayIcon::Trigger) { + this->show(); +/* + QQmlEngine engine; + QQmlComponent component(&engine, QUrl::fromLocalFile("Desktop.qml")); + QObject *object = component.create(); + QMetaObject::invokeMethod(object, "toggleShow"); + delete object; +*/ + qDebug() << "window closed is " << m_windowClosed; if (m_windowClosed){ m_windowClosed = false; -// m_restore->setDisabled(m_windowClosed); - /* - * raise(); - activateWindow(); - showNormal(); - */ +// raise(); +// activateWindow(); +// showNormal(); } else { m_windowClosed = true; - hide(); -// m_restore->setEnabled(m_windowClosed); - + // hide(); } } } diff --git a/src/notification.h b/src/notification.h index be78fcdc..244cfec8 100644 --- a/src/notification.h +++ b/src/notification.h @@ -1,62 +1,61 @@ /* * * 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 Q_PROPERTY (bool windowClosed READ windowClosed WRITE setWindowClosed NOTIFY windowClosedChanged) public: void setWindowClosed(bool val); bool windowClosed() const; - void toggle(); Notification(); + virtual ~Notification(); signals: void windowClosedChanged(); public slots: - void iconActivated(QSystemTrayIcon::ActivationReason reason); + Q_INVOKABLE void iconActivated(QSystemTrayIcon::ActivationReason reason); private: void createActions(); void createTrayIcon(); QAction *m_quitAction; QAction *m_restore; QMenu *m_trayIconMenu; // QSystemTrayIcon *m_systrayIcon; //have to discuss it through bool m_windowClosed; }; - #endif // NOTIFICATION_H diff --git a/src/userdata.cpp b/src/userdata.cpp index 5851b123..ef418b9f 100644 --- a/src/userdata.cpp +++ b/src/userdata.cpp @@ -1,260 +1,264 @@ /* * * 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 "userdata.h" #include "roommodel.h" #include "ddpclient.h" #include "notification.h" UserData *UserData::m_self = 0; QString UserData::authToken() const { return m_authToken; } QString UserData::userName() const { return m_userName; } QString UserData::userID() const { return m_userID; } QString UserData::password() const { return m_password; } void UserData::setAuthToken(const QString& token) { qDebug() << "Setting token to" << token; QSettings s; m_authToken = token; s.setValue("authToken", token); } void UserData::setPassword(const QString& password) { m_password = password; } void UserData::setUserName(const QString& username) { m_userName = username; QSettings s; s.setValue("username", username); emit userNameChanged(); } void UserData::setUserID(const QString& userID) { m_userName = userID; QSettings s; s.setValue("userID", userID); emit userIDChanged(); } RoomModel * UserData::roomModel() { if (!m_roomModel) { qDebug() << "creating new RoomModel"; m_roomModel = new RoomModel(this); qDebug() << m_roomModel; // m_roomModel->reset(); } return m_roomModel; } DDPClient * UserData::ddp() { if (!m_ddp) { m_ddp = new DDPClient(serverURL()); connect(m_ddp, &DDPClient::loginStatusChanged, this, &UserData::loginStatusChanged); // connect(m_ddp, &DDPClient::loginStatusChanged, this, [=](){qDebug() << "Signal received";}); } return m_ddp; } Notification * UserData::notification() { qDebug() <<"self notification()"; - if (!m_notification) { + if (m_notification){ + qDebug() << "m_notification already exists"; + m_notification = NULL; + m_notification = new Notification(); + m_notification->show(); + } + if (m_notification == NULL) { qDebug() << "if: creating new Notification object"; m_notification = new Notification(); m_notification->show(); -// qDebug() << m_notification; } else { + qDebug() << "i m in else of m_notification"; m_notification->show(); } return m_notification; } void UserData::showNotification(const QString userName, QString message ) { qDebug() << "showNotification() self"; - if ( UserData::m_notification->windowClosed() && ( UserData::self()->loginStatus() == DDPClient::LoggedIn) ){ + if ( m_notification->windowClosed() && ( UserData::self()->loginStatus() == DDPClient::LoggedIn) ){ QString title("New Message"); //This can be enhanced later QString msg = QString("%1 \n %2").arg(userName).arg(message); - if ( msg.length() >= 20 ){ - msg.replace(20, msg.length()-20, "..."); - } +// if ( msg.length() >= 20 ){ +// msg.replace(20, msg.length()-20, "..."); +// } m_notification->showMessage(title, msg, QSystemTrayIcon::Information, 5000 ); } } void UserData::sendMessage(const QString &roomID, const QString &message) { QString json = "{\"rid\": \"%1\", \"msg\": \"%2\"}"; json = json.arg(roomID, message); ddp()->method("sendMessage", QJsonDocument::fromJson(json.toUtf8())); } MessageModel * UserData::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 UserData::serverURL() const { return m_serverURL; } void UserData::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 UserData::loginStatus() { if (m_ddp) { return ddp()->loginStatus(); } else { return DDPClient::LoggedOut; } } void UserData::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 UserData::logOut() { setAuthToken(QString()); setPassword(QString()); // m_ddp->logOut(); 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 UserData::cacheBasePath() const { if (m_serverURL.isEmpty()) { return QString(); } return QStandardPaths::writableLocation(QStandardPaths::CacheLocation)+'/'+m_serverURL; } // QString UserData::activeRoom() const // { // return m_activeRoom; // } // void UserData::setActiveRoom(const QString& activeRoom) // { // m_activeRoom = activeRoom; // // roomModel()->setActiveRoom(activeRoom); // emit activeRoomChanged(); // } RoomWrapper * UserData::getRoom(const QString& roomID) { return roomModel()->findRoom(roomID); } UserData::UserData(QObject* parent): QObject(parent), m_ddp(0), m_roomModel(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(); // roomModel()->reset(); } UserData * UserData::self() { -// qDebug()<<"in self"; if (!m_self) { - qDebug() << "i m in if of self"; m_self = new UserData; m_self->ddp(); // Create DDP object so we try to connect at startup m_self->notification(); m_self->roomModel()->reset(); // m_self->getModelForRoom("GENERAL"); } return m_self; }