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