Mercurial > trustbridge
comparison ui/mainwindow.cpp @ 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 | 7e2f14c7aba2 |
children | c12825a651ed |
comparison
equal
deleted
inserted
replaced
15:95e1b6edf2fc | 16:225a5ec20dad |
---|---|
5 #include <QSystemTrayIcon> | 5 #include <QSystemTrayIcon> |
6 #include <QAction> | 6 #include <QAction> |
7 #include <QDialog> | 7 #include <QDialog> |
8 #include <QMenu> | 8 #include <QMenu> |
9 #include <QApplication> | 9 #include <QApplication> |
10 #include <QFile> | |
10 | 11 |
11 #include "certificatelist.h" | 12 #include "certificatelist.h" |
12 #include "downloader.h" | 13 #include "downloader.h" |
13 | 14 |
14 MainWindow::MainWindow() { | 15 MainWindow::MainWindow() { |
19 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); | 20 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
20 } | 21 } |
21 | 22 |
22 void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) | 23 void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) |
23 { | 24 { |
24 showMessage(); | |
25 qDebug() << "Activated for reason: " << reason; | |
26 switch (reason) { | 25 switch (reason) { |
27 case QSystemTrayIcon::Trigger: | 26 case QSystemTrayIcon::Trigger: |
28 case QSystemTrayIcon::DoubleClick: | |
29 case QSystemTrayIcon::MiddleClick: | 27 case QSystemTrayIcon::MiddleClick: |
30 showMessage(); | 28 showMessage(); |
29 break; | |
30 case QSystemTrayIcon::DoubleClick: | |
31 // TODO show menu | |
31 break; | 32 break; |
32 default: | 33 default: |
33 ; | 34 ; |
34 } | 35 } |
35 } | 36 } |
36 | 37 |
37 void MainWindow::showMessage() | 38 void MainWindow::showMessage() |
38 { | 39 { |
39 mTrayIcon->showMessage("Hello", "World", QSystemTrayIcon::Information, | 40 if (!mCurMessage.isEmpty()) { |
40 10000); | 41 mTrayIcon->showMessage(QApplication::applicationName(), mCurMessage, |
42 QSystemTrayIcon::Information, 5000); | |
43 } | |
41 } | 44 } |
42 void MainWindow::manualCheck() | 45 |
46 /** @brief check the integrity of available files. | |
47 * | |
48 * Do not use this as a trust check as this only works on | |
49 * FileNames where the underlying files can change. This | |
50 * is just meant to check if the downloaded data was somehow | |
51 * removed or corrupted. | |
52 * | |
53 */ | |
54 void MainWindow::verifyAvailableData() | |
43 { | 55 { |
44 Downloader* downloader = new Downloader(this, QString::fromLatin1("")); | 56 QString listFileName = mSettings.value("List/available").toString(); |
45 connect(downloader, &Downloader::finished, downloader, &QObject::deleteLater); | 57 QString swFileName = mSettings.value("Software/available").toString(); |
58 | |
59 if (!listFileName.isEmpty()) { | |
60 const char *cFileName = listFileName.toLocal8Bit().constData(); | |
61 char *data = NULL; | |
62 size_t size; | |
63 | |
64 if (readAndVerifyList(cFileName, &data, &size) != Valid) { | |
65 // Probably a bug when Qt fileName is encoded and cFileName | |
66 // fails because of this. This needs a unit test! | |
67 // Maybe check that the file is in our data directory | |
68 QFile::remove(listFileName); | |
69 mSettings.remove("List/available"); | |
70 mSettings.remove("List/availableDate"); | |
71 } | |
72 | |
73 free(data); // We only needed verify | |
74 } else { | |
75 // Make sure the available notation is also removed | |
76 mSettings.remove("List/available"); | |
77 mSettings.remove("List/availableDate"); | |
78 } | |
79 | |
80 if (!swFileName.isEmpty()) { | |
81 // TODO | |
82 } else { | |
83 mSettings.remove("Software/available"); | |
84 mSettings.remove("Software/availableDate"); | |
85 } | |
86 } | |
87 | |
88 void MainWindow::handleNewList(const QString& fileName, const QDateTime& modDate) { | |
89 | |
90 mCurMessage = tr("An updated certificate list is available. Click here to install."); | |
91 setState(NewListAvailable); | |
92 mSettings.setValue("List/available", fileName); | |
93 mSettings.setValue("List/availableDate", modDate); | |
94 showMessage(); | |
95 } | |
96 | |
97 void MainWindow::handleNewSW(const QString& fileName, const QDateTime& modDate) { | |
98 mCurMessage = tr("An update for %1 is available. Click here to install.").arg( | |
99 QApplication::applicationName()); | |
100 setState(NewSoftwareAvailable); | |
101 mSettings.setValue("Software/available", fileName); | |
102 mSettings.setValue("Software/availableDate", modDate); | |
103 | |
104 qDebug() << "Settings value: " << mSettings.value("Software/available"); | |
105 mSettings.sync(); | |
106 showMessage(); | |
107 } | |
108 | |
109 void MainWindow::checkUpdates() | |
110 { | |
111 verifyAvailableData(); | |
112 | |
113 QDateTime listAvailableLastMod = mSettings.value("List/availableDate").toDateTime(); | |
114 QDateTime swAvailableLastMod = mSettings.value("Software/availableDate").toDateTime(); | |
115 | |
116 if (!listAvailableLastMod.isValid()) { | |
117 listAvailableLastMod = mSettings.value("List/installedLastMod").toDateTime(); | |
118 } | |
119 | |
120 if (!swAvailableLastMod.isValid()) { | |
121 swAvailableLastMod = mSettings.value("List/installedLastMod").toDateTime(); | |
122 } | |
123 | |
124 Downloader* downloader = new Downloader(this, QString::fromLatin1("www.files.kolab.org"), | |
125 QByteArray(), swAvailableLastMod, listAvailableLastMod); | |
126 connect(downloader, SIGNAL(newListAvailable(const QString&, const QDateTime&)), | |
127 this, SLOT(handleNewList(const QString&, const QDateTime&))); | |
128 connect(downloader, SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&)), | |
129 this, SLOT(handleNewSW(const QString&, const QDateTime&))); | |
130 connect(downloader, SIGNAL(finished()), downloader, SLOT(deleteLater())); | |
131 connect(downloader, SIGNAL(error(const QString &, Downloader::ErrorCode)), | |
132 this, SLOT(downloaderError(const QString &, Downloader::ErrorCode))); | |
46 downloader->start(); | 133 downloader->start(); |
47 } | 134 } |
135 | |
136 | |
137 void MainWindow::downloaderError(const QString &message, Downloader::ErrorCode error) | |
138 { | |
139 // TODO decide what to show when and how. | |
140 mCurMessage = message; | |
141 } | |
142 | |
48 | 143 |
49 void MainWindow::createActions() | 144 void MainWindow::createActions() |
50 { | 145 { |
51 mCheckUpdates = new QAction(tr("Check for Updates"), this); | 146 mCheckUpdates = new QAction(tr("Check for Updates"), this); |
52 connect(mCheckUpdates, SIGNAL(triggered()), this, SLOT(manualCheck())); | 147 connect(mCheckUpdates, SIGNAL(triggered()), this, SLOT(checkUpdates())); |
53 mQuitAction = new QAction(tr("Quit"), this); | 148 mQuitAction = new QAction(tr("Quit"), this); |
54 connect(mQuitAction, SIGNAL(triggered()), qApp, SLOT(quit())); | 149 connect(mQuitAction, SIGNAL(triggered()), qApp, SLOT(quit())); |
55 } | 150 } |
56 | 151 |
57 void MainWindow::createTrayIcon() | 152 void MainWindow::createTrayIcon() |
67 | 162 |
68 mTrayIcon->setIcon(trayImg); | 163 mTrayIcon->setIcon(trayImg); |
69 setWindowIcon(trayImg); | 164 setWindowIcon(trayImg); |
70 mTrayIcon->show(); | 165 mTrayIcon->show(); |
71 mTrayIcon->setToolTip(tr("m13ui")); | 166 mTrayIcon->setToolTip(tr("m13ui")); |
72 showMessage(); | |
73 } | 167 } |