Changed repo structure
[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 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24
25 #include "NotifyManager.h"
26 #include <QMessageBox>
27 #ifdef Q_WS_MAEMO_5
28     #include <QMaemo5InformationBox>
29 #endif
30 #include <QDebug>
31 #include <QLabel>
32
33 NotifyManager::NotifyManager(QObject *parent) :
34     QObject(parent)
35 {
36 }
37
38
39 void NotifyManager::notificationClosed() {
40     QObject* dialog = QObject::sender();
41
42     QHash<QString, QDialog*>::Iterator it;
43
44     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
45         if(it.value() == dialog) {
46             activeNotifies.remove(it.key());
47             it.value()->deleteLater();
48             return;
49         }
50     }
51 }
52
53 void NotifyManager::screenChanged() {
54     QHash<QString, QDialog*>::Iterator it;
55
56     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
57         it.value()->hide();
58         it.value()->show();
59     }
60 }
61
62 void NotifyManager::showNotification(Notify::NotifyType type, QString text) {
63
64     if(activeNotifies.contains(text)) {
65         return;
66     }
67
68     #ifdef Q_WS_MAEMO_5
69         QMaemo5InformationBox* mbox =
70                 new QMaemo5InformationBox(0);
71
72         QLabel* infoLabel = new QLabel(text);
73         infoLabel->setAlignment(Qt::AlignCenter);
74         infoLabel->setWordWrap(true);
75
76         mbox->setWidget(infoLabel);
77     #else
78         QMessageBox* mbox =
79                 new QMessageBox(qobject_cast<QWidget*>(this->parent()));
80
81     #endif
82
83
84     switch(type) {
85
86     case Notify::Info:
87         #ifdef Q_WS_MAEMO_5
88             mbox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
89         #else
90             mbox->setText(text);
91             mbox->setWindowTitle(tr("Information"));
92             mbox->setIcon(QMessageBox::Information);
93
94         #endif
95         break;
96
97     case Notify::Warning:
98         #ifndef Q_WS_MAEMO_5
99                 mbox->setText(text);
100                 mbox->setWindowTitle(tr("Warning"));
101                 mbox->setIcon(QMessageBox::Warning);
102                 break;
103         #endif
104
105     case Notify::Error:
106         #ifdef Q_WS_MAEMO_5
107             mbox->setTimeout(QMaemo5InformationBox::NoTimeout);
108         #else
109             mbox->setText(text);
110             mbox->setWindowTitle(tr("Error"));
111             mbox->setIcon(QMessageBox::Critical);
112         #endif
113         break;
114     }
115
116     activeNotifies.insert(text, mbox);
117     connect(mbox, SIGNAL(finished(int)), this, SLOT(notificationClosed()));
118     mbox->exec();
119 }