From 75efed5ec488015ad26588412752dab36ace8627 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Tue, 14 Jun 2016 18:54:13 +0530 Subject: [PATCH] Add extension while saving streams and sessions, if user doesn't specify one. Fixes #187 --- client/mainwindow.cpp | 17 +++++++++++++++++ client/portswindow.cpp | 21 ++++++++++++++++++--- common/streamfileformat.cpp | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index b7032d2..4d15b68 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -220,11 +220,28 @@ void MainWindow::on_actionSaveSession_triggered() if (fileTypes.size()) fileType = fileTypes.at(0); +_retry: fileName = QFileDialog::getSaveFileName(this, tr("Save Session"), fileName, fileTypes.join(";;"), &fileType, options); if (fileName.isEmpty()) goto _exit; + if (QFileInfo(fileName).suffix().isEmpty()) { + QString fileExt = fileType.section(QRegExp("[\\*\\)]"), 1, 1); + qDebug("Adding extension '%s' to '%s'", + qPrintable(fileExt), qPrintable(fileName)); + fileName.append(fileExt); + if (QFileInfo(fileName).exists()) { + if (QMessageBox::warning(this, tr("Overwrite File?"), + QString("The file \"%1\" already exists.\n\n" + "Do you wish to overwrite it?") + .arg(QFileInfo(fileName).fileName()), + QMessageBox::Yes|QMessageBox::No, + QMessageBox::No) != QMessageBox::Yes) + goto _retry; + } + } + if (!saveSession(fileName, fileType, errorStr)) QMessageBox::critical(this, qApp->applicationName(), errorStr); else if (!errorStr.isEmpty()) diff --git a/client/portswindow.cpp b/client/portswindow.cpp index cabbb37..76f7ee7 100644 --- a/client/portswindow.cpp +++ b/client/portswindow.cpp @@ -895,9 +895,8 @@ void PortsWindow::on_actionSave_Streams_triggered() current = proxyPortModel->mapToSource(current); // On Mac OS with Native Dialog, getSaveFileName() ignores fileType - // which we need.On some Linux distros the native dialog can't - // distinguish between Ostinato(*) and PCAP(*) -#if defined(Q_OS_MAC) || defined(Q_OS_UNIX) + // which we need +#if defined(Q_OS_MAC) options |= QFileDialog::DontUseNativeDialog; #endif @@ -912,6 +911,22 @@ _retry: if (fileName.isEmpty()) goto _exit; + if (QFileInfo(fileName).suffix().isEmpty()) { + QString fileExt = fileType.section(QRegExp("[\\*\\)]"), 1, 1); + qDebug("Adding extension '%s' to '%s'", + qPrintable(fileExt), qPrintable(fileName)); + fileName.append(fileExt); + if (QFileInfo(fileName).exists()) { + if (QMessageBox::warning(this, tr("Overwrite File?"), + QString("The file \"%1\" already exists.\n\n" + "Do you wish to overwrite it?") + .arg(QFileInfo(fileName).fileName()), + QMessageBox::Yes|QMessageBox::No, + QMessageBox::No) != QMessageBox::Yes) + goto _retry; + } + } + fileType = fileType.remove(QRegExp("\\(.*\\)")).trimmed(); if (!fileType.startsWith("Ostinato") && !fileType.startsWith("Python")) diff --git a/common/streamfileformat.cpp b/common/streamfileformat.cpp index d00dda6..4e79ff7 100644 --- a/common/streamfileformat.cpp +++ b/common/streamfileformat.cpp @@ -51,7 +51,7 @@ QStringList StreamFileFormat::supportedFileTypes(Operation op) fileTypes << "Ostinato (*.ostm)" - << "PCAP (*)" + << "PCAP (*.pcap)" << "PDML (*.pdml)"; if (op == kSaveFile)