changeset 16:225a5ec20dad

Use QSettings and manage downloader from mainwindow.
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 19 Feb 2014 10:45:29 +0000
parents 95e1b6edf2fc
children c12825a651ed
files ui/main.cpp ui/mainwindow.cpp ui/mainwindow.h
diffstat 3 files changed, 143 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/ui/main.cpp	Wed Feb 19 10:45:06 2014 +0000
+++ b/ui/main.cpp	Wed Feb 19 10:45:29 2014 +0000
@@ -5,6 +5,17 @@
 #include <QtPlugin>
 #include <QMessageBox>
 
+#ifndef VERSION
+#define VERSION "0.0.1"
+#endif
+
+#ifndef APPNAME
+#define APPNAME "m13ui"
+#endif
+
+#ifndef ORGANIZATION
+#define ORGANIZATION "m13org"
+#endif
 
 #ifdef Q_OS_WIN
  Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
@@ -17,13 +28,17 @@
     QApplication app (argc, argv);
 
     if (!QSystemTrayIcon::isSystemTrayAvailable()) {
-        QMessageBox::critical(0, QObject::tr("m13ui"),
+        QMessageBox::critical(0, QString::fromLatin1(APPNAME),
                               QObject::tr("Couldn't detect any system tray "
                                           "on this system. This software can only "
                                           "be used in a desktop environment."));
         return 1;
     }
+
     QApplication::setQuitOnLastWindowClosed(false);
+    QApplication::setOrganizationName(QString::fromLatin1(ORGANIZATION));
+    QApplication::setApplicationName(QString::fromLatin1(APPNAME));
+    QApplication::setApplicationVersion(QString::fromLatin1(VERSION));
 
     MainWindow mainWin;
     mainWin.show();
--- a/ui/mainwindow.cpp	Wed Feb 19 10:45:06 2014 +0000
+++ b/ui/mainwindow.cpp	Wed Feb 19 10:45:29 2014 +0000
@@ -7,6 +7,7 @@
 #include <QDialog>
 #include <QMenu>
 #include <QApplication>
+#include <QFile>
 
 #include "certificatelist.h"
 #include "downloader.h"
@@ -21,14 +22,14 @@
 
 void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
 {
-    showMessage();
-    qDebug() << "Activated for reason: " << reason;
     switch (reason) {
     case QSystemTrayIcon::Trigger:
-    case QSystemTrayIcon::DoubleClick:
     case QSystemTrayIcon::MiddleClick:
         showMessage();
         break;
+    case QSystemTrayIcon::DoubleClick:
+        // TODO show menu
+        break;
     default:
         ;
     }
@@ -36,20 +37,114 @@
 
 void MainWindow::showMessage()
 {
-    mTrayIcon->showMessage("Hello", "World", QSystemTrayIcon::Information,
-                          10000);
+    if (!mCurMessage.isEmpty()) {
+        mTrayIcon->showMessage(QApplication::applicationName(), mCurMessage,
+                               QSystemTrayIcon::Information, 5000);
+    }
 }
-void MainWindow::manualCheck()
+
+/** @brief check the integrity of available files.
+ *
+ * Do not use this as a trust check as this only works on
+ * FileNames where the underlying files can change. This
+ * is just meant to check if the downloaded data was somehow
+ * removed or corrupted.
+ *
+ */
+void MainWindow::verifyAvailableData()
 {
-    Downloader* downloader = new Downloader(this, QString::fromLatin1(""));
-    connect(downloader, &Downloader::finished, downloader, &QObject::deleteLater);
+    QString listFileName = mSettings.value("List/available").toString();
+    QString swFileName = mSettings.value("Software/available").toString();
+
+    if (!listFileName.isEmpty()) {
+        const char *cFileName = listFileName.toLocal8Bit().constData();
+        char *data = NULL;
+        size_t size;
+
+        if (readAndVerifyList(cFileName, &data, &size) != Valid) {
+            // Probably a bug when Qt fileName is encoded and cFileName
+            // fails because of this. This needs a unit test!
+            // Maybe check that the file is in our data directory
+            QFile::remove(listFileName);
+            mSettings.remove("List/available");
+            mSettings.remove("List/availableDate");
+        }
+
+        free(data); // We only needed verify
+    } else {
+        // Make sure the available notation is also removed
+        mSettings.remove("List/available");
+        mSettings.remove("List/availableDate");
+    }
+
+    if (!swFileName.isEmpty()) {
+        // TODO
+    } else {
+        mSettings.remove("Software/available");
+        mSettings.remove("Software/availableDate");
+    }
+}
+
+void MainWindow::handleNewList(const QString& fileName, const QDateTime& modDate) {
+
+    mCurMessage = tr("An updated certificate list is available. Click here to install.");
+    setState(NewListAvailable);
+    mSettings.setValue("List/available", fileName);
+    mSettings.setValue("List/availableDate", modDate);
+    showMessage();
+}
+
+void MainWindow::handleNewSW(const QString& fileName, const QDateTime& modDate) {
+    mCurMessage = tr("An update for %1 is available. Click here to install.").arg(
+                QApplication::applicationName());
+    setState(NewSoftwareAvailable);
+    mSettings.setValue("Software/available", fileName);
+    mSettings.setValue("Software/availableDate", modDate);
+
+    qDebug() << "Settings value: " << mSettings.value("Software/available");
+    mSettings.sync();
+    showMessage();
+}
+
+void MainWindow::checkUpdates()
+{
+    verifyAvailableData();
+
+    QDateTime listAvailableLastMod = mSettings.value("List/availableDate").toDateTime();
+    QDateTime swAvailableLastMod = mSettings.value("Software/availableDate").toDateTime();
+
+    if (!listAvailableLastMod.isValid()) {
+        listAvailableLastMod = mSettings.value("List/installedLastMod").toDateTime();
+    }
+
+    if (!swAvailableLastMod.isValid()) {
+        swAvailableLastMod = mSettings.value("List/installedLastMod").toDateTime();
+    }
+
+    Downloader* downloader = new Downloader(this, QString::fromLatin1("www.files.kolab.org"),
+            QByteArray(), swAvailableLastMod, listAvailableLastMod);
+    connect(downloader, SIGNAL(newListAvailable(const QString&, const QDateTime&)),
+            this, SLOT(handleNewList(const QString&, const QDateTime&)));
+    connect(downloader, SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&)),
+            this, SLOT(handleNewSW(const QString&, const QDateTime&)));
+    connect(downloader, SIGNAL(finished()), downloader, SLOT(deleteLater()));
+    connect(downloader, SIGNAL(error(const QString &, Downloader::ErrorCode)),
+            this, SLOT(downloaderError(const QString &, Downloader::ErrorCode)));
     downloader->start();
 }
 
+
+void MainWindow::downloaderError(const QString &message, Downloader::ErrorCode error)
+{
+    // TODO decide what to show when and how.
+    mCurMessage = message;
+}
+
+
 void MainWindow::createActions()
 {
     mCheckUpdates = new QAction(tr("Check for Updates"), this);
-    connect(mCheckUpdates, SIGNAL(triggered()), this, SLOT(manualCheck()));
+    connect(mCheckUpdates, SIGNAL(triggered()), this, SLOT(checkUpdates()));
     mQuitAction = new QAction(tr("Quit"), this);
     connect(mQuitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
 }
@@ -69,5 +164,4 @@
     setWindowIcon(trayImg);
     mTrayIcon->show();
     mTrayIcon->setToolTip(tr("m13ui"));
-    showMessage();
 }
--- a/ui/mainwindow.h	Wed Feb 19 10:45:06 2014 +0000
+++ b/ui/mainwindow.h	Wed Feb 19 10:45:29 2014 +0000
@@ -3,6 +3,9 @@
 
 #include <QSystemTrayIcon>
 #include <QDialog>
+#include <QSettings>
+
+#include "downloader.h"
 class QMenu;
 class QAction;
 
@@ -16,22 +19,41 @@
     void setMessage(const QString message) {mCurMessage = message;}
     QString getMessage() {return mCurMessage;}
 
+    enum CurrentState {
+        BeforeDownload,
+        NewListAvailable,
+        NewSoftwareAvailable,
+        TransferError
+    };
+
+    CurrentState getState() {return mCurState;}
+    void setState(CurrentState state) {mCurState = state;}
+
 private slots:
     void showMessage();
     void iconActivated(QSystemTrayIcon::ActivationReason reason);
-    void manualCheck();
+    void checkUpdates();
+    void handleNewList(const QString& fileName, const QDateTime& modDate);
+    void handleNewSW(const QString& fileName, const QDateTime& modDate);
+    void downloaderError(const QString &message, Downloader::ErrorCode error);
 
 private:
+    void verifyAvailableData();
     void createTrayIcon();
     void createActions();
 
     QString mCurMessage;
+    QString mInstalledSWVersion;
+    QString mInstalledListVersion;
+
+    QSettings mSettings(QSettings::IniFormat);
 
     QSystemTrayIcon *mTrayIcon;
 
     QMenu *mTrayMenu;
     QAction *mCheckUpdates;
     QAction *mQuitAction;
+    CurrentState mCurState;
 };
 
 #endif // MAINWINDOW_H

http://wald.intevation.org/projects/trustbridge/