32abb55c62fe4e1629c75a8062d7f00e06d0fe8a
[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
116     initializeUI();
117 }
118
119
120 void XdxfDialog::initializeUI() {
121 #ifndef Q_WS_MAEMO_5
122
123     if(type != New){
124         emit setNew(false);
125         emit setPlugin(plugin->type().toUpper());
126         emit setFrom(plugin->langFrom());
127         emit setTo(plugin->langTo());
128         emit setDescription(plugin->name());
129         emit setInfo(plugin->infoNote());
130     }
131     else{
132         emit setNew(true);
133         connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
134                 this, SLOT(fileDownloaded(QString)));
135     }
136
137     if(!plugin) {
138         emit setCheckedOptimalize(true);
139         emit setCheckedStrip(true);
140 //        accentsCheckBox->setEnabled(false);
141         _generateCache = true;
142         _accents = true;
143         _dictionaryFilePath = "";
144     }
145     else if(plugin && plugin->settings()->value("cached") == "true") {
146         emit setCheckedOptimalize(true);
147         emit setCheckedStrip(true);
148 //        accentsCheckBox->setEnabled(false);
149         _generateCache = true;
150         _accents = true;
151     }
152     else {
153         emit setCheckedOptimalize(false);
154         _generateCache = false;
155         if(plugin->settings()->value("strip_accents") == "true") {
156             emit setCheckedStrip(true);
157             _accents = true;
158         }
159         else {
160             emit setCheckedStrip(false);
161             _accents = false;
162         }
163     }
164     if(type == New)
165         emit setButtonText("Add");
166     else
167         emit setButtonText("Save settings");
168
169 #else
170     mainVerticalLayout = new QVBoxLayout;
171     widget = new QWidget;
172     widget->setLayout(mainVerticalLayout);
173
174     infoLabel = new QLabel;
175     infoLabel->setWordWrap(true);
176
177     if(type == New) {
178         setWindowTitle(tr("Add new XDXF dictionary"));
179
180         browseLayout = new QHBoxLayout;
181
182         QVBoxLayout* buttonLayout = new QVBoxLayout;
183         browseButton = new QPushButton(tr("Browse"));
184         browseButton->setMaximumWidth(150);
185
186         downloadButton = new QPushButton(tr("Download"));
187         downloadButton->setMaximumWidth(150);
188
189         infoLayout = new QHBoxLayout;
190         infoLabel->setText(tr("Dictionary file: not selected"));
191
192         browseLayout->addWidget(infoLabel,0,Qt::AlignLeft);
193         browseLayout->addLayout(buttonLayout);
194         buttonLayout->addWidget(browseButton,0,Qt::AlignLeft);
195         buttonLayout->addWidget(downloadButton,0,Qt::AlignLeft);
196
197         mainVerticalLayout->addLayout(browseLayout);
198     }
199     else {
200         setWindowTitle(tr("XDXF Settings"));
201
202         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
203                        tr("From: ") + plugin->langFrom() + "\n" +
204                        tr("To: ") + plugin->langTo() + "\n" +
205                        tr("Description: ") + plugin->name() + "\n" +
206                        plugin->infoNote());
207         mainVerticalLayout->addWidget(infoLabel);
208     }
209
210     accentsLayout = new QHBoxLayout;
211     accentsCheckBox = new QCheckBox(tr("Strip accents"));
212     accentsCheckBox->setToolTip(accentsToolTip);
213     accentsLayout->addWidget(accentsCheckBox); 
214     #ifdef Q_WS_MAEMO_5
215         accentsInfoToolButton = new QToolButton;
216         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
217         accentsLayout->addWidget(accentsInfoToolButton);
218     #endif
219
220     cacheLayout = new QHBoxLayout;
221     cacheCheckBox = new QCheckBox(tr("Optimize"));
222     cacheCheckBox->setToolTip(cacheToolTip);
223     cacheLayout->addWidget(cacheCheckBox);
224
225     #ifdef Q_WS_MAEMO_5
226         cacheInfoToolButton = new QToolButton;
227         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
228         cacheLayout->addWidget(cacheInfoToolButton);
229     #endif
230
231     mainVerticalLayout->addLayout(cacheLayout);
232     mainVerticalLayout->addLayout(accentsLayout);
233
234     //load old setting if exists
235     if(!plugin) {
236         cacheCheckBox->setChecked(true);
237         accentsCheckBox->setChecked(true);
238         accentsCheckBox->setEnabled(false);
239         _generateCache = true;
240         _accents = true;
241         _dictionaryFilePath = "";
242     }
243     else if(plugin && plugin->settings()->value("cached") == "true") {
244         cacheCheckBox->setChecked(true);
245         accentsCheckBox->setChecked(true);
246         accentsCheckBox->setEnabled(false);
247         _generateCache = true;
248         _accents = true;
249     }
250     else {
251         cacheCheckBox->setChecked(false);
252         _generateCache = false;
253
254         if(plugin->settings()->value("strip_accents") == "true") {
255             accentsCheckBox->setChecked(true);
256             _accents = true;
257         }
258         else {
259             accentsCheckBox->setChecked(false);
260             _accents = false;
261         }
262     }
263
264     confirmButton = new QPushButton;
265     mainVerticalLayout->addWidget(confirmButton);
266     if(type == New)
267         confirmButton->setText(tr("Add"));
268     else
269         confirmButton->setText(tr("Save settings"));
270
271     scrollArea = new QScrollArea;
272     scrollArea->setWidget(widget);
273     scrollArea->setWidgetResizable(true);
274
275     #ifdef Q_WS_MAEMO_5
276         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
277     #else
278         if(type==New) {
279             infoLabel->setMinimumWidth(250);
280             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
281         }
282     #endif
283
284     layout = new QHBoxLayout;
285     layout->addWidget(scrollArea);
286     setLayout(layout);
287
288     #ifndef Q_WS_MAEMO_5
289         setMinimumSize(385,200);
290     #else
291         setMinimumHeight(350);
292     #endif
293
294     scrollArea->setLineWidth(0);
295     scrollArea->setMidLineWidth(0);
296     scrollArea->setFrameStyle(QFrame::NoFrame);
297 #endif
298 }
299
300
301 void XdxfDialog::setAccents(bool accents) {
302     qDebug()<<"setAcents"<<accents;
303     _accents = accents;
304 }
305
306
307 void XdxfDialog::setGenerateCache(bool generate) {
308     _generateCache = generate;
309 #ifndef Q_WS_MAEMO_5
310     if(generate) {
311         _lastAccents = _accents;
312         emit setCheckedOptimalize(true);
313         emit setCheckedStrip(true);
314     }
315     else{
316         emit setCheckedStrip(_lastAccents);
317     }
318 //  accentsCheckBox->setEnabled(!generate);
319 #else
320     if(generate) {
321         _lastAccents = _accents;
322         accentsCheckBox->setChecked(true);
323     }
324     else
325         accentsCheckBox->setChecked(_lastAccents);
326     accentsCheckBox->setEnabled(!generate);
327 #endif
328 }
329
330
331 void XdxfDialog::selectFile() {
332     QString fileName = QFileDialog::getOpenFileName(this,
333                                      tr("Select dictionary file"),
334                                      _dictionaryFilePath,
335                                      tr("XDXF Files (*.xdxf)"),
336                                      NULL,
337                                      NULL);
338     if (!fileName.isEmpty()) {
339         _dictionaryFilePath = fileName;
340         #ifndef Q_WS_MAEMO_5
341             emit setPath(tr("Dictionary file: %1").arg(fileName));
342         #else
343             infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
344             updateGeometry();
345         #endif
346     }
347 }
348
349
350 void XdxfDialog::fileDownloaded(QString) {
351 #ifndef Q_WS_MAEMO_5
352     qDebug()<<"fileDownloaded";
353     emit setPath(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
354     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
355 #else
356     infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
357     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
358     updateGeometry();
359 #endif
360 }
361
362
363 void XdxfDialog::downloadFile() {
364     XdxfPlugin::dictDownloader.download(this);
365 }
366
367
368 void XdxfDialog::saveSettings() {
369     _settings = new Settings;
370     if(plugin) {
371         foreach(QString key, plugin->settings()->keys())
372             _settings->setValue(key, plugin->settings()->value(key));
373     }
374     else
375         _settings->setValue("path", _dictionaryFilePath);
376     if(_generateCache)
377         _settings->setValue("generateCache", "true");
378     else
379         _settings->setValue("generateCache", "false");
380
381     if(_accents)
382         _settings->setValue("strip_accents", "true");
383     else
384         _settings->setValue("strip_accents", "false");
385 }
386
387
388 void XdxfDialog::accept() {
389     if(type == New && _dictionaryFilePath.isEmpty()) {
390         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
391         return;
392     }
393
394     saveSettings();
395     QDialog::accept();
396 }
397
398
399 Settings* XdxfDialog::getSettings() {
400     return _settings;
401 }
402
403
404 #ifdef Q_WS_MAEMO_5
405     void XdxfDialog::showCacheInfo() {
406         Q_EMIT notify(Notify::Warning, cacheToolTip);
407     }
408
409
410     void XdxfDialog::showAccentsInfo() {
411         Q_EMIT notify(Notify::Warning, accentsToolTip);
412     }
413 #endif
414