aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/manager/preference_manager.cpp
blob: 54c2cfb7b037fd8074c17bcb4de6f36ac790dd98 (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
/* preference_manager.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <ui/qt/manager/preference_manager.h>
#include <ui/qt/manager/wireshark_preference.h>

#include <QMetaMethod>

PreferenceFactory::~PreferenceFactory() {}

QMap<int, PreferenceFactory *> & PreferenceManager::factories()
{
    static QMap<int, PreferenceFactory *> inst = QMap<int, PreferenceFactory *>();
    return inst;
}

PreferenceManager::PreferenceManager(QObject * parent)
    : QObject(parent)
{}

PreferenceManager::~PreferenceManager()
{
    /* As this is a singleton, this is the point, where we can clear the registry */
    PreferenceManager::factories().clear();
}

PreferenceManager * PreferenceManager::instance()
{
    static PreferenceManager* _inst = 0;
    if ( ! _inst )
        _inst = new PreferenceManager();

    return _inst;
}

void PreferenceManager::registerType(int pref, PreferenceFactory * factory)
{
    Q_ASSERT(pref >= 0);

    if ( PreferenceManager::factories().contains(pref) || ! factory )
        return;

    PreferenceManager::factories()[pref] = factory;
}

WiresharkPreference * PreferenceManager::getPreference(PrefsItem * pref)
{
    if ( ! pref )
        return Q_NULLPTR;

    int key = pref->getPrefType();
    if ( ! PreferenceManager::factories().contains(key) )
        return Q_NULLPTR;

    /* All actions are parented with this manager, to clear the objects together with the manager */
    WiresharkPreference * wspref = qobject_cast<WiresharkPreference *>(PreferenceManager::factories()[key]->create(this));
    if ( wspref )
        wspref->setPrefsItem(pref);

    return wspref;
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */