Merge branch 'qml' of ssh://drop.maemo.org/git/mdictionary into qml
[mdictionary] / src / mdictionary / gui / NotifyManager.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! \file NotifyManager.cpp
23     \brief Manages notifications in applications
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28 #include "NotifyManager.h"
29 #include <QMessageBox>
30 #ifdef Q_WS_MAEMO_5
31     #include <QMaemo5InformationBox>
32 #endif
33 #include <QDebug>
34 #include <QLabel>
35
36 NotifyManager::NotifyManager(QObject *parent) :
37     QObject(parent)
38 {
39     menu = 0;
40 }
41
42
43 void NotifyManager::notificationClosed() {
44     QObject* dialog = QObject::sender();
45
46     QHash<QString, QDialog*>::Iterator it;
47
48     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
49         if(it.value() == dialog) {
50             activeNotifies.remove(it.key());
51             it.value()->deleteLater();
52             return;
53         }
54     }
55 }
56
57 void NotifyManager::screenChanged() {
58     QHash<QString, QDialog*>::Iterator it;
59
60     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
61         it.value()->hide();
62         it.value()->show();
63     }
64 }
65
66 void NotifyManager::showNotification(Notify::NotifyType type, QString text) {
67     if(activeNotifies.contains(text)) {
68         return;
69     }
70
71     QWidget* parent;
72
73     if(menu && menu->isVisible())
74         parent = menu;
75     else
76         parent = qobject_cast<QWidget*>(this->parent());
77
78     #ifdef Q_WS_MAEMO_5
79         QMaemo5InformationBox* mbox =
80                 new QMaemo5InformationBox(parent);
81
82         QLabel* infoLabel = new QLabel(text);
83         infoLabel->setAlignment(Qt::AlignCenter);
84         infoLabel->setWordWrap(true);
85
86
87         mbox->setWidget(infoLabel);
88
89     #else
90         QMessageBox* mbox =
91                 new QMessageBox(parent);
92
93     #endif
94
95
96
97     switch(type) {
98
99     case Notify::Info:
100         #ifdef Q_WS_MAEMO_5
101             mbox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
102         #else
103             mbox->setText(text);
104             mbox->setWindowTitle(tr("Information"));
105             mbox->setIcon(QMessageBox::Information);
106
107
108
109         #endif
110         break;
111
112     case Notify::Warning:
113         #ifndef Q_WS_MAEMO_5
114                 mbox->setText(text);
115                 mbox->setWindowTitle(tr("Warning"));
116                 mbox->setIcon(QMessageBox::Warning);
117                 break;
118         #endif
119
120     case Notify::Error:
121         #ifdef Q_WS_MAEMO_5
122             mbox->setTimeout(QMaemo5InformationBox::NoTimeout);
123             ((QLabel*)mbox->widget())->setContentsMargins(5,15,5,15);
124         #else
125             mbox->setText(text);
126             mbox->setWindowTitle(tr("Error"));
127             mbox->setIcon(QMessageBox::Critical);
128         #endif
129         break;
130     }
131     activeNotifies.insert(text, mbox);
132     connect(mbox, SIGNAL(finished(int)), this, SLOT(notificationClosed()));
133     mbox->exec();
134 }
135
136 void NotifyManager::setMenu(QWidget *w) {
137     menu = w;
138 }