#include <QFile>
#include <QString>
+#include <QDebug>
/*! ConfigGenerator generatesconfiguration files (with default values),
if given file dosent exists.
-
-For now only css is generateed in this way, but iam planign to refactor all
-configuration files to work that way.
*/
class ConfigGenerator {
return true; if file dosent exists but cannot be generated returns false
*/
bool generateCss(QString file);
+
+
+ /*! Generates default configuration files
+ \param file file to be generated
+ \return if file was generated - returns true; if file alredy exists
+ return true; if file dosent exists but cannot be generated returns false
+ */
+ bool generateDefaultConfig(QString file);
+
+private:
+ bool generateFile(QString source, QString file);
};
#endif // CONFIGGENERATOR_H
void Backbone::init() {
+ _dictNum = 0;
_dir = QDir::homePath() + "/.mdictionary/";
if(!_configPath.size())
_configPath = _dir + "mdictionary.config";
- if(!_defaultConfigPath.size())
- _defaultConfigPath = _dir + "mdictionary.defaults";
if(!_pluginPath.size())
_pluginPath = "/usr/lib/mdictionary";
- _historyLen = 10;
- _searchLimit = 15;
//Install default config files
ConfigGenerator confGen;
confGen.generateCss(_dir + "style.css");
+ confGen.generateDefaultConfig(_configPath);
-
-
- loadPrefs(_defaultConfigPath);
-
- // Default configuration are stored in separate config file and we dont want
- // to update it
- _defaultPluginPath = _pluginPath;
- _defaultHistoryLen = _historyLen;
- _defaultSearchLimit = _searchLimit;
loadPrefs(_configPath);
loadPlugins();
- loadDicts(_defaultConfigPath, true);
loadDicts(_configPath);
connect(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
QThreadPool::globalInstance()->setMaxThreadCount(
QThreadPool::globalInstance()->maxThreadCount()+1);
- _history = new History(5, this);
- _dictNum = 0;
+ _history = new History(_historyLen, this);
}
_pluginPath = pluginPath;
_configPath = configPath;
- _defaultConfigPath = configPath;
dryRun = false;
if(dry)
dryRun = true;
Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
+ init();
_dicts = QHash<CommonDictInterface*, bool > (b._dicts);
_plugins = QList<CommonDictInterface* > (b._plugins);
_result = QHash<QString, Translation* > (b._result);
SIGNAL(notify(Notify::NotifyType,QString)));
}
+
+
void Backbone::removeDictionary(CommonDictInterface *dict) {
_dicts.remove(dict);
delete dict;
}
}
+
+
+
QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
QDir plug(QDir::toNativeSeparators(dir));
if(!plug.exists()) {
Q_EMIT notify(Notify::Error,
QString("%1 plugin cannot be loaded: %2.")
.arg(file).arg(loader.errorString()));
- qDebug()<< file << " " << loader.errorString();
continue;
}
QObject *pl = loader.instance();
-void Backbone::saveDefaultPrefs(QSettings *set) {
- if(dryRun)
- return;
- set->setValue("general/plugin_path", _defaultPluginPath);
- set->setValue("general/history_size", _defaultHistoryLen);
- set->setValue("general/search_limit", _defaultSearchLimit);
-}
-
-
-
-void Backbone::loadDicts(QString fileName, bool _default) {
+void Backbone::loadDicts(QString fileName) {
if(dryRun)
return;
}
bool active = set.value("active",1).toBool();
- if(_default)
- plugSet->setValue("_default_", "true");
-
set.endGroup();
addInternalDictionary(plug->getNew(plugSet), active);
}
QSettings set(file.filePath(), QSettings::IniFormat);
set.clear();
- QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
- QDir defConfDir(defFile.dir());
- if(!defConfDir.exists())
- defConfDir.mkpath(defFile.dir().path());
- QSettings defSet(defFile.filePath(), QSettings::IniFormat);
- defSet.clear();
savePrefs(&set);
- saveDefaultPrefs(&defSet);
foreach(CommonDictInterface* dict, _dicts.keys()){
if(!dict || !dict->settings())
continue;
- if(!dict->settings()->keys().contains("_default_"))
- saveState(&set, dict->settings(), _dicts[dict], dict->hash());
- else
- saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
+ saveState(&set, dict->settings(), _dicts[dict], dict->hash());
}
}
QFutureWatcher<QString> _htmlResultWatcher;
- QString _pluginPath, _defaultPluginPath;
+ QString _pluginPath;
QString _configPath;
- QString _defaultConfigPath;
QString _dir;
- int _defaultSearchLimit;
- int _historyLen, _defaultHistoryLen;
+ int _historyLen;
bool dryRun; // mainly for testing - when true then doesn't bother configs etc
bool stopped; // true when user stops searching/fetching
QStringList getFilesFromDir(QString dir, QStringList nameFilter);
void loadPlugins(); //< locate and load plugins
void loadPrefs(QString fileName);
- void loadDicts(QString fileName, bool _default=false);
+ void loadDicts(QString fileName);
void saveState(QSettings*, Settings*, bool, uint);
void addInternalDictionary(CommonDictInterface*, bool);
void savePrefs(QSettings*);
- void saveDefaultPrefs(QSettings*);
CommonDictInterface* plugin(QString type); // search for given type plugin
QList<CommonDictInterface*> activeDicts();