// Created by Bartosz Szatkowski
#include "backbone.h"
+#include <QDebug>
+
+void Backbone::init() {
+ _searchLimit = 10;
+ _interval = 250; //msec
+ _pluginPath = "/usr/lib/mdictionary/";
+ loadPlugins();
+
+ if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
+ qDebug() << "Timer signal not connected";
+}
Backbone::Backbone(QObject *parent)
: QObject(parent)
{
- searchLimitv = 10;
+ init();
}
Backbone::~Backbone()
{
- QListIterator<CommonDictInterface*> it(dicts.keys());
+ QListIterator<CommonDictInterface*> it(_dicts.keys());
while(it.hasNext())
delete it.next();
- it = QListIterator<CommonDictInterface*>(plugins);
+ it = QListIterator<CommonDictInterface*>(_plugins);
while(it.hasNext())
delete it.next();
Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
- dicts = QHash<CommonDictInterface*, bool > (b.dicts);
- plugins = QList<CommonDictInterface* > (b.plugins);
+ init();
+ _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
+ _plugins = QList<CommonDictInterface* > (b._plugins);
_result = QHash<QString, Translation* > (b._result);
- searchLimitv = b.searchLimit();
+ _searchLimit = b.searchLimit();
}
int Backbone::searchLimit() const {
- return searchLimitv;
+ return _searchLimit;
}
QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
- return dicts;
+ return _dicts;
}
QList<CommonDictInterface* > Backbone::getPlugins() {
- return plugins;
+ return _plugins;
}
void Backbone::stopSearching() {
- foreach(CommonDictInterface* dict, dicts.keys())
+ _timer.stop();
+ _innerResult.clear();
+ foreach(CommonDictInterface* dict, _dicts.keys())
dict->stop();
}
void Backbone::search(QString word) {
- //TODO add running searches in new threads
+ _timer.stop();
_result.clear();
- activeSearchNum = 0;
- foreach(CommonDictInterface* dict, dicts.keys())
- if(dicts[dict] == 1) {
- activeSearchNum ++;
+ _innerResult.clear();
+
+ _timer.start(_interval);
+ foreach(CommonDictInterface* dict, _dicts.keys())
+ if(_dicts[dict] == 1) {
+ QFuture<QList<Translation*> > tr =
+ QtConcurrent::run(dict,
+ &CommonDictInterface::searchWordList,word,
+ searchLimit());
+ _innerResult.append(tr);
}
- foreach(CommonDictInterface* dict, dicts.keys())
- if(dicts[dict] == 1) {
- dict->search(word, searchLimit());
- }
}
void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
- foreach(CommonDictInterface* dict, dicts.keys())
+ foreach(CommonDictInterface* dict, _dicts.keys())
if(activeDicts.contains(dict))
- dicts[dict] = 1;
+ _dicts[dict] = 1;
else
- dicts[dict] = 0;
+ _dicts[dict] = 0;
}
void Backbone::addDictionary(CommonDictInterface* dict) {
- dicts[dict] = 1;
- connect(dict, SIGNAL(finalTranslation(QList<Translation*>)),
- this, SLOT(translation(QList<Translation*>)),
- Qt::UniqueConnection);
+ dict->setHash(_dicts.size()+1);
+ _dicts[dict] = 1;
+ }
+
+ void Backbone::removeDictionary(CommonDictInterface *dict) {
+ _dicts.remove(dict);
+
}
int Backbone::activeSearches() const {
- return activeSearchNum;
+ return _innerResult.size();
}
-void Backbone::translation(QList<Translation *> trans) {
- activeSearchNum--;
- foreach(Translation* t, trans)
- _result.insert(t->key(), t);
-
- if(activeSearchNum < 1)
+void Backbone::translation() {
+ foreach(QFuture<QList<Translation*> > trans, _innerResult) {
+ if(!trans.isFinished())
+ continue;
+ QList<Translation*> tList = trans.result();
+ foreach(Translation* t, tList)
+ _result.insert(t->key(), t);
+ _innerResult.removeOne(trans);
+ }
+ if(!_innerResult.size()) {
+ _timer.stop();
Q_EMIT ready();
+ }
}
void Backbone::loadPlugins() {
- QObject *pl = QPluginLoader("xdxf.so").instance();
- if(!pl)
+ QDir plug(QDir::toNativeSeparators(_pluginPath));
+ if(!plug.exists()) {
+ qDebug() << plug.absolutePath() << " folder dosen't exists";
return;
- CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
- plugins.append(plugin);
- addDictionary(plugin);
+ }
+ QStringList nameFilter;
+ nameFilter << "*.so";
+ plug.setFilter(QDir::Files);
+ QStringList files = plug.entryList(nameFilter);
+ qDebug() << files;
+
+
+ foreach(QString file, files) {
+ QPluginLoader loader(plug.absoluteFilePath(file));
+ if(!loader.load()) {
+ qDebug()<< file << " " << loader.errorString();
+ continue;
+ }
+ QObject *pl = loader.instance();
+
+ CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
+ _plugins.append(plugin);
+ addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
+ }
}