Bug fix: unable download and add xdxf dictionary more than once
[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     menu = 0;
37 }
38
39
40 void NotifyManager::notificationClosed() {
41     QObject* dialog = QObject::sender();
42
43     QHash<QString, QDialog*>::Iterator it;
44
45     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
46         if(it.value() == dialog) {
47             activeNotifies.remove(it.key());
48             it.value()->deleteLater();
49             return;
50         }
51     }
52 }
53
54 void NotifyManager::screenChanged() {
55     QHash<QString, QDialog*>::Iterator it;
56
57     for(it = activeNotifies.begin(); it != activeNotifies.end(); it++) {
58         it.value()->hide();
59         it.value()->show();
60     }
61 }
62
63 void NotifyManager::showNotification(Notify::NotifyType type, QString text) {
64     if(activeNotifies.contains(text)) {
65         return;
66     }
67
68     QWidget* parent;
69
70     if(menu && menu->isVisible())
71         parent = menu;
72     else
73         parent = qobject_cast<QWidget*>(this->parent());
74
75     #ifdef Q_WS_MAEMO_5
76         QMaemo5InformationBox* mbox =
77                 new QMaemo5InformationBox(parent);
78
79         QLabel* infoLabel = new QLabel(text);
80         infoLabel->setAlignment(Qt::AlignCenter);
81         infoLabel->setWordWrap(true);
82
83
84         mbox->setWidget(infoLabel);
85
86     #else
87         QMessageBox* mbox =
88                 new QMessageBox(parent);
89
90     #endif
91
92
93
94     switch(type) {
95
96     case Notify::Info:
97         #ifdef Q_WS_MAEMO_5
98             mbox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
99         #else
100             mbox->setText(text);
101             mbox->setWindowTitle(tr("Information"));
102             mbox->setIcon(QMessageBox::Information);
103
104
105
106         #endif
107         break;
108
109     case Notify::Warning:
110         #ifndef Q_WS_MAEMO_5
111                 mbox->setText(text);
112                 mbox->setWindowTitle(tr("Warning"));
113                 mbox->setIcon(QMessageBox::Warning);
114                 break;
115         #endif
116
117     case Notify::Error:
118         #ifdef Q_WS_MAEMO_5
119             mbox->setTimeout(QMaemo5InformationBox::NoTimeout);
120             ((QLabel*)mbox->widget())->setContentsMargins(5,15,5,15);
121         #else
122             mbox->setText(text);
123             mbox->setWindowTitle(tr("Error"));
124             mbox->setIcon(QMessageBox::Critical);
125         #endif
126         break;
127     }
128     activeNotifies.insert(text, mbox);
129     connect(mbox, SIGNAL(finished(int)), this, SLOT(notificationClosed()));
130     mbox->exec();
131 }
132
133 void NotifyManager::setMenu(QWidget *w) {
134     menu = w;
135 }