From aee5770a4974df4c532dd4b87261018cb442ee46 Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Sun, 24 Jul 2022 16:55:08 +0200
Subject: [PATCH] MISC: Properly keep SubAss key on save even if have a
 internal ass

---
 src/Lib/Document/CRTPSubDocument.hh           |  4 +++-
 .../CRTPSubDocument/AssSubDocument.cc         |  1 +
 src/Lib/Document/VivyDocument.cc              | 20 ++++++-------------
 src/Lib/Document/VivyDocument.hh              |  2 +-
 4 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/src/Lib/Document/CRTPSubDocument.hh b/src/Lib/Document/CRTPSubDocument.hh
index 96a2607b..169c1096 100644
--- a/src/Lib/Document/CRTPSubDocument.hh
+++ b/src/Lib/Document/CRTPSubDocument.hh
@@ -130,10 +130,12 @@ class AssSubDocument final : public CRTPSubDocument<AssDocumentType, AssSubDocum
     const QStringList &suffixList           = Vivy::Utils::assFileSuffix;
 
 public:
-    static std::unique_ptr<AssSubDocument> fromInternal(const QJsonValue &internal) noexcept
+    static std::unique_ptr<AssSubDocument> fromInternal(const QJsonValue &internal,
+                                                        const QString &baseAss) noexcept
     {
         auto ret = std::unique_ptr<AssSubDocument>(new AssSubDocument());
         ret->logDebug() << "Init ASS subdocument from internal";
+        ret->filePath = baseAss;
 
         try {
             ret->initFromInternal(internal); // May throw
diff --git a/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc b/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
index 96366e4a..1cd2d3cd 100644
--- a/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
+++ b/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
@@ -18,6 +18,7 @@ AssSubDocument::initFromPath(const QString &path)
 void
 AssSubDocument::initFromInternal(const QJsonValue &internal)
 {
+    internalAss = true;
     styles.clear();
     lines.clear();
     assFactory = new Ass::AssFactory(internal);
diff --git a/src/Lib/Document/VivyDocument.cc b/src/Lib/Document/VivyDocument.cc
index 8dc4064c..b04f3e41 100644
--- a/src/Lib/Document/VivyDocument.cc
+++ b/src/Lib/Document/VivyDocument.cc
@@ -80,18 +80,8 @@ VivyDocument::loadSaveJsonDocumentFile_ALPHA(VivyDocument *const self, const QJs
         !video.isUndefined() && !self->loadSubDocument(video.toString(), Capabilities::VideoAble))
         throw std::runtime_error("Failed to load video sub document");
 
-    if (QJsonValue internalAssSource = json[KeySubDocuments][KeyInternalAssSource];
-        !internalAssSource.isUndefined() && internalAssSource.toBool()) {
-        // ASS is inside Vivy document
-        throw std::runtime_error("The internal ASS feature is not supported for now");
-    } else if (QJsonValue ass = json[KeySubDocuments][KeySubAss]; !ass.isUndefined()) {
-        // ASS in its own ASS file
-        if (!self->loadSubDocument(ass.toString(), Capabilities::AssAble))
-            throw std::runtime_error("Failed to load ASS sub document");
-    }
-
     /*
-     * TODO: for now, if there is an internal ass we use it and ignore the SubAss file
+     * TODO: for now, if there is an internal ass we use it and "ignore" the SubAss file
      * In the future, both should be loaded to allow the user to switch between the 2
      * (e.g. a "reference" ass and the vivy ass)
      */
@@ -100,7 +90,9 @@ VivyDocument::loadSaveJsonDocumentFile_ALPHA(VivyDocument *const self, const QJs
         // ASS is inside Vivy document
         if (QJsonValue internalAssSource = json[KeySubDocuments][KeyInternalAssSource];
             !internalAssSource.isUndefined() && internalAssSource.isArray()) {
-            if (!self->loadSubDocument(internalAssSource, Capabilities::AssAble)) {
+            if (!self->loadSubDocument(internalAssSource,
+                                       json[KeySubDocuments][KeySubAss].toString(),
+                                       Capabilities::AssAble)) {
                 throw std::runtime_error("Failed to load ASS sub document");
             }
         }
@@ -223,7 +215,7 @@ VivyDocument::loadSubDocument(const QString &subName, VivyDocument::Capabilities
 }
 
 bool
-VivyDocument::loadSubDocument(const QJsonValue &internalAss,
+VivyDocument::loadSubDocument(const QJsonValue &internalAss, const QString &baseAss,
                               VivyDocument::Capabilities asType) noexcept
 {
     logDebug() << "ASS sub-doc: Trying to open file internal ASS";
@@ -234,7 +226,7 @@ VivyDocument::loadSubDocument(const QJsonValue &internalAss,
             assDocument.reset();
         }
 
-        assDocument = AssSubDocument::fromInternal(internalAss);
+        assDocument = AssSubDocument::fromInternal(internalAss, baseAss);
         if (assDocument)
             addDocumentType(AssAble);
     }
diff --git a/src/Lib/Document/VivyDocument.hh b/src/Lib/Document/VivyDocument.hh
index e2f4d25b..4582bf0c 100644
--- a/src/Lib/Document/VivyDocument.hh
+++ b/src/Lib/Document/VivyDocument.hh
@@ -83,7 +83,7 @@ public:
     void save() override;
     bool loadSubDocument(const QString &) noexcept;
     bool loadSubDocument(const QString &, Capabilities) noexcept;
-    bool loadSubDocument(const QJsonValue &, VivyDocument::Capabilities) noexcept;
+    bool loadSubDocument(const QJsonValue &, const QString &, VivyDocument::Capabilities) noexcept;
 
     // Getters
     std::shared_ptr<AudioSubDocument> getAudioSubDocument() const noexcept;
-- 
GitLab