andre@871: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
andre@871:  * Software engineering by Intevation GmbH
andre@871:  *
andre@871:  * This file is Free Software under the GNU GPL (v>=2)
andre@871:  * and comes with ABSOLUTELY NO WARRANTY!
andre@871:  * See LICENSE.txt for details.
andre@871:  */
andre@871: 
andre@871: #include "trayicon.h"
andre@871: 
andre@871: #include <QMessageBox>
andre@871: #include <QSystemTrayIcon>
andre@871: #include <QDebug>
andre@871: #include <QAbstractButton>
andre@871: #include <QMainWindow>
andre@871: 
andre@871: TrayIcon::TrayIcon (QObject *parent) :
andre@871:     QSystemTrayIcon (parent),
andre@969:     useAlt(false),
andre@969:     mMessageBox(NULL)
andre@871: {
andre@871:     if (QSystemTrayIcon::isSystemTrayAvailable() &&
andre@871:         qgetenv("XDG_CURRENT_DESKTOP") != "Unity") {
andre@871:         return;
andre@871:     }
andre@871:     qDebug() << "Platform is without systray. Using Alternative notifications";
andre@871:     useAlt = true;
andre@871: 
andre@871: }
andre@871: 
andre@871: void TrayIcon::show() {
andre@871:     if (useAlt) {
andre@871:         qDebug() << "Would show";
andre@871:         return;
andre@871:     }
andre@871:     QSystemTrayIcon::show();
andre@871: }
andre@871: 
andre@871: void TrayIcon::showMessage(const QString &title, const QString &msg,
andre@871:                            QSystemTrayIcon::MessageIcon msg_icon, int msecs) {
andre@871:     if (!useAlt) {
andre@871:         QSystemTrayIcon::showMessage(title, msg, msg_icon, msecs);
andre@871:         return;
andre@871:     }
andre@969:     if (!mMessageBox) {
andre@969:         mMessageBox = new QMessageBox (QMessageBox::Information,
andre@969:                                        title, msg, QMessageBox::Ok | QMessageBox::No,
andre@969:                                        qobject_cast<QWidget*>(parent()));
andre@969:         connect(mMessageBox->button(QMessageBox::Ok), SIGNAL(clicked()), this, SIGNAL(messageClicked()));
andre@969:         connect(mMessageBox, SIGNAL(destroyed()), this, SLOT(messageDestroyed()));
andre@969:     }
andre@970:     mMessageBox->setIconPixmap(icon().pixmap(mMessageBox->iconPixmap().size()));
andre@970:     mMessageBox->setWindowIcon(icon());
andre@969:     mMessageBox->button(QMessageBox::Ok)->setText (tr("Install update"));
andre@969:     mMessageBox->button(QMessageBox::No)->setText (tr("Remind me later"));
andre@969:     mMessageBox->show();
andre@871: }
andre@969: 
andre@969: void TrayIcon::messageDestroyed() {
andre@969:     mMessageBox = NULL;
andre@969: }