Added searching for word list in stardict
[mdictionary] / src / plugins / stardict / StarDialog.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 "StarDialog.h"
25 #include <QDebug>
26 #include <QFile>
27
28 StarDialog::StarDialog(StarDictPlugin *plugin,
29                        StarDialogType type,
30                        QWidget *parent) :
31     QDialog(parent) {
32     this->plugin = plugin;
33     this->type = type;
34
35     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
36
37     initializeUI();
38
39
40     connect(accentsCheckBox, SIGNAL(toggled(bool)),
41             this, SLOT(setAccents(bool)));
42
43     #ifdef Q_WS_MAEMO_5
44         connect(accentsInfoToolButton, SIGNAL(clicked()),
45
46     #endif
47
48     if(type == New) {
49         connect(browseButton, SIGNAL(clicked()),
50                 this, SLOT(selectFile()));
51     }
52
53     connect(confirmButton, SIGNAL(clicked()),
54             this, SLOT(accept()));
55
56 }
57
58
59 void StarDialog::initializeUI() {
60     mainVerticalLayout = new QVBoxLayout;
61     widget = new QWidget;
62     widget->setLayout(mainVerticalLayout);
63
64     infoLabel = new QLabel;
65     infoLabel->setWordWrap(true);
66
67     if(type == New) {
68         setWindowTitle(tr("Add new StarDict dictionary"));
69
70         browseLayout = new QHBoxLayout;
71         browseButton = new QPushButton(tr("Browse"));
72         infoLabel->setText(tr("Dictionary file: not selected"));
73
74         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
75         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
76
77         mainVerticalLayout->addLayout(browseLayout);
78     }
79     else {
80         setWindowTitle(tr("StarDict Settings"));
81
82         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
83                        tr("From: ") + plugin->langFrom() + "\n" +
84                        tr("To: ") + plugin->langTo() + "\n" +
85                        tr("Description: ") + plugin->name() + "\n" +
86                        plugin->infoNote());
87         mainVerticalLayout->addWidget(infoLabel);
88     }
89
90     accentsLayout = new QHBoxLayout;
91     accentsCheckBox = new QCheckBox(tr("Strip accents"));
92     accentsCheckBox->setToolTip(accentsToolTip);
93     accentsLayout->addWidget(accentsCheckBox); 
94     #ifdef Q_WS_MAEMO_5
95         accentsInfoToolButton = new QToolButton;
96         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
97         accentsLayout->addWidget(accentsInfoToolButton);
98     #endif
99
100
101     mainVerticalLayout->addLayout(accentsLayout);
102
103
104     //load old setting if exists
105     if(!plugin) {
106         accentsCheckBox->setChecked(true);
107         accentsCheckBox->setEnabled(false);
108         _accents = true;
109         _dictionaryFilePath = "";
110     }
111     else if(plugin && plugin->settings()->value("cached") == "true") {
112         accentsCheckBox->setChecked(true);
113         accentsCheckBox->setEnabled(false);
114         _accents = true;
115     }
116     else {
117         if(plugin->settings()->value("strip_accents") == "true") {
118             accentsCheckBox->setChecked(true);
119             _accents = true;
120         }
121         else {
122             accentsCheckBox->setChecked(false);
123             _accents = false;
124         }
125     }
126
127     confirmButton = new QPushButton;
128     mainVerticalLayout->addWidget(confirmButton);
129     if(type == New) {
130         confirmButton->setText(tr("Add"));
131     }
132     else {
133         confirmButton->setText(tr("Save settings"));
134     }
135
136     scrollArea = new QScrollArea;
137     scrollArea->setWidget(widget);
138     scrollArea->setWidgetResizable(true);
139     #ifdef Q_WS_MAEMO_5
140         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
141     #else
142         if(type==New) {
143             infoLabel->setMinimumWidth(200);
144             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
145             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
146             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
147         }
148     #endif
149
150     layout = new QHBoxLayout;
151     layout->addWidget(scrollArea);
152     setLayout(layout);
153
154     #ifndef Q_WS_MAEMO_5
155         setMinimumSize(400,200);
156     #else
157         setMinimumHeight(350);
158     #endif
159
160     scrollArea->setLineWidth(0);
161     scrollArea->setMidLineWidth(0);
162     scrollArea->setFrameStyle(QFrame::NoFrame);
163
164
165 }
166
167
168 void StarDialog::setAccents(bool accents) {
169     _accents = accents;
170 }
171
172
173 void StarDialog::selectFile() {
174     QString fileName = QFileDialog::getOpenFileName(this,
175                                      tr("Select dictionary file"),
176                                      _dictionaryFilePath,
177                                      tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
178                                      NULL,
179                                      NULL);
180
181     if (!fileName.isEmpty()) {
182         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
183         _dictionaryFilePath = fileName;
184         if (_dictionaryFilePath.endsWith(".tar.bz2")){
185                 _isCompressed = true;
186         }
187         else {
188                 _isCompressed = false;
189         }
190         updateGeometry();
191     }
192 }
193
194 void StarDialog::saveSettings() {
195     _settings = new Settings;
196     if(plugin) {
197         foreach(QString key, plugin->settings()->keys())
198             _settings->setValue(key, plugin->settings()->value(key));
199     }
200     //else {
201         _settings->setValue("path", _dictionaryFilePath);
202         _settings->setValue("ifoFileName", _dictName + ".ifo");
203         _settings->setValue("idxFileName", _dictName + ".idx");
204         _settings->setValue("dictFileName", _dictName + ".dict");
205         if (QFile::exists(_dictName + ".syn") == true) {
206                 _settings->setValue("synFileName", _dictName + ".syn");
207         }
208     //}
209     if(_accents)
210         _settings->setValue("strip_accents", "true");
211     else
212         _settings->setValue("strip_accents", "false");
213 }
214
215 void StarDialog::accept() {
216     if(type == New && _dictionaryFilePath.isEmpty()) {
217         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
218
219         return;
220     }
221
222     if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
223         Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
224         return;
225     }
226
227     saveSettings();
228     QDialog::accept();
229 }
230
231 bool StarDialog::checkFiles() {
232         if (!_isCompressed){
233                _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
234                if (QFile::exists(_dictName + ".idx") == false && QFile::exists(_dictName + ".idx.gz") == false) {
235                        return false;
236                }
237                if (QFile::exists(_dictName + ".dict") == false && QFile::exists(_dictName + ".dict.dz") == false) {
238                        return false;
239                }
240                return true;
241         }
242         else {
243                 //TODO: untar files (?)
244                 return false;
245         }
246 }
247
248 Settings* StarDialog::getSettings() {
249     return _settings;
250 }
251
252 #ifdef Q_WS_MAEMO_5
253     void StarDialog::showAccentsInfo() {
254         Q_EMIT notify(Notify::Warning, accentsToolTip);
255     }
256 #endif
257