From b60127ec0a27db5d3ed7a9b73615837fbc69b07e Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 26 Aug 2021 09:29:02 +0200
Subject: [PATCH] LIB: Re-add the rename operation, now can have save, rename
 and copy on documents

---
 src/Lib/AbstractDocument.hh      |  5 +--
 src/Lib/Document/VivyDocument.cc | 52 +++++++++++++++++++++++++-------
 src/Lib/Document/VivyDocument.hh |  4 +++
 src/Lib/Script/ScriptDocument.cc |  8 +++++
 src/Lib/Script/ScriptDocument.hh |  1 +
 5 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/src/Lib/AbstractDocument.hh b/src/Lib/AbstractDocument.hh
index 2ac84928..0703f5c2 100644
--- a/src/Lib/AbstractDocument.hh
+++ b/src/Lib/AbstractDocument.hh
@@ -76,8 +76,9 @@ protected:
     const Uuid uuid{};
 
 public:
-    virtual void copy(const QString &) = 0;
-    virtual void save()                = 0;
+    virtual void copy(const QString &)   = 0;
+    virtual void rename(const QString &) = 0;
+    virtual void save()                  = 0;
 
     QString getName() const noexcept { return name; }
     Type getType() const noexcept { return type; }
diff --git a/src/Lib/Document/VivyDocument.cc b/src/Lib/Document/VivyDocument.cc
index 32c97625..0bc030af 100644
--- a/src/Lib/Document/VivyDocument.cc
+++ b/src/Lib/Document/VivyDocument.cc
@@ -233,19 +233,11 @@ VivyDocument::detectDocumentType(const QFileInfo &file, Capabilities *ableType)
 void
 VivyDocument::copy(const QString &newName)
 {
-    const QString newNameWithExtension =
-        (newName.right(fileSuffix.size()) == fileSuffix) ? newName : newName + "." + fileSuffix;
-    const QFileInfo newPath(documentLocation, newNameWithExtension);
+    const QFileInfo newPath = computeFileInfo(newName);
 
     // Rename + create the disk on disk as it's a memory document
-    if (documentOptions & MemoryDocumentCreation) {
-        documentName     = newPath.baseName();
-        documentLocation = newPath.absoluteDir();
-        documentOptions  = static_cast<Options>(documentOptions & (~MemoryDocumentCreation));
-        qDebug().nospace() << "Renaming a memory file => create it on disk with { "
-                           << documentLocation << ", " << documentName << " }";
-        save();
-    }
+    if (documentOptions & MemoryDocumentCreation)
+        return saveMemoryFile(newPath);
 
     // Compute new paths, the document is really on disk initially
     else {
@@ -257,6 +249,44 @@ VivyDocument::copy(const QString &newName)
     }
 }
 
+void
+VivyDocument::rename(const QString &newName)
+{
+    const QFileInfo newPath = computeFileInfo(newName);
+
+    // Rename + create the disk on disk as it's a memory document
+    if (documentOptions & MemoryDocumentCreation)
+        return saveMemoryFile(newPath);
+
+    // Compute new paths, the document is really on disk initially
+    else {
+        qDebug() << "Renaming a real file";
+        renameWith(newPath, [=, this]() noexcept -> void {
+            documentLocation = newPath.dir();
+            documentName     = newPath.baseName();
+        });
+    }
+}
+
+QFileInfo
+VivyDocument::computeFileInfo(const QString &newName) const noexcept
+{
+    const QString newNameWithExtension =
+        (newName.right(fileSuffix.size()) == fileSuffix) ? newName : newName + "." + fileSuffix;
+    return QFileInfo(documentLocation, newNameWithExtension);
+}
+
+void
+VivyDocument::saveMemoryFile(const QFileInfo &newPath)
+{
+    documentName     = newPath.baseName();
+    documentLocation = newPath.absoluteDir();
+    documentOptions  = static_cast<Options>(documentOptions & (~MemoryDocumentCreation));
+    qDebug().nospace() << "Renaming a memory file => create it on disk with { " << documentLocation
+                       << ", " << documentName << " }";
+    save();
+}
+
 std::shared_ptr<AudioSubDocument>
 VivyDocument::getAudioSubDocument() const noexcept
 {
diff --git a/src/Lib/Document/VivyDocument.hh b/src/Lib/Document/VivyDocument.hh
index e5841422..48086b84 100644
--- a/src/Lib/Document/VivyDocument.hh
+++ b/src/Lib/Document/VivyDocument.hh
@@ -68,6 +68,9 @@ private:
     static bool detectDocumentType(const QFileInfo &file, Capabilities *) noexcept;
 
     void addDocumentType(Capabilities) noexcept;
+    void saveMemoryFile(const QFileInfo &);
+    QFileInfo computeFileInfo(const QString &) const noexcept;
+
     QJsonDocument getSaveJsonDocumentFile() const;
     void loadSaveJsonDocumentFile(const QJsonDocument &);
 
@@ -81,6 +84,7 @@ public:
     explicit VivyDocument(const QString &name, Options opt = NoOption);
 
     void copy(const QString &) override;
+    void rename(const QString &) override;
     void save() override;
     bool loadSubDocument(const QString &) noexcept;
     bool loadSubDocument(const QString &, Capabilities) noexcept;
diff --git a/src/Lib/Script/ScriptDocument.cc b/src/Lib/Script/ScriptDocument.cc
index 89e019db..715451b6 100644
--- a/src/Lib/Script/ScriptDocument.cc
+++ b/src/Lib/Script/ScriptDocument.cc
@@ -22,6 +22,14 @@ ScriptDocument::copy(const QString &newName)
     copyWith(newPath, [=, this]() noexcept { name = newName; });
 }
 
+void
+ScriptDocument::rename(const QString &newName)
+{
+    /* Compute new paths */
+    const QFileInfo newPath(newName);
+    renameWith(newPath, [=, this]() noexcept { name = newName; });
+}
+
 void
 ScriptDocument::save()
 {
diff --git a/src/Lib/Script/ScriptDocument.hh b/src/Lib/Script/ScriptDocument.hh
index 8242e8fd..3799669c 100644
--- a/src/Lib/Script/ScriptDocument.hh
+++ b/src/Lib/Script/ScriptDocument.hh
@@ -16,6 +16,7 @@ public:
     explicit ScriptDocument(const QString &name);
 
     void copy(const QString &) override;
+    void rename(const QString &) override;
     void save() override;
 
     void attachTextDocument(const QTextDocument *const) noexcept;
-- 
GitLab