From e28f85cbb11c3a31dee469011fa939cc4ab4bbd9 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 27 Jun 2021 18:32:53 +0200
Subject: [PATCH] DOCUMENT: The newDocument method in the document store
 handles the name

The new document's name is handled internally, if the user want to
consult it he should the returned weak pointer.
---
 src/Document/VivyDocumentStore.cc | 12 ++----------
 src/Document/VivyDocumentStore.hh |  4 +++-
 src/MainWindow.cc                 | 13 +++++++------
 3 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/src/Document/VivyDocumentStore.cc b/src/Document/VivyDocumentStore.cc
index d7f6946c..9cdbcaa8 100644
--- a/src/Document/VivyDocumentStore.cc
+++ b/src/Document/VivyDocumentStore.cc
@@ -31,19 +31,11 @@ VivyDocumentStore::isDocumentPresent(const QString &name) noexcept
 }
 
 
-#define NEW_DOCUMENT_BASENAME "Untitled "
 std::weak_ptr<VivyDocument>
-VivyDocumentStore::newDocument(QString &name)
+VivyDocumentStore::newDocument()
 {
-    uint docNumber{1};
-    QString newDocName;
+    QString newDocName = newDocumentBaseName + QString::number(newDocumentNumber++);
 
-    do {
-        newDocName = NEW_DOCUMENT_BASENAME + QString::number(docNumber);
-        docNumber++;
-    } while (documents.contains(newDocName));
-
-    name = newDocName;
     if (auto ret = std::make_shared<VivyDocument>(newDocName)) {
         qDebug() << "Create new document " << newDocName;
         documents[newDocName] = ret;
diff --git a/src/Document/VivyDocumentStore.hh b/src/Document/VivyDocumentStore.hh
index e4495ab6..3781f7ed 100644
--- a/src/Document/VivyDocumentStore.hh
+++ b/src/Document/VivyDocumentStore.hh
@@ -20,7 +20,7 @@ public:
 
     /* Create/load documents */
     std::weak_ptr<VivyDocument> loadDocument(const QString &file);
-    std::weak_ptr<VivyDocument> newDocument(QString &name);
+    std::weak_ptr<VivyDocument> newDocument();
 
     /* Get to see if a document is already present or not */
     [[nodiscard("handle-it")]] bool isDocumentPresent(const QString &name) noexcept;
@@ -34,6 +34,8 @@ public:
 
 private:
     QMap<QString, std::shared_ptr<VivyDocument>> documents;
+    uint newDocumentNumber{1};
+    static inline const QString newDocumentBaseName = "Untitled ";
 };
 
 #endif // VIVY_DOCUMENTSTORE_H
diff --git a/src/MainWindow.cc b/src/MainWindow.cc
index 85c062f6..de27dce6 100644
--- a/src/MainWindow.cc
+++ b/src/MainWindow.cc
@@ -89,16 +89,17 @@ MainWindow::closeDocument(int index) noexcept
 void
 MainWindow::newDocument() noexcept
 {
-    QString baseName;
-    std::weak_ptr<VivyDocument> document = vivyApp->documentStore.newDocument(baseName);
+    std::weak_ptr<VivyDocument> document = vivyApp->documentStore.newDocument();
 
     try {
         VivyDocumentView *documentView = new VivyDocumentView(document);
-        qDebug() << "View constructed successfully";
         documents->addTab(documentView, document.lock()->getName());
+        qDebug() << "View constructed successfully";
     } catch (const std::runtime_error &e) {
-        qCritical() << "Failed to create the document view for the new document " << baseName;
-        throw;
+        qCritical() << "Failed to create a new empty document";
+        if (auto locked = document.lock()) {
+            vivyApp->documentStore.closeDocument(locked->getName());
+        }
     }
 }
 
@@ -128,7 +129,7 @@ MainWindow::openDocument() noexcept
         documents->addTab(documentView, document.lock()->getName());
     } catch (const std::runtime_error &e) {
         qCritical() << "Failed to create the document view for" << baseName << "with path" << filename;
-        throw std::runtime_error("failed to create a document view");
+        vivyApp->documentStore.closeDocument(baseName);
     }
 }
 
-- 
GitLab