14691e77aaa57ac5a80675c878840a53de7bad52
[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     if(bytes == 0 && it != end)
148         while(*it != '\0')
149             ret.append(*it++);
150     else
151         for(int i = 0; i < bytes && it != end; i++)
152             ret.append(*it++);
153     return ret;
154 }
155
156
157 QString StarDictPlugin::interpret(QByteArray::iterator it,
158         QByteArray::iterator end, QChar mode,QString key, bool last) {
159     QString result;
160     if(mode == 'm')
161         result += QString::fromUtf8(read(it++, end));
162     else if(mode == 'l')
163         result += QString::fromUtf8(read(it++, end));
164     else if(mode ==  'g'){
165         result += "<key>" + key + "</key>";
166         result += QString::fromUtf8(read(it++, end));
167     }
168     else if(mode == 't')
169         result += QString::fromUtf8(read(it++, end));
170     else if(mode == 'x'){
171         result += QString::fromUtf8(read(it++, end));
172         result.replace("</k>","</key><t>");
173         result.replace("<k>","</t><key>");
174         int pos=result.indexOf("</t>");
175         if(pos!=-1)
176             result.remove(pos,4);
177         result+="</t>";
178     }
179     else if(mode == 'y')
180         result += QString::fromUtf8(read(it++, end));
181     else if(mode == 'k')
182         result += QString::fromUtf8(read(it++, end));
183     else if(mode == 'w')
184         result += QString::fromUtf8(read(it++, end));
185     else if(mode == 'h')
186         result += QString::fromUtf8(read(it++, end));
187     else if(mode == 'r')
188         result += QString::fromUtf8(read(it++, end));
189
190     else if(mode == 'W') {
191         if(!last) {
192             QByteArray tmp ;
193             tmp.append(*(it++));
194             tmp.append(*(it++));
195             tmp.append(*(it++));
196             tmp.append(*(it));
197             result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
198         } else
199             result += read(it++, end);
200     } else if(mode == 'P') {
201         if(!last) {
202             QByteArray tmp ;
203             tmp.append(*(it++));
204             tmp.append(*(it++));
205             tmp.append(*(it++));
206             tmp.append(*(it));
207             result += read(it++, end, (qint32)qFromBigEndian(tmp.data()));
208         } else
209             result += read(it++, end);
210     }
211     return result;
212 }
213
214
215 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
216     QString result;
217     if(mode == "") {
218         for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
219             char tmp = *(++it);
220             result += interpret(--it, raw.end(), tmp, key);
221         }
222     } else {
223         QByteArray::iterator it = raw.begin();
224         foreach(QChar tmp, mode) {
225             result += interpret(it, raw.end(), tmp, key);
226         }
227     }
228     return result;
229 }
230
231
232
233 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
234     if(!dictReader)
235         return "";
236     QByteArray raw = dictReader->readString(offset, len);
237     qDebug()<<"mod"<<settings()->value("sametypesequence");
238     return format(raw, settings()->value("sametypesequence"),key);
239 }
240
241
242
243 void StarDictPlugin::stop() {
244     stopped=true;
245 }
246
247
248 DictDialog* StarDictPlugin::dictDialog() {
249      return _dictDialog;
250 }
251
252
253 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
254     StarDictPlugin *plugin = new StarDictPlugin();
255
256     connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
257             this, SIGNAL(notify(Notify::NotifyType,QString)));
258
259     ((StarDictDialog*)plugin->dictDialog())->
260             setLastDialogParent(_dictDialog->lastDialogParent());
261
262
263
264     if(settings && plugin->setSettings(settings)) {
265
266         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
267                 this, SIGNAL(notify(Notify::NotifyType,QString)));
268         plugin->getDictionaryInfo();
269         plugin->dictReader = StarDictReaderFactory::
270                 createReader(settings->value("dictFileName"));
271         return plugin;
272     }
273     else {
274         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
275                 this, SIGNAL(notify(Notify::NotifyType,QString)));
276         delete plugin;
277         return 0;
278     }
279 }
280
281
282 bool StarDictPlugin::isAvailable() const {
283     return true;
284 }
285
286
287 Settings* StarDictPlugin::settings() {
288     return _settings;
289 }
290
291
292 bool StarDictPlugin::isCached() {
293     return false;
294 }
295
296
297 bool StarDictPlugin::setSettings(const Settings *sett) {
298     if(sett) {
299         foreach(QString key, sett->keys())
300             _settings->setValue(key, sett->value(key));
301
302     } else
303         return false;
304     Q_EMIT settingsChanged();
305     return true;
306 }
307
308
309 bool StarDictPlugin::getDictionaryInfo() {
310     QFile file(settings()->value("ifoFileName"));
311     if(!QFile::exists(_settings->value("ifoFileName"))
312                 || !file.open(QFile::ReadOnly | QFile::Text)) {
313        Q_EMIT notify(Notify::Warning,
314                QString(tr("StarDict dictionary cannot be read from file")));
315         qDebug()<<"Error: could not open the file";
316         return false;
317     }
318
319     QTextStream in(&file);
320     while (!in.atEnd()) {
321         QString line = in.readLine();
322         QStringList list = line.split("=");
323         if(list.size() == 2) {
324             settings()->setValue(list.at(0),list.at(1));
325         }
326     }
327
328     _name = settings()->value("bookname");
329
330     return true;
331 }
332
333
334 QIcon* StarDictPlugin::icon() {
335     return &_icon;
336 }
337
338
339 int StarDictPlugin::countWords() {
340     return 0;
341 }
342
343
344
345 void StarDictPlugin::clean() {
346
347 }
348
349
350 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)