Work on the parsing of .ifo file
[mdictionary] / src / plugins / stardict / StarDictPlugin.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 stardictplugin.cpp
23 */
24
25 #include "StarDictPlugin.h"
26 #include <QDebug>
27 #include "../../include/Notify.h"
28 #include <QTranslator>
29 #include <QCoreApplication>
30 #include <QThread>
31
32 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
33                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
34     _settings = new Settings();
35     _dictDialog = new StarDictDialog(this, this);
36
37     connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
38             this, SIGNAL(notify(Notify::NotifyType,QString)));
39
40
41     _settings->setValue("type","stardict");
42     _icon = QIcon("/usr/share/mdictionary/xdxf.png");
43     _wordsCount = -1;
44     stopped = false;
45
46     initAccents();
47 }
48
49 void StarDictPlugin::retranslate() {
50     QString locale = QLocale::system().name();
51
52     QTranslator *translator = new QTranslator(this);
53
54     if(!translator->load(":/xdxf/translations/" + locale)) {
55         translator->load(":/xdxf/translations/en_US");
56     }
57     QCoreApplication::installTranslator(translator);
58 }
59
60
61 StarDictPlugin::~StarDictPlugin() {
62     delete _settings;
63     delete _dictDialog;
64 }
65
66
67 QString StarDictPlugin::langFrom() const {
68     return _langFrom;
69 }
70
71
72 QString StarDictPlugin::langTo() const {
73     return  _langTo;
74 }
75
76
77 QString StarDictPlugin::name() const {
78     return  _name;
79 }
80
81
82 QString StarDictPlugin::type() const {
83     return QString("stardict");
84 }
85
86
87 QString StarDictPlugin::infoNote() const {
88     return _infoNote;
89 }
90
91
92 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
93     QSet<Translation*> translations;
94     return translations.toList();
95 }
96
97
98
99 QString StarDictPlugin::search(QString key) {
100     return "";
101 }
102
103 StarDictSettings* StarDictPlugin::parseIfoFile()
104 {
105     _ifoFileSettings = new StarDictSettings();
106
107     QFile file(settings()->value("path"));
108     if (!file.open(QIODevice::ReadOnly))
109     {
110         return 0;
111     }
112     QTextStream in(&file);
113     while (!in.atEnd()) {
114         QString line = in.readLine();
115         QStringList list = line.split("=");
116         if(line.contains("version"))
117         {
118             _ifoFileSettings->setVersion(list.at(1));
119         }
120         else if(line.contains("bookname"))
121         {
122             _ifoFileSettings->setBookname(list.at(1));
123         }
124         else if(line.contains("wordcount"))
125         {
126             _ifoFileSettings->setWordcount(list.at(1).toInt());
127         }
128         else if(line.contains("idxfilesize"))
129         {
130             _ifoFileSettings->setIdxfilesize(list.at(1).toInt());
131         }
132         else if(line.contains("idxoffsetbits"))
133         {
134             _ifoFileSettings->setidxoffsetbits(list.at(1).toInt());
135         }
136         else if(line.contains("author"))
137         {
138             _ifoFileSettings->setAuthor(list.at(1));
139         }
140         else if(line.contains("email"))
141         {
142             _ifoFileSettings->setEmail(list.at(1));
143         }
144         else if(line.contains("website"))
145         {
146             _ifoFileSettings->setWebsite(list.at(1));
147         }
148         else if(line.contains("description"))
149         {
150             _ifoFileSettings->setDescription(list.at(1));
151         }
152         else if(line.contains("date"))
153         {
154             _ifoFileSettings->setDate(list.at(1));
155         }
156         else if(line.contains("sametypesequence"))
157         {
158             _ifoFileSettings->setSametypesequence(list.at(1));
159         }
160     }
161     return _ifoFileSettings;
162
163 }
164
165
166 void StarDictPlugin::stop() {
167     stopped=true;
168 }
169
170
171 DictDialog* StarDictPlugin::dictDialog() {
172      return _dictDialog;
173 }
174
175
176 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
177     StarDictPlugin *plugin = new StarDictPlugin();
178
179     connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
180             this, SIGNAL(notify(Notify::NotifyType,QString)));
181
182     ((StarDictDialog*)plugin->dictDialog())->setLastDialogParent(_dictDialog->lastDialogParent());
183
184
185
186     if(settings && plugin->setSettings(settings)) {
187
188         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
189                 this, SIGNAL(notify(Notify::NotifyType,QString)));
190         return plugin;
191     }
192     else {
193         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
194                 this, SIGNAL(notify(Notify::NotifyType,QString)));
195         delete plugin;
196         return 0;
197     }
198 }
199
200
201 bool StarDictPlugin::isAvailable() const {
202     return true;
203 }
204
205
206 Settings* StarDictPlugin::settings() {
207     return _settings;
208 }
209
210
211 bool StarDictPlugin::isCached() {
212     return false;
213 }
214
215
216 bool StarDictPlugin::setSettings(const Settings *settings) {
217     if(settings) {
218
219     } else
220         return false;
221     Q_EMIT settingsChanged();
222     return true;
223 }
224
225
226 bool StarDictPlugin::getDictionaryInfo() {
227     QFile dictionaryFile(_settings->value("path"));
228     if(!QFile::exists(_settings->value("path"))
229                 || !dictionaryFile.open(QFile::ReadOnly | QFile::Text)) {
230        Q_EMIT notify(Notify::Warning,
231                QString(tr("StarDict dictionary cannot be read from file")));
232         qDebug()<<"Error: could not open the file";
233         return false;
234     }
235
236     return false;
237 }
238
239
240 QIcon* StarDictPlugin::icon() {
241     return &_icon;
242 }
243
244
245 int StarDictPlugin::countWords() {
246     return 0;
247 }
248
249
250
251 void StarDictPlugin::clean() {
252
253 }
254
255
256 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)