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