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
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#ifndef SMOLBOTE_WEBPROFILEMANAGER_H
#define SMOLBOTE_WEBPROFILEMANAGER_H
#include "singleton.hpp"
#include "webprofile.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMap>
#include <QMenu>
#include <functional>
void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false);
template <bool use_global = true>
class consumable(unconsumed) WebProfileManager
{
public:
return_typestate(unconsumed) WebProfileManager()
{
static_assert(use_global);
}
return_typestate(unconsumed) WebProfileManager(const QStringList &paths, const QString &default_id,
const QString &search = QString(), const QUrl &homepage = QUrl(), const QUrl &newtab = QUrl())
{
static_assert(!use_global);
for(const auto &path : paths) {
const auto id = QFileInfo(path).baseName();
Profile profile;
profile.settings = WebProfile::load(path, search, homepage, newtab);
profile.ptr = WebProfile::load(id, profile.settings, true);
profiles[id] = profile;
}
if(!profiles.contains(default_id)) {
auto *settings = WebProfile::load(QString(), search, homepage, newtab);
profiles[default_id] = Profile{
.settings = settings,
.ptr = WebProfile::load(default_id, settings, true),
};
}
WebProfile::setDefaultProfile(profiles[default_id].ptr);
}
~WebProfileManager();
WebProfileManager(const WebProfileManager &) = delete;
WebProfileManager &operator=(const WebProfileManager &) = delete;
return_typestate(unconsumed) WebProfileManager(WebProfileManager<false> && other param_typestate(unconsumed))
{
static_assert(!use_global);
profiles = std::move(other.profiles);
other.consume();
}
WebProfileManager &operator=(WebProfileManager &&) = delete;
callable_when(unconsumed) [[nodiscard]] WebProfile *profile(const QString &id) const
{
return profiles.value(id).ptr;
}
callable_when(unconsumed) void add(const QString &id, WebProfile *profile, QSettings *settings)
{
if constexpr(use_global) {
return;
}
if(profile != nullptr && settings != nullptr) {
profiles[id] = Profile{ settings, profile, false };
}
}
callable_when(unconsumed) void deleteProfile(const QString &id)
{
if constexpr(use_global) {
return;
}
if(profiles.contains(id)) {
profiles[id].selfDestruct = true;
}
}
callable_when(unconsumed) [[nodiscard]] QStringList idList() const
{
return profiles.keys();
}
callable_when(unconsumed) void walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)>) const;
callable_when(unconsumed) void make_global();
private:
set_typestate(consumed) void consume() {}
struct Profile {
QSettings *settings = nullptr;
WebProfile *ptr = nullptr;
bool selfDestruct = false;
};
QMap<QString, Profile> profiles;
};
#endif // SMOLBOTE_PROFILEMANAGER_H
|