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