aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webprofilemanager.h
blob: 0e18d5fb6365a65e153d4b2a06b0228c4b8220f2 (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
/*
 * 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 "webprofile.h"
#include <QDir>
#include <QFile>
#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
{
    Q_OBJECT

    friend class Browser;
    friend void profileMenu(QMenu *, const std::function<void(WebProfile *)> &, WebProfile *current, bool);

public:
    explicit WebProfileManager(QObject *parent);
    ~WebProfileManager() override;

    static auto instance() -> const WebProfileManager *;
    static void setInstance(WebProfileManager *ptr);

    /** 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;

    /** Set a profile for deletion
     * param id         The profile ID
     * return void
     */
    [[deprecated]] void deleteProfile(const QString &id);

    const QStringList idList() const
    {
        return profiles.keys();
    }
    QString id(WebProfile *profile) const
    {
        QMapIterator<QString, Profile> i(profiles);
        while(i.hasNext()) {
            i.next();
            if(i.value().ptr == profile)
                return i.key();
        }
        return QString();
    }

protected:
    WebProfile *add(const QString &id, const QString &path = QString(), bool isOffTheRecord = true);

private:
    struct Profile {
        WebProfile *ptr = nullptr;
        QSettings *settings = nullptr;
        bool selfDestruct = false;
    };

    QMap<QString, Profile> profiles;
};

#endif // SMOLBOTE_PROFILEMANAGER_H