aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/plugins/populaterandomtext.cpp
blob: d9f148a603fe8a9be6f7404a0703b948b72ed600 (plain) (blame)
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
#include "populaterandomtext.h"
#include "common/utils.h"
#include "common/unused.h"
#include "services/populatemanager.h"

PopulateRandomText::PopulateRandomText()
{
}

QString PopulateRandomText::getTitle() const
{
    return tr("Random text");
}

PopulateEngine* PopulateRandomText::createEngine()
{
    return new PopulateRandomTextEngine();
}

bool PopulateRandomTextEngine::beforePopulating(Db* db, const QString& table)
{
    UNUSED(db);
    UNUSED(table);
    qsrand(QDateTime::currentDateTime().toTime_t());
    range = cfg.PopulateRandomText.MaxLength.get() - cfg.PopulateRandomText.MinLength.get() + 1;

    chars = "";

    if (cfg.PopulateRandomText.UseCustomSets.get())
    {
        chars = cfg.PopulateRandomText.CustomCharacters.get();
    }
    else if (cfg.PopulateRandomText.IncludeBinary.get())
    {
        for (int i = 0; i < 256; i++)
            chars.append(QChar((char)i));
    }
    else
    {
        if (cfg.PopulateRandomText.IncludeAlpha.get())
            chars += QStringLiteral("abcdefghijklmnopqrstuvwxyz");

        if (cfg.PopulateRandomText.IncludeNumeric.get())
            chars += QStringLiteral("0123456789");

        if (cfg.PopulateRandomText.IncludeWhitespace.get())
            chars += QStringLiteral(" \t\n");
    }

    return !chars.isEmpty();
}

QVariant PopulateRandomTextEngine::nextValue(bool& nextValueError)
{
    UNUSED(nextValueError);
    int lgt = (qrand() % range) + cfg.PopulateRandomText.MinLength.get();
    return randStr(lgt, chars);
}

void PopulateRandomTextEngine::afterPopulating()
{
}

CfgMain* PopulateRandomTextEngine::getConfig()
{
    return &cfg;
}

QString PopulateRandomTextEngine::getPopulateConfigFormName() const
{
    return QStringLiteral("PopulateRandomTextConfig");
}

bool PopulateRandomTextEngine::validateOptions()
{
    bool rangeValid = (cfg.PopulateRandomText.MinLength.get() <= cfg.PopulateRandomText.MaxLength.get());
    POPULATE_MANAGER->handleValidationFromPlugin(rangeValid, cfg.PopulateRandomText.MaxLength, QObject::tr("Maximum length cannot be less than minimum length."));

    bool useCustom = cfg.PopulateRandomText.UseCustomSets.get();
    bool useBinary = cfg.PopulateRandomText.IncludeBinary.get();
    POPULATE_MANAGER->updateVisibilityAndEnabled(cfg.PopulateRandomText.IncludeAlpha, true, !useCustom && !useBinary);
    POPULATE_MANAGER->updateVisibilityAndEnabled(cfg.PopulateRandomText.IncludeNumeric, true, !useCustom && !useBinary);
    POPULATE_MANAGER->updateVisibilityAndEnabled(cfg.PopulateRandomText.IncludeWhitespace, true, !useCustom && !useBinary);
    POPULATE_MANAGER->updateVisibilityAndEnabled(cfg.PopulateRandomText.IncludeBinary, true, !useCustom);
    POPULATE_MANAGER->updateVisibilityAndEnabled(cfg.PopulateRandomText.CustomCharacters, true, useCustom);

    bool customValid = !useCustom || !cfg.PopulateRandomText.CustomCharacters.get().isEmpty();
    POPULATE_MANAGER->handleValidationFromPlugin(customValid, cfg.PopulateRandomText.CustomCharacters, QObject::tr("Custom character set cannot be empty."));

    return rangeValid && customValid;
}