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 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) {
179 result += "<key>" + key + "</key>";
180 result += "<t>" + QString::fromUtf8(read(it++, end)) + "</t>";
184 /* delete "small" tag from g translation*/
185 int indexOfSmall=result.indexOf("<small>");
186 while(indexOfSmall!=-1){
187 int indexOfEndSmall= result.indexOf("</small>");
188 if(indexOfEndSmall!=-1)
189 result.remove(indexOfSmall,indexOfEndSmall-indexOfSmall+8);
190 indexOfSmall=result.indexOf("<small>");
194 result += QString::fromUtf8(read(it++, end));
195 result.replace("</k>","</key><t>");
196 result.replace("<k>","</t><key>");
197 int pos=result.indexOf("</t>");
199 result.remove(pos,4);
200 if(result.contains("<t>"))
204 // W & P Dont know whether mDictionary would ever handle binary stardict format
205 // to be honest dont see any kind of adventages (can't find any binary dict)
208 else if(mode == 'W') {
209 result += "<key>" + key + "</key>";
216 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
218 result += read(it++, end);
219 } else if(mode == 'P') {
220 result += "<key>" + key + "</key>";
227 result += read(it++, end, (qint32)qFromBigEndian(*(qint32*)tmp.data()));
229 result += read(it++, end);
235 QString StarDictPlugin::format(QByteArray raw, QString mode,QString key) {
238 for(QByteArray::iterator it = raw.begin(); it != raw.end(); it++) {
240 result += interpret(--it, raw.end(), tmp, key);
243 QByteArray::iterator it = raw.begin();
244 foreach(QChar tmp, mode) {
245 result += interpret(it, raw.end(), tmp, key);
252 QString StarDictPlugin::search(QString key, qint64 offset, qint32 len) {
253 QString dictPath = settings()->value("dictFileName");
254 StarDictReader *reader = StarDictReaderFactory::createReader(dictPath);
255 QByteArray raw = reader->readString(offset, len);
256 QString result= format(raw, settings()->value("sametypesequence"),key);
263 void StarDictPlugin::stop() {
268 DictDialog* StarDictPlugin::dictDialog() {
273 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
274 StarDictPlugin *plugin = new StarDictPlugin();
276 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
277 this, SIGNAL(notify(Notify::NotifyType,QString)));
279 ((StarDictDialog*)plugin->dictDialog())->
280 setLastDialogParent(_dictDialog->lastDialogParent());
282 if(settings && plugin->setSettings(settings)) {
283 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
284 this, SIGNAL(notify(Notify::NotifyType,QString)));
285 plugin->getDictionaryInfo();
289 disconnect( plugin, SIGNAL(notify(Notify::NotifyType,QString)),
290 this, SIGNAL(notify(Notify::NotifyType,QString)));
297 bool StarDictPlugin::isAvailable() const {
302 Settings* StarDictPlugin::settings() {
307 bool StarDictPlugin::setSettings(const Settings *sett) {
309 foreach(QString key, sett->keys())
310 _settings->setValue(key, sett->value(key));
314 Q_EMIT settingsChanged();
319 bool StarDictPlugin::getDictionaryInfo() {
320 QFile file(settings()->value("ifoFileName"));
321 if(!QFile::exists(_settings->value("ifoFileName"))
322 || !file.open(QFile::ReadOnly | QFile::Text)) {
323 Q_EMIT notify(Notify::Warning,
324 QString(tr("StarDict dictionary cannot be read from file")));
325 qDebug()<<"Error: could not open the file";
328 QTextStream in(&file);
329 while (!in.atEnd()) {
330 QString line = in.readLine();
331 QStringList list = line.split("=");
332 if(list.size() == 2) {
333 settings()->setValue(list.at(0),list.at(1));
336 _name = settings()->value("bookname");
341 QIcon* StarDictPlugin::icon() {
345 QString StarDictPlugin::iconPath(){
349 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)