add initial support for tray icon and notifications
[makneto-zunavac1.git] / src / ui-mobile / main-window.cpp
blobd46601c28524faef7cca61a675ad0c4d5a92a37d
1 /*
2 * Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <QDebug>
22 #include "main-window.h"
24 // TODO: change tray icon related with global status
26 MainWindow::MainWindow(QApplication *app, QObject *parent) :
27 QMainWindow(),
28 _app(app),
29 _trayIcon(NULL),
30 _trayIconMenu(NULL),
31 _minimizeAction(NULL),
32 _maximizeAction(NULL),
33 _restoreAction(NULL),
34 _quitAction(NULL) {
36 QIcon icon = QIcon(":/declarative/img/status-online.png");
37 qDebug() << "SessionModel: tray sizes:" << icon.availableSizes();
39 setWindowIcon(icon);
40 setWindowTitle("Makneto Mobile");
42 if (QSystemTrayIcon::isSystemTrayAvailable()) {
44 createActions();
45 _trayIconMenu = new QMenu(this);
46 _trayIconMenu->addAction(_minimizeAction);
47 _trayIconMenu->addAction(_maximizeAction);
48 _trayIconMenu->addAction(_restoreAction);
49 _trayIconMenu->addSeparator();
50 _trayIconMenu->addAction(_quitAction);
52 _trayIcon = new QSystemTrayIcon(icon, this);
53 _trayIcon->setContextMenu(_trayIconMenu);
55 _trayIcon->setToolTip("Makneto");
57 _trayIcon->show();
59 connect(_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
60 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
62 } else {
63 qWarning() << "MainWindow: This environment doesn't support QSystemTrayIcon";
66 _app->setQuitOnLastWindowClosed(true);
67 if (_app->quitOnLastWindowClosed()) {
68 qDebug() << "MainWindow: will quit on close";
72 void MainWindow::createActions() {
73 _minimizeAction = new QAction(tr("Mi&nimize"), this);
74 connect(_minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
76 _maximizeAction = new QAction(tr("Ma&ximize"), this);
77 connect(_maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
79 _restoreAction = new QAction(tr("&Restore"), this);
80 connect(_restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
82 _quitAction = new QAction(tr("&Quit"), this);
83 connect(_quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
86 void MainWindow::closeEvent(QCloseEvent *event) {
87 if (_trayIcon && _trayIcon->isVisible()) {
88 qWarning() << "MainWindow: The program will keep running in the system tray.";
89 qWarning() << "MainWindow: To terminate the program, choose Quit in the context menu of the system tray entry.";
90 toggleVisibility();
91 event->ignore();
92 } else {
93 qWarning() << "MainWindow: Close window and exit.";
94 event->accept();
98 void MainWindow::toggleVisibility(){
99 if (isVisible())
100 _geometry = saveGeometry();
101 setVisible(!isVisible());
102 if (isVisible())
103 restoreGeometry(_geometry);
106 void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) {
107 switch (reason) {
108 case QSystemTrayIcon::Trigger:
109 case QSystemTrayIcon::DoubleClick:
110 toggleVisibility();
111 break;
112 case QSystemTrayIcon::MiddleClick:
113 break;
114 default:
119 MainWindow::~MainWindow() {
120 if (_trayIcon)
121 _trayIcon->deleteLater();
122 if (_trayIconMenu)
123 _trayIconMenu->deleteLater();
124 if (_minimizeAction)
125 _minimizeAction->deleteLater();
126 if (_maximizeAction)
127 _maximizeAction->deleteLater();
128 if (_restoreAction)
129 _restoreAction->deleteLater();
130 if (_quitAction)
131 _quitAction->deleteLater();
134 #include "main-window.moc"