Added support for notifications.

Leonardo Robol [2011-10-27 15:14]
Added support for notifications.
Filename
Larss.pro
include/feedpoller.h
include/mainwindow.h
larss/feedpoller.cpp
larss/mainwindow.cpp
qtresources.qrc
diff --git a/Larss.pro b/Larss.pro
index e19a7e7..594659e 100644
--- a/Larss.pro
+++ b/Larss.pro
@@ -34,3 +34,6 @@ FORMS    += ui/mainwindow.ui \
     ui/editcategorydialog.ui

 INCLUDEPATH += ./include
+
+RESOURCES += \
+    qtresources.qrc
diff --git a/include/feedpoller.h b/include/feedpoller.h
index 1e0340d..0029d17 100644
--- a/include/feedpoller.h
+++ b/include/feedpoller.h
@@ -21,6 +21,7 @@ namespace Larss {
     signals:
         void startLoadingFeed (QString name);
         void finishedLoadingFeed (QString name);
+        void newElementsNotification (QString title, QString body);

     public slots:
         void networkManagerReplyFinished(QNetworkReply* reply);
@@ -71,8 +72,23 @@ namespace Larss {
          */
         void run();

+        /**
+         * @brief Convert the content of the pubDate tag
+         * into a QDateTime object.
+         */
         QDateTime pubDateToDateTime (QString pubDate);

+        /**
+         * @brief The time that the last bubble has been shown.
+         */
+        QDateTime lastShownBubble;
+
+        /**
+         * @brief True if the last bubble shown was of the type
+         * "Many news to read, not listing them all".
+         */
+        bool floodedBubbleQueue;
+
     };
 }

diff --git a/include/mainwindow.h b/include/mainwindow.h
index ab44d93..b9a7ab4 100644
--- a/include/mainwindow.h
+++ b/include/mainwindow.h
@@ -6,6 +6,7 @@
 #include "feedmodel.h"
 #include "feedpoller.h"
 #include <QSqlDatabase>
+#include <QtGui>

 namespace Ui {
     class MainWindow;
@@ -55,6 +56,11 @@ public slots:
      */
     void loadFeed (const QModelIndex& index);

+    /**
+     * @brief Show a notification about new loaded elements.
+     */
+    void showNewElement (QString title, QString body);
+
 protected:
     bool eventFilter (QObject *object, QEvent *event);
     void closeEvent(QCloseEvent *event);
@@ -63,9 +69,25 @@ private:
     Ui::MainWindow *ui;
     void do_exit();

+    /**
+     * @brief The database used for
+     * storing all the feed-related data.
+     */
     QSqlDatabase db;
+
+    /**
+     * The model describing feeds and categories.
+     */
     FeedModel *feedModel;
+
+    /**
+     * @brief The model describing the news.
+     */
     RssParser *rssParser;
+
+    /**
+     * @brief The poller for the news.
+     */
     FeedPoller *poller;

     /**
@@ -73,6 +95,11 @@ private:
      * for the loaded in the webview.
      */
     QString loadedNews;
+
+    /**
+     * @brief System tray icon used to show the notifications.
+     */
+    QSystemTrayIcon *systrayIcon;
 };

 }
diff --git a/larss/feedpoller.cpp b/larss/feedpoller.cpp
index be46756..fa735b3 100644
--- a/larss/feedpoller.cpp
+++ b/larss/feedpoller.cpp
@@ -22,6 +22,10 @@ FeedPoller::FeedPoller(QObject *parent, RssParser *parser, FeedModel *model) :
     manager = new QNetworkAccessManager ();
     manager->connect(manager, SIGNAL(finished(QNetworkReply*)),
                      this, SLOT(networkManagerReplyFinished(QNetworkReply*)));
+
+    // We don't have a flooded bubble queue since there are no bubble queue
+    // yet.
+    floodedBubbleQueue = false;
 }

 void
@@ -192,6 +196,37 @@ FeedPoller::networkManagerReplyFinished(QNetworkReply *reply)
                 if (!parser->insertRecord(-1, record))
                     qDebug () << "Error inserting record";

+                // Notify of the newly inserted record.
+                if (floodedBubbleQueue)
+                {
+                    // Check if enought time has passed.
+                    QDateTime now = QDateTime::currentDateTimeUtc();
+                    if (now >= lastShownBubble.addSecs(10))
+                    {
+                        lastShownBubble = now;
+                        newElementsNotification(record.value("title").toString(),
+                                                record.value("description").toString());
+                        floodedBubbleQueue = false;
+                    }
+                }
+                else
+                {
+                    QDateTime now = QDateTime::currentDateTimeUtc();
+                    if (now <= lastShownBubble.addSecs(10))
+                    {
+                        floodedBubbleQueue = true;
+                        lastShownBubble = now;
+                        newElementsNotification(tr("New items to read"),
+                                                tr("New elements have been loaded in Larss."));
+                    }
+                    else
+                    {
+                        lastShownBubble = QDateTime::currentDateTimeUtc();
+                        newElementsNotification(record.value("title").toString(),
+                                               record.value("description").toString());
+                    }
+                }
+
                 // Yield to try making the UI responsive.
                 yieldCurrentThread();
             }
diff --git a/larss/mainwindow.cpp b/larss/mainwindow.cpp
index 53a968b..7966b95 100644
--- a/larss/mainwindow.cpp
+++ b/larss/mainwindow.cpp
@@ -74,12 +74,23 @@ MainWindow::MainWindow(QWidget *parent) :
         QModelIndex index = rootItem->child(i, 0)->index();
         ui->feedTreeView->expand(index);
     }
+
+    systrayIcon = new QSystemTrayIcon (QIcon (":/images/larss.png"));
+    systrayIcon->setVisible(true);
+
+    poller->connect(poller, SIGNAL(newElementsNotification(QString,QString)),
+                    this, SLOT(showNewElement(QString,QString)));
+
 }

 MainWindow::~MainWindow()
 {
     db.close();
+    delete systrayIcon;
     delete ui;
+    delete feedModel;
+    delete rssParser;
+    delete poller;
 }

 void
@@ -88,6 +99,12 @@ MainWindow::loadingFeedStart(QString feedName)
     ui->statusBar->showMessage(tr("Updating feed '%1'...").arg(feedName), 2000);
 }

+void
+MainWindow::showNewElement(QString title, QString body)
+{
+    systrayIcon->showMessage(title, body);
+}
+
 void MainWindow::closeEvent(QCloseEvent *event)
 {
     Q_UNUSED(event);
diff --git a/qtresources.qrc b/qtresources.qrc
new file mode 100644
index 0000000..ffdc804
--- /dev/null
+++ b/qtresources.qrc
@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/images">
+        <file>images/larss.png</file>
+    </qresource>
+</RCC>
ViewGit