summaryrefslogtreecommitdiffstats
path: root/ui/qt/preference_editor_frame.cpp
blob: eec2571ac16ede2d49737d722f4adb6876d245e0 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* preference_editor_frame.h
 *
 * 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 <epan/prefs.h>
#include <epan/prefs-int.h>
#include <epan/decode_as.h>

#include <ui/preference_utils.h>
#include <ui/simple_dialog.h>

#include "preference_editor_frame.h"
#include <ui_preference_editor_frame.h>

#include <ui/qt/utils/qt_ui_utils.h>
#include <ui/qt/widgets/wireshark_file_dialog.h>
#include <wsutil/utf8_entities.h>

#include "main_application.h"

#include <QPushButton>
#include <QKeyEvent>
#include <QRegularExpression>

PreferenceEditorFrame::PreferenceEditorFrame(QWidget *parent) :
    AccordionFrame(parent),
    ui(new Ui::PreferenceEditorFrame),
    module_(NULL),
    pref_(NULL),
    new_uint_(0),
    new_str_(""),
    new_range_(NULL)
{
    ui->setupUi(this);

#ifdef Q_OS_MAC
    foreach (QWidget *w, findChildren<QWidget *>()) {
        w->setAttribute(Qt::WA_MacSmallSize, true);
    }
#endif

    connect(ui->preferenceBrowseButton, &QPushButton::clicked, this, &PreferenceEditorFrame::browsePushButtonClicked);

    // Disconnect textChanged signal for DissectorSyntaxLineEdit.
    disconnect(ui->preferenceLineEdit, &DissectorSyntaxLineEdit::textChanged, NULL, NULL);
}

PreferenceEditorFrame::~PreferenceEditorFrame()
{
    delete ui;
}

void PreferenceEditorFrame::editPreference(preference *pref, pref_module *module)
{
    pref_ = pref;
    module_ = module;

    if (!pref || !module) {
        hide();
        return;
    }

    ui->modulePreferencesToolButton->setText(tr("Open %1 preferences…").arg(module_->title));

    pref_stash(pref_, NULL);
    ui->preferenceTitleLabel->setText(QString("%1:").arg(prefs_get_title(pref)));

    // Convert the pref description from plain text to rich text.
    QString description = html_escape(prefs_get_description(pref));
    description.replace('\n', "<br>");
    QString tooltip = QString("<span>%1</span>").arg(description);
    ui->preferenceTitleLabel->setToolTip(tooltip);
    ui->preferenceLineEdit->setToolTip(tooltip);

    ui->preferenceLineEdit->clear();
    ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty);

    // Disconnect previous textChanged signal.
    disconnect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged, this, NULL);

    bool show = false;
    bool browse_button = false;

    switch (prefs_get_type(pref_)) {
    case PREF_UINT:
        connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged,
                this, &PreferenceEditorFrame::uintLineEditTextEdited);
        show = true;
        break;
    case PREF_SAVE_FILENAME:
    case PREF_OPEN_FILENAME:
    case PREF_DIRNAME:
        browse_button = true;
        // Fallthrough
    case PREF_STRING:
    case PREF_PASSWORD:
    case PREF_DISSECTOR:
        connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged,
                this, &PreferenceEditorFrame::stringLineEditTextEdited);
        show = true;
        break;
    case PREF_RANGE:
    case PREF_DECODE_AS_RANGE:
        connect(ui->preferenceLineEdit, &SyntaxLineEdit::textChanged,
                this, &PreferenceEditorFrame::rangeLineEditTextEdited);
        show = true;
        break;
    default:
        break;
    }

    if (show) {
        // Enable completion only for display filter search.
        if (prefs_get_type(pref_) == PREF_DISSECTOR) {
            ui->preferenceLineEdit->allowCompletion(true);
            ui->preferenceLineEdit->updateDissectorNames();
            ui->preferenceLineEdit->setDefaultPlaceholderText();
        } else {
            ui->preferenceLineEdit->allowCompletion(false);
            ui->preferenceLineEdit->setPlaceholderText("");
        }

        ui->preferenceLineEdit->setText(gchar_free_to_qstring(prefs_pref_to_str(pref_, pref_stashed)).remove(QRegularExpression("\n\t")));
        ui->preferenceBrowseButton->setHidden(!browse_button);
        animatedShow();
    }
}

void PreferenceEditorFrame::uintLineEditTextEdited(const QString &new_str)
{
    if (new_str.isEmpty()) {
        new_uint_ = prefs_get_uint_value_real(pref_, pref_stashed);
        ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty);
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
        return;
    }

    bool ok;
    uint new_uint = new_str.toUInt(&ok, 0);
    if (ok) {
        new_uint_ = new_uint;
        ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid);
    } else {
        new_uint_ = prefs_get_uint_value_real(pref_, pref_stashed);
        ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid);
    }
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
}

void PreferenceEditorFrame::stringLineEditTextEdited(const QString &new_str)
{
    bool ok = true;
    new_str_ = new_str;

    if (prefs_get_type(pref_) == PREF_DISSECTOR) {
        ui->preferenceLineEdit->checkDissectorName(new_str_);
        ok = (ui->preferenceLineEdit->syntaxState() != SyntaxLineEdit::Invalid);
    }

    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok);
}

void PreferenceEditorFrame::browsePushButtonClicked()
{
    QString caption = mainApp->windowTitleString(prefs_get_title(pref_));
    QString dir = prefs_get_string_value(pref_, pref_stashed);
    QString filename;

    switch (prefs_get_type(pref_)) {
    case PREF_SAVE_FILENAME:
        filename = WiresharkFileDialog::getSaveFileName(this, caption, dir);
        break;
    case PREF_OPEN_FILENAME:
        filename = WiresharkFileDialog::getOpenFileName(this, caption, dir);
        break;
    case PREF_DIRNAME:
        filename = WiresharkFileDialog::getExistingDirectory(this, caption, dir);
        break;
    }

    if (!filename.isEmpty()) {
        ui->preferenceLineEdit->setText(filename);
    }
}

void PreferenceEditorFrame::rangeLineEditTextEdited(const QString &new_str)
{
    range_t *new_range = NULL;

    convert_ret_t ret = range_convert_str(NULL, &new_range, new_str.toUtf8().constData(), prefs_get_max_value(pref_));
    wmem_free(NULL, new_range_);
    new_range_ = new_range;

    if (ret == CVT_NO_ERROR) {
        if (new_str.isEmpty()) {
            ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Empty);
        } else {
            ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Valid);
        }
    } else {
        ui->preferenceLineEdit->setSyntaxState(SyntaxLineEdit::Invalid);
    }

    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ret == CVT_NO_ERROR);
}

void PreferenceEditorFrame::showEvent(QShowEvent *event)
{
    ui->preferenceLineEdit->setFocus();
    ui->preferenceLineEdit->selectAll();

    AccordionFrame::showEvent(event);
}

void PreferenceEditorFrame::on_modulePreferencesToolButton_clicked()
{
    if (module_) {
        emit showProtocolPreferences(module_->name);
    }
    on_buttonBox_rejected();
}

void PreferenceEditorFrame::on_preferenceLineEdit_returnPressed()
{
    if (ui->buttonBox->button(QDialogButtonBox::Ok)->isEnabled()) {
        on_buttonBox_accepted();
    }
}

void PreferenceEditorFrame::on_buttonBox_accepted()
{
    unsigned int changed_flags = 0;
    unsigned int apply = 0;
    switch(prefs_get_type(pref_)) {
    case PREF_UINT:
        apply = prefs_set_uint_value(pref_, new_uint_, pref_stashed);
        break;
    case PREF_STRING:
    case PREF_SAVE_FILENAME:
    case PREF_OPEN_FILENAME:
    case PREF_DIRNAME:
    case PREF_DISSECTOR:
        apply = prefs_set_string_value(pref_, new_str_.toStdString().c_str(), pref_stashed);
        break;
    case PREF_PASSWORD:
        apply = prefs_set_password_value(pref_, new_str_.toStdString().c_str(), pref_stashed);
        break;
    case PREF_RANGE:
    case PREF_DECODE_AS_RANGE:
        apply = prefs_set_range_value(pref_, new_range_, pref_stashed);
        break;
    default:
        break;
    }

    if (apply && module_) {
        changed_flags = module_->prefs_changed_flags;
        pref_unstash_data_t unstashed_data;

        unstashed_data.module = module_;
        unstashed_data.handle_decode_as = true;

        pref_unstash(pref_, &unstashed_data);
        prefs_apply(module_);
        prefs_main_write();

        char* err = NULL;
        if (save_decode_as_entries(&err) < 0)
        {
            simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err);
            g_free(err);
        }
    }
    on_buttonBox_rejected();
    // Emit signals once UI is hidden
    if (apply) {
        if (changed_flags & PREF_EFFECT_FIELDS) {
            mainApp->emitAppSignal(MainApplication::FieldsChanged);
        }
        mainApp->emitAppSignal(MainApplication::PacketDissectionChanged);
        mainApp->emitAppSignal(MainApplication::PreferencesChanged);
    }
}

void PreferenceEditorFrame::on_buttonBox_rejected()
{
    pref_ = NULL;
    module_ = NULL;
    wmem_free(NULL, new_range_);
    new_range_ = NULL;
    animatedHide();
}

void PreferenceEditorFrame::keyPressEvent(QKeyEvent *event)
{
    if (pref_ && module_ && (event->modifiers() == Qt::NoModifier)) {
        if (event->key() == Qt::Key_Escape) {
            on_buttonBox_rejected();
        } else if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
            if (ui->buttonBox->button(QDialogButtonBox::Ok)->isEnabled()) {
                on_buttonBox_accepted();
            } else if (ui->preferenceLineEdit->syntaxState() == SyntaxLineEdit::Invalid) {
                mainApp->pushStatus(MainApplication::FilterSyntax, tr("Invalid value."));
            }
        }
    }

    AccordionFrame::keyPressEvent(event);
}