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 // Created by Bartosz Szatkowski
27 void Backbone::init() {
29 _interval = 250; //msec
30 if(!_pluginPath.size())
31 _pluginPath = "/usr/lib/mdictionary/";
34 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
35 qDebug() << "Timer signal not connected";
38 Backbone::Backbone(QObject *parent)
44 Backbone::Backbone(QString pluginPath, QObject *parent)
47 _pluginPath = pluginPath;
55 QListIterator<CommonDictInterface*> it(_dicts.keys());
60 it = QListIterator<CommonDictInterface*>(_plugins);
64 QHashIterator<QString, Translation*> it2(_result);
66 delete it2.next().value();
73 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
75 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
76 _plugins = QList<CommonDictInterface* > (b._plugins);
77 _result = QHash<QString, Translation* > (b._result);
78 _searchLimit = b.searchLimit();
84 int Backbone::searchLimit() const {
91 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
98 QList<CommonDictInterface* > Backbone::getPlugins() {
105 QList<QString> Backbone::getHistory() {
112 QMultiHash<QString, Translation*> Backbone::result() {
119 void Backbone::stopSearching() {
121 _innerResult.clear();
122 foreach(CommonDictInterface* dict, _dicts.keys())
129 void Backbone::search(QString word) {
132 _innerResult.clear();
134 _timer.start(_interval);
135 foreach(CommonDictInterface* dict, _dicts.keys())
136 if(_dicts[dict] == 1) {
137 QFuture<QList<Translation*> > tr =
138 QtConcurrent::run(dict,
139 &CommonDictInterface::searchWordList,word,
141 _innerResult.append(tr);
149 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
150 foreach(CommonDictInterface* dict, _dicts.keys())
151 if(activeDicts.contains(dict))
160 void Backbone::addDictionary(CommonDictInterface* dict) {
161 dict->setHash(_dicts.size()+1);
165 void Backbone::removeDictionary(CommonDictInterface *dict) {
172 void Backbone::quit() {
179 int Backbone::activeSearches() const {
180 return _innerResult.size();
185 void Backbone::translation() {
186 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
187 if(!trans.isFinished())
189 QList<Translation*> tList = trans.result();
190 foreach(Translation* t, tList)
191 _result.insert(t->key(), t);
192 _innerResult.removeOne(trans);
194 if(!_innerResult.size()) {
203 void Backbone::loadPlugins() {
204 QDir plug(QDir::toNativeSeparators(_pluginPath));
206 qDebug() << plug.absolutePath() << " folder dosen't exists";
209 QStringList nameFilter;
210 nameFilter << "*.so";
211 plug.setFilter(QDir::Files);
212 QStringList files = plug.entryList(nameFilter);
216 foreach(QString file, files) {
217 QPluginLoader loader(plug.absoluteFilePath(file));
219 qDebug()<< file << " " << loader.errorString();
222 QObject *pl = loader.instance();
224 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
225 _plugins.append(plugin);
226 addDictionary(plugin->getNew(0)); //TODO change 0 to real settings