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
25 #include "StarDictPlugin.h"
27 uint qHash(const TranslationStarDict &key) {
28 return qHash(key.key());
31 StarDictPlugin::StarDictPlugin(QObject *parent) : CommonDictInterface(parent),
32 _langFrom(""), _langTo(""),_name(""), _infoNote("") {
33 _settings = new Settings();
34 _dictDialog = new StarDictDialog(this, this);
36 connect(_dictDialog, SIGNAL(notify(Notify::NotifyType,QString)),
37 this, SIGNAL(notify(Notify::NotifyType,QString)));
40 _settings->setValue("type","stardict");
41 _icon = QIcon("/usr/share/mdictionary/xdxf.png");
48 void StarDictPlugin::retranslate() {
49 QString locale = QLocale::system().name();
51 QTranslator *translator = new QTranslator(this);
53 if(!translator->load(":/xdxf/translations/" + locale)) {
54 translator->load(":/xdxf/translations/en_US");
56 QCoreApplication::installTranslator(translator);
60 StarDictPlugin::~StarDictPlugin() {
66 QString StarDictPlugin::langFrom() const {
71 QString StarDictPlugin::langTo() const {
76 QString StarDictPlugin::name() const {
81 QString StarDictPlugin::type() const {
82 return QString("stardict");
86 QString StarDictPlugin::infoNote() const {
91 QList<Translation*> StarDictPlugin::searchWordList(QString word, int limit) {
92 qDebug() << "StarDict searachWordList" << word;
93 QList<TranslationStarDict> translations;
95 if(settings()->value("idxoffsetbits") == "32" ||
96 settings()->value("idxoffsetbits") == "")
100 int wordcount = settings()->value("wordcount").toInt(&err);
102 return QList<Translation*>();
104 QString idxPath = settings()->value("idxFileName");
105 StarDictReader * reader = StarDictReaderFactory::createReader(idxPath);
107 qint64 offset = 0, len = 0;
108 QRegExp keyword(word, Qt::CaseInsensitive, QRegExp::Wildcard);
111 while(counter < wordcount) {
113 fkey = reader->readKeyword();
115 offset = reader->readInt32BigEndian();
117 offset = reader->readInt64BigEndian();
118 len = reader->readInt32BigEndian();
120 if(keyword.exactMatch(fkey)) {
121 TranslationStarDict tran(fkey, infoNote(), this);
122 int id = translations.indexOf(tran);
124 tran.add(offset, len);
125 translations.push_front(tran);
127 translations[id].add(offset, len);
132 QList<Translation*> ret;
133 QListIterator<TranslationStarDict> it(translations);
135 ret.push_back(new TranslationStarDict(it.next()));
143 QString StarDictPlugin::search(QString key) {
149 void StarDictPlugin::stop() {
154 DictDialog* StarDictPlugin::dictDialog() {
159 CommonDictInterface* StarDictPlugin::getNew(const Settings *settings) const {
160 StarDictPlugin *plugin = new StarDictPlugin();
162 connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
163 this, SIGNAL(notify(Notify::NotifyType,QString)));
165 ((StarDictDialog*)plugin->dictDialog())->
166 setLastDialogParent(_dictDialog->lastDialogParent());
170 if(settings && plugin->setSettings(settings)) {
172 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
173 this, SIGNAL(notify(Notify::NotifyType,QString)));
174 plugin->getDictionaryInfo();
178 disconnect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
179 this, SIGNAL(notify(Notify::NotifyType,QString)));
186 bool StarDictPlugin::isAvailable() const {
191 Settings* StarDictPlugin::settings() {
196 bool StarDictPlugin::isCached() {
201 bool StarDictPlugin::setSettings(const Settings *sett) {
203 foreach(QString key, sett->keys())
204 _settings->setValue(key, sett->value(key));
208 Q_EMIT settingsChanged();
213 bool StarDictPlugin::getDictionaryInfo() {
214 QFile file(settings()->value("ifoFileName"));
215 if(!QFile::exists(_settings->value("ifoFileName"))
216 || !file.open(QFile::ReadOnly | QFile::Text)) {
217 Q_EMIT notify(Notify::Warning,
218 QString(tr("StarDict dictionary cannot be read from file")));
219 qDebug()<<"Error: could not open the file";
223 QTextStream in(&file);
224 while (!in.atEnd()) {
225 QString line = in.readLine();
226 QStringList list = line.split("=");
227 if(list.size() == 2) {
228 settings()->setValue(list.at(0),list.at(1));
236 QIcon* StarDictPlugin::icon() {
241 int StarDictPlugin::countWords() {
247 void StarDictPlugin::clean() {
252 Q_EXPORT_PLUGIN2(stardict, StarDictPlugin)