1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! \file stardictplugin.cpp
25 #include "StarDictPlugin.h"
27 uint qHash(const TranslationStarDict &key) {
28 return qHash(key.key());
31 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
32 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
33 _settings = new Settings();
34 _dictDialog = new StarDictDialog(this, this);
36 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
37 this, SIGNAL(notify(Notify::NotifyType,QString)));
40 _settings->setValue("type","stardict");
41 _icon = QIcon("/usr/share/mdictionary/stardict.png");
48 void StarDictPlugin::retranslate() {
49 QString locale = QLocale::system().name();
51 QTranslator *translator = new QTranslator(this);
53 if(!translator->load(":/xdxf/translations/" + locale)) {
54 translator->load(":/xdxf/translations/en_US");
56 QCoreApplication::installTranslator(translator);
60 StarDictPlugin::~StarDictPlugin() {
66 QString StarDictPlugin::langFrom() const {
71 QString StarDictPlugin::langTo() const {
76 QString StarDictPlugin::name() const {
81 QString StarDictPlugin::type() const {
82 return QString("stardict");
86 QString StarDictPlugin::infoNote() const {
91 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
92 qDebug() << "StarDict searachWordList" << word;
93 QList<TranslationStarDict> translations;
95 if(settings()->value("idxoffsetbits") == "32" ||
96 settings()->value("idxoffsetbits") == "")
100 int wordcount = settings()->value("wordcount").toInt(&err);
102 return QList<Translation*>();
104 QString idxPath = settings()->value("idxFileName");
105 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
107 qint64 offset = 0, len = 0;
108 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
111 int counterLimit = 0;
112 while(counter < wordcount && (counterLimit<limit || limit==0)) {
114 fkey = reader->readKeyword();
116 offset = reader->readInt32BigEndian();
118 offset = reader->readInt64BigEndian();
119 len = reader->readInt32BigEndian();
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);
127 tran.add(offset, len);
128 translations.push_front(tran);
130 translations[id].add(offset, len);
134 QList<Translation*> ret;
135 QListIterator<TranslationStarDict> it(translations);
137 ret.push_back(new TranslationStarDict(it.next()));
144 QByteArray StarDictPlugin::read(QByteArray::iterator it,
145 QByteArray::iterator end, int bytes) {
148 if(bytes == 0 && it != end){
149 while(*it != '\0' && it != end)
151 if(it == end) qDebug()<<"end";
152 else qDebug()<<"000";
155 for(int i = 0; i < bytes && it != end; i++)
161 QString StarDictPlugin::interpret(QByteArray::iterator it,
162 QByteArray::iterator end, QChar mode,QString key, bool last) {
165 result += "<key>" + key + "</key>";
166 result += QString::fromUtf8(read(it++, end));
168 else if(mode == 'l'){
169 result += "<key>" + key + "</key>";
170 result += QString::fromUtf8(read(it++, end));
172 else if(mode == 'g'){
173 result += "<key>" + key + "</key>";
174 result += QString::fromUtf8(read(it++, end));
176 /* delete "small" tag from translation*/
177 int indexOfSmall=result.indexOf("<small>");
178 while(indexOfSmall!=-1){
179 qDebug()<<"przed"<<result;
180 int indexOfEndSmall= result.indexOf("</small>");
181 if(indexOfEndSmall!=-1)
182 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
183 indexOfSmall=result.indexOf("<small>");
184 qDebug()<<"po"<<result;
187 else if(mode == 't'){
188 result += "<key>" + key + "</key>";
189 result += QString::fromUtf8(read(it++, end));
191 else if(mode == 'x'){
192 result += QString::fromUtf8(read(it++, end));
193 result.replace("</k>","</key><t>");
194 result.replace("<k>","</t><key>");
195 int pos=result.indexOf("</t>");
197 result.remove(pos,4);
198 if(result.contains("<t>"))
201 else if(mode == 'y') {
202 result += "<key>" + key + "</key>";
203 result += QString::fromUtf8(read(it++, end));
205 else if(mode == 'k'){
206 result += "<key>" + key + "</key>";
207 result += QString::fromUtf8(read(it++, end));
209 else if(mode == 'w'){
210 result += "<key>" + key + "</key>";
211 result += QString::fromUtf8(read(it++, end));
213 else if(mode == 'h'){
214 result += "<key>" + key + "</key>";
215 result += QString::fromUtf8(read(it++, end));
217 else if(mode == 'r'){
218 result += "<key>" + key + "</key>";
219 result += QString::fromUtf8(read(it++, end));
222 // Dont know whether mDictionary would ever handle binary stardict format
223 // to be honest dont see any kind of adventages (can't find any binary dict)
226 else if(mode == 'W') {
227 result += "<key>" + key + "</key>";
234 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
236 result += read(it++, end);
237 } else if(mode == 'P') {
238 result += "<key>" + key + "</key>";
245 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
247 result += read(it++, end);
250 qDebug()<<"wynik "<<result;
254 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
257 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
259 result += interpret(--it, raw.end(), tmp, key);
262 QByteArray::iterator it = raw.begin();
263 foreach(QChar tmp, mode) {
264 result += interpret(it, raw.end(), tmp, key);
272 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
273 QString dictPath = settings()->value("dictFileName");
274 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
276 QByteArray raw = reader->readString(offset, len);
277 qDebug()<<"mod"<<settings()->value("sametypesequence");
278 QString result= format(raw, settings()->value("sametypesequence"),key);
285 void StarDictPlugin::stop() {
290 DictDialog* StarDictPlugin::dictDialog() {
295 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
296 StarDictPlugin *plugin = new StarDictPlugin();
298 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
299 this, SIGNAL(notify(Notify::NotifyType,QString)));
301 ((StarDictDialog*)plugin->dictDialog())->
302 setLastDialogParent(_dictDialog->lastDialogParent());
304 if(settings && plugin->setSettings(settings)) {
305 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
306 this, SIGNAL(notify(Notify::NotifyType,QString)));
307 plugin->getDictionaryInfo();
311 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
312 this, SIGNAL(notify(Notify::NotifyType,QString)));
319 bool StarDictPlugin::isAvailable() const {
324 Settings* StarDictPlugin::settings() {
329 bool StarDictPlugin::isCached() {
334 bool StarDictPlugin::setSettings(const Settings *sett) {
336 foreach(QString key, sett->keys())
337 _settings->setValue(key, sett->value(key));
341 Q_EMIT settingsChanged();
346 bool StarDictPlugin::getDictionaryInfo() {
347 QFile file(settings()->value("ifoFileName"));
348 if(!QFile::exists(_settings->value("ifoFileName"))
349 || !file.open(QFile::ReadOnly | QFile::Text)) {
350 Q_EMIT notify(Notify::Warning,
351 QString(tr("StarDict dictionary cannot be read from file")));
352 qDebug()<<"Error: could not open the file";
356 QTextStream in(&file);
357 while (!in.atEnd()) {
358 QString line = in.readLine();
359 QStringList list = line.split("=");
360 if(list.size() == 2) {
361 settings()->setValue(list.at(0),list.at(1));
365 _name = settings()->value("bookname");
370 QIcon* StarDictPlugin::icon() {
375 int StarDictPlugin::countWords() {
381 void StarDictPlugin::clean() {
386 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)