sw/source/uibase/app/docsh.cxx |   65 ++++++++++++++++++++++++++---------------
 1 file changed, 42 insertions(+), 23 deletions(-)

New commits:
commit 0a0c10c0502906bebf9ea8c732d63809d5080dd6
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Fri Apr 17 14:13:26 2020 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Fri Apr 17 15:11:14 2020 +0200

    tdf#131621: fix crash in mergedlo.dll
    
    When the embedded database comes from the template this document was
    created from, the document might not have old URL yet. We need to
    construct the embedded data source URL from the template URL to keep
    the relative references correct.
    
    Change-Id: I5607113e0ae88fdb825d8a645c0968d2fe22ebd7
    Co-authored-by: Julien Nabet <serval2...@yahoo.fr>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/92398
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sw/source/uibase/app/docsh.cxx b/sw/source/uibase/app/docsh.cxx
index ad31952acc8c..d6d4dd480b64 100644
--- a/sw/source/uibase/app/docsh.cxx
+++ b/sw/source/uibase/app/docsh.cxx
@@ -436,32 +436,51 @@ bool SwDocShell::SaveAs( SfxMedium& rMedium )
         // We have an embedded data source definition, need to re-store it,
         // otherwise relative references will break when the new file is in a
         // different directory.
-        uno::Reference<sdb::XDatabaseContext> xDatabaseContext = 
sdb::DatabaseContext::create(comphelper::getProcessComponentContext());
-
-        const INetURLObject& rOldURLObject = GetMedium()->GetURLObject();
-        auto xContext(comphelper::getProcessComponentContext());
-        auto xUri = css::uri::UriReferenceFactory::create(xContext)
-            
->parse(rOldURLObject.GetMainURL(INetURLObject::DecodeMechanism::NONE));
-        assert(xUri.is());
-        xUri = 
css::uri::VndSunStarPkgUrlReferenceFactory::create(xContext)->createVndSunStarPkgUrlReference(xUri);
-        assert(xUri.is());
-        OUString const aURL = xUri->getUriReference() + "/"
-            + INetURLObject::encode(pMgr->getEmbeddedName(),
-                INetURLObject::PART_FPATH, 
INetURLObject::EncodeMechanism::All);
-
-        bool bCopyTo = GetCreateMode() == SfxObjectCreateMode::EMBEDDED;
-        if (!bCopyTo)
+
+        OUString 
aURL(GetMedium()->GetURLObject().GetMainURL(INetURLObject::DecodeMechanism::NONE));
+        if (aURL.isEmpty())
         {
-            if (const SfxBoolItem* pSaveToItem
-                = SfxItemSet::GetItem(rMedium.GetItemSet(), SID_SAVETO, false))
-                bCopyTo = pSaveToItem->GetValue();
+            // No old URL - is this a new document created from a template 
with embedded DS?
+            // Try to get the template URL to reconstruct the embedded data 
source URL
+            const css::beans::PropertyValues& rArgs = GetMedium()->GetArgs();
+            const auto aURLIter = std::find_if(rArgs.begin(), rArgs.end(),
+                                               [](const auto& v) { return 
v.Name == "URL"; });
+            if (aURLIter != rArgs.end())
+                aURLIter->Value >>= aURL;
         }
 
-        uno::Reference<sdb::XDocumentDataSource> 
xDataSource(xDatabaseContext->getByName(aURL), uno::UNO_QUERY);
-        uno::Reference<frame::XStorable> 
xStorable(xDataSource->getDatabaseDocument(), uno::UNO_QUERY);
-        SwDBManager::StoreEmbeddedDataSource(xStorable, 
rMedium.GetOutputStorage(),
-                                             pMgr->getEmbeddedName(),
-                                             rMedium.GetName(), bCopyTo);
+        if (!aURL.isEmpty())
+        {
+            auto xContext(comphelper::getProcessComponentContext());
+            auto xUri = 
css::uri::UriReferenceFactory::create(xContext)->parse(aURL);
+            assert(xUri.is());
+            xUri = css::uri::VndSunStarPkgUrlReferenceFactory::create(xContext)
+                       ->createVndSunStarPkgUrlReference(xUri);
+            assert(xUri.is());
+            aURL = xUri->getUriReference() + "/"
+                   + INetURLObject::encode(pMgr->getEmbeddedName(), 
INetURLObject::PART_FPATH,
+                                           
INetURLObject::EncodeMechanism::All);
+
+            bool bCopyTo = GetCreateMode() == SfxObjectCreateMode::EMBEDDED;
+            if (!bCopyTo)
+            {
+                if (const SfxBoolItem* pSaveToItem
+                    = SfxItemSet::GetItem(rMedium.GetItemSet(), SID_SAVETO, 
false))
+                    bCopyTo = pSaveToItem->GetValue();
+            }
+
+            auto xDatabaseContext = sdb::DatabaseContext::create(xContext);
+            uno::Reference<sdb::XDocumentDataSource> 
xDataSource(xDatabaseContext->getByName(aURL),
+                                                                 
uno::UNO_QUERY);
+            if (xDataSource)
+            {
+                uno::Reference<frame::XStorable> 
xStorable(xDataSource->getDatabaseDocument(),
+                                                           uno::UNO_QUERY);
+                SwDBManager::StoreEmbeddedDataSource(xStorable, 
rMedium.GetOutputStorage(),
+                                                     pMgr->getEmbeddedName(), 
rMedium.GetName(),
+                                                     bCopyTo);
+            }
+        }
     }
 
     // #i62875#
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to