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
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
) :
22 ts_ui_(new Ui::TimeShiftDialog
),
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(QString(
36 "QCheckBox#setTwoCheckBox {"
39 "QLabel#extrapolateLabel {"
44 .arg(rb_label_offset
+ cb_label_offset
)
48 if (cap_file_
->current_frame
) {
49 ts_ui_
->setOneFrameLineEdit
->setText(QString::number(cap_file_
->current_frame
->num
));
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);
61 TimeShiftDialog::~TimeShiftDialog()
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
)
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
) {
91 ts_ui_
->setOneFrameLineEdit
->syntaxState() == SyntaxLineEdit::Valid
&&
92 ts_ui_
->setOneTimeLineEdit
->syntaxState() == SyntaxLineEdit::Valid
) {
95 } else if (ts_ui_
->unshiftAllButton
->isChecked()) {
99 if (syntax_err_
.isEmpty()) {
100 ts_ui_
->errorLabel
->clear();
101 ts_ui_
->errorLabel
->setStyleSheet(" QLabel { margin-top: 0.5em; }");
103 ts_ui_
->errorLabel
->setText(syntax_err_
);
104 ts_ui_
->errorLabel
->setStyleSheet(QString(
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
)
119 unsigned frame_num
= frame_le
.text().toUInt(&frame_valid
);
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
);
127 syntax_err_
= tr("Frame numbers must be between 1 and %1.").arg(cap_file_
->count
);
129 syntax_err_
= tr("Invalid frame number.");
132 frame_le
.setSyntaxState(SyntaxLineEdit::Valid
);
136 void TimeShiftDialog::checkDateTime(SyntaxLineEdit
&time_le
)
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
);
150 time_le
.setSyntaxState(SyntaxLineEdit::Valid
);
154 void TimeShiftDialog::on_shiftAllButton_toggled(bool)
159 void TimeShiftDialog::on_setOneButton_toggled(bool)
164 void TimeShiftDialog::on_unshiftAllButton_toggled(bool)
169 void TimeShiftDialog::on_setTwoCheckBox_toggled(bool)
174 void TimeShiftDialog::on_shiftAllTimeLineEdit_textChanged(const QString
&sa_text
)
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
);
189 ts_ui_
->shiftAllTimeLineEdit
->setSyntaxState(SyntaxLineEdit::Valid
);
191 ts_ui_
->shiftAllButton
->setChecked(true);
195 void TimeShiftDialog::on_setOneFrameLineEdit_textChanged(const QString
&)
197 checkFrameNumber(*ts_ui_
->setOneFrameLineEdit
);
198 ts_ui_
->setOneButton
->setChecked(true);
201 void TimeShiftDialog::on_setOneTimeLineEdit_textChanged(const QString
&)
203 checkDateTime(*ts_ui_
->setOneTimeLineEdit
);
204 ts_ui_
->setOneButton
->setChecked(true);
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);
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);
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;
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()
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_
);
255 syntax_err_
= err_str
;
263 void TimeShiftDialog::on_buttonBox_helpRequested()
265 mainApp
->helpTopicAction(HELP_TIME_SHIFT_DIALOG
);