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 _pluginPath = "/usr/lib/mdictionary/";
33 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
34 qDebug() << "Timer signal not connected";
37 Backbone::Backbone(QObject *parent)
47 QListIterator<CommonDictInterface*> it(_dicts.keys());
52 it = QListIterator<CommonDictInterface*>(_plugins);
56 QHashIterator<QString, Translation*> it2(_result);
58 delete it2.next().value();
65 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
67 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
68 _plugins = QList<CommonDictInterface* > (b._plugins);
69 _result = QHash<QString, Translation* > (b._result);
70 _searchLimit = b.searchLimit();
76 int Backbone::searchLimit() const {
83 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
90 QList<CommonDictInterface* > Backbone::getPlugins() {
97 QList<QString> Backbone::getHistory() {
104 QMultiHash<QString, Translation*> Backbone::result() {
111 void Backbone::stopSearching() {
113 _innerResult.clear();
114 foreach(CommonDictInterface* dict, _dicts.keys())
121 void Backbone::search(QString word) {
124 _innerResult.clear();
126 _timer.start(_interval);
127 foreach(CommonDictInterface* dict, _dicts.keys())
128 if(_dicts[dict] == 1) {
129 QFuture<QList<Translation*> > tr =
130 QtConcurrent::run(dict,
131 &CommonDictInterface::searchWordList,word,
133 _innerResult.append(tr);
141 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
142 foreach(CommonDictInterface* dict, _dicts.keys())
143 if(activeDicts.contains(dict))
152 void Backbone::addDictionary(CommonDictInterface* dict) {
153 dict->setHash(_dicts.size()+1);
157 void Backbone::removeDictionary(CommonDictInterface *dict) {
164 void Backbone::quit() {
171 int Backbone::activeSearches() const {
172 return _innerResult.size();
177 void Backbone::translation() {
178 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
179 if(!trans.isFinished())
181 QList<Translation*> tList = trans.result();
182 foreach(Translation* t, tList)
183 _result.insert(t->key(), t);
184 _innerResult.removeOne(trans);
186 if(!_innerResult.size()) {
195 void Backbone::loadPlugins() {
196 QDir plug(QDir::toNativeSeparators(_pluginPath));
198 qDebug() << plug.absolutePath() << " folder dosen't exists";
201 QStringList nameFilter;
202 nameFilter << "*.so";
203 plug.setFilter(QDir::Files);
204 QStringList files = plug.entryList(nameFilter);
208 foreach(QString file, files) {
209 QPluginLoader loader(plug.absoluteFilePath(file));
211 qDebug()<< file << " " << loader.errorString();
214 QObject *pl = loader.instance();
216 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
217 _plugins.append(plugin);
218 addDictionary(plugin->getNew(0)); //TODO change 0 to real settings