aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/studiowelcome/userpresets.cpp
blob: a643df3fdd3da1f66ac5c752ba0ab6338e6b363e (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "userpresets.h"
#include "algorithm.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

#include <coreplugin/icore.h>
#include <memory>
#include <utils/qtcassert.h>

using namespace StudioWelcome;

FileStoreIo::FileStoreIo(const QString &fileName)
    : m_file{std::make_unique<QFile>(fullFilePath(fileName))}
{}

QByteArray FileStoreIo::read() const
{
    m_file->open(QFile::ReadOnly | QFile::Text);
    QByteArray data = m_file->readAll();
    m_file->close();

    return data;
}

void FileStoreIo::write(const QByteArray &data)
{
    m_file->open(QFile::WriteOnly | QFile::Text);
    m_file->write(data);
    m_file->close();
}

QString FileStoreIo::fullFilePath(const QString &fileName) const
{
    return Core::ICore::userResourcePath(fileName).toString();
}

UserPresetsStore::UserPresetsStore(const QString &fileName, StorePolicy policy)
    : m_store{std::make_unique<FileStoreIo>(fileName)}
    , m_policy{policy}
{}

UserPresetsStore::UserPresetsStore(std::unique_ptr<StoreIo> &&fileStore,
                                   StorePolicy policy)
    : m_store{std::move(fileStore)}
    , m_policy{policy}
{}

void UserPresetsStore::savePresets(const std::vector<UserPresetData> &presetItems)
{
    QJsonArray jsonArray;

    for (const auto &preset : presetItems) {
        QJsonObject obj({{"categoryId", preset.categoryId},
                         {"wizardName", preset.wizardName},
                         {"name", preset.name},
                         {"screenSize", preset.screenSize},
                         {"useQtVirtualKeyboard", preset.useQtVirtualKeyboard},
                         {"qtVersion", preset.qtVersion},
                         {"styleName", preset.styleName}});

        jsonArray.append(QJsonValue{obj});
    }

    QJsonDocument doc(jsonArray);
    QByteArray data = doc.toJson();

    m_store->write(data);
}

bool UserPresetsStore::save(const UserPresetData &newPreset)
{
    QTC_ASSERT(newPreset.isValid(), return false);

    std::vector<UserPresetData> presetItems = fetchAll();

    if (m_policy == StorePolicy::UniqueNames) {
        if (Utils::anyOf(presetItems, [&newPreset](const UserPresetData &p) {
                return p.name == newPreset.name;
            })) {
            return false;
        }
    } else if (m_policy == StorePolicy::UniqueValues) {
        if (Utils::containsItem(presetItems, newPreset))
            return false;
    }

    if (m_reverse)
        Utils::prepend(presetItems, newPreset);
    else
        presetItems.push_back(newPreset);

    if (m_maximum > -1 && static_cast<int>(presetItems.size()) > m_maximum) {
        if (m_reverse)
            presetItems.pop_back();
        else
            presetItems.erase(std::cbegin(presetItems));
    }

    savePresets(presetItems);

    return true;
}

void UserPresetsStore::remove(const QString &category, const QString &name)
{
    std::vector<UserPresetData> presetItems = fetchAll();
    auto item = Utils::take(presetItems, [&](const UserPresetData &p) {
        return p.categoryId == category && p.name == name;
    });

    if (!item)
        return;

    savePresets(presetItems);
}

std::vector<UserPresetData> UserPresetsStore::remove(const UserPresetData &preset)
{
    std::vector<UserPresetData> presetItems = fetchAll();
    bool erased = Utils::erase_one(presetItems, preset);
    if (erased)
        savePresets(presetItems);

    return presetItems;
}

std::vector<UserPresetData> UserPresetsStore::fetchAll() const
{
    QByteArray data = m_store->read();

    const QJsonDocument doc = QJsonDocument::fromJson(data);
    if (!doc.isArray())
        return {};

    std::vector<UserPresetData> result;
    const QJsonArray jsonArray = doc.array();

    for (const QJsonValue &value: jsonArray) {
        if (!value.isObject())
            continue;

        const QJsonObject obj = value.toObject();
        UserPresetData preset;

        preset.categoryId = obj["categoryId"].toString();
        preset.wizardName = obj["wizardName"].toString();
        preset.name = obj["name"].toString();
        preset.screenSize = obj["screenSize"].toString();
        preset.useQtVirtualKeyboard = obj["useQtVirtualKeyboard"].toBool();
        preset.qtVersion = obj["qtVersion"].toString();
        preset.styleName = obj["styleName"].toString();

        if (preset.isValid())
            result.push_back(preset);
    }

    return result;
}