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