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