/* * 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 *s_instance = nullptr; template <> void WebProfileManager::make_global() { if(s_instance == nullptr) { s_instance = this; } } template <> WebProfileManager::~WebProfileManager() = default; template <> WebProfileManager::~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::profile(const QString &id) const { return s_instance->profile(id); } template <> QStringList WebProfileManager::idList() const { return s_instance->idList(); } template <> void WebProfileManager::walk(std::function f) const { for(auto iter = profiles.begin(); iter != profiles.end(); ++iter) { f(iter.key(), iter.value().ptr, iter.value().settings); } } template <> void WebProfileManager::walk(std::function f) const { s_instance->walk(f); } void profileMenu(QMenu *menu, const std::function &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); }); }