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
23 \author Jakub Jaszczynski
26 #include "StarDictPlugin.h"
29 uint qHash(const TranslationStarDict &key) {
30 return qHash(key.key());
34 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
35 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
36 _settings = new Settings();
37 _dictDialog = new StarDictDialog(this, this);
39 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
40 this, SIGNAL(notify(Notify::NotifyType,QString)));
42 _settings->setValue("type","stardict");
43 _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(":/stardict/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);
105 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
106 word.indexOf("_")==-1 && word.indexOf("%")==-1)
108 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
110 if((settings()->value("idxoffsetbits") == "32") ||
111 (settings()->value("idxoffsetbits") == ""))
115 return QList<Translation*>();
118 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
120 fkey = reader->readKeyword();
122 offset = reader->readInt32BigEndian();
124 offset = reader->readInt64BigEndian();
125 len = reader->readInt32BigEndian();
127 if(keyword.exactMatch(fkey)) {
128 TranslationStarDict tran(fkey, infoNote(), this);
129 // qDebug() << "off/len" << offset << len;
130 int id = translations.indexOf(tran);
132 tran.add(offset, len);
133 translations.push_front(tran);
135 translations[id].add(offset, len);
139 QList<Translation*> ret;
140 QListIterator<TranslationStarDict> it(translations);
142 ret.push_back(new TranslationStarDict(it.next()));
147 QByteArray StarDictPlugin::read(QByteArray::iterator it,
148 QByteArray::iterator end, int bytes) {
151 if(bytes == 0 && it != end){
152 while(*it != '\0' && it != end)
156 for(int i = 0; i < bytes && it != end; i++)
162 QString StarDictPlugin::interpret(QByteArray::iterator it,
163 QByteArray::iterator end, QChar mode,QString key, bool last) {
165 // qDebug()<<"****** mode: "<<mode;
175 result += "<key>" + key + "</key>";
176 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
180 /* delete "small" tag from g translation*/
181 int indexOfSmall=result.indexOf("<small>");
182 while(indexOfSmall!=-1){
183 int indexOfEndSmall= result.indexOf("</small>");
184 if(indexOfEndSmall!=-1)
185 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
186 indexOfSmall=result.indexOf("<small>");
190 result += QString::fromUtf8(read(it++, end));
191 result.replace("</k>","</key><t>");
192 result.replace("<k>","</t><key>");
193 int pos=result.indexOf("</t>");
195 result.remove(pos,4);
196 if(result.contains("<t>"))
200 // W & P Dont know whether mDictionary would ever handle binary stardict format
201 // to be honest dont see any kind of adventages (can't find any binary dict)
204 else if(mode == 'W') {
205 result += "<key>" + key + "</key>";
212 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
214 result += read(it++, end);
215 } else if(mode == 'P') {
216 result += "<key>" + key + "</key>";
223 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
225 result += read(it++, end);
231 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
234 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
236 result += interpret(--it, raw.end(), tmp, key);
239 QByteArray::iterator it = raw.begin();
240 foreach(QChar tmp, mode) {
241 result += interpret(it, raw.end(), tmp, key);
248 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
249 QString dictPath = settings()->value("dictFileName");
250 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
251 QByteArray raw = reader->readString(offset, len);
252 QString result= format(raw, settings()->value("sametypesequence"),key);
259 void StarDictPlugin::stop() {
264 DictDialog* StarDictPlugin::dictDialog() {
269 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
270 StarDictPlugin *plugin = new StarDictPlugin();
272 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
273 this, SIGNAL(notify(Notify::NotifyType,QString)));
275 ((StarDictDialog*)plugin->dictDialog())->
276 setLastDialogParent(_dictDialog->lastDialogParent());
278 if(settings && plugin->setSettings(settings)) {
279 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
280 this, SIGNAL(notify(Notify::NotifyType,QString)));
281 plugin->getDictionaryInfo();
285 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
286 this, SIGNAL(notify(Notify::NotifyType,QString)));
293 bool StarDictPlugin::isAvailable() const {
298 Settings* StarDictPlugin::settings() {
303 bool StarDictPlugin::setSettings(const Settings *sett) {
305 foreach(QString key, sett->keys())
306 _settings->setValue(key, sett->value(key));
310 Q_EMIT settingsChanged();
315 bool StarDictPlugin::getDictionaryInfo() {
316 QFile file(settings()->value("ifoFileName"));
317 if(!QFile::exists(_settings->value("ifoFileName"))
318 || !file.open(QFile::ReadOnly | QFile::Text)) {
319 Q_EMIT notify(Notify::Warning,
320 QString(tr("StarDict dictionary cannot be read from file")));
321 qDebug()<<"Error: could not open the file";
324 QTextStream in(&file);
325 while (!in.atEnd()) {
326 QString line = in.readLine();
327 QStringList list = line.split("=");
328 if(list.size() == 2) {
329 settings()->setValue(list.at(0),list.at(1));
332 _name = settings()->value("bookname");
337 QIcon* StarDictPlugin::icon() {
341 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)