Added searching for word list in 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/xdxf.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 _infoNote;
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     while(counter < wordcount) {
112         counter++;
113         fkey = reader->readKeyword();
114         if(is32b)
115             offset = reader->readInt32BigEndian();
116         else
117             offset = reader->readInt64BigEndian();
118         len = reader->readInt32BigEndian();
119
120         if(keyword.exactMatch(fkey)) {
121             TranslationStarDict tran(fkey, infoNote(), this);
122             int id = translations.indexOf(tran);
123             if(id == -1) {
124                 tran.add(offset, len);
125                 translations.push_front(tran);
126             } else
127                 translations[id].add(offset, len);
128         }
129
130
131     }
132     QList<Translation*> ret;
133     QListIterator<TranslationStarDict> it(translations);
134     while(it.hasNext())
135         ret.push_back(new TranslationStarDict(it.next()));
136
137
138     return ret;
139 }
140
141
142
143 QString StarDictPlugin::search(QString key) {
144     return "";
145 }
146
147
148
149 void StarDictPlugin::stop() {
150     stopped=true;
151 }
152
153
154 DictDialog* StarDictPlugin::dictDialog() {
155      return _dictDialog;
156 }
157
158
159 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
160     StarDictPlugin *plugin = new StarDictPlugin();
161
162     connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
163             this, SIGNAL(notify(Notify::NotifyType,QString)));
164
165     ((StarDictDialog*)plugin->dictDialog())->
166             setLastDialogParent(_dictDialog->lastDialogParent());
167
168
169
170     if(settings && plugin->setSettings(settings)) {
171
172         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
173                 this, SIGNAL(notify(Notify::NotifyType,QString)));
174         plugin->getDictionaryInfo();
175         return plugin;
176     }
177     else {
178         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
179                 this, SIGNAL(notify(Notify::NotifyType,QString)));
180         delete plugin;
181         return 0;
182     }
183 }
184
185
186 bool StarDictPlugin::isAvailable() const {
187     return true;
188 }
189
190
191 Settings* StarDictPlugin::settings() {
192     return _settings;
193 }
194
195
196 bool StarDictPlugin::isCached() {
197     return false;
198 }
199
200
201 bool StarDictPlugin::setSettings(const Settings *sett) {
202     if(sett) {
203         foreach(QString key, sett->keys())
204             _settings->setValue(key, sett->value(key));
205
206     } else
207         return false;
208     Q_EMIT settingsChanged();
209     return true;
210 }
211
212
213 bool StarDictPlugin::getDictionaryInfo() {
214     QFile file(settings()->value("ifoFileName"));
215     if(!QFile::exists(_settings->value("ifoFileName"))
216                 || !file.open(QFile::ReadOnly | QFile::Text)) {
217        Q_EMIT notify(Notify::Warning,
218                QString(tr("StarDict dictionary cannot be read from file")));
219         qDebug()<<"Error: could not open the file";
220         return false;
221     }
222
223     QTextStream in(&file);
224     while (!in.atEnd()) {
225         QString line = in.readLine();
226         QStringList list = line.split("=");
227         if(list.size() == 2) {
228             settings()->setValue(list.at(0),list.at(1));
229         }
230     }
231
232     return true;
233 }
234
235
236 QIcon* StarDictPlugin::icon() {
237     return &_icon;
238 }
239
240
241 int StarDictPlugin::countWords() {
242     return 0;
243 }
244
245
246
247 void StarDictPlugin::clean() {
248
249 }
250
251
252 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)