code clean
[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     \file XdxfDialog.cpp
23     \brief Implementation of xdxf plugin's dialogs.
24
25     \author Mateusz Półrola <mateusz.polrola@gmail.com>
26 */
27
28 #include "XdxfDialog.h"
29 #include <QDebug>
30
31 XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
32                        XdxfDialogType type,
33                        QWidget *parent) :
34                 QDialog(parent) {
35
36     this->plugin = plugin;
37     this->type = type;
38
39 #ifndef Q_WS_MAEMO_5
40
41     view= new QDeclarativeView();
42     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/XdxfDialog.qml"));
43     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
44     view->setAlignment(Qt::AlignCenter);
45     view->show();
46
47     mainLayout = new QVBoxLayout;
48     mainLayout->addWidget(view);
49     setLayout(mainLayout);
50     view->setWindowTitle(tr("Xdxf Settings"));
51
52     QGraphicsObject *rootObject = view->rootObject();
53
54     connect(this, SIGNAL(setPlugin(QVariant)),
55            rootObject, SLOT(setPlugin(QVariant)));
56     connect(this, SIGNAL(setFrom(QVariant)),
57            rootObject, SLOT(setFrom(QVariant)));
58     connect(this, SIGNAL(setTo(QVariant)),
59            rootObject, SLOT(setTo(QVariant)));
60     connect(this, SIGNAL(setDescription(QVariant)),
61            rootObject, SLOT(setDescription(QVariant)));
62     connect(this, SIGNAL(setInfo(QVariant)),
63            rootObject, SLOT(setInfo(QVariant)));
64     connect(this, SIGNAL(setCheckedOptimalize(QVariant)),
65            rootObject, SLOT(setCheckedOptimalize(QVariant)));
66     connect(this, SIGNAL(setCheckedStrip(QVariant)),
67            rootObject, SLOT(setCheckedStrip(QVariant)));
68     connect(this,SIGNAL(setButtonText(QVariant)),
69             rootObject, SLOT(setButtonText(QVariant)));
70     connect(this,SIGNAL(setNew(QVariant)),
71             rootObject, SLOT(setNew(QVariant)));
72     connect(this,SIGNAL(setPath(QVariant)),
73             rootObject, SLOT(setPath(QVariant)));
74
75     connect(rootObject, SIGNAL(saveButtonClicked()),
76            this, SLOT(accept()));
77     connect(rootObject, SIGNAL(downloadButtonClicked()),
78            this, SLOT(downloadFile()));
79     connect(rootObject, SIGNAL(browseButtonClicked()),
80            this, SLOT(selectFile()));
81
82     connect(rootObject, SIGNAL(optimalizeCheckboxChanged(bool)),
83            this, SLOT(setGenerateCache(bool)));
84     connect(rootObject, SIGNAL(stripCheckboxChanged(bool)),
85            this, SLOT(setAccents(bool)));
86
87 #else
88
89     cacheToolTip = tr("Optimize for quicker searches (may take some time)");
90     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
91
92     connect(cacheCheckBox, SIGNAL(toggled(bool)),
93             this, SLOT(setGenerateCache(bool)));
94     connect(accentsCheckBox, SIGNAL(toggled(bool)),
95             this, SLOT(setAccents(bool)));
96
97     #ifdef Q_WS_MAEMO_5
98         connect(accentsInfoToolButton, SIGNAL(clicked()),
99                  this, SLOT(showAccentsInfo()));
100         connect(cacheInfoToolButton, SIGNAL(clicked()),
101                  this, SLOT(showCacheInfo()));
102     #endif
103
104     if(type == New) {
105         connect(browseButton, SIGNAL(clicked()),
106                 this, SLOT(selectFile()));
107         connect(downloadButton, SIGNAL(clicked()),
108                 this, SLOT(downloadFile()));
109         connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
110                 this, SLOT(fileDownloaded(QString)));
111     }
112     connect(confirmButton, SIGNAL(clicked()),
113             this, SLOT(accept()));
114 #endif
115     initializeUI();
116 }
117
118
119 void XdxfDialog::initializeUI() {
120 #ifndef Q_WS_MAEMO_5
121
122     if(type != New){
123         emit setNew(false);
124         emit setPlugin(plugin->type().toUpper());
125         emit setFrom(plugin->langFrom());
126         emit setTo(plugin->langTo());
127         emit setDescription(plugin->name());
128         emit setInfo(plugin->infoNote());
129         emit setButtonText("Save settings");
130     }
131     else{
132         emit setButtonText("Add");
133         emit setNew(true);
134         connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
135                 this, SLOT(fileDownloaded(QString)));
136     }
137
138     if(!plugin) {
139         emit setCheckedOptimalize(true);
140         emit setCheckedStrip(true);
141 //        accentsCheckBox->setEnabled(false);
142         _generateCache = true;
143         _accents = true;
144         _dictionaryFilePath = "";
145     }
146     else if(plugin && plugin->settings()->value("cached") == "true") {
147         emit setCheckedOptimalize(true);
148         emit setCheckedStrip(true);
149 //        accentsCheckBox->setEnabled(false);
150         _generateCache = true;
151         _accents = true;
152     }
153     else {
154         emit setCheckedOptimalize(false);
155         _generateCache = false;
156         if(plugin->settings()->value("strip_accents") == "true") {
157             emit setCheckedStrip(true);
158             _accents = true;
159         }
160         else {
161             emit setCheckedStrip(false);
162             _accents = false;
163         }
164     }
165
166 #else
167     mainVerticalLayout = new QVBoxLayout;
168     widget = new QWidget;
169     widget->setLayout(mainVerticalLayout);
170
171     infoLabel = new QLabel;
172     infoLabel->setWordWrap(true);
173
174     if(type == New) {
175         setWindowTitle(tr("Add new XDXF dictionary"));
176
177         browseLayout = new QHBoxLayout;
178
179         QVBoxLayout* buttonLayout = new QVBoxLayout;
180         browseButton = new QPushButton(tr("Browse"));
181         browseButton->setMaximumWidth(150);
182
183         downloadButton = new QPushButton(tr("Download"));
184         downloadButton->setMaximumWidth(150);
185
186         infoLayout = new QHBoxLayout;
187         infoLabel->setText(tr("Dictionary file: not selected"));
188
189         browseLayout->addWidget(infoLabel,0,Qt::AlignLeft);
190         browseLayout->addLayout(buttonLayout);
191         buttonLayout->addWidget(browseButton,0,Qt::AlignLeft);
192         buttonLayout->addWidget(downloadButton,0,Qt::AlignLeft);
193
194         mainVerticalLayout->addLayout(browseLayout);
195     }
196     else {
197         setWindowTitle(tr("XDXF Settings"));
198
199         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
200                        tr("From: ") + plugin->langFrom() + "\n" +
201                        tr("To: ") + plugin->langTo() + "\n" +
202                        tr("Description: ") + plugin->name() + "\n" +
203                        plugin->infoNote());
204         mainVerticalLayout->addWidget(infoLabel);
205     }
206
207     accentsLayout = new QHBoxLayout;
208     accentsCheckBox = new QCheckBox(tr("Strip accents"));
209     accentsCheckBox->setToolTip(accentsToolTip);
210     accentsLayout->addWidget(accentsCheckBox); 
211     #ifdef Q_WS_MAEMO_5
212         accentsInfoToolButton = new QToolButton;
213         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
214         accentsLayout->addWidget(accentsInfoToolButton);
215     #endif
216
217     cacheLayout = new QHBoxLayout;
218     cacheCheckBox = new QCheckBox(tr("Optimize"));
219     cacheCheckBox->setToolTip(cacheToolTip);
220     cacheLayout->addWidget(cacheCheckBox);
221
222     #ifdef Q_WS_MAEMO_5
223         cacheInfoToolButton = new QToolButton;
224         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
225         cacheLayout->addWidget(cacheInfoToolButton);
226     #endif
227
228     mainVerticalLayout->addLayout(cacheLayout);
229     mainVerticalLayout->addLayout(accentsLayout);
230
231     //load old setting if exists
232     if(!plugin) {
233         cacheCheckBox->setChecked(true);
234         accentsCheckBox->setChecked(true);
235         accentsCheckBox->setEnabled(false);
236         _generateCache = true;
237         _accents = true;
238         _dictionaryFilePath = "";
239     }
240     else if(plugin && plugin->settings()->value("cached") == "true") {
241         cacheCheckBox->setChecked(true);
242         accentsCheckBox->setChecked(true);
243         accentsCheckBox->setEnabled(false);
244         _generateCache = true;
245         _accents = true;
246     }
247     else {
248         cacheCheckBox->setChecked(false);
249         _generateCache = false;
250
251         if(plugin->settings()->value("strip_accents") == "true") {
252             accentsCheckBox->setChecked(true);
253             _accents = true;
254         }
255         else {
256             accentsCheckBox->setChecked(false);
257             _accents = false;
258         }
259     }
260
261     confirmButton = new QPushButton;
262     mainVerticalLayout->addWidget(confirmButton);
263     if(type == New)
264         confirmButton->setText(tr("Add"));
265     else
266         confirmButton->setText(tr("Save settings"));
267
268     scrollArea = new QScrollArea;
269     scrollArea->setWidget(widget);
270     scrollArea->setWidgetResizable(true);
271
272     #ifdef Q_WS_MAEMO_5
273         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
274     #else
275         if(type==New) {
276             infoLabel->setMinimumWidth(250);
277             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
278         }
279     #endif
280
281     layout = new QHBoxLayout;
282     layout->addWidget(scrollArea);
283     setLayout(layout);
284
285     #ifndef Q_WS_MAEMO_5
286         setMinimumSize(385,200);
287     #else
288         setMinimumHeight(350);
289     #endif
290
291     scrollArea->setLineWidth(0);
292     scrollArea->setMidLineWidth(0);
293     scrollArea->setFrameStyle(QFrame::NoFrame);
294 #endif
295 }
296
297
298 void XdxfDialog::setAccents(bool accents) {
299     _accents = accents;
300 }
301
302
303 void XdxfDialog::setGenerateCache(bool generate) {
304     _generateCache = generate;
305 #ifndef Q_WS_MAEMO_5
306     if(generate) {
307         _lastAccents = _accents;
308         emit setCheckedOptimalize(true);
309         emit setCheckedStrip(true);
310     }
311     else{
312         emit setCheckedStrip(_lastAccents);
313     }
314 //  accentsCheckBox->setEnabled(!generate);
315 #else
316     if(generate) {
317         _lastAccents = _accents;
318         accentsCheckBox->setChecked(true);
319     }
320     else
321         accentsCheckBox->setChecked(_lastAccents);
322     accentsCheckBox->setEnabled(!generate);
323 #endif
324 }
325
326
327 void XdxfDialog::selectFile() {
328     QString fileName = QFileDialog::getOpenFileName(this,
329                                      tr("Select dictionary file"),
330                                      _dictionaryFilePath,
331                                      tr("XDXF Files (*.xdxf)"),
332                                      NULL,
333                                      NULL);
334     if (!fileName.isEmpty()) {
335         _dictionaryFilePath = fileName;
336         #ifndef Q_WS_MAEMO_5
337             emit setPath(tr("Dictionary file: %1").arg(fileName));
338         #else
339             infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
340             updateGeometry();
341         #endif
342     }
343 }
344
345
346 void XdxfDialog::fileDownloaded(QString) {
347 #ifndef Q_WS_MAEMO_5
348     emit setPath(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
349     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
350 #else
351     infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
352     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
353     updateGeometry();
354 #endif
355 }
356
357
358 void XdxfDialog::downloadFile() {
359     XdxfPlugin::dictDownloader.download(this);
360 }
361
362
363 void XdxfDialog::saveSettings() {
364     _settings = new Settings;
365     if(plugin) {
366         foreach(QString key, plugin->settings()->keys())
367             _settings->setValue(key, plugin->settings()->value(key));
368     }
369     else
370         _settings->setValue("path", _dictionaryFilePath);
371     if(_generateCache)
372         _settings->setValue("generateCache", "true");
373     else
374         _settings->setValue("generateCache", "false");
375
376     if(_accents)
377         _settings->setValue("strip_accents", "true");
378     else
379         _settings->setValue("strip_accents", "false");
380 }
381
382
383 void XdxfDialog::accept() {
384     if(type == New && _dictionaryFilePath.isEmpty()) {
385         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
386         return;
387     }
388
389     saveSettings();
390     QDialog::accept();
391 }
392
393
394 Settings* XdxfDialog::getSettings() {
395     return _settings;
396 }
397
398
399 #ifdef Q_WS_MAEMO_5
400     void XdxfDialog::showCacheInfo() {
401         Q_EMIT notify(Notify::Warning, cacheToolTip);
402     }
403
404
405     void XdxfDialog::showAccentsInfo() {
406         Q_EMIT notify(Notify::Warning, accentsToolTip);
407     }
408 #endif
409