aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webprofilemanager.cpp
blob: 9ae296001494f7e49456a0650d19bf53176705bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * 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"
#include <QActionGroup>

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);
    });
}