Added download progress notification
[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         infoLayout = new QHBoxLayout;
98         infoLayout->addWidget(infoLabel,0,Qt::AlignLeft);
99         infoLabel->setText(tr("Dictionary file: not selected"));
100
101         browseLayout->addLayout(buttonLayout);
102         buttonLayout->addWidget(browseButton);
103         buttonLayout->addWidget(downloadButton);
104
105         mainVerticalLayout->addLayout(browseLayout);
106         mainVerticalLayout->addLayout(infoLayout);
107     }
108     else {
109         setWindowTitle(tr("XDXF Settings"));
110
111         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
112                        tr("From: ") + plugin->langFrom() + "\n" +
113                        tr("To: ") + plugin->langTo() + "\n" +
114                        tr("Description: ") + plugin->name() + "\n" +
115                        plugin->infoNote());
116         mainVerticalLayout->addWidget(infoLabel);
117     }
118
119     accentsLayout = new QHBoxLayout;
120     accentsCheckBox = new QCheckBox(tr("Strip accents"));
121     accentsCheckBox->setToolTip(accentsToolTip);
122     accentsLayout->addWidget(accentsCheckBox); 
123     #ifdef Q_WS_MAEMO_5
124         accentsInfoToolButton = new QToolButton;
125         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
126         accentsLayout->addWidget(accentsInfoToolButton);
127     #endif
128
129     cacheLayout = new QHBoxLayout;
130     cacheCheckBox = new QCheckBox(tr("Optimize"));
131     cacheCheckBox->setToolTip(cacheToolTip);
132     cacheLayout->addWidget(cacheCheckBox);
133
134     #ifdef Q_WS_MAEMO_5
135         cacheInfoToolButton = new QToolButton;
136         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
137         cacheLayout->addWidget(cacheInfoToolButton);
138     #endif
139
140     mainVerticalLayout->addLayout(cacheLayout);
141     mainVerticalLayout->addLayout(accentsLayout);
142
143
144     //load old setting if exists
145     if(!plugin) {
146         cacheCheckBox->setChecked(true);
147         accentsCheckBox->setChecked(true);
148         accentsCheckBox->setEnabled(false);
149         _generateCache = true;
150         _accents = true;
151         _dictionaryFilePath = "";
152     }
153     else if(plugin && plugin->settings()->value("cached") == "true") {
154         cacheCheckBox->setChecked(true);
155         accentsCheckBox->setChecked(true);
156         accentsCheckBox->setEnabled(false);
157         _generateCache = true;
158         _accents = true;
159     }
160     else {
161         cacheCheckBox->setChecked(false);
162         _generateCache = false;
163
164         if(plugin->settings()->value("strip_accents") == "true") {
165             accentsCheckBox->setChecked(true);
166             _accents = true;
167         }
168         else {
169             accentsCheckBox->setChecked(false);
170             _accents = false;
171         }
172     }
173
174     confirmButton = new QPushButton;
175     mainVerticalLayout->addWidget(confirmButton);
176     if(type == New) {
177         confirmButton->setText(tr("Add"));
178     }
179     else {
180         confirmButton->setText(tr("Save settings"));
181     }
182
183     scrollArea = new QScrollArea;
184     scrollArea->setWidget(widget);
185     scrollArea->setWidgetResizable(true);
186     #ifdef Q_WS_MAEMO_5
187         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
188     #else
189         if(type==New) {
190             infoLabel->setMinimumWidth(550);
191             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
192             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
193             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
194         }
195     #endif
196
197     layout = new QHBoxLayout;
198     layout->addWidget(scrollArea);
199     setLayout(layout);
200
201     #ifndef Q_WS_MAEMO_5
202         setMinimumSize(600,200);
203     #else
204         setMinimumHeight(350);
205     #endif
206
207     scrollArea->setLineWidth(0);
208     scrollArea->setMidLineWidth(0);
209     scrollArea->setFrameStyle(QFrame::NoFrame);
210
211
212 }
213
214
215 void XdxfDialog::setAccents(bool accents) {
216     _accents = accents;
217 }
218
219 void XdxfDialog::setGenerateCache(bool generate) {
220     _generateCache = generate;
221
222     if(generate) {
223         _lastAccents = _accents;
224         accentsCheckBox->setChecked(true);
225     }
226     else
227         accentsCheckBox->setChecked(_lastAccents);
228
229     accentsCheckBox->setEnabled(!generate);
230 }
231
232 void XdxfDialog::selectFile() {
233     QString fileName = QFileDialog::getOpenFileName(this,
234                                      tr("Select dictionary file"),
235                                      _dictionaryFilePath,
236                                      tr("XDXF Files (*.xdxf)"),
237                                      NULL,
238                                      NULL);
239
240     if (!fileName.isEmpty()) {
241         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
242         _dictionaryFilePath = fileName;
243         updateGeometry();
244     }
245 }
246
247 void XdxfDialog::downloadFile() {
248    XdxfPlugin::dictDownloader.download(this);
249 }
250
251 void XdxfDialog::saveSettings() {
252     _settings = new Settings;
253     if(plugin) {
254         foreach(QString key, plugin->settings()->keys())
255             _settings->setValue(key, plugin->settings()->value(key));
256     }
257     else {
258         _settings->setValue("path", _dictionaryFilePath);
259     }
260
261     if(_generateCache)
262         _settings->setValue("generateCache", "true");
263     else
264         _settings->setValue("generateCache", "false");
265
266     if(_accents)
267         _settings->setValue("strip_accents", "true");
268     else
269         _settings->setValue("strip_accents", "false");
270 }
271
272 void XdxfDialog::accept() {
273     if(type == New && _dictionaryFilePath.isEmpty()) {
274         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
275
276         return;
277     }
278
279     saveSettings();
280     QDialog::accept();
281 }
282
283 Settings* XdxfDialog::getSettings() {
284     return _settings;
285 }
286
287 #ifdef Q_WS_MAEMO_5
288     void XdxfDialog::showCacheInfo() {
289         Q_EMIT notify(Notify::Warning, cacheToolTip);
290     }
291
292     void XdxfDialog::showAccentsInfo() {
293         Q_EMIT notify(Notify::Warning, accentsToolTip);
294     }
295 #endif
296