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