diff options
Diffstat (limited to 'lib/webengine/webprofilemanager.cpp')
-rw-r--r-- | lib/webengine/webprofilemanager.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/webengine/webprofilemanager.cpp b/lib/webengine/webprofilemanager.cpp new file mode 100644 index 0000000..5cc83f8 --- /dev/null +++ b/lib/webengine/webprofilemanager.cpp @@ -0,0 +1,83 @@ +/* + * 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 + */ + +#include "webprofilemanager.h" +#include "webprofile.h" + +static WebProfileManager<false> *s_instance = nullptr; + +template <> +void WebProfileManager<false>::make_global() +{ + if(s_instance == nullptr) { + s_instance = this; + } +} + +template <> +WebProfileManager<true>::~WebProfileManager() = default; + +template <> +WebProfileManager<false>::~WebProfileManager() +{ + for(Profile p : qAsConst(profiles)) { + if(p.selfDestruct && p.settings != nullptr) { + if(!p.ptr->isOffTheRecord()) { + if(!p.ptr->persistentStoragePath().isEmpty()) + QDir(p.ptr->persistentStoragePath()).removeRecursively(); + if(!p.ptr->cachePath().isEmpty()) + QDir(p.ptr->cachePath()).removeRecursively(); + } + const QString filename = p.settings->fileName(); + delete p.settings; + QFile::remove(filename); + } else if(p.settings != nullptr) { + p.settings->sync(); + delete p.settings; + } + } +} + +template <> +WebProfile *WebProfileManager<true>::profile(const QString &id) const +{ + return s_instance->profile(id); +} + +template <> +QStringList WebProfileManager<true>::idList() const +{ + return s_instance->idList(); +} + +template <> +void WebProfileManager<false>::walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)> f) const +{ + for(auto iter = profiles.begin(); iter != profiles.end(); ++iter) { + f(iter.key(), iter.value().ptr, iter.value().settings); + } +} + +template <> +void WebProfileManager<true>::walk(std::function<void(const QString &id, WebProfile *profile, QSettings *settings)> f) const +{ + s_instance->walk(f); +} + +void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current, bool checkable) +{ + auto *group = new QActionGroup(menu); + QObject::connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater); + + s_instance->walk([=](const QString &, WebProfile *profile, const QSettings *) { + auto *action = menu->addAction(profile->name(), profile, [=]() { callback(profile); }); + action->setCheckable(checkable); + action->setChecked(profile == current); + group->addAction(action); + }); +} |