code clean
[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     \file StarDialog.cpp
23     \brief Implementation of stardict plugin's dialogs.
24
25     \author Mateusz Półrola <mateusz.polrola@gmail.com>
26 */
27
28 #include "StarDialog.h"
29 #include <QDebug>
30 #include <QFile>
31
32 StarDialog::StarDialog(StarDictPlugin *plugin, StarDialogType type,
33                        QWidget *parent) : QDialog(parent) {                                        
34    this->plugin = plugin;
35    this->type = type;
36
37 #ifndef Q_WS_MAEMO_5
38     view= new QDeclarativeView();
39     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/StarDictDialog.qml"));
40     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
41     view->setAlignment(Qt::AlignCenter);
42     view->show();
43
44     mainLayout = new QVBoxLayout;
45     mainLayout->addWidget(view);
46     setLayout(mainLayout);
47     view->setWindowTitle(tr("StarDict Settings"));
48
49     QGraphicsObject *rootObject = view->rootObject();
50
51     connect(this, SIGNAL(setInfo(QVariant)),
52            rootObject, SLOT(setInfo(QVariant)));
53     connect(this,SIGNAL(setButtonText(QVariant)),
54             rootObject, SLOT(setButtonText(QVariant)));
55     connect(this,SIGNAL(setNew(QVariant)),
56             rootObject, SLOT(setNew(QVariant)));
57     connect(this,SIGNAL(setPath(QVariant)),
58             rootObject, SLOT(setPath(QVariant)));
59
60     connect(rootObject, SIGNAL(saveButtonClicked()),
61             this, SLOT(accept()));
62     connect(rootObject, SIGNAL(browseButtonClicked()),
63             this, SLOT(selectFile()));
64     connect(rootObject, SIGNAL(heightChange(int)),
65             this, SLOT(heightChange(int)));
66
67 #else
68    if(type == New)
69        connect(browseButton, SIGNAL(clicked()),this, SLOT(selectFile()));
70    connect(confirmButton, SIGNAL(clicked()),this, SLOT(accept()));
71 #endif
72     initializeUI();
73 }
74
75 void StarDialog::heightChange(int height){
76     if(height>sizeHint().height())
77         resize(sizeHint().width(),height);
78     lastHeight=height;
79 }
80
81 void StarDialog::resizeEvent(QResizeEvent *e){
82     QDialog::resizeEvent(e);
83     if(lastHeight>sizeHint().height()){
84         resize(sizeHint().width(),lastHeight);
85     }
86 }
87
88 void StarDialog::initializeUI() {
89 #ifndef Q_WS_MAEMO_5
90     if(type != New){
91         emit setNew(false);
92         QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" +
93             tr("Book name: ") + plugin->settings()->value("bookname") + "\n" +
94             tr("Version: ") + plugin->settings()->value("version") + "\n" +
95             tr("Word count: ") + plugin->settings()->value("wordcount") + "\n" +
96             tr("Author: ") + plugin->settings()->value("author") + "\n" +
97             tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
98             tr("Website: ") + plugin->settings()->value("website") + "\n" +
99             tr("Description: ") + plugin->settings()->value("description")+"\n"+
100             tr("Date: ") + plugin->settings()->value("date");
101         emit setInfo(info);
102         emit setButtonText("Save settings");
103     }
104     else{
105         emit setNew(true);
106         emit setButtonText("Add");
107     }
108
109     if(!plugin)
110         _dictionaryFilePath = "";
111
112 #else
113     mainVerticalLayout = new QVBoxLayout;
114     widget = new QWidget;
115     widget->setLayout(mainVerticalLayout);
116     infoLabel = new QLabel;
117     infoLabel->setWordWrap(true);
118     QVBoxLayout* buttonLayout = new QVBoxLayout;
119
120     if(type == New) {
121         browseLayout = new QHBoxLayout;
122         browseButton = new QPushButton(tr("Browse"));
123         browseButton->setMaximumWidth(150);
124         infoLabel->setText(tr("Dictionary file: not selected"));
125         setWindowTitle(tr("Add new StarDict dictionary"));
126         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
127         browseLayout->addLayout(buttonLayout);
128         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
129         mainVerticalLayout->addLayout(browseLayout);
130     }
131     else {
132         setWindowTitle(tr("StarDict Settings"));
133         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
134                 tr("Book name: ") + plugin->settings()->value("bookname") 
135                         + "\n" +
136                 tr("Version: ") + plugin->settings()->value("version") + "\n" +
137                 tr("Word count: ") + plugin->settings()->value("wordcount") 
138                         + "\n" +
139                 tr("Author: ") + plugin->settings()->value("author") + "\n" +
140                 tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
141                 tr("Website: ") + plugin->settings()->value("website") + "\n" +
142                 tr("Description: ") + plugin->settings()->value("description") 
143                         + "\n" +
144                tr("Date: ") + plugin->settings()->value("date"));
145         mainVerticalLayout->addWidget(infoLabel);
146     }
147
148     if(!plugin)
149         _dictionaryFilePath = "";
150
151     confirmButton = new QPushButton;
152     mainVerticalLayout->addWidget(confirmButton);
153     if(type == New)
154         confirmButton->setText(tr("Add"));
155     else
156         confirmButton->setText(tr("Save settings"));
157
158     scrollArea = new QScrollArea;
159     scrollArea->setWidget(widget);
160     scrollArea->setWidgetResizable(true);
161     #ifdef Q_WS_MAEMO_5
162         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
163     #else
164         if(type==New) {
165             infoLabel->setMinimumWidth(200);
166 //            setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
167 //            setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
168             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
169         }
170     #endif
171
172     layout = new QHBoxLayout;
173     layout->addWidget(scrollArea);
174     setLayout(layout);
175
176     #ifndef Q_WS_MAEMO_5
177         setMinimumSize(400,200);
178     #else
179         setMinimumHeight(350);
180     #endif
181
182     scrollArea->setLineWidth(0);
183     scrollArea->setMidLineWidth(0);
184     scrollArea->setFrameStyle(QFrame::NoFrame);
185 #endif
186 }
187
188
189 void StarDialog::selectFile() {
190     QString fileName = QFileDialog::getOpenFileName(this,
191                      tr("Select dictionary file"),
192                      _dictionaryFilePath,
193                      tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
194                      NULL,
195                      NULL);
196     if (!fileName.isEmpty()) {
197 #ifndef Q_WS_MAEMO_5
198         emit setPath(tr("Dictionary file: %1").arg(fileName));
199 #else
200         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
201 #endif
202         _dictionaryFilePath = fileName;
203         if (_dictionaryFilePath.endsWith(".tar.bz2"))
204             _isCompressed = true;
205         else
206             _isCompressed = false;
207 #ifndef Q_WS_MAEMO_5
208         resize(view->sizeHint());
209
210 #else
211         updateGeometry();
212 #endif
213     }
214 }
215
216
217 void StarDialog::saveSettings() {
218
219     _settings = new Settings;
220     if(plugin)
221         foreach(QString key, plugin->settings()->keys())
222             _settings->setValue(key, plugin->settings()->value(key));
223
224     if(_settings->value("path")=="")
225         _settings->setValue("path", _dictionaryFilePath);
226
227     if(_settings->value("ifoFileName")=="")
228         _settings->setValue("ifoFileName", _dictName + ".ifo");
229
230     if(_settings->value("idxFileName")=="")
231         _settings->setValue("idxFileName", _dictName + ".idx");
232
233     if(_settings->value("dictFileName")==""){
234         if (QFile::exists(_dictName + ".dict.dz") == true)
235             _settings->setValue("dictFileName", _dictName + ".dict.dz");
236         else
237             _settings->setValue("dictFileName", _dictName + ".dict");
238     }
239     if(_settings->value("synFileName")=="")
240         if (QFile::exists(_dictName + ".syn") == true)
241             _settings->setValue("synFileName", _dictName + ".syn");
242 }
243
244
245 void StarDialog::accept() {
246     if(type == New && _dictionaryFilePath.isEmpty()) {
247         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
248         return;
249     }
250
251     if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
252         Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
253         return;
254     }
255
256     saveSettings();
257     QDialog::accept();
258 }
259
260
261 bool StarDialog::checkFiles() {
262     if (!_isCompressed) {
263         if (_dictionaryFilePath.right(2) == "dz") {
264             _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
265             _dictName = _dictName.left(_dictName.lastIndexOf("."));
266         }
267         else{
268             _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
269         }
270
271         if (QFile::exists(_dictName + ".idx") == false
272                 && QFile::exists(_dictName + ".idx.gz") == false) {
273             return false;
274         }
275         if (QFile::exists(_dictName + ".dict") == false
276                 && QFile::exists(_dictName + ".dict.dz") == false) {
277             return false;
278         }
279         return true;
280     }
281     else
282         return false;
283 }
284
285
286 Settings* StarDialog::getSettings() {
287     return _settings;
288 }
289