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"
28 uint qHash(const TranslationStarDict &key) {
29 return qHash(key.key());
33 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
34 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
35 _settings = new Settings();
36 _dictDialog = new StarDictDialog(this, this);
38 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
39 this, SIGNAL(notify(Notify::NotifyType,QString)));
41 _settings->setValue("type","stardict");
42 _icon = QIcon("/usr/share/mdictionary/stardict.png");
50 void StarDictPlugin::retranslate() {
51 QString locale = QLocale::system().name();
52 QTranslator *translator = new QTranslator(this);
54 if(!translator->load(":/stardict/translations/" + locale)) {
55 translator->load(":/stardicts/translations/en_US");
57 QCoreApplication::installTranslator(translator);
61 StarDictPlugin::~StarDictPlugin() {
67 QString StarDictPlugin::langFrom() const {
72 QString StarDictPlugin::langTo() const {
77 QString StarDictPlugin::name() const {
82 QString StarDictPlugin::type() const {
83 return QString("stardict");
87 QString StarDictPlugin::infoNote() const {
92 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
93 QList<TranslationStarDict> translations;
96 int wordcount = settings()->value("wordcount").toInt(&err);
99 QString idxPath = settings()->value("idxFileName");
100 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
102 qint64 offset = 0, len = 0;
103 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
105 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
106 word.indexOf("_")==-1 && word.indexOf("%")==-1)
110 if((settings()->value("idxoffsetbits") == "32") ||
111 (settings()->value("idxoffsetbits") == ""))
114 return QList<Translation*>();
117 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
119 fkey = reader->readKeyword();
121 offset = reader->readInt32BigEndian();
123 offset = reader->readInt64BigEndian();
124 len = reader->readInt32BigEndian();
126 if(keyword.exactMatch(fkey)) {
127 TranslationStarDict tran(fkey, infoNote(), this);
128 qDebug() << "off/len" << offset << len;
129 int id = translations.indexOf(tran);
131 tran.add(offset, len);
132 translations.push_front(tran);
134 translations[id].add(offset, len);
138 QList<Translation*> ret;
139 QListIterator<TranslationStarDict> it(translations);
141 ret.push_back(new TranslationStarDict(it.next()));
146 QByteArray StarDictPlugin::read(QByteArray::iterator it,
147 QByteArray::iterator end, int bytes) {
150 if(bytes == 0 && it != end){
151 while(*it != '\0' && it != end)
159 for(int i = 0; i < bytes && it != end; i++)
165 QString StarDictPlugin::interpret(QByteArray::iterator it,
166 QByteArray::iterator end, QChar mode,QString key, bool last) {
168 qDebug()<<"****** mode: "<<mode;
170 result += "<key>" + key + "</key>";
171 result += QString::fromUtf8(read(it++, end));
173 else if(mode == 'l'){
174 result += "<key>" + key + "</key>";
175 result += QString::fromUtf8(read(it++, end));
177 else if(mode == 'g'){
178 result += "<key>" + key + "</key>";
179 result += QString::fromUtf8(read(it++, end));
181 /* delete "small" tag from 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>");
190 else if(mode == 't'){
191 result += "<key>" + key + "</key>";
192 result += QString::fromUtf8(read(it++, end));
194 else if(mode == 'x'){
195 result += QString::fromUtf8(read(it++, end));
196 result.replace("</k>","</key><t>");
197 result.replace("<k>","</t><key>");
198 int pos=result.indexOf("</t>");
200 result.remove(pos,4);
201 if(result.contains("<t>"))
204 else if(mode == 'y') {
205 result += "<key>" + key + "</key>";
206 result += QString::fromUtf8(read(it++, end));
208 else if(mode == 'k'){
209 result += "<key>" + key + "</key>";
210 result += QString::fromUtf8(read(it++, end));
212 else if(mode == 'w'){
213 result += "<key>" + key + "</key>";
214 result += QString::fromUtf8(read(it++, end));
216 else if(mode == 'h'){
217 result += "<key>" + key + "</key>";
218 result += QString::fromUtf8(read(it++, end));
220 else if(mode == 'r'){
221 result += "<key>" + key + "</key>";
222 result += QString::fromUtf8(read(it++, end));
226 // Dont know whether mDictionary would ever handle binary stardict format
227 // to be honest dont see any kind of adventages (can't find any binary dict)
230 else if(mode == 'W') {
231 result += "<key>" + key + "</key>";
238 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
240 result += read(it++, end);
241 } else if(mode == 'P') {
242 result += "<key>" + key + "</key>";
249 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
251 result += read(it++, end);
254 qDebug()<<"wynik "<<result;
259 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
262 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
264 result += interpret(--it, raw.end(), tmp, key);
267 QByteArray::iterator it = raw.begin();
268 foreach(QChar tmp, mode) {
269 result += interpret(it, raw.end(), tmp, key);
277 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
278 QString dictPath = settings()->value("dictFileName");
279 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
280 QByteArray raw = reader->readString(offset, len);
281 QString result= format(raw, settings()->value("sametypesequence"),key);
288 void StarDictPlugin::stop() {
293 DictDialog* StarDictPlugin::dictDialog() {
298 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
299 StarDictPlugin *plugin = new StarDictPlugin();
301 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
302 this, SIGNAL(notify(Notify::NotifyType,QString)));
304 ((StarDictDialog*)plugin->dictDialog())->
305 setLastDialogParent(_dictDialog->lastDialogParent());
307 if(settings && plugin->setSettings(settings)) {
308 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
309 this, SIGNAL(notify(Notify::NotifyType,QString)));
310 plugin->getDictionaryInfo();
314 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
315 this, SIGNAL(notify(Notify::NotifyType,QString)));
322 bool StarDictPlugin::isAvailable() const {
327 Settings* StarDictPlugin::settings() {
332 bool StarDictPlugin::isCached() {
337 bool StarDictPlugin::setSettings(const Settings *sett) {
339 foreach(QString key, sett->keys())
340 _settings->setValue(key, sett->value(key));
344 Q_EMIT settingsChanged();
349 bool StarDictPlugin::getDictionaryInfo() {
350 QFile file(settings()->value("ifoFileName"));
351 if(!QFile::exists(_settings->value("ifoFileName"))
352 || !file.open(QFile::ReadOnly | QFile::Text)) {
353 Q_EMIT notify(Notify::Warning,
354 QString(tr("StarDict dictionary cannot be read from file")));
355 qDebug()<<"Error: could not open the file";
358 QTextStream in(&file);
359 while (!in.atEnd()) {
360 QString line = in.readLine();
361 QStringList list = line.split("=");
362 if(list.size() == 2) {
363 settings()->setValue(list.at(0),list.at(1));
366 _name = settings()->value("bookname");
371 QIcon* StarDictPlugin::icon() {
376 int StarDictPlugin::countWords() {
382 void StarDictPlugin::clean() {
387 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)