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");
49 void StarDictPlugin::retranslate() {
50 QString locale = QLocale::system().name();
51 QTranslator *translator = new QTranslator(this);
53 if(!translator->load(":/stardict/translations/" + locale)) {
54 translator->load(":/stardicts/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 QList<TranslationStarDict> translations;
95 int wordcount = settings()->value("wordcount").toInt(&err);
98 QString idxPath = settings()->value("idxFileName");
99 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
101 qint64 offset = 0, len = 0;
102 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
104 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
105 word.indexOf("_")==-1 && word.indexOf("%")==-1)
108 if((settings()->value("idxoffsetbits") == "32") ||
109 (settings()->value("idxoffsetbits") == ""))
113 return QList<Translation*>();
116 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
118 fkey = reader->readKeyword();
120 offset = reader->readInt32BigEndian();
122 offset = reader->readInt64BigEndian();
123 len = reader->readInt32BigEndian();
125 if(keyword.exactMatch(fkey)) {
126 TranslationStarDict tran(fkey, infoNote(), this);
127 qDebug() << "off/len" << offset << len;
128 int id = translations.indexOf(tran);
130 tran.add(offset, len);
131 translations.push_front(tran);
133 translations[id].add(offset, len);
137 QList<Translation*> ret;
138 QListIterator<TranslationStarDict> it(translations);
140 ret.push_back(new TranslationStarDict(it.next()));
145 QByteArray StarDictPlugin::read(QByteArray::iterator it,
146 QByteArray::iterator end, int bytes) {
149 if(bytes == 0 && it != end){
150 while(*it != '\0' && it != end)
154 for(int i = 0; i < bytes && it != end; i++)
160 QString StarDictPlugin::interpret(QByteArray::iterator it,
161 QByteArray::iterator end, QChar mode,QString key, bool last) {
163 qDebug()<<"****** mode: "<<mode;
174 result += "<key>" + key + "</key>";
175 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
179 /* delete "small" tag from g translation*/
180 int indexOfSmall=result.indexOf("<small>");
181 while(indexOfSmall!=-1){
182 int indexOfEndSmall= result.indexOf("</small>");
183 if(indexOfEndSmall!=-1)
184 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
185 indexOfSmall=result.indexOf("<small>");
189 result += QString::fromUtf8(read(it++, end));
190 result.replace("</k>","</key><t>");
191 result.replace("<k>","</t><key>");
192 int pos=result.indexOf("</t>");
194 result.remove(pos,4);
195 if(result.contains("<t>"))
199 // Dont know whether mDictionary would ever handle binary stardict format
200 // to be honest dont see any kind of adventages (can't find any binary dict)
203 else if(mode == 'W') {
204 result += "<key>" + key + "</key>";
211 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
213 result += read(it++, end);
214 } else if(mode == 'P') {
215 result += "<key>" + key + "</key>";
222 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
224 result += read(it++, end);
227 qDebug()<<"wynik "<<result;
232 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
235 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
237 result += interpret(--it, raw.end(), tmp, key);
240 QByteArray::iterator it = raw.begin();
241 foreach(QChar tmp, mode) {
242 result += interpret(it, raw.end(), tmp, key);
249 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
250 QString dictPath = settings()->value("dictFileName");
251 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
252 QByteArray raw = reader->readString(offset, len);
253 QString result= format(raw, settings()->value("sametypesequence"),key);
260 void StarDictPlugin::stop() {
265 DictDialog* StarDictPlugin::dictDialog() {
270 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
271 StarDictPlugin *plugin = new StarDictPlugin();
273 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
274 this, SIGNAL(notify(Notify::NotifyType,QString)));
276 ((StarDictDialog*)plugin->dictDialog())->
277 setLastDialogParent(_dictDialog->lastDialogParent());
279 if(settings && plugin->setSettings(settings)) {
280 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
281 this, SIGNAL(notify(Notify::NotifyType,QString)));
282 plugin->getDictionaryInfo();
286 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
287 this, SIGNAL(notify(Notify::NotifyType,QString)));
294 bool StarDictPlugin::isAvailable() const {
299 Settings* StarDictPlugin::settings() {
304 bool StarDictPlugin::setSettings(const Settings *sett) {
306 foreach(QString key, sett->keys())
307 _settings->setValue(key, sett->value(key));
311 Q_EMIT settingsChanged();
316 bool StarDictPlugin::getDictionaryInfo() {
317 QFile file(settings()->value("ifoFileName"));
318 if(!QFile::exists(_settings->value("ifoFileName"))
319 || !file.open(QFile::ReadOnly | QFile::Text)) {
320 Q_EMIT notify(Notify::Warning,
321 QString(tr("StarDict dictionary cannot be read from file")));
322 qDebug()<<"Error: could not open the file";
325 QTextStream in(&file);
326 while (!in.atEnd()) {
327 QString line = in.readLine();
328 QStringList list = line.split("=");
329 if(list.size() == 2) {
330 settings()->setValue(list.at(0),list.at(1));
333 _name = settings()->value("bookname");
338 QIcon* StarDictPlugin::icon() {
343 int StarDictPlugin::countWords() {
348 void StarDictPlugin::clean() {
353 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)