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 \brief Implementation of stardict plugin's main class.
25 \author Jakub Jaszczynski
28 #include "StarDictPlugin.h"
31 uint qHash(const TranslationStarDict &key) {
32 return qHash(key.key());
36 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
37 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
38 _settings = new Settings();
39 _dictDialog = new StarDictDialog(this, this);
41 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
42 this, SIGNAL(notify(Notify::NotifyType,QString)));
44 _settings->setValue("type","stardict");
45 _iconPath = "/usr/share/mdictionary/stardict.png";
46 _icon = QIcon(_iconPath);
49 _settings->setValue("strip_accents","true");
56 void StarDictPlugin::retranslate() {
57 QString locale = QLocale::system().name();
58 QTranslator *translator = new QTranslator(this);
60 if(!translator->load(":/stardict/translations/" + locale)) {
61 translator->load(":/stardict/translations/en_US");
63 QCoreApplication::installTranslator(translator);
67 StarDictPlugin::~StarDictPlugin() {
73 QString StarDictPlugin::langFrom() const {
78 QString StarDictPlugin::langTo() const {
83 QString StarDictPlugin::name() const {
88 QString StarDictPlugin::type() const {
89 return QString("stardict");
93 QString StarDictPlugin::infoNote() const {
98 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
99 QList<TranslationStarDict> translations;
102 int wordcount = settings()->value("wordcount").toInt(&err);
104 int counterLimit = 0;
105 QString idxPath = settings()->value("idxFileName");
106 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
111 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
112 word.indexOf("_")==-1 && word.indexOf("%")==-1)
114 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
116 if((settings()->value("idxoffsetbits") == "32") ||
117 (settings()->value("idxoffsetbits") == ""))
121 return QList<Translation*>();
124 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
126 fkey = reader->readKeyword();
128 offset = reader->readInt32BigEndian();
130 offset = reader->readInt64BigEndian();
131 len = reader->readInt32BigEndian();
133 if(keyword.exactMatch(fkey) ) {
134 TranslationStarDict tran(fkey, infoNote(), this);
135 // qDebug() << "off/len" << offset << len;
136 int id = translations.indexOf(tran);
138 tran.add(offset, len);
139 translations.push_front(tran);
141 translations[id].add(offset, len);
145 QList<Translation*> ret;
146 QListIterator<TranslationStarDict> it(translations);
148 ret.push_back(new TranslationStarDict(it.next()));
153 QByteArray StarDictPlugin::read(QByteArray::iterator it,
154 QByteArray::iterator end, int bytes) {
157 if(bytes == 0 && it != end){
158 while(*it != '\0' && it != end)
162 for(int i = 0; i < bytes && it != end; i++)
168 QString StarDictPlugin::interpret(QByteArray::iterator it,
169 QByteArray::iterator end, QChar mode,QString key, bool) {
171 // qDebug()<<"****** mode: "<<mode;
181 result += "<key>" + key + "</key>";
182 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
186 /* delete "small" tag from g translation*/
187 int indexOfSmall=result.indexOf("<small>");
188 while(indexOfSmall!=-1){
189 int indexOfEndSmall= result.indexOf("</small>");
190 if(indexOfEndSmall!=-1)
191 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
192 indexOfSmall=result.indexOf("<small>");
196 result += QString::fromUtf8(read(it++, end));
197 result.replace("</k>","</key><t>");
198 result.replace("<k>","</t><key>");
199 int pos=result.indexOf("</t>");
201 result.remove(pos,4);
202 if(result.contains("<t>"))
206 // W & P Dont know whether mDictionary would ever handle binary stardict format
207 // to be honest dont see any kind of adventages (can't find any binary dict)
210 else if(mode == 'W') {
211 result += "<key>" + key + "</key>";
218 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
220 result += read(it++, end);
221 } else if(mode == 'P') {
222 result += "<key>" + key + "</key>";
229 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
231 result += read(it++, end);
237 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
240 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
242 result += interpret(--it, raw.end(), tmp, key);
245 QByteArray::iterator it = raw.begin();
246 foreach(QChar tmp, mode) {
247 result += interpret(it, raw.end(), tmp, key);
254 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
255 QString dictPath = settings()->value("dictFileName");
256 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
257 QByteArray raw = reader->readString(offset, len);
258 QString result= format(raw, settings()->value("sametypesequence"),key);
265 void StarDictPlugin::stop() {
270 DictDialog* StarDictPlugin::dictDialog() {
275 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
276 StarDictPlugin *plugin = new StarDictPlugin();
278 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
279 this, SIGNAL(notify(Notify::NotifyType,QString)));
281 ((StarDictDialog*)plugin->dictDialog())->
282 setLastDialogParent(_dictDialog->lastDialogParent());
284 if(settings && plugin->setSettings(settings)) {
285 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
286 this, SIGNAL(notify(Notify::NotifyType,QString)));
287 plugin->getDictionaryInfo();
291 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
292 this, SIGNAL(notify(Notify::NotifyType,QString)));
299 bool StarDictPlugin::isAvailable() const {
304 Settings* StarDictPlugin::settings() {
309 bool StarDictPlugin::setSettings(const Settings *sett) {
311 foreach(QString key, sett->keys())
312 _settings->setValue(key, sett->value(key));
316 Q_EMIT settingsChanged();
321 bool StarDictPlugin::getDictionaryInfo() {
322 QFile file(settings()->value("ifoFileName"));
323 if(!QFile::exists(_settings->value("ifoFileName"))
324 || !file.open(QFile::ReadOnly | QFile::Text)) {
325 Q_EMIT notify(Notify::Warning,
326 QString(tr("StarDict dictionary cannot be read from file")));
327 qDebug()<<"Error: could not open the file";
330 QTextStream in(&file);
331 while (!in.atEnd()) {
332 QString line = in.readLine();
333 QStringList list = line.split("=");
334 if(list.size() == 2) {
335 settings()->setValue(list.at(0),list.at(1));
338 _name = settings()->value("bookname");
343 QIcon* StarDictPlugin::icon() {
347 QString StarDictPlugin::iconPath(){
351 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)