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