From 7167ce41b61d2ba2cdb526777a4233eb84a3b66a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sat, 6 Dec 2014 17:33:25 -0500 Subject: Imported Upstream version 2.99.6 --- .../plugins/populatedictionary.cpp | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 SQLiteStudio3/coreSQLiteStudio/plugins/populatedictionary.cpp (limited to 'SQLiteStudio3/coreSQLiteStudio/plugins/populatedictionary.cpp') diff --git a/SQLiteStudio3/coreSQLiteStudio/plugins/populatedictionary.cpp b/SQLiteStudio3/coreSQLiteStudio/plugins/populatedictionary.cpp new file mode 100644 index 0000000..3d78de5 --- /dev/null +++ b/SQLiteStudio3/coreSQLiteStudio/plugins/populatedictionary.cpp @@ -0,0 +1,94 @@ +#include "populatedictionary.h" +#include "services/populatemanager.h" +#include "services/notifymanager.h" +#include "common/unused.h" +#include +#include +#include + +PopulateDictionary::PopulateDictionary() +{ +} + +QString PopulateDictionary::getTitle() const +{ + return tr("Dictionary", "dictionary populating plugin name"); +} + +PopulateEngine*PopulateDictionary::createEngine() +{ + return new PopulateDictionaryEngine(); +} + +bool PopulateDictionaryEngine::beforePopulating(Db* db, const QString& table) +{ + UNUSED(db); + UNUSED(table); + QFile file(cfg.PopulateDictionary.File.get()); + if (!file.open(QIODevice::ReadOnly)) + { + notifyError(QObject::tr("Could not open dictionary file %1 for reading.").arg(cfg.PopulateDictionary.File.get())); + return false; + } + QTextStream stream(&file); + QString dataStr = stream.readAll(); + file.close(); + + if (cfg.PopulateDictionary.Lines.get()) + dictionary = dataStr.split("\n"); + else + dictionary = dataStr.split(QRegExp("\\s+")); + + if (dictionary.size() == 0) + dictionary << QString(); + + dictionaryPos = 0; + dictionarySize = dictionary.size(); + if (cfg.PopulateDictionary.Random.get()) + qsrand(QDateTime::currentDateTime().toTime_t()); + + return true; +} + +QVariant PopulateDictionaryEngine::nextValue(bool& nextValueError) +{ + UNUSED(nextValueError); + if (cfg.PopulateDictionary.Random.get()) + { + int r = qrand() % dictionarySize; + return dictionary[r]; + } + else + { + if (dictionaryPos >= dictionarySize) + dictionaryPos = 0; + + return dictionary[dictionaryPos++]; + } +} + +void PopulateDictionaryEngine::afterPopulating() +{ + dictionary.clear(); + dictionarySize = 0; + dictionaryPos = 0; +} + +CfgMain* PopulateDictionaryEngine::getConfig() +{ + return &cfg; +} + +QString PopulateDictionaryEngine::getPopulateConfigFormName() const +{ + return QStringLiteral("PopulateDictionaryConfig"); +} + +bool PopulateDictionaryEngine::validateOptions() +{ + QFileInfo fi(cfg.PopulateDictionary.File.get()); + bool fileValid = fi.exists() && fi.isReadable() && !fi.isDir(); + POPULATE_MANAGER->handleValidationFromPlugin(fileValid, cfg.PopulateDictionary.File, QObject::tr("Dictionary file must exist and be readable.")); + + return fileValid; +} -- cgit v1.2.3