diff options
Diffstat (limited to 'lib/webengine/webprofilemanager.h')
-rw-r--r-- | lib/webengine/webprofilemanager.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/lib/webengine/webprofilemanager.h b/lib/webengine/webprofilemanager.h new file mode 100644 index 0000000..e5df6d5 --- /dev/null +++ b/lib/webengine/webprofilemanager.h @@ -0,0 +1,126 @@ +/* + * 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 "webprofile.h" +#include <QDir> +#include <QFile> +#include <QFileInfo> +#include <QMap> +#include <QMenu> +#include <functional> + +#if defined(__clang__) +#define consumable(X) [[clang::consumable(X)]] +#define return_typestate(X) [[clang::return_typestate(X)]] +#define set_typestate(X) [[clang::set_typestate(X)]] +#define callable_when(X) [[clang::callable_when(X)]] +#define param_typestate(X) [[clang::param_typestate(X)]] +#else +#define consumable(X) +#define return_typestate(X) +#define set_typestate(X) +#define callable_when(X) +#define param_typestate(X) +#endif + +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 |