From 6d012202186262f99a2859b7e80b77fb42436b00 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 25 Jun 2021 19:11:15 +0200
Subject: [PATCH] DOCUMENT: Add the current document for the window to help the
 save operations

---
 src/MainWindow.cc | 25 +++++++++++++++++++++++++
 src/MainWindow.hh |  2 ++
 2 files changed, 27 insertions(+)

diff --git a/src/MainWindow.cc b/src/MainWindow.cc
index ae85a01d..2f1c36ad 100644
--- a/src/MainWindow.cc
+++ b/src/MainWindow.cc
@@ -3,6 +3,7 @@
 #include "MainWindow.hh"
 #include "DialogHelp.hh"
 #include "VivyDocumentView.hh"
+#include "Utils.hh"
 
 #include <QFileInfo>
 #include <QStandardPaths>
@@ -47,11 +48,25 @@ MainWindow::openDialogHelp() noexcept
 void
 MainWindow::saveFile() noexcept
 {
+    try {
+        if (auto document = getCurrentDocument().lock()) {
+            qDebug() << "Request to save the document" << document->getName();
+        }
+    } catch (const std::runtime_error &e) {
+        qCritical() << "Failed to save current document:" << e.what();
+    }
 }
 
 void
 MainWindow::saveFileAs() noexcept
 {
+    try {
+        if (auto document = getCurrentDocument().lock()) {
+            qDebug() << "Request to save the document" << document->getName();
+        }
+    } catch (const std::runtime_error &e) {
+        qCritical() << "Failed to save current document:" << e.what();
+    }
 }
 
 void
@@ -100,3 +115,13 @@ MainWindow::openDocument() noexcept
         throw;
     }
 }
+
+std::weak_ptr<VivyDocument>
+MainWindow::getCurrentDocument() const
+{
+    VivyDocumentView *currentView = reinterpret_cast<VivyDocumentView *>(documents->currentWidget());
+    if (currentView)
+        return currentView->getDocument();
+    else
+        throw std::runtime_error("no current document");
+}
diff --git a/src/MainWindow.hh b/src/MainWindow.hh
index 3cf48823..23b5adbe 100644
--- a/src/MainWindow.hh
+++ b/src/MainWindow.hh
@@ -25,6 +25,8 @@ public:
     explicit MainWindow(VivyApplication *) noexcept;
     ~MainWindow() noexcept = default;
 
+    std::weak_ptr<VivyDocument> getCurrentDocument() const;
+
 private slots:
     void openDocument() noexcept;
     void closeDocument(int index) noexcept;
-- 
GitLab