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