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");
46 _settings->setValue("strip_accents","true");
53 void StarDictPlugin::retranslate() {
54 QString locale = QLocale::system().name();
55 QTranslator *translator = new QTranslator(this);
57 if(!translator->load(":/stardict/translations/" + locale)) {
58 translator->load(":/stardict/translations/en_US");
60 QCoreApplication::installTranslator(translator);
64 StarDictPlugin::~StarDictPlugin() {
70 QString StarDictPlugin::langFrom() const {
75 QString StarDictPlugin::langTo() const {
80 QString StarDictPlugin::name() const {
85 QString StarDictPlugin::type() const {
86 return QString("stardict");
90 QString StarDictPlugin::infoNote() const {
95 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
96 QList<TranslationStarDict> translations;
99 int wordcount = settings()->value("wordcount").toInt(&err);
101 int counterLimit = 0;
102 QString idxPath = settings()->value("idxFileName");
103 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
108 if( word.indexOf("*")==-1 && word.indexOf("?")==-1 &&
109 word.indexOf("_")==-1 && word.indexOf("%")==-1)
111 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
113 if((settings()->value("idxoffsetbits") == "32") ||
114 (settings()->value("idxoffsetbits") == ""))
118 return QList<Translation*>();
121 while(counter < wordcount && (counterLimit<limit || limit==0) && !stopped) {
123 fkey = reader->readKeyword();
125 offset = reader->readInt32BigEndian();
127 offset = reader->readInt64BigEndian();
128 len = reader->readInt32BigEndian();
130 if(keyword.exactMatch(fkey) ) {
131 TranslationStarDict tran(fkey, infoNote(), this);
132 // qDebug() << "off/len" << offset << len;
133 int id = translations.indexOf(tran);
135 tran.add(offset, len);
136 translations.push_front(tran);
138 translations[id].add(offset, len);
142 QList<Translation*> ret;
143 QListIterator<TranslationStarDict> it(translations);
145 ret.push_back(new TranslationStarDict(it.next()));
150 QByteArray StarDictPlugin::read(QByteArray::iterator it,
151 QByteArray::iterator end, int bytes) {
154 if(bytes == 0 && it != end){
155 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) {
168 // qDebug()<<"****** mode: "<<mode;
178 result += "<key>" + key + "</key>";
179 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
183 /* delete "small" tag from g translation*/
184 int indexOfSmall=result.indexOf("<small>");
185 while(indexOfSmall!=-1){
186 int indexOfEndSmall= result.indexOf("</small>");
187 if(indexOfEndSmall!=-1)
188 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
189 indexOfSmall=result.indexOf("<small>");
193 result += QString::fromUtf8(read(it++, end));
194 result.replace("</k>","</key><t>");
195 result.replace("<k>","</t><key>");
196 int pos=result.indexOf("</t>");
198 result.remove(pos,4);
199 if(result.contains("<t>"))
203 // W & P Dont know whether mDictionary would ever handle binary stardict format
204 // to be honest dont see any kind of adventages (can't find any binary dict)
207 else if(mode == 'W') {
208 result += "<key>" + key + "</key>";
215 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
217 result += read(it++, end);
218 } else if(mode == 'P') {
219 result += "<key>" + key + "</key>";
226 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
228 result += read(it++, end);
234 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
237 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
239 result += interpret(--it, raw.end(), tmp, key);
242 QByteArray::iterator it = raw.begin();
243 foreach(QChar tmp, mode) {
244 result += interpret(it, raw.end(), tmp, key);
251 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
252 QString dictPath = settings()->value("dictFileName");
253 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
254 QByteArray raw = reader->readString(offset, len);
255 QString result= format(raw, settings()->value("sametypesequence"),key);
262 void StarDictPlugin::stop() {
267 DictDialog* StarDictPlugin::dictDialog() {
272 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
273 StarDictPlugin *plugin = new StarDictPlugin();
275 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
276 this, SIGNAL(notify(Notify::NotifyType,QString)));
278 ((StarDictDialog*)plugin->dictDialog())->
279 setLastDialogParent(_dictDialog->lastDialogParent());
281 if(settings && plugin->setSettings(settings)) {
282 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
283 this, SIGNAL(notify(Notify::NotifyType,QString)));
284 plugin->getDictionaryInfo();
288 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
289 this, SIGNAL(notify(Notify::NotifyType,QString)));
296 bool StarDictPlugin::isAvailable() const {
301 Settings* StarDictPlugin::settings() {
306 bool StarDictPlugin::setSettings(const Settings *sett) {
308 foreach(QString key, sett->keys())
309 _settings->setValue(key, sett->value(key));
313 Q_EMIT settingsChanged();
318 bool StarDictPlugin::getDictionaryInfo() {
319 QFile file(settings()->value("ifoFileName"));
320 if(!QFile::exists(_settings->value("ifoFileName"))
321 || !file.open(QFile::ReadOnly | QFile::Text)) {
322 Q_EMIT notify(Notify::Warning,
323 QString(tr("StarDict dictionary cannot be read from file")));
324 qDebug()<<"Error: could not open the file";
327 QTextStream in(&file);
328 while (!in.atEnd()) {
329 QString line = in.readLine();
330 QStringList list = line.split("=");
331 if(list.size() == 2) {
332 settings()->setValue(list.at(0),list.at(1));
335 _name = settings()->value("bookname");
340 QIcon* StarDictPlugin::icon() {
344 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)