[Pkg-owncloud-commits] [owncloud-client] 136/470: Notifications: Refactor - create a notification handler class

Sandro Knauß hefee-guest at moszumanska.debian.org
Thu May 12 16:24:54 UTC 2016


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 2d1ab27cb52b318f30e9825091d94dc5444ff363
Author: Klaas Freitag <freitag at owncloud.com>
Date:   Thu Mar 10 17:09:36 2016 +0100

    Notifications: Refactor - create a notification handler class
    
    That cleans the ActivityWidget class
---
 src/gui/CMakeLists.txt                |   1 +
 src/gui/activitywidget.cpp            | 152 ++++++++++------------------------
 src/gui/activitywidget.h              |   9 +-
 src/gui/servernotificationhandler.cpp | 103 +++++++++++++++++++++++
 src/gui/servernotificationhandler.h   |  47 +++++++++++
 5 files changed, 200 insertions(+), 112 deletions(-)

diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index d7c12b8..031a1f6 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -87,6 +87,7 @@ set(client_SRCS
     synclogdialog.cpp
     notificationwidget.cpp
     notificationconfirmjob.cpp
+    servernotificationhandler.cpp
     creds/credentialsfactory.cpp
     creds/httpcredentialsgui.cpp
     creds/shibbolethcredentials.cpp
diff --git a/src/gui/activitywidget.cpp b/src/gui/activitywidget.cpp
index 191cb29..01f5b48 100644
--- a/src/gui/activitywidget.cpp
+++ b/src/gui/activitywidget.cpp
@@ -35,6 +35,7 @@
 #include "QProgressIndicator.h"
 #include "notificationwidget.h"
 #include "notificationconfirmjob.h"
+#include "servernotificationhandler.h"
 
 #include "ui_activitywidget.h"
 
@@ -181,7 +182,7 @@ void ActivityListModel::startFetchJob(AccountState* s)
 void ActivityListModel::slotActivitiesReceived(const QVariantMap& json, int statusCode)
 {
     auto activities = json.value("ocs").toMap().value("data").toList();
-    qDebug() << "*** activities" << activities;
+    // qDebug() << "*** activities" << activities;
 
     ActivityList list;
     AccountState* ast = qvariant_cast<AccountState*>(sender()->property("AccountStatePtr"));
@@ -278,7 +279,8 @@ void ActivityListModel::slotRemoveAccount(AccountState *ast )
 
 ActivityWidget::ActivityWidget(QWidget *parent) :
     QWidget(parent),
-    _ui(new Ui::ActivityWidget)
+    _ui(new Ui::ActivityWidget),
+    _notificationRequests(0)
 {
     _ui->setupUi(this);
 
@@ -317,9 +319,6 @@ ActivityWidget::ActivityWidget(QWidget *parent) :
 
     connect( _ui->_activityList, SIGNAL(activated(QModelIndex)), this,
              SLOT(slotOpenFile(QModelIndex)));
-
-    connect( this, SIGNAL(newNotificationList(ActivityList)), this,
-             SLOT(slotBuildNotificationDisplay(ActivityList)) );
 }
 
 ActivityWidget::~ActivityWidget()
@@ -330,7 +329,18 @@ ActivityWidget::~ActivityWidget()
 void ActivityWidget::slotRefresh(AccountState *ptr)
 {
     _model->slotRefreshActivity(ptr);
-    slotFetchNotifications(ptr);
+
+    // start a server notification handler if no notification requests
+    // are running
+    if( _notificationRequests == 0 ) {
+        ServerNotificationHandler *snh = new ServerNotificationHandler;
+        connect(snh, SIGNAL(newNotificationList(ActivityList)), this,
+                SLOT(slotBuildNotificationDisplay(ActivityList)));
+
+        snh->slotFetchNotifications(ptr);
+    } else {
+        qDebug() << "========> notification request counter not zero.";
+    }
 }
 
 void ActivityWidget::slotRemoveAccount( AccountState *ptr )
@@ -433,80 +443,29 @@ void ActivityWidget::slotOpenFile(QModelIndex indx)
     }
 }
 
-void ActivityWidget::slotFetchNotifications(AccountState *ptr)
-{
-    /* start the notification fetch job as well */
-    if( !ptr) {
-        return;
-    }
-
-    // check if the account has notifications enabled. If the capabilities are
-    // not yet valid, its assumed that notifications are available.
-    if( ptr->account() && ptr->account()->capabilities().isValid() ) {
-        if( ! ptr->account()->capabilities().notificationsAvailable() ) {
-            qDebug() << "Account" << ptr->account()->displayName() << "does not have notifications enabled.";
-            return;
-        }
-    }
-
-    // if the previous notification job has finished, start next.
-    if( !_notificationJob ) {
-        _notificationJob = new JsonApiJob( ptr->account(), QLatin1String("ocs/v2.php/apps/notifications/api/v1/notifications"), this );
-        QObject::connect(_notificationJob.data(), SIGNAL(jsonReceived(QVariantMap, int)),
-                         this, SLOT(slotNotificationsReceived(QVariantMap, int)));
-        _notificationJob->setProperty("AccountStatePtr", QVariant::fromValue<AccountState*>(ptr));
-
-        qDebug() << "Start fetching notifications for " << ptr->account()->displayName();
-        _notificationJob->start();
-    } else {
-        qDebug() << "Notification Job still running, not starting a new one.";
-    }
-}
-
-
-void ActivityWidget::slotNotificationsReceived(const QVariantMap& json, int statusCode)
+// GUI: Display the notifications
+void ActivityWidget::slotBuildNotificationDisplay(const ActivityList& list)
 {
-    if( statusCode != 200 ) {
-        qDebug() << "Failed for Notifications";
-        return;
-    }
-
-    auto notifies = json.value("ocs").toMap().value("data").toList();
-
-    AccountState* ai = qvariant_cast<AccountState*>(sender()->property("AccountStatePtr"));
-
-    qDebug() << "Notifications for " << ai->account()->displayName() << notifies;
-
-    ActivityList list;
     int newGuiLogs = 0;
 
-    foreach( auto element, notifies ) {
-        Activity a;
-        auto json   = element.toMap();
-        a._type     = Activity::NotificationType;
-        a._accName  = ai->account()->displayName();
-        a._id       = json.value("notification_id").toLongLong();
-        a._subject  = json.value("subject").toString();
-        a._message  = json.value("message").toString();
-        QString s   = json.value("link").toString();
-        if( !s.isEmpty() ) {
-            a._link     = QUrl(s);
-        }
-        a._dateTime = json.value("datetime").toDateTime();
-        a._dateTime.setTimeSpec(Qt::UTC);
-
-        auto actions = json.value("actions").toList();
-        foreach( auto action, actions) {
-            auto actionJson = action.toMap();
-            ActivityLink al;
-            al._label = QUrl::fromPercentEncoding(actionJson.value("label").toByteArray());
-            al._link  = actionJson.value("link").toString();
-            al._verb  = actionJson.value("type").toString();
-            al._isPrimary = actionJson.value("primary").toBool();
+    foreach( auto activity, list ) {
+        NotificationWidget *widget = 0;
 
-            a._links.append(al);
+        if( _widgetForNotifId.contains(activity._id) ) {
+            widget = _widgetForNotifId[activity._id];
+        } else {
+            widget = new NotificationWidget(this);
+            connect(widget, SIGNAL(sendNotificationRequest(QString, QString, QString)),
+                    this, SLOT(slotSendNotificationRequest(QString, QString, QString)));
+            _notificationsLayout->addWidget(widget);
+            // _ui->_notifyScroll->setMinimumHeight( widget->height());
+            _ui->_notifyScroll->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
+            _widgetForNotifId[activity._id] = widget;
         }
 
+        widget->setAccountName( activity._accName );
+        widget->setActivity( activity );
+
         // handle gui logs. In order to NOT annoy the user with every fetching of the
         // notifications the notification id is stored in a Set. Only if an id
         // is not in the set, it qualifies for guiLog.
@@ -514,16 +473,17 @@ void ActivityWidget::slotNotificationsReceived(const QVariantMap& json, int stat
         // will repeat the gui log.
 
         // after one hour, clear the gui log notification store
-        if( _guiLogTimer.elapsed() > 60*1000 ) {
+        if( _guiLogTimer.elapsed() > 60*60*1000 ) {
             _guiLoggedNotifications.clear();
         }
-        if( !_guiLoggedNotifications.contains(a._id)) {
+        if( !_guiLoggedNotifications.contains(activity._id)) {
             newGuiLogs++;
-            _guiLoggedNotifications.insert(a._id);
+            _guiLoggedNotifications.insert(activity._id);
         }
-        list.append(a);
     }
-    emit newNotificationList( list );
+
+    _ui->_notifyLabel->setHidden( _widgetForNotifId.isEmpty() );
+    _ui->_notifyScroll->setHidden( _widgetForNotifId.isEmpty() );
 
     if( newGuiLogs > 0 ) {
         // restart the gui log timer now that we show a notification
@@ -532,33 +492,6 @@ void ActivityWidget::slotNotificationsReceived(const QVariantMap& json, int stat
         emit guiLog(tr("Notifications - Action Required"),
                     tr("You received %n new notification(s) from the server!", "", newGuiLogs));
     }
-    _notificationJob->deleteLater();
-    _notificationJob = 0;
-}
-
-// GUI: Display the notifications
-void ActivityWidget::slotBuildNotificationDisplay(const ActivityList& list)
-{
-    foreach( auto activity, list ) {
-        NotificationWidget *widget = 0;
-
-        if( _widgetForNotifId.contains(activity._id) ) {
-            widget = _widgetForNotifId[activity._id];
-        } else {
-            widget = new NotificationWidget(this);
-            connect(widget, SIGNAL(sendNotificationRequest(QString, QString, QString)),
-                    this, SLOT(slotSendNotificationRequest(QString, QString, QString)));
-            _notificationsLayout->addWidget(widget);
-            // _ui->_notifyScroll->setMinimumHeight( widget->height());
-            _ui->_notifyScroll->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
-            _widgetForNotifId[activity._id] = widget;
-        }
-
-        widget->setAccountName( activity._accName );
-        widget->setActivity( activity );
-    }
-    _ui->_notifyLabel->setHidden( list.count() == 0 );
-    _ui->_notifyScroll->setHidden( list.count() == 0 );
 }
 
 void ActivityWidget::slotSendNotificationRequest(const QString& accountName, const QString& link, const QString& verb)
@@ -580,14 +513,19 @@ void ActivityWidget::slotSendNotificationRequest(const QString& accountName, con
             connect( job, SIGNAL( jobFinished(QString, int)),
                      this, SLOT(slotNotifyServerFinished(QString, int)) );
             job->start();
+
+            // count the number of running notification requests. If this member var
+            // is larger than zero, no new fetching of notifications is started
+            _notificationRequests++;
         }
     } else {
-        qDebug() << "Notificatio Links: Invalid verb:" << verb;
+        qDebug() << "Notification Links: Invalid verb:" << verb;
     }
 }
 
 void ActivityWidget::endNotificationRequest( NotificationWidget *widget, int replyCode )
 {
+    _notificationRequests--;
     if( widget ) {
         widget->slotNotificationRequestFinished(replyCode);
     }
diff --git a/src/gui/activitywidget.h b/src/gui/activitywidget.h
index 44b4a17..62ef46e 100644
--- a/src/gui/activitywidget.h
+++ b/src/gui/activitywidget.h
@@ -171,7 +171,6 @@ public slots:
     void slotRefresh(AccountState* ptr);
     void slotRemoveAccount( AccountState *ptr );
     void slotAccountActivityStatus(AccountState *ast, int statusCode);
-    void slotFetchNotifications(AccountState *ptr);
 
 signals:
     void guiLog(const QString&, const QString&);
@@ -181,7 +180,6 @@ signals:
     void newNotificationList(const ActivityList& list);
 
 private slots:
-    void slotNotificationsReceived(const QVariantMap& json, int statusCode);
     void slotBuildNotificationDisplay(const ActivityList& list);
     void slotSendNotificationRequest(const QString &accountName, const QString& link, const QString& verb);
     void slotNotifyNetworkError( QNetworkReply* );
@@ -196,12 +194,13 @@ private:
 
     QSet<QString> _accountsWithoutActivities;
     QMap<int, NotificationWidget*> _widgetForNotifId;
-    QPointer<JsonApiJob> _notificationJob;
+    QElapsedTimer _guiLogTimer;
+    QSet<int> _guiLoggedNotifications;
+    int _notificationRequests;
+
     ActivityListModel *_model;
     QVBoxLayout *_notificationsLayout;
 
-    QElapsedTimer _guiLogTimer;
-    QSet<int> _guiLoggedNotifications;
 };
 
 
diff --git a/src/gui/servernotificationhandler.cpp b/src/gui/servernotificationhandler.cpp
new file mode 100644
index 0000000..bfd06d7
--- /dev/null
+++ b/src/gui/servernotificationhandler.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) by Klaas Freitag <freitag at owncloud.com>
+ *
+ * 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; version 2 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.
+ */
+
+#include "servernotificationhandler.h"
+#include "accountstate.h"
+#include "capabilities.h"
+#include "json.h"
+#include "networkjobs.h"
+
+namespace OCC
+{
+
+ServerNotificationHandler::ServerNotificationHandler(QObject *parent)
+    : QObject(parent)
+{
+
+}
+
+void ServerNotificationHandler::slotFetchNotifications(AccountState *ptr)
+{
+    /* start the notification fetch job as well */
+    if( !ptr) {
+        return;
+    }
+
+    // check if the account has notifications enabled. If the capabilities are
+    // not yet valid, its assumed that notifications are available.
+    if( ptr->account() && ptr->account()->capabilities().isValid() ) {
+        if( ! ptr->account()->capabilities().notificationsAvailable() ) {
+            qDebug() << "Account" << ptr->account()->displayName() << "does not have notifications enabled.";
+            return;
+        }
+    }
+
+    // if the previous notification job has finished, start next.
+    _notificationJob = new JsonApiJob( ptr->account(), QLatin1String("ocs/v2.php/apps/notifications/api/v1/notifications"), this );
+    QObject::connect(_notificationJob.data(), SIGNAL(jsonReceived(QVariantMap, int)),
+                     this, SLOT(slotNotificationsReceived(QVariantMap, int)));
+    _notificationJob->setProperty("AccountStatePtr", QVariant::fromValue<AccountState*>(ptr));
+
+    qDebug() << "Start fetching notifications for " << ptr->account()->displayName();
+    _notificationJob->start();
+}
+
+void ServerNotificationHandler::slotNotificationsReceived(const QVariantMap& json, int statusCode)
+{
+    if( statusCode != 200 ) {
+        qDebug() << "Failed for Notifications";
+        return;
+    }
+
+    auto notifies = json.value("ocs").toMap().value("data").toList();
+
+    AccountState* ai = qvariant_cast<AccountState*>(sender()->property("AccountStatePtr"));
+
+    // qDebug() << "Notifications for " << ai->account()->displayName() << notifies;
+
+    ActivityList list;
+
+    foreach( auto element, notifies ) {
+        Activity a;
+        auto json   = element.toMap();
+        a._type     = Activity::NotificationType;
+        a._accName  = ai->account()->displayName();
+        a._id       = json.value("notification_id").toLongLong();
+        a._subject  = json.value("subject").toString();
+        a._message  = json.value("message").toString();
+        QString s   = json.value("link").toString();
+        if( !s.isEmpty() ) {
+            a._link     = QUrl(s);
+        }
+        a._dateTime = json.value("datetime").toDateTime();
+        a._dateTime.setTimeSpec(Qt::UTC);
+
+        auto actions = json.value("actions").toList();
+        foreach( auto action, actions) {
+            auto actionJson = action.toMap();
+            ActivityLink al;
+            al._label = QUrl::fromPercentEncoding(actionJson.value("label").toByteArray());
+            al._link  = actionJson.value("link").toString();
+            al._verb  = actionJson.value("type").toString();
+            al._isPrimary = actionJson.value("primary").toBool();
+
+            a._links.append(al);
+        }
+        list.append(a);
+    }
+    emit newNotificationList( list );
+
+    deleteLater();
+}
+
+}
diff --git a/src/gui/servernotificationhandler.h b/src/gui/servernotificationhandler.h
new file mode 100644
index 0000000..280b219
--- /dev/null
+++ b/src/gui/servernotificationhandler.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) by Klaas Freitag <freitag at owncloud.com>
+ *
+ * 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; version 2 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.
+ */
+
+#ifndef SERVERNOTIFICATIONHANDLER_H
+#define SERVERNOTIFICATIONHANDLER_H
+
+#include <QtCore>
+
+#include "activitywidget.h"
+
+namespace OCC
+{
+
+class ServerNotificationHandler : public QObject
+{
+    Q_OBJECT
+public:
+    explicit ServerNotificationHandler(QObject *parent = 0);
+
+signals:
+    void newNotificationList(ActivityList);
+
+public slots:
+    void slotFetchNotifications(AccountState *ptr);
+
+private slots:
+    void slotNotificationsReceived(const QVariantMap& json, int statusCode);
+
+private:
+    QPointer<JsonApiJob> _notificationJob;
+
+
+};
+
+}
+
+#endif // SERVERNOTIFICATIONHANDLER_H

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list