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 _icon = QIcon("/usr/share/mdictionary/stardict.png");
48 _settings->setValue("strip_accents","true");
55 void StarDictPlugin::retranslate() {
56 QString locale = QLocale::system().name();
57 QTranslator *translator = new QTranslator(this);
59 if(!translator->load(":/stardict/translations/" + locale)) {
60 translator->load(":/stardict/translations/en_US");
62 QCoreApplication::installTranslator(translator);
66 StarDictPlugin::~StarDictPlugin() {
72 QString StarDictPlugin::langFrom() const {
77 QString StarDictPlugin::langTo() const {
82 QString StarDictPlugin::name() const {
87 QString StarDictPlugin::type() const {
88 return QString("stardict");
92 QString StarDictPlugin::infoNote() const {
97 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
98 QList<TranslationStarDict> translations;
101 int wordcount = settings()->value("wordcount").toInt(&err);
103 int counterLimit = 0;
104 QString idxPath = settings()->value("idxFileName");
105 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
110 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
111 word.indexOf("_")==-1 && word.indexOf("%")==-1)
113 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
115 if((settings()->value("idxoffsetbits") == "32") ||
116 (settings()->value("idxoffsetbits") == ""))
120 return QList<Translation*>();
123 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
125 fkey = reader->readKeyword();
127 offset = reader->readInt32BigEndian();
129 offset = reader->readInt64BigEndian();
130 len = reader->readInt32BigEndian();
132 if(keyword.exactMatch(fkey) ) {
133 TranslationStarDict tran(fkey, infoNote(), this);
134 // qDebug() << "off/len" << offset << len;
135 int id = translations.indexOf(tran);
137 tran.add(offset, len);
138 translations.push_front(tran);
140 translations[id].add(offset, len);
144 QList<Translation*> ret;
145 QListIterator<TranslationStarDict> it(translations);
147 ret.push_back(new TranslationStarDict(it.next()));
152 QByteArray StarDictPlugin::read(QByteArray::iterator it,
153 QByteArray::iterator end, int bytes) {
156 if(bytes == 0 && it != end){
157 while(*it != '\0' && it != end)
161 for(int i = 0; i < bytes && it != end; i++)
167 QString StarDictPlugin::interpret(QByteArray::iterator it,
168 QByteArray::iterator end, QChar mode,QString key, bool) {
170 // qDebug()<<"****** mode: "<<mode;
180 result += "<key>" + key + "</key>";
181 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
185 /* delete "small" tag from g translation*/
186 int indexOfSmall=result.indexOf("<small>");
187 while(indexOfSmall!=-1){
188 int indexOfEndSmall= result.indexOf("</small>");
189 if(indexOfEndSmall!=-1)
190 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
191 indexOfSmall=result.indexOf("<small>");
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>"))
205 // W & P Dont know whether mDictionary would ever handle binary stardict format
206 // to be honest dont see any kind of adventages (can't find any binary dict)
209 else if(mode == 'W') {
210 result += "<key>" + key + "</key>";
217 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
219 result += read(it++, end);
220 } else if(mode == 'P') {
221 result += "<key>" + key + "</key>";
228 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
230 result += read(it++, end);
236 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
239 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
241 result += interpret(--it, raw.end(), tmp, key);
244 QByteArray::iterator it = raw.begin();
245 foreach(QChar tmp, mode) {
246 result += interpret(it, raw.end(), tmp, key);
253 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
254 QString dictPath = settings()->value("dictFileName");
255 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
256 QByteArray raw = reader->readString(offset, len);
257 QString result= format(raw, settings()->value("sametypesequence"),key);
264 void StarDictPlugin::stop() {
269 DictDialog* StarDictPlugin::dictDialog() {
274 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
275 StarDictPlugin *plugin = new StarDictPlugin();
277 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
278 this, SIGNAL(notify(Notify::NotifyType,QString)));
280 ((StarDictDialog*)plugin->dictDialog())->
281 setLastDialogParent(_dictDialog->lastDialogParent());
283 if(settings && plugin->setSettings(settings)) {
284 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
285 this, SIGNAL(notify(Notify::NotifyType,QString)));
286 plugin->getDictionaryInfo();
290 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
291 this, SIGNAL(notify(Notify::NotifyType,QString)));
298 bool StarDictPlugin::isAvailable() const {
303 Settings* StarDictPlugin::settings() {
308 bool StarDictPlugin::setSettings(const Settings *sett) {
310 foreach(QString key, sett->keys())
311 _settings->setValue(key, sett->value(key));
315 Q_EMIT settingsChanged();
320 bool StarDictPlugin::getDictionaryInfo() {
321 QFile file(settings()->value("ifoFileName"));
322 if(!QFile::exists(_settings->value("ifoFileName"))
323 || !file.open(QFile::ReadOnly | QFile::Text)) {
324 Q_EMIT notify(Notify::Warning,
325 QString(tr("StarDict dictionary cannot be read from file")));
326 qDebug()<<"Error: could not open the file";
329 QTextStream in(&file);
330 while (!in.atEnd()) {
331 QString line = in.readLine();
332 QStringList list = line.split("=");
333 if(list.size() == 2) {
334 settings()->setValue(list.at(0),list.at(1));
337 _name = settings()->value("bookname");
342 QIcon* StarDictPlugin::icon() {
346 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)