Added notifications and canceling to xdxf download dialog
[mdictionary] / src / plugins / xdxf / XdxfDialog.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 //Created by Mateusz Półrola
23
24 #include "XdxfDialog.h"
25 #include <QDebug>
26
27 XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
28                        XdxfDialogType type,
29                        QWidget *parent) :
30     QDialog(parent) {
31     this->plugin = plugin;
32     this->type = type;
33
34
35     cacheToolTip = tr("Optimize for quicker searches (may take some time)");
36     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
37
38     initializeUI();
39
40     connect(cacheCheckBox, SIGNAL(toggled(bool)),
41             this, SLOT(setGenerateCache(bool)));
42
43     connect(accentsCheckBox, SIGNAL(toggled(bool)),
44             this, SLOT(setAccents(bool)));
45
46     #ifdef Q_WS_MAEMO_5
47         connect(accentsInfoToolButton, SIGNAL(clicked()),
48                  this, SLOT(showAccentsInfo()));
49
50         connect(cacheInfoToolButton, SIGNAL(clicked()),
51                  this, SLOT(showCacheInfo()));
52     #endif
53
54     if(type == New) {
55         connect(browseButton, SIGNAL(clicked()),
56                 this, SLOT(selectFile()));
57
58         connect(downloadButton, SIGNAL(clicked()),
59                 this, SLOT(downloadFile()));
60
61         connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
62                 this, SLOT(fileDownloaded(QString)));
63     }
64
65     connect(confirmButton, SIGNAL(clicked()),
66             this, SLOT(accept()));
67
68 }
69
70 void XdxfDialog::fileDownloaded(QString name) {
71     infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
72     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
73     updateGeometry();
74 }
75
76
77 void XdxfDialog::initializeUI() {
78     mainVerticalLayout = new QVBoxLayout;
79     widget = new QWidget;
80     widget->setLayout(mainVerticalLayout);
81
82     infoLabel = new QLabel;
83     infoLabel->setWordWrap(true);
84
85     if(type == New) {
86         setWindowTitle(tr("Add new XDXF dictionary"));
87
88         browseLayout = new QHBoxLayout;
89
90         QHBoxLayout* buttonLayout = new QHBoxLayout;
91         browseButton = new QPushButton(tr("Browse"));
92         browseButton->setMaximumWidth(100);
93
94         downloadButton = new QPushButton(tr("Download"));
95         downloadButton->setMaximumWidth(150);
96
97         infoLabel->setText(tr("Dictionary file: not selected"));
98
99         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
100
101         browseLayout->addLayout(buttonLayout);
102         buttonLayout->addWidget(browseButton);
103         buttonLayout->addWidget(downloadButton);
104
105         mainVerticalLayout->addLayout(browseLayout);
106     }
107     else {
108         setWindowTitle(tr("XDXF Settings"));
109
110         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
111                        tr("From: ") + plugin->langFrom() + "\n" +
112                        tr("To: ") + plugin->langTo() + "\n" +
113                        tr("Description: ") + plugin->name() + "\n" +
114                        plugin->infoNote());
115         mainVerticalLayout->addWidget(infoLabel);
116     }
117
118     accentsLayout = new QHBoxLayout;
119     accentsCheckBox = new QCheckBox(tr("Strip accents"));
120     accentsCheckBox->setToolTip(accentsToolTip);
121     accentsLayout->addWidget(accentsCheckBox); 
122     #ifdef Q_WS_MAEMO_5
123         accentsInfoToolButton = new QToolButton;
124         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
125         accentsLayout->addWidget(accentsInfoToolButton);
126     #endif
127
128     cacheLayout = new QHBoxLayout;
129     cacheCheckBox = new QCheckBox(tr("Optimize"));
130     cacheCheckBox->setToolTip(cacheToolTip);
131     cacheLayout->addWidget(cacheCheckBox);
132     #ifdef Q_WS_MAEMO_5
133         cacheInfoToolButton = new QToolButton;
134         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
135         cacheLayout->addWidget(cacheInfoToolButton);
136     #endif
137
138     mainVerticalLayout->addLayout(cacheLayout);
139     mainVerticalLayout->addLayout(accentsLayout);
140
141
142     //load old setting if exists
143     if(!plugin) {
144         cacheCheckBox->setChecked(true);
145         accentsCheckBox->setChecked(true);
146         accentsCheckBox->setEnabled(false);
147         _generateCache = true;
148         _accents = true;
149         _dictionaryFilePath = "";
150     }
151     else if(plugin && plugin->settings()->value("cached") == "true") {
152         cacheCheckBox->setChecked(true);
153         accentsCheckBox->setChecked(true);
154         accentsCheckBox->setEnabled(false);
155         _generateCache = true;
156         _accents = true;
157     }
158     else {
159         cacheCheckBox->setChecked(false);
160         _generateCache = false;
161
162         if(plugin->settings()->value("strip_accents") == "true") {
163             accentsCheckBox->setChecked(true);
164             _accents = true;
165         }
166         else {
167             accentsCheckBox->setChecked(false);
168             _accents = false;
169         }
170     }
171
172     confirmButton = new QPushButton;
173     mainVerticalLayout->addWidget(confirmButton);
174     if(type == New) {
175         confirmButton->setText(tr("Add"));
176     }
177     else {
178         confirmButton->setText(tr("Save settings"));
179     }
180
181     scrollArea = new QScrollArea;
182     scrollArea->setWidget(widget);
183     scrollArea->setWidgetResizable(true);
184     #ifdef Q_WS_MAEMO_5
185         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
186     #else
187         if(type==New) {
188             infoLabel->setMinimumWidth(250);
189             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
190             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
191             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
192         }
193     #endif
194
195     layout = new QHBoxLayout;
196     layout->addWidget(scrollArea);
197     setLayout(layout);
198
199     #ifndef Q_WS_MAEMO_5
200         setMinimumSize(450,200);
201     #else
202         setMinimumHeight(350);
203     #endif
204
205     scrollArea->setLineWidth(0);
206     scrollArea->setMidLineWidth(0);
207     scrollArea->setFrameStyle(QFrame::NoFrame);
208
209
210 }
211
212
213 void XdxfDialog::setAccents(bool accents) {
214     _accents = accents;
215 }
216
217 void XdxfDialog::setGenerateCache(bool generate) {
218     _generateCache = generate;
219
220     if(generate) {
221         _lastAccents = _accents;
222         accentsCheckBox->setChecked(true);
223     }
224     else
225         accentsCheckBox->setChecked(_lastAccents);
226
227     accentsCheckBox->setEnabled(!generate);
228 }
229
230 void XdxfDialog::selectFile() {
231     QString fileName = QFileDialog::getOpenFileName(this,
232                                      tr("Select dictionary file"),
233                                      _dictionaryFilePath,
234                                      tr("XDXF Files (*.xdxf)"),
235                                      NULL,
236                                      NULL);
237
238     if (!fileName.isEmpty()) {
239         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
240         _dictionaryFilePath = fileName;
241         updateGeometry();
242     }
243 }
244
245 void XdxfDialog::downloadFile() {
246     XdxfPlugin::dictDownloader.download(this);
247 }
248
249 void XdxfDialog::saveSettings() {
250     _settings = new Settings;
251     if(plugin) {
252         foreach(QString key, plugin->settings()->keys())
253             _settings->setValue(key, plugin->settings()->value(key));
254     }
255     else {
256         _settings->setValue("path", _dictionaryFilePath);
257     }
258
259     if(_generateCache)
260         _settings->setValue("generateCache", "true");
261     else
262         _settings->setValue("generateCache", "false");
263
264     if(_accents)
265         _settings->setValue("strip_accents", "true");
266     else
267         _settings->setValue("strip_accents", "false");
268 }
269
270 void XdxfDialog::accept() {
271     if(type == New && _dictionaryFilePath.isEmpty()) {
272         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
273
274         return;
275     }
276
277     saveSettings();
278     QDialog::accept();
279 }
280
281 Settings* XdxfDialog::getSettings() {
282     return _settings;
283 }
284
285 #ifdef Q_WS_MAEMO_5
286     void XdxfDialog::showCacheInfo() {
287         Q_EMIT notify(Notify::Warning, cacheToolTip);
288     }
289
290     void XdxfDialog::showAccentsInfo() {
291         Q_EMIT notify(Notify::Warning, accentsToolTip);
292     }
293 #endif
294