code clean
[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      \brief Implementation of stardict plugin's main class.
24
25      \author Jakub Jaszczynski
26 */
27
28 #include "StarDictPlugin.h"
29
30 /**/
31 uint qHash(const TranslationStarDict &key) {
32    return qHash(key.key());
33 }
34
35
36 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
37                     _langFrom(""), _langTo(""),_name(""), _infoNote("") {
38     _settings = new Settings();
39     _dictDialog = new StarDictDialog(this, this);
40
41     connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
42             this, SIGNAL(notify(Notify::NotifyType,QString)));
43
44     _settings->setValue("type","stardict");
45     _iconPath = "/usr/share/mdictionary/stardict.png";
46     _icon = QIcon(_iconPath);
47     stopped = false;
48
49     _settings->setValue("strip_accents","true");
50
51     initAccents();
52
53 }
54
55
56 void StarDictPlugin::retranslate() {
57     QString locale = QLocale::system().name();
58     QTranslator *translator = new QTranslator(this);
59
60     if(!translator->load(":/stardict/translations/" + locale)) {
61         translator->load(":/stardict/translations/en_US");
62     }
63     QCoreApplication::installTranslator(translator);
64 }
65
66
67 StarDictPlugin::~StarDictPlugin() {
68     delete _settings;
69     delete _dictDialog;
70 }
71
72
73 QString StarDictPlugin::langFrom() const {
74     return _langFrom;
75 }
76
77
78 QString StarDictPlugin::langTo() const {
79     return  _langTo;
80 }
81
82
83 QString StarDictPlugin::name() const {
84     return  _name;
85 }
86
87
88 QString StarDictPlugin::type() const {
89     return QString("stardict");
90 }
91
92
93 QString StarDictPlugin::infoNote() const {
94     return _name;
95 }
96
97
98 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
99     QList<TranslationStarDict> translations;
100     bool is32b = false;
101     bool err = 0;
102     int wordcount = settings()->value("wordcount").toInt(&err);
103     int counter = 0;
104     int counterLimit = 0;
105     QString idxPath = settings()->value("idxFileName");
106     StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
107     QString fkey;
108     qint64 offset = 0,
109     len = 0;
110
111     if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
112         word.indexOf("_")==-1 && word.indexOf("%")==-1)
113         word+="*";
114     QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
115
116     if((settings()->value("idxoffsetbits") == "32") ||
117                          (settings()->value("idxoffsetbits") == ""))
118         is32b = true;
119
120     if(!err)
121         return QList<Translation*>();
122
123     stopped=false;
124     while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
125         counter++;
126         fkey = reader->readKeyword();
127         if(is32b)
128             offset = reader->readInt32BigEndian();
129         else
130             offset = reader->readInt64BigEndian();
131         len = reader->readInt32BigEndian();
132
133         if(keyword.exactMatch(fkey) ) {
134             TranslationStarDict tran(fkey, infoNote(), this);
135             int id = translations.indexOf(tran);
136             if(id == -1) {
137                 tran.add(offset, len);
138                 translations.push_front(tran);
139             } else
140                 translations[id].add(offset, len);
141             counterLimit++;
142         }
143     }
144     QList<Translation*> ret;
145     QListIterator<TranslationStarDict> it(translations);
146     while(it.hasNext())
147         ret.push_back(new TranslationStarDict(it.next()));
148     return ret;
149 }
150
151
152 QByteArray StarDictPlugin::read(QByteArray::iterator it,
153         QByteArray::iterator end, int bytes) {
154     QByteArray ret;
155
156     if(bytes == 0 && it != end){
157         while(*it != '\0' && it != end)
158             ret.append(*it++);
159     }
160     else
161         for(int i = 0; i < bytes && it != end; i++)
162             ret.append(*it++);
163     return ret;
164 }
165
166
167 QString StarDictPlugin::interpret(QByteArray::iterator it,
168         QByteArray::iterator end, QChar mode,QString key, bool) {
169     QString result;
170     if(mode == 'm'
171             || mode == 'l'
172             || mode == 'g'
173             || mode == 't'
174             || mode == 'y'
175             || mode == 'k'
176             || mode == 'w'
177             || mode == 'h'
178             || mode == 'r'){
179         result += "<key>" + key + "</key>";
180         result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
181     }
182
183     if(mode ==  'g'){
184         /* delete "small" tag  from g translation*/
185         int indexOfSmall=result.indexOf("<small>");
186         while(indexOfSmall!=-1){
187             int indexOfEndSmall= result.indexOf("</small>");
188             if(indexOfEndSmall!=-1)
189                 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
190             indexOfSmall=result.indexOf("<small>");
191         }
192     }
193     if(mode == 'x'){
194         result += QString::fromUtf8(read(it++, end));
195         result.replace("</k>","</key><t>");
196         result.replace("<k>","</t><key>");
197         int pos=result.indexOf("</t>");
198         if(pos!=-1)
199             result.remove(pos,4);
200         if(result.contains("<t>"))
201             result+="</t>";
202     }
203
204 // W & P Dont know whether mDictionary would ever handle binary stardict format
205 // to be honest dont see any kind of adventages (can't find any binary dict)
206  /*
207     }
208     else if(mode == 'W') {
209         result += "<key>" + key + "</key>";
210         if(!last) {
211             QByteArray tmp ;
212             tmp.append(*(it++));
213             tmp.append(*(it++));
214             tmp.append(*(it++));
215             tmp.append(*(it));
216             result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
217         } else
218             result += read(it++, end);
219     } else if(mode == 'P') {
220         result += "<key>" + key + "</key>";
221         if(!last) {
222             QByteArray tmp ;
223             tmp.append(*(it++));
224             tmp.append(*(it++));
225             tmp.append(*(it++));
226             tmp.append(*(it));
227             result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
228         } else
229             result += read(it++, end);
230     } */
231     return result;
232 }
233
234
235 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
236     QString result;
237     if(mode == "") {
238         for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
239             char tmp = *(++it);
240             result += interpret(--it, raw.end(), tmp, key);
241         }
242     } else {
243         QByteArray::iterator it = raw.begin();
244         foreach(QChar tmp, mode) {
245             result += interpret(it, raw.end(), tmp, key);
246         }
247     }
248     return result;
249 }
250
251
252 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
253     QString dictPath = settings()->value("dictFileName");
254     StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
255     QByteArray raw = reader->readString(offset, len);
256     QString result= format(raw, settings()->value("sametypesequence"),key);
257
258     delete reader;
259     return result;
260 }
261
262
263 void StarDictPlugin::stop() {
264     stopped=true;
265 }
266
267
268 DictDialog* StarDictPlugin::dictDialog() {
269      return _dictDialog;
270 }
271
272
273 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
274     StarDictPlugin *plugin = new StarDictPlugin();
275
276     connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
277             this, SIGNAL(notify(Notify::NotifyType,QString)));
278
279     ((StarDictDialog*)plugin->dictDialog())->
280             setLastDialogParent(_dictDialog->lastDialogParent());
281
282     if(settings && plugin->setSettings(settings)) {
283         disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
284                     this, SIGNAL(notify(Notify::NotifyType,QString)));
285         plugin->getDictionaryInfo();
286         return plugin;
287     }
288     else {
289         disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
290                     this, SIGNAL(notify(Notify::NotifyType,QString)));
291         delete plugin;
292         return 0;
293     }
294 }
295
296
297 bool StarDictPlugin::isAvailable() const {
298     return true;
299 }
300
301
302 Settings* StarDictPlugin::settings() {
303     return _settings;
304 }
305
306
307 bool StarDictPlugin::setSettings(const Settings *sett) {
308     if(sett) {
309         foreach(QString key, sett->keys())
310             _settings->setValue(key, sett->value(key));
311
312     } else
313         return false;
314     Q_EMIT settingsChanged();
315     return true;
316 }
317
318
319 bool StarDictPlugin::getDictionaryInfo() {
320     QFile file(settings()->value("ifoFileName"));
321     if(!QFile::exists(_settings->value("ifoFileName"))
322                 || !file.open(QFile::ReadOnly | QFile::Text)) {
323         Q_EMIT notify(Notify::Warning,
324                QString(tr("StarDict dictionary cannot be read from file")));
325         qDebug()<<"Error: could not open the file";
326         return false;
327     }
328     QTextStream in(&file);
329     while (!in.atEnd()) {
330         QString line = in.readLine();
331         QStringList list = line.split("=");
332         if(list.size() == 2) {
333             settings()->setValue(list.at(0),list.at(1));
334         }
335     }
336     _name = settings()->value("bookname");
337     return true;
338 }
339
340
341 QIcon* StarDictPlugin::icon() {
342     return &_icon;
343 }
344
345 QString StarDictPlugin::iconPath(){
346     return _iconPath;
347 }
348
349 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)