summaryrefslogtreecommitdiffhomepage
path: root/main-window/module-mixin.cpp
blob: 5c4c32345b4007bbbe365b3176b28bafaa8b2454 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "module-mixin.hpp"

#include <algorithm>

namespace OTR_MIXIN_NS(module_mixin) {

std::tuple<dylib_ptr, int>
module_mixin::module_by_name(const QString& name, const dylib_list& list) const
{
    auto it = std::find_if(list.cbegin(), list.cend(), [&name](const dylib_ptr& lib) {
        if (!lib)
            return name.isEmpty();
        else
            return name == lib->module_name;
    });

    if (it == list.cend())
        return { nullptr, -1 };
    else
        return { *it, int(std::distance(list.cbegin(), it)) };
}

//static
void show_window(QWidget& d, bool fresh)
{
    if (fresh)
    {
        d.setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | d.windowFlags());
        d.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

        d.show();
        d.adjustSize();
        d.raise();
    }
    else
    {
        d.show();
        d.raise();
    }
}

template<typename t, typename F>
static bool mk_window_common(std::unique_ptr<t>& d, F&& fun)
{
    bool fresh = false;

    if (!d)
        d = fun(), fresh = !!d;

    if (d)
        show_window(*d, fresh);

    return fresh;
}

template<typename t>
static bool mk_dialog(std::unique_ptr<t>& place, const std::shared_ptr<dylib>& lib)
{
    using u = std::unique_ptr<t>;

    return mk_window_common(place, [&] {
        if (lib && lib->Dialog)
            return u{ (t*)lib->Dialog() };
        else
            return u{};
    });
}

dylib_ptr module_mixin::current_tracker()
{
    auto [ptr, idx] = module_by_name(s.tracker_dll, modules.trackers());
    return ptr;
}

dylib_ptr module_mixin::current_protocol()
{
    auto [ptr, idx] = module_by_name(s.protocol_dll, modules.protocols());
    return ptr;
}

dylib_ptr module_mixin::current_filter()
{
    auto [ptr, idx] = module_by_name(s.filter_dll, modules.filters());
    return ptr;
}

void module_mixin::show_tracker_settings_()
{
    if (mk_dialog(tracker_dialog, current_tracker()) && state.work && state.work->libs.pTracker)
        tracker_dialog->register_tracker(&*state.work->libs.pTracker);
    if (tracker_dialog)
        QObject::connect(&*tracker_dialog, &ITrackerDialog::closing,
                         &fuzz, [this] { tracker_dialog = nullptr; });
}

void module_mixin::show_proto_settings_()
{
    if (mk_dialog(proto_dialog, current_protocol()) && state.work && state.work->libs.pProtocol)
        proto_dialog->register_protocol(&*state.work->libs.pProtocol);
    if (proto_dialog)
        QObject::connect(&*proto_dialog, &IProtocolDialog::closing,
                         &fuzz, [this] { proto_dialog = nullptr; });
}

void module_mixin::show_filter_settings_()
{
    if (mk_dialog(filter_dialog, current_filter()) && state.work && state.work->libs.pFilter)
        filter_dialog->register_filter(&*state.work->libs.pFilter);
    if (filter_dialog)
        QObject::connect(&*filter_dialog, &IFilterDialog::closing,
                         &fuzz, [this] { filter_dialog = nullptr; });
}

// this template function must go to a separate function like "options_mixin".
template<typename t, typename... Args>
static bool mk_window(std::unique_ptr<t>& place, Args&&... params)
{
    return mk_window_common(place, [&] {
        return std::make_unique<t>(params...);
    });
}

module_mixin::module_mixin() = default;
module_mixin::~module_mixin() = default;

} // ns