79018fdefc99a78f800b14860a3cdd4a8974833c
[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     \author Mateusz Półrola <mateusz.polrola@gmail.com>
24 */
25
26 #include "StarDialog.h"
27 #include <QDebug>
28 #include <QFile>
29
30 StarDialog::StarDialog(StarDictPlugin *plugin, StarDialogType type,
31                        QWidget *parent) : QDialog(parent) {
32    this->plugin = plugin;
33    this->type = type;
34    initializeUI();
35    if(type == New)
36        connect(browseButton, SIGNAL(clicked()),this, SLOT(selectFile()));
37    connect(confirmButton, SIGNAL(clicked()),this, SLOT(accept()));
38 }
39
40
41 void StarDialog::initializeUI() {
42     mainVerticalLayout = new QVBoxLayout;
43     widget = new QWidget;
44     widget->setLayout(mainVerticalLayout);
45     infoLabel = new QLabel;
46     infoLabel->setWordWrap(true);
47     QVBoxLayout* buttonLayout = new QVBoxLayout;
48
49     if(type == New) {
50         browseLayout = new QHBoxLayout;
51         browseButton = new QPushButton(tr("Browse"));
52         browseButton->setMaximumWidth(150);
53         infoLabel->setText(tr("Dictionary file: not selected"));
54         setWindowTitle(tr("Add new StarDict dictionary"));
55         infoLabel->setText(tr("Dictionary file: not selected"));
56         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
57         browseLayout->addLayout(buttonLayout);
58         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
59         mainVerticalLayout->addLayout(browseLayout);
60     }
61     else {
62         setWindowTitle(tr("StarDict Settings"));
63         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
64                 tr("Book name: ") + plugin->settings()->value("bookname") 
65                         + "\n" +
66                 tr("Version: ") + plugin->settings()->value("version") + "\n" +
67                 tr("Word count: ") + plugin->settings()->value("wordcount") 
68                         + "\n" +
69                 tr("Author: ") + plugin->settings()->value("author") + "\n" +
70                 tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
71                 tr("Website: ") + plugin->settings()->value("website") + "\n" +
72                 tr("Description: ") + plugin->settings()->value("description") 
73                         + "\n" +
74                tr("Date: ") + plugin->settings()->value("date"));
75         mainVerticalLayout->addWidget(infoLabel);
76     }
77
78     if(!plugin)
79         _dictionaryFilePath = "";
80
81     confirmButton = new QPushButton;
82     mainVerticalLayout->addWidget(confirmButton);
83     if(type == New)
84         confirmButton->setText(tr("Add"));
85     else
86         confirmButton->setText(tr("Save settings"));
87
88     scrollArea = new QScrollArea;
89     scrollArea->setWidget(widget);
90     scrollArea->setWidgetResizable(true);
91     #ifdef Q_WS_MAEMO_5
92         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
93     #else
94         if(type==New) {
95             infoLabel->setMinimumWidth(200);
96 //            setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
97 //            setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
98             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
99         }
100     #endif
101
102     layout = new QHBoxLayout;
103     layout->addWidget(scrollArea);
104     setLayout(layout);
105
106     #ifndef Q_WS_MAEMO_5
107         setMinimumSize(400,200);
108     #else
109         setMinimumHeight(350);
110     #endif
111
112     scrollArea->setLineWidth(0);
113     scrollArea->setMidLineWidth(0);
114     scrollArea->setFrameStyle(QFrame::NoFrame);
115 }
116
117
118 void StarDialog::selectFile() {
119     QString fileName = QFileDialog::getOpenFileName(this,
120                      tr("Select dictionary file"),
121                      _dictionaryFilePath,
122                      tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
123                      NULL,
124                      NULL);
125     if (!fileName.isEmpty()) {
126         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
127         _dictionaryFilePath = fileName;
128         if (_dictionaryFilePath.endsWith(".tar.bz2"))
129             _isCompressed = true;
130         else
131             _isCompressed = false;
132         updateGeometry();
133     }
134 }
135
136
137 void StarDialog::saveSettings() {
138     _settings = new Settings;
139
140     if(plugin)
141         foreach(QString key, plugin->settings()->keys())
142             _settings->setValue(key, plugin->settings()->value(key));
143
144     if(_settings->value("path")=="")
145         _settings->setValue("path", _dictionaryFilePath);
146
147     if(_settings->value("ifoFileName")=="")
148         _settings->setValue("ifoFileName", _dictName + ".ifo");
149
150     if(_settings->value("idxFileName")=="")
151         _settings->setValue("idxFileName", _dictName + ".idx");
152
153     if(_settings->value("dictFileName")==""){
154         if (QFile::exists(_dictName + ".dict.dz") == true)
155             _settings->setValue("dictFileName", _dictName + ".dict.dz");
156         else
157             _settings->setValue("dictFileName", _dictName + ".dict");
158     }
159
160     if(_settings->value("synFileName")=="")
161         if (QFile::exists(_dictName + ".syn") == true)
162             _settings->setValue("synFileName", _dictName + ".syn");
163 }
164
165
166 void StarDialog::accept() {
167     if(type == New && _dictionaryFilePath.isEmpty()) {
168         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
169         return;
170     }
171
172     if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
173         Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
174         return;
175     }
176
177     saveSettings();
178     QDialog::accept();
179 }
180
181
182 bool StarDialog::checkFiles() {
183     if (!_isCompressed) {
184         if (_dictionaryFilePath.right(2) == "dz") {
185             _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
186             _dictName = _dictName.left(_dictName.lastIndexOf("."));
187         }
188         else{
189             _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
190         }
191
192         if (QFile::exists(_dictName + ".idx") == false
193                 && QFile::exists(_dictName + ".idx.gz") == false) {
194             return false;
195         }
196         if (QFile::exists(_dictName + ".dict") == false
197                 && QFile::exists(_dictName + ".dict.dz") == false) {
198             return false;
199         }
200         return true;
201     }
202     else
203         return false;
204 }
205
206
207 Settings* StarDialog::getSettings() {
208     return _settings;
209 }
210