1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include "populatedictionary.h"
#include "services/populatemanager.h"
#include "services/notifymanager.h"
#include "common/unused.h"
#include <QFileInfo>
#include <QFile>
#include <QTextStream>
#include <QRandomGenerator>
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(QRegExp("(\r\n|\n|\r)"));
else
dictionary = dataStr.split(QRegExp("\\s+"));
if (dictionary.size() == 0)
dictionary << QString();
dictionaryPos = 0;
dictionarySize = dictionary.size();
if (cfg.PopulateDictionary.Random.get())
QRandomGenerator::system()->seed(QDateTime::currentDateTime().toTime_t());
return true;
}
QVariant PopulateDictionaryEngine::nextValue(bool& nextValueError)
{
UNUSED(nextValueError);
if (cfg.PopulateDictionary.Random.get())
{
int r = QRandomGenerator::system()->generate() % 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;
}
|