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