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