diff mbox series

[v2,12/23] kernel-shark-qt: Add "File exists" dialog.

Message ID 20181016155232.5257-13-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Headers show
Series Add Qt-based GUI for KernelShark | expand

Commit Message

Yordan Karadzhov Oct. 16, 2018, 3:53 p.m. UTC
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 <ykaradzhov@vmware.com>
---
 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 mbox series

Patch

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.