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