aboutsummaryrefslogtreecommitdiff
path: root/lib/webprofile/webprofilemanager.h
blob: 3d86def807ddb2885a248d664529f2a87ae35e2e (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
85
/*
 * 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>

struct ProfileDefault_t
{
    QString search;
    QString homepage;
    QString newtab;
};

class WebProfileManager : public QObject
{
    Q_OBJECT
public:
    explicit WebProfileManager(const ProfileDefault_t &profileSection, QObject *parent);
    ~WebProfileManager();

    /** Create a profile with specified id
     * param id         The profile ID
     * param path       The path to the profile settings
     * param isOffTheRecord     Off-the-record toggle
     * return WebProfile* The profile, or nullptr if one could not be created
     */
    WebProfile *profile(const QString &id, const QString &path = QString(), bool isOffTheRecord = true);

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

    void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false) const;

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

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

        QVariant value(const QString &key, const QVariant &defaultValue) const
        {
            if(settings == nullptr)
                return defaultValue;
            else
                return settings->value(key, defaultValue);
        }
    };

    QMap<QString, Profile> profiles;
    const ProfileDefault_t defaults;
};

#endif // SMOLBOTE_PROFILEMANAGER_H