Fixed some methods name missmatch
[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     QList<TranslationStarDict> translations;
93     bool is32b = false;
94     if(settings()->value("idxoffsetbits") == "32" ||
95             settings()->value("idxoffsetbits") == "")
96        is32b = true;
97
98     bool err = 0;
99     int wordcount = settings()->value("wordcount").toInt(&err);
100     if(err)
101         return QList<Translation*>();
102
103     QString idxPath = settings()->value("idxFileName");
104     StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
105
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())->setLastDialogParent(_dictDialog->lastDialogParent());
166
167
168
169     if(settings && plugin->setSettings(settings)) {
170
171         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
172                 this, SIGNAL(notify(Notify::NotifyType,QString)));
173         return plugin;
174     }
175     else {
176         disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
177                 this, SIGNAL(notify(Notify::NotifyType,QString)));
178         delete plugin;
179         return 0;
180     }
181 }
182
183
184 bool StarDictPlugin::isAvailable() const {
185     return true;
186 }
187
188
189 Settings* StarDictPlugin::settings() {
190     return _settings;
191 }
192
193
194 bool StarDictPlugin::isCached() {
195     return false;
196 }
197
198
199 bool StarDictPlugin::setSettings(const Settings *settings) {
200     if(settings) {
201
202     } else
203         return false;
204     Q_EMIT settingsChanged();
205     return true;
206 }
207
208
209 bool StarDictPlugin::getDictionaryInfo() {
210     QFile file(settings()->value("ifoFileName"));
211     if(!QFile::exists(_settings->value("ifoFileName"))
212                 || !file.open(QFile::ReadOnly | QFile::Text)) {
213        Q_EMIT notify(Notify::Warning,
214                QString(tr("StarDict dictionary cannot be read from file")));
215         qDebug()<<"Error: could not open the file";
216         return false;
217     }
218
219     QTextStream in(&file);
220     while (!in.atEnd()) {
221         QString line = in.readLine();
222         QStringList list = line.split("=");
223         if(list.size() == 2) {
224             settings()->setValue(list.at(0),list.at(1));
225         }
226     }
227
228     return true;
229 }
230
231
232 QIcon* StarDictPlugin::icon() {
233     return &_icon;
234 }
235
236
237 int StarDictPlugin::countWords() {
238     return 0;
239 }
240
241
242
243 void StarDictPlugin::clean() {
244
245 }
246
247
248 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)