#include <QDebug>
void Backbone::init() {
- _searchLimit = 10;
_interval = 250; //msec
- if(!_pluginPath.size())
- _pluginPath = "/usr/lib/mdictionary/";
if(!_configPath.size())
_configPath = QDir::homePath() + "/.mdictionary";
+
+ loadPrefs();
+
+
+ if(!_pluginPath.size())
+ _pluginPath = "/usr/lib/mdictionary/";
+
loadPlugins();
- loadPreferences();
+ loadDicts();
if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
qDebug() << "Timer signal not connected";
- void Backbone::addDictionary(CommonDictInterface* dict) {
+ void Backbone::addDictionary(CommonDictInterface* dict, bool active) {
dict->setHash(_dicts.size()+1);
- _dicts[dict] = 1;
+ _dicts[dict] = active;
+ connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
}
void Backbone::removeDictionary(CommonDictInterface *dict) {
}
}
+
+
CommonDictInterface* Backbone::plugin(QString type) {
foreach(CommonDictInterface* plugin, _plugins)
if(plugin->type() == type)
return 0;
}
-void Backbone::loadPreferences() {
+
+
+void Backbone::loadPrefs() {
+ QDir confDir(_configPath);
+ if(!confDir.exists())
+ qDebug() << "Configuration file dosn't exists (" << _configPath << ")";
+ QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
+ _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
+ _historyLen = set.value("general/history_length", 10).toInt();
+ _searchLimit = set.value("general/search_limit", 15).toInt();
+}
+
+
+
+void Backbone::loadDicts() {
QDir confDir(_configPath);
if(!confDir.exists())
qDebug() << confDir.mkpath(_configPath);
QStringList items = set.childKeys();
foreach(QString item, items)
plugSet->setValue(item, set.value(item, "").toString());
+ addDictionary(plug->getNew(plugSet), set.value("active",1).toBool());
set.endGroup();
- addDictionary(plug->getNew(plugSet));
}
}
+void Backbone::dictUpdated() {
+ qDebug() << "UPDATE";
+ QDir confDir(_configPath);
+ if(!confDir.exists())
+ qDebug() << confDir.mkpath(_configPath);
+ QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
+ foreach(CommonDictInterface* dict, _dicts.keys())
+ saveState(&set, dict->settings(), _dicts[dict], dict->hash());
+}
+
+void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
+ , uint hash) {
+ QString section;
+ section.append("dictionary_%1").arg(hash);
+ QList<QString> keys = plugSet->keys();
+ foreach(QString key, keys)
+ set->setValue(section + "/" + key, plugSet->value(key));
+ set->setValue(section + "/active", active);
+
+}
+
//saving dictionaries state
//saving selection state
/*! adds new dictionary and activate it
\param dict dictionary to be added
*/
- void addDictionary(CommonDictInterface* dict);
+ void addDictionary(CommonDictInterface* dict, bool active = 1);
//! stops all current activity - emiting signal \see closeOk
*/
void removeDictionary(CommonDictInterface* dict);
+ /*! saves plugins new state/configuration after each change */
+ void dictUpdated();
+
// TODO addToBookmark(Translation*);
// TODO removeFromBookmark(Translation*);
QList<QFuture<QList<Translation*> > > _innerResult;
QMultiHash<QString, Translation*> _result;
QTimer _timer;
- int _searchLimit;
- int _activeSearchNum;
QTime _time;
- int _interval; //Search fetching timer.timeout interval in msec
QString _pluginPath;
QString _configPath;
+ int _searchLimit;
+ int _activeSearchNum;
+ int _interval; //Search fetching timer.timeout interval in msec
+ int _historyLen;
void init();
QStringList getFilesFromDir(QString dir, QStringList nameFilter);
void loadPlugins(); //< locate and load plugins
- void loadPreferences();
+ void loadPrefs();
+ void loadDicts();
+ void saveState(QSettings*, Settings*, bool, uint);
CommonDictInterface* plugin(QString type); //< search for given type plugin
//void writeConfig(QString key, QString value);
\param key
*/
QString value(const QString key) const {
- if(!settings.contains(key)) {
+ if(!_settings.contains(key)) {
return QString();
}
- return settings[key];
+ return _settings[key];
}
//! sets key to value
void setValue(const QString key, const QString value) {
- settings.insert(key, value);
+ _settings.insert(key, value);
+ }
+
+ QList<QString> keys() {
+ return _settings.keys();
}
private:
- QHash<QString, QString> settings;
+ QHash<QString, QString> _settings;
};
#endif // SETTINGS_H