From patchwork Tue Oct 16 15:53:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 10759587 Return-Path: Received: from mail-eopbgr710064.outbound.protection.outlook.com ([40.107.71.64]:54000 "EHLO NAM05-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727006AbeJPXoh (ORCPT ); Tue, 16 Oct 2018 19:44:37 -0400 From: Yordan Karadzhov To: "rostedt@goodmis.org" CC: "linux-trace-devel@vger.kernel.org" Subject: [PATCH v2 12/23] kernel-shark-qt: Add "File exists" dialog. Date: Tue, 16 Oct 2018 15:53:11 +0000 Message-ID: <20181016155232.5257-13-ykaradzhov@vmware.com> References: <20181016155232.5257-1-ykaradzhov@vmware.com> In-Reply-To: <20181016155232.5257-1-ykaradzhov@vmware.com> Content-Language: en-US MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org List-ID: Content-Length: 3456 A helper function for launching a "File exists" dialog is added to KsWidgetsLib. This function asks the user, before overwriting an existing file. The "File exists" dialog function is used by the KsMainWindow widget when saving the configuration of the filters and sessions. Signed-off-by: Yordan Karadzhov --- kernel-shark-qt/src/KsMainWindow.cpp | 22 +++++++-------------- kernel-shark-qt/src/KsWidgetsLib.cpp | 29 ++++++++++++++++++++++++++++ kernel-shark-qt/src/KsWidgetsLib.hpp | 7 +++++++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/kernel-shark-qt/src/KsMainWindow.cpp b/kernel-shark-qt/src/KsMainWindow.cpp index d30f752..b9fb587 100644 --- a/kernel-shark-qt/src/KsMainWindow.cpp +++ b/kernel-shark-qt/src/KsMainWindow.cpp @@ -397,20 +397,7 @@ void KsMainWindow::_exportSession() if (!fileName.endsWith(".json")) { fileName += ".json"; if (QFileInfo(fileName).exists()) { - QString msg("A file "); - QMessageBox msgBox; - - msg += fileName; - msg += " already exists."; - msgBox.setText(msg); - msgBox.setInformativeText("Do you want to replace it?"); - - msgBox.setStandardButtons(QMessageBox::Save | - QMessageBox::Cancel); - - msgBox.setDefaultButton(QMessageBox::Cancel); - - if (msgBox.exec() == QMessageBox::Cancel) + if (!KsWidgetsLib::fileExistsDialog(fileName)) return; } } @@ -461,8 +448,13 @@ void KsMainWindow::_exportFilter() if (fileName.isEmpty()) return; - if (!fileName.endsWith(".json")) + if (!fileName.endsWith(".json")) { fileName += ".json"; + if (QFileInfo(fileName).exists()) { + if (!KsWidgetsLib::fileExistsDialog(fileName)) + return; + } + } kshark_export_all_event_filters(kshark_ctx, &conf); kshark_save_config_file(fileName.toStdString().c_str(), conf); diff --git a/kernel-shark-qt/src/KsWidgetsLib.cpp b/kernel-shark-qt/src/KsWidgetsLib.cpp index f7fca09..b4b62a4 100644 --- a/kernel-shark-qt/src/KsWidgetsLib.cpp +++ b/kernel-shark-qt/src/KsWidgetsLib.cpp @@ -76,6 +76,35 @@ KsMessageDialog::KsMessageDialog(QString message, QWidget *parent) this->setLayout(&_layout); } +namespace KsWidgetsLib +{ + +/** + * @brief Launch a File exists dialog. Use this function to ask the user + * before overwriting an existing file. + * + * @param fileName: the name of the file. + * + * @returns True if the user wants to overwrite the file. Otherwise + */ +bool fileExistsDialog(QString fileName) +{ + QString msg("A file "); + QMessageBox msgBox; + + msg += fileName; + msg += " already exists."; + msgBox.setText(msg); + msgBox.setInformativeText("Do you want to replace it?"); + + msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Cancel); + + return (msgBox.exec() == QMessageBox::Save); +} + +}; // KsWidgetsLib + /** * @brief Create KsCheckBoxWidget. * diff --git a/kernel-shark-qt/src/KsWidgetsLib.hpp b/kernel-shark-qt/src/KsWidgetsLib.hpp index b9ba35a..89c196a 100644 --- a/kernel-shark-qt/src/KsWidgetsLib.hpp +++ b/kernel-shark-qt/src/KsWidgetsLib.hpp @@ -66,6 +66,13 @@ public: /** The width of the KsMessageDialog widget. */ #define KS_MSG_DIALOG_WIDTH (SCREEN_WIDTH / 10) +namespace KsWidgetsLib +{ + +bool fileExistsDialog(QString fileName); + +}; // KsWidgetsLib + /** * The KsCheckBoxWidget class is the base class of all CheckBox widget used * by KernelShark.