Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / ui / qt / time_shift_dialog.cpp
blobf1b2fe045b00e358e1afcc738dd2d8ef26904669
1 /* time_shift_dialog.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "time_shift_dialog.h"
11 #include <ui_time_shift_dialog.h>
13 #include "main_application.h"
15 #include <ui/time_shift.h>
16 #include <ui/qt/utils/color_utils.h>
18 #include <QStyleOption>
20 TimeShiftDialog::TimeShiftDialog(QWidget *parent, capture_file *cf) :
21 QDialog(parent),
22 ts_ui_(new Ui::TimeShiftDialog),
23 cap_file_(cf),
24 apply_button_(NULL)
26 ts_ui_->setupUi(this);
27 setWindowTitle(mainApp->windowTitleString(tr("Time Shift")));
28 apply_button_ = ts_ui_->buttonBox->button(QDialogButtonBox::Apply);
29 apply_button_->setDefault(true);
30 connect(apply_button_, &QPushButton::clicked, this, &TimeShiftDialog::applyTimeShift);
32 QStyleOption style_opt;
33 int rb_label_offset = ts_ui_->shiftAllButton->style()->subElementRect(QStyle::SE_RadioButtonContents, &style_opt).left();
34 int cb_label_offset = ts_ui_->shiftAllButton->style()->subElementRect(QStyle::SE_CheckBoxContents, &style_opt).left();
35 setStyleSheet(QStringLiteral(
36 "QCheckBox#setTwoCheckBox {"
37 " margin-left: %1px;"
38 "}"
39 "QLabel#extrapolateLabel {"
40 " margin-left: %2px;"
41 "}"
43 .arg(rb_label_offset)
44 .arg(rb_label_offset + cb_label_offset)
47 if (cap_file_) {
48 if (cap_file_->current_frame) {
49 ts_ui_->setOneFrameLineEdit->setText(QString::number(cap_file_->current_frame->num));
50 } else {
51 ts_ui_->setOneFrameLineEdit->setText(QString::number(cap_file_->first_displayed));
53 ts_ui_->setTwoFrameLineEdit->setText(QString::number(cap_file_->last_displayed));
56 ts_ui_->shiftAllButton->setChecked(true);
57 ts_ui_->setTwoCheckBox->setChecked(false);
58 enableWidgets();
61 TimeShiftDialog::~TimeShiftDialog()
63 delete ts_ui_;
66 void TimeShiftDialog::enableWidgets()
68 bool enable_two = ts_ui_->setOneButton->isChecked();
69 bool enable_apply = false;
71 ts_ui_->setTwoCheckBox->setEnabled(enable_two);
72 ts_ui_->setTwoFrameLineEdit->setEnabled(enable_two);
73 ts_ui_->setTwoToLabel->setEnabled(enable_two);
74 ts_ui_->setTwoTimeLineEdit->setEnabled(enable_two);
75 ts_ui_->extrapolateLabel->setEnabled(enable_two && ts_ui_->setTwoCheckBox->isChecked());
77 if (ts_ui_->shiftAllButton->isChecked()) {
78 if (ts_ui_->shiftAllTimeLineEdit->syntaxState() == SyntaxLineEdit::Valid)
79 enable_apply = true;
80 } else if (ts_ui_->setOneButton->isChecked()) {
81 bool set_two_valid = false;
82 if (ts_ui_->setTwoCheckBox->isChecked()) {
83 if (ts_ui_->setTwoFrameLineEdit->syntaxState() == SyntaxLineEdit::Valid &&
84 ts_ui_->setTwoTimeLineEdit->syntaxState() == SyntaxLineEdit::Valid) {
85 set_two_valid = true;
87 } else {
88 set_two_valid = true;
90 if (set_two_valid &&
91 ts_ui_->setOneFrameLineEdit->syntaxState() == SyntaxLineEdit::Valid &&
92 ts_ui_->setOneTimeLineEdit->syntaxState() == SyntaxLineEdit::Valid) {
93 enable_apply = true;
95 } else if (ts_ui_->unshiftAllButton->isChecked()) {
96 enable_apply = true;
99 if (syntax_err_.isEmpty()) {
100 ts_ui_->errorLabel->clear();
101 ts_ui_->errorLabel->setStyleSheet(" QLabel { margin-top: 0.5em; }");
102 } else {
103 ts_ui_->errorLabel->setText(syntax_err_);
104 ts_ui_->errorLabel->setStyleSheet(QStringLiteral(
105 "QLabel {"
106 " margin-top: 0.5em;"
107 " background-color: %2;"
110 .arg(ColorUtils::warningBackground().name())
113 apply_button_->setEnabled(enable_apply);
116 void TimeShiftDialog::checkFrameNumber(SyntaxLineEdit &frame_le)
118 bool frame_valid;
119 unsigned frame_num = frame_le.text().toUInt(&frame_valid);
121 syntax_err_.clear();
122 if (frame_le.text().isEmpty()) {
123 frame_le.setSyntaxState(SyntaxLineEdit::Empty);
124 } else if (!frame_valid || !cap_file_ || frame_num < 1 || frame_num > cap_file_->count) {
125 frame_le.setSyntaxState(SyntaxLineEdit::Invalid);
126 if (cap_file_) {
127 syntax_err_ = tr("Frame numbers must be between 1 and %1.").arg(cap_file_->count);
128 } else {
129 syntax_err_ = tr("Invalid frame number.");
131 } else {
132 frame_le.setSyntaxState(SyntaxLineEdit::Valid);
136 void TimeShiftDialog::checkDateTime(SyntaxLineEdit &time_le)
138 int Y, M, D, h, m;
139 long double s;
140 const char *err_str;
142 syntax_err_.clear();
143 if (time_le.text().isEmpty()) {
144 time_le.setSyntaxState(SyntaxLineEdit::Empty);
145 } else if ((err_str = time_string_parse(time_le.text().toUtf8().constData(),
146 &Y, &M, &D, NULL, &h, &m, &s)) != NULL) {
147 syntax_err_ = err_str;
148 time_le.setSyntaxState(SyntaxLineEdit::Invalid);
149 } else {
150 time_le.setSyntaxState(SyntaxLineEdit::Valid);
154 void TimeShiftDialog::on_shiftAllButton_toggled(bool)
156 enableWidgets();
159 void TimeShiftDialog::on_setOneButton_toggled(bool)
161 enableWidgets();
164 void TimeShiftDialog::on_unshiftAllButton_toggled(bool)
166 enableWidgets();
169 void TimeShiftDialog::on_setTwoCheckBox_toggled(bool)
171 enableWidgets();
174 void TimeShiftDialog::on_shiftAllTimeLineEdit_textChanged(const QString &sa_text)
176 int h, m;
177 long double s;
178 bool neg;
179 const char *err_str;
181 syntax_err_.clear();
182 if (sa_text.isEmpty()) {
183 ts_ui_->shiftAllTimeLineEdit->setSyntaxState(SyntaxLineEdit::Empty);
184 } else if ((err_str = time_string_parse(sa_text.toUtf8().constData(),
185 NULL, NULL, NULL, &neg, &h, &m, &s)) != NULL) {
186 syntax_err_ = err_str;
187 ts_ui_->shiftAllTimeLineEdit->setSyntaxState(SyntaxLineEdit::Invalid);
188 } else {
189 ts_ui_->shiftAllTimeLineEdit->setSyntaxState(SyntaxLineEdit::Valid);
191 ts_ui_->shiftAllButton->setChecked(true);
192 enableWidgets();
195 void TimeShiftDialog::on_setOneFrameLineEdit_textChanged(const QString &)
197 checkFrameNumber(*ts_ui_->setOneFrameLineEdit);
198 ts_ui_->setOneButton->setChecked(true);
199 enableWidgets();
201 void TimeShiftDialog::on_setOneTimeLineEdit_textChanged(const QString &)
203 checkDateTime(*ts_ui_->setOneTimeLineEdit);
204 ts_ui_->setOneButton->setChecked(true);
205 enableWidgets();
208 void TimeShiftDialog::on_setTwoFrameLineEdit_textChanged(const QString &)
210 checkFrameNumber(*ts_ui_->setTwoFrameLineEdit);
211 if (ts_ui_->setTwoCheckBox->isEnabled())
212 ts_ui_->setTwoCheckBox->setChecked(true);
213 enableWidgets();
216 void TimeShiftDialog::on_setTwoTimeLineEdit_textChanged(const QString &)
218 checkDateTime(*ts_ui_->setTwoTimeLineEdit);
219 if (ts_ui_->setTwoCheckBox->isEnabled())
220 ts_ui_->setTwoCheckBox->setChecked(true);
221 enableWidgets();
224 void TimeShiftDialog::applyTimeShift()
226 const char *err_str = NULL;
228 if (!cap_file_ || cap_file_->state == FILE_CLOSED || cap_file_->state == FILE_READ_PENDING) return;
230 syntax_err_.clear();
231 if (cap_file_->state == FILE_READ_IN_PROGRESS) {
232 syntax_err_ = tr("Time shifting is not available while capturing packets.");
233 } else if (ts_ui_->shiftAllButton->isChecked()) {
234 err_str = time_shift_all(cap_file_,
235 ts_ui_->shiftAllTimeLineEdit->text().toUtf8().constData());
236 } else if (ts_ui_->setOneButton->isChecked()) {
237 if (!ts_ui_->setTwoCheckBox->isChecked()) {
238 err_str = time_shift_settime(cap_file_,
239 ts_ui_->setOneFrameLineEdit->text().toUInt(),
240 ts_ui_->setOneTimeLineEdit->text().toUtf8().constData()
242 } else {
243 err_str = time_shift_adjtime(cap_file_,
244 ts_ui_->setOneFrameLineEdit->text().toUInt(),
245 ts_ui_->setOneTimeLineEdit->text().toUtf8().constData(),
246 ts_ui_->setTwoFrameLineEdit->text().toUInt(),
247 ts_ui_->setTwoTimeLineEdit->text().toUtf8().constData()
250 } else if (ts_ui_->unshiftAllButton->isChecked()) {
251 err_str = time_shift_undo(cap_file_);
254 if (err_str) {
255 syntax_err_ = err_str;
256 } else {
257 emit timeShifted();
260 enableWidgets();
263 void TimeShiftDialog::on_buttonBox_helpRequested()
265 mainApp->helpTopicAction(HELP_TIME_SHIFT_DIALOG);