Qt: Refactor testCaptureFileClose

Cleanup arguments and simplify code for button texts.

Change-Id: Ie505650889212082e088a525f4b82e62b9177b0d
Reviewed-on: https://code.wireshark.org/review/13180
Petri-Dish: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
This commit is contained in:
Stig Bjørlykke 2016-01-11 10:46:02 +01:00
parent d6ca6cbe74
commit da798683de
3 changed files with 39 additions and 28 deletions

View File

@ -739,7 +739,7 @@ void MainWindow::closeEvent(QCloseEvent *event) {
}
QString before_what(tr(" before quitting"));
if (!testCaptureFileClose(TRUE, before_what)) {
if (!testCaptureFileClose(before_what, QuitButtons)) {
event->ignore();
return;
}
@ -1072,7 +1072,7 @@ void MainWindow::importCaptureFile() {
ImportTextDialog import_dlg;
QString before_what(tr(" before importing a new capture"));
if (!testCaptureFileClose(FALSE, before_what))
if (!testCaptureFileClose(before_what))
return;
import_dlg.exec();
@ -1505,7 +1505,7 @@ void MainWindow::fileAddExtension(QString &file_name, int file_type, bool compre
}
}
bool MainWindow::testCaptureFileClose(bool from_quit, QString before_what, bool restart) {
bool MainWindow::testCaptureFileClose(QString before_what, FileCloseButtons buttons) {
bool capture_in_progress = false;
bool do_close_file = false;
@ -1579,38 +1579,43 @@ bool MainWindow::testCaptureFileClose(bool from_quit, QString before_what, bool
msg_dialog.addButton(QMessageBox::Cancel);
if (capture_in_progress) {
if (restart) {
saveButton = msg_dialog.addButton(tr("Save before Continue"), QMessageBox::AcceptRole);
QString saveButtonText;
if (buttons == RestartButtons) {
saveButtonText = tr("Save before Continue");
} else {
saveButton = msg_dialog.addButton(tr("Stop and Save"), QMessageBox::AcceptRole);
saveButtonText = tr("Stop and Save");
}
saveButton = msg_dialog.addButton(saveButtonText, QMessageBox::AcceptRole);
} else {
saveButton = msg_dialog.addButton(QMessageBox::Save);
}
msg_dialog.setDefaultButton(saveButton);
if (from_quit) {
if (capture_file_.capFile()->state == FILE_READ_IN_PROGRESS) {
discardButton = msg_dialog.addButton(tr("Stop and Quit without Saving"),
QMessageBox::DestructiveRole);
} else {
discardButton = msg_dialog.addButton(tr("Quit without Saving"),
QMessageBox::DestructiveRole);
QString discardButtonText;
if (capture_in_progress) {
switch (buttons) {
case QuitButtons:
discardButtonText = tr("Stop and Quit without Saving");
break;
case RestartButtons:
discardButtonText = tr("Continue without Saving");
break;
default:
discardButtonText = tr("Stop and Continue without Saving");
break;
}
} else {
if (capture_in_progress) {
if (restart) {
discardButton = msg_dialog.addButton(tr("Continue without Saving"),
QMessageBox::DestructiveRole);
}
else {
discardButton = msg_dialog.addButton(tr("Stop and Continue without Saving"),
QMessageBox::DestructiveRole);
}
} else {
discardButton = msg_dialog.addButton(tr("Continue &without Saving"), QMessageBox::DestructiveRole);
switch (buttons) {
case QuitButtons:
discardButtonText = tr("Quit without Saving");
break;
case RestartButtons:
default:
discardButtonText = tr("Continue without Saving");
break;
}
}
discardButton = msg_dialog.addButton(discardButtonText, QMessageBox::DestructiveRole);
msg_dialog.exec();
/* According to the Qt doc:

View File

@ -120,6 +120,12 @@ private:
CopySelectedValue
};
enum FileCloseButtons {
DefaultButtons,
QuitButtons,
RestartButtons
};
Ui::MainWindow *main_ui_;
QMenu *open_recent_menu_;
QSplitter master_split_;
@ -179,7 +185,7 @@ private:
void exportDissections(export_type_e export_type);
void fileAddExtension(QString &file_name, int file_type, bool compressed);
bool testCaptureFileClose(bool from_quit = false, QString before_what = QString(), bool restart = false);
bool testCaptureFileClose(QString before_what = QString(), FileCloseButtons buttons = DefaultButtons);
void captureStop();
void initMainToolbarIcons();

View File

@ -202,7 +202,7 @@ bool MainWindow::openCaptureFile(QString cf_path, QString read_filter, unsigned
}
}
if (!testCaptureFileClose(false)) {
if (!testCaptureFileClose()) {
return false;
}
@ -3532,7 +3532,7 @@ void MainWindow::on_actionCaptureStart_triggered()
/* XXX - will closing this remove a temporary file? */
QString before_what(tr(" before starting a new capture"));
if (testCaptureFileClose(FALSE, before_what)) {
if (testCaptureFileClose(before_what)) {
startCapture();
} else {
// simply clicking the button sets it to 'checked' even though we've
@ -3550,7 +3550,7 @@ void MainWindow::on_actionCaptureStop_triggered()
void MainWindow::on_actionCaptureRestart_triggered()
{
QString before_what(tr(" before restarting a new capture"));
if (!testCaptureFileClose(false, before_what, true))
if (!testCaptureFileClose(before_what, RestartButtons))
return;
/* TODO: GTK use only this: capture_restart(&cap_session_); */