/* * 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 #include #include #include #include #include void profileMenu(QMenu *menu, const std::function &callback, WebProfile *current = nullptr, bool checkable = false); template 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)) { Profile profile; profile.settings = WebProfile::load(QString(), search, homepage, newtab); profile.ptr = WebProfile::load(default_id, profile.settings, true); profiles[default_id] = profile; } WebProfile::setDefaultProfile(profiles[default_id].ptr); } ~WebProfileManager(); WebProfileManager(const WebProfileManager &) = delete; WebProfileManager &operator=(const WebProfileManager &) = delete; return_typestate(unconsumed) WebProfileManager(WebProfileManager && 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{ profile, settings, 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) const; callable_when(unconsumed) void make_global(); private: set_typestate(consumed) void consume() {} struct Profile { WebProfile *ptr = nullptr; QSettings *settings = nullptr; bool selfDestruct = false; }; QMap profiles; }; #endif // SMOLBOTE_PROFILEMANAGER_H