6f18d02af319e8eff03faabdfb05b31994b76cc2
[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("Book name: ") + plugin->settings()->value("bookname") 
84                         + "\n" +
85                 tr("Version: ") + plugin->settings()->value("version") + "\n" +
86                 tr("Word count: ") + plugin->settings()->value("wordcount") 
87                         + "\n" +
88                 tr("Author: ") + plugin->settings()->value("author") + "\n" +
89                 tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
90                 tr("Website: ") + plugin->settings()->value("website") + "\n" +
91                 tr("Description: ") + plugin->settings()->value("description") 
92                         + "\n" +
93                tr("Date: ") + plugin->settings()->value("date"));
94         mainVerticalLayout->addWidget(infoLabel);
95     }
96
97     accentsLayout = new QHBoxLayout;
98     accentsCheckBox = new QCheckBox(tr("Strip accents"));
99     accentsCheckBox->setToolTip(accentsToolTip);
100     accentsLayout->addWidget(accentsCheckBox); 
101     #ifdef Q_WS_MAEMO_5
102         accentsInfoToolButton = new QToolButton;
103         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
104         accentsLayout->addWidget(accentsInfoToolButton);
105     #endif
106
107
108     mainVerticalLayout->addLayout(accentsLayout);
109
110
111     //load old setting if exists
112     if(!plugin) {
113         accentsCheckBox->setChecked(true);
114         accentsCheckBox->setEnabled(false);
115         _accents = true;
116         _dictionaryFilePath = "";
117     }
118     else if(plugin && plugin->settings()->value("cached") == "true") {
119         accentsCheckBox->setChecked(true);
120         accentsCheckBox->setEnabled(false);
121         _accents = true;
122     }
123     else {
124         if(plugin->settings()->value("strip_accents") == "true") {
125             accentsCheckBox->setChecked(true);
126             _accents = true;
127         }
128         else {
129             accentsCheckBox->setChecked(false);
130             _accents = false;
131         }
132     }
133
134     confirmButton = new QPushButton;
135     mainVerticalLayout->addWidget(confirmButton);
136     if(type == New) {
137         confirmButton->setText(tr("Add"));
138     }
139     else {
140         confirmButton->setText(tr("Save settings"));
141     }
142
143     scrollArea = new QScrollArea;
144     scrollArea->setWidget(widget);
145     scrollArea->setWidgetResizable(true);
146     #ifdef Q_WS_MAEMO_5
147         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
148     #else
149         if(type==New) {
150             infoLabel->setMinimumWidth(200);
151             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
152             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
153             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
154         }
155     #endif
156
157     layout = new QHBoxLayout;
158     layout->addWidget(scrollArea);
159     setLayout(layout);
160
161     #ifndef Q_WS_MAEMO_5
162         setMinimumSize(400,200);
163     #else
164         setMinimumHeight(350);
165     #endif
166
167     scrollArea->setLineWidth(0);
168     scrollArea->setMidLineWidth(0);
169     scrollArea->setFrameStyle(QFrame::NoFrame);
170
171
172 }
173
174
175 void StarDialog::setAccents(bool accents) {
176     _accents = accents;
177 }
178
179
180 void StarDialog::selectFile() {
181     QString fileName = QFileDialog::getOpenFileName(this,
182                                      tr("Select dictionary file"),
183                                      _dictionaryFilePath,
184                                      tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
185                                      NULL,
186                                      NULL);
187
188     if (!fileName.isEmpty()) {
189         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
190         _dictionaryFilePath = fileName;
191         if (_dictionaryFilePath.endsWith(".tar.bz2")){
192                 _isCompressed = true;
193         }
194         else {
195                 _isCompressed = false;
196         }
197         updateGeometry();
198     }
199 }
200
201 void StarDialog::saveSettings() {
202     _settings = new Settings;
203     if(plugin) {
204         foreach(QString key, plugin->settings()->keys())
205             _settings->setValue(key, plugin->settings()->value(key));
206     }
207     //else {
208         _settings->setValue("path", _dictionaryFilePath);
209         _settings->setValue("ifoFileName", _dictName + ".ifo");
210         _settings->setValue("idxFileName", _dictName + ".idx");
211         if (QFile::exists(_dictName + ".dict.dz") == true) {
212                 _settings->setValue("dictFileName", _dictName + ".dict.dz");
213         }
214         else {
215                 _settings->setValue("dictFileName", _dictName + ".dict");
216         }
217
218         if (QFile::exists(_dictName + ".syn") == true) {
219                 _settings->setValue("synFileName", _dictName + ".syn");
220         }
221     //}
222     if(_accents)
223         _settings->setValue("strip_accents", "true");
224     else
225         _settings->setValue("strip_accents", "false");
226 }
227
228 void StarDialog::accept() {
229     if(type == New && _dictionaryFilePath.isEmpty()) {
230         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
231
232         return;
233     }
234
235     if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
236         Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
237         return;
238     }
239
240     saveSettings();
241     QDialog::accept();
242 }
243
244 bool StarDialog::checkFiles() {
245         if (!_isCompressed) {
246
247             if (_dictionaryFilePath.right(2) == "dz") {
248                 _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
249                 _dictName = _dictName.left(_dictName.lastIndexOf("."));
250             }
251             else {
252                 _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
253             }
254
255             if (QFile::exists(_dictName + ".idx") == false && QFile::exists(_dictName + ".idx.gz") == false) {
256                 return false;
257             }
258             if (QFile::exists(_dictName + ".dict") == false && QFile::exists(_dictName + ".dict.dz") == false) {
259                 return false;
260             }
261             return true;
262         }
263         else {
264             //TODO: untar files (?)
265             return false;
266         }
267 }
268
269 Settings* StarDialog::getSettings() {
270     return _settings;
271 }
272
273 #ifdef Q_WS_MAEMO_5
274     void StarDialog::showAccentsInfo() {
275         Q_EMIT notify(Notify::Warning, accentsToolTip);
276     }
277 #endif
278