diff options
Diffstat (limited to 'src/webengine/webprofilemanager.h')
-rw-r--r-- | src/webengine/webprofilemanager.h | 102 |
1 files changed, 68 insertions, 34 deletions
diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h index 0e18d5f..22fb31c 100644 --- a/src/webengine/webprofilemanager.h +++ b/src/webengine/webprofilemanager.h @@ -9,63 +9,97 @@ #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 <QObject> -#include <QSettings> #include <functional> void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false); -class Browser; -class WebProfileManager : public QObject +template <bool use_global = true> +class consumable(unconsumed) WebProfileManager { - Q_OBJECT - - friend class Browser; - friend void profileMenu(QMenu *, const std::function<void(WebProfile *)> &, WebProfile *current, bool); - public: - explicit WebProfileManager(QObject *parent); - ~WebProfileManager() override; + 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; + } - static auto instance() -> const WebProfileManager *; - static void setInstance(WebProfileManager *ptr); + 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(); - /** Create a profile with specified id - * param id The profile ID - * return WebProfile* The profile, or nullptr if one could not be created - */ - WebProfile *profile(const QString &id) const; + WebProfileManager(const WebProfileManager &) = delete; + WebProfileManager &operator=(const WebProfileManager &) = delete; - /** Set a profile for deletion - * param id The profile ID - * return void - */ - [[deprecated]] void deleteProfile(const QString &id); + 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; - const QStringList idList() const + callable_when(unconsumed) [[nodiscard]] WebProfile *profile(const QString &id) const { - return profiles.keys(); + 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 }; + } } - QString id(WebProfile *profile) const + + callable_when(unconsumed) void deleteProfile(const QString &id) { - QMapIterator<QString, Profile> i(profiles); - while(i.hasNext()) { - i.next(); - if(i.value().ptr == profile) - return i.key(); + if constexpr(use_global) { + return; + } + + if(profiles.contains(id)) { + profiles[id].selfDestruct = true; } - return QString(); } -protected: - WebProfile *add(const QString &id, const QString &path = QString(), bool isOffTheRecord = true); + callable_when(unconsumed) [[nodiscard]] QStringList idList() const + { + return profiles.keys(); + } + + callable_when(unconsumed) void walk(std::function<void(const QString &id, WebProfile *profile, const QSettings *settings)>) const; + + callable_when(unconsumed) void make_global(); private: + set_typestate(consumed) void consume() {} + struct Profile { WebProfile *ptr = nullptr; QSettings *settings = nullptr; |