Mercurial > trustbridge
changeset 871:4efd6378c001
(issue51) Add fallback in case systemtray is unavailable.
If no systemtray is available it will now show a message box
as notification.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 06 Aug 2014 18:03:23 +0200 |
parents | e93ae5233217 |
children | 02e357826417 |
files | ui/CMakeLists.txt ui/main.cpp ui/mainwindow.cpp ui/mainwindow.h ui/trayicon.cpp ui/trayicon.h |
diffstat | 6 files changed, 109 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/ui/CMakeLists.txt Wed Aug 06 11:55:02 2014 +0200 +++ b/ui/CMakeLists.txt Wed Aug 06 18:03:23 2014 +0200 @@ -41,6 +41,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/processwaitdialog.cpp ${CMAKE_CURRENT_SOURCE_DIR}/textoverlaybutton.cpp ${CMAKE_CURRENT_SOURCE_DIR}/taskscheduler.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/trayicon.cpp ${CERTIFICATELIST_SOURCES} ${DOWNLOADER_SOURCES} )
--- a/ui/main.cpp Wed Aug 06 11:55:02 2014 +0200 +++ b/ui/main.cpp Wed Aug 06 18:03:23 2014 +0200 @@ -15,7 +15,6 @@ #include <QApplication> #include <QFile> -#include <QSystemTrayIcon> #include <QtPlugin> #include <QMessageBox> #include <QSettings> @@ -107,7 +106,7 @@ QLocale::system().name(); } app.installTranslator(&translator); - +/* if ((!QSystemTrayIcon::isSystemTrayAvailable() || !QSystemTrayIcon::supportsMessages()) && trayMode) { QMessageBox::critical(0, QString::fromLatin1(APPNAME), @@ -116,7 +115,7 @@ "be used in a desktop environment.")); return 1; } - +*/ #ifdef Q_OS_WIN { TaskScheduler taskSched;
--- a/ui/mainwindow.cpp Wed Aug 06 11:55:02 2014 +0200 +++ b/ui/mainwindow.cpp Wed Aug 06 18:03:23 2014 +0200 @@ -11,7 +11,6 @@ #include <QProcess> #include <QProgressDialog> #include <QMessageBox> -#include <QSystemTrayIcon> #include <QAction> #include <QDialog> #include <QDir> @@ -42,6 +41,7 @@ #include "binverify.h" #include "processhelp.h" #include "processwaitdialog.h" +#include "trayicon.h" // The amount of time in minutes stay silent if we have // something to say @@ -233,7 +233,11 @@ /* Retry the download again in 10 - 20 minutes */ QTimer::singleShot(600000 + (qrand() % 60000), this, SLOT(checkUpdates())); } else { - mCurMessage = tr("An updated certificate list is available. Click here to install."); + if (mTrayIcon->isAlternative()) { + mCurMessage = tr("An updated certificate list is available."); + } else { + mCurMessage = tr("An updated certificate list is available.") +" " + tr("Click here to install."); + } setState(NewListAvailable); showMessage(); loadCertificateList(); @@ -241,9 +245,14 @@ } void MainWindow::handleNewSW(const QString& fileName, const QDateTime& modDate) { - mCurMessage = tr("An update for %1 is available.\n" - "Click here to download and install the update.").arg( + if (mTrayIcon->isAlternative()) { + mCurMessage = tr("An update for %1 is available.").arg( QApplication::applicationName()); + } else { + mCurMessage = QString(tr("An update for %1 is available.") + "\n" + + tr("Click here to download and install the update.")).arg( + QApplication::applicationName()); + } setState(NewSoftwareAvailable); mSettings.setValue("Software/available", fileName); mSettings.setValue("Software/availableDate", modDate); @@ -437,7 +446,7 @@ mTrayMenu->addAction(mCheckUpdates); mTrayMenu->addAction(mQuitAction); - mTrayIcon = new QSystemTrayIcon(this); + mTrayIcon = new TrayIcon(this); mTrayIcon->setContextMenu(mTrayMenu); mTrayIcon->setIcon(trayImg);
--- a/ui/mainwindow.h Wed Aug 06 11:55:02 2014 +0200 +++ b/ui/mainwindow.h Wed Aug 06 18:03:23 2014 +0200 @@ -13,7 +13,6 @@ * @brief Main UI controller */ -#include <QSystemTrayIcon> #include <QMainWindow> #include <QSettings> #include <QMenuBar> @@ -28,6 +27,7 @@ #include "certificatelist.h" #include "certificatelistwidget.h" #include "textoverlaybutton.h" +#include "trayicon.h" class QMenu; class QAction; class QTimer; @@ -219,7 +219,7 @@ QSettings mSettings; - QSystemTrayIcon *mTrayIcon; + TrayIcon *mTrayIcon; QTimer *mMessageTimer; QMenu *mTrayMenu; QAction *mCheckUpdates;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/trayicon.cpp Wed Aug 06 18:03:23 2014 +0200 @@ -0,0 +1,51 @@ +/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ + +#include "trayicon.h" + +#include <QMessageBox> +#include <QSystemTrayIcon> +#include <QDebug> +#include <QAbstractButton> +#include <QMainWindow> + +TrayIcon::TrayIcon (QObject *parent) : + QSystemTrayIcon (parent), + useAlt(false) +{ + if (QSystemTrayIcon::isSystemTrayAvailable() && + qgetenv("XDG_CURRENT_DESKTOP") != "Unity") { + return; + } + qDebug() << "Platform is without systray. Using Alternative notifications"; + useAlt = true; + +} + +void TrayIcon::show() { + if (useAlt) { + qDebug() << "Would show"; + return; + } + QSystemTrayIcon::show(); +} + +void TrayIcon::showMessage(const QString &title, const QString &msg, + QSystemTrayIcon::MessageIcon msg_icon, int msecs) { + if (!useAlt) { + QSystemTrayIcon::showMessage(title, msg, msg_icon, msecs); + return; + } + QMessageBox *theMessage = new QMessageBox (QMessageBox::Information, + title, msg, QMessageBox::Ok | QMessageBox::No, qobject_cast<QWidget*>(parent())); + theMessage->setIconPixmap (icon().pixmap(theMessage->iconPixmap().size())); + theMessage->button(QMessageBox::Ok)->setText (tr("Install update")); + theMessage->button(QMessageBox::No)->setText (tr("Remind me later")); + connect(theMessage->button(QMessageBox::Ok), SIGNAL(clicked()), this, SIGNAL(messageClicked())); + theMessage->show(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/trayicon.h Wed Aug 06 18:03:23 2014 +0200 @@ -0,0 +1,39 @@ +#ifndef TRAYICON_H +#define TRAYICON_H +/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ + +#include <QWidget> +#include <QSystemTrayIcon> + + +/**@brief Notification interface. + * + * This class provides an inherited implmentation of QSystemTrayIcon + * to be more flexible on platforms where no SystemTray is available. + * + * This class should become obsolete once Qt improves the support for + * StatusNotifier icons that are used in Plasma 5 and Unity. + */ +class TrayIcon : public QSystemTrayIcon +{ + Q_OBJECT +public: + TrayIcon (QObject *parent); + + void show(); + bool isAlternative() {return useAlt;} + +public Q_SLOTS: + void showMessage(const QString &title, const QString &msg, + QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int msecs = 10000); +private: + bool useAlt; +}; + +#endif // TRAYICON_H