[Libreoffice-commits] core.git: 2 commits - sw/inc sw/source

Miklos Vajna vmiklos at collabora.co.uk
Wed Jun 3 10:29:34 PDT 2015


 sw/inc/dbmgr.hxx                             |   10 ++++++
 sw/inc/swdbdata.hxx                          |    6 +---
 sw/source/core/doc/doc.cxx                   |    9 ------
 sw/source/core/doc/docfmt.cxx                |    1 
 sw/source/core/doc/docnew.cxx                |    5 +++
 sw/source/uibase/dbui/dbmgr.cxx              |   39 ++++++++++++++++++++++++---
 sw/source/uibase/uno/SwXDocumentSettings.cxx |   10 +++---
 7 files changed, 57 insertions(+), 23 deletions(-)

New commits:
commit d611b2e0b2b713ffaf8b2ec75e6a74180316f50b
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Wed Jun 3 19:11:57 2015 +0200

    sw: add SwDBManager::RevokeDataSource()
    
    And call it from the SwDoc dtor, so that embedded data source
    definitions registered on opening are deregistered on close.
    
    Change-Id: I40309389d44dcbc225f6f2ef98bb20579e12d650

diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx
index e397e57..657e957 100644
--- a/sw/inc/dbmgr.hxx
+++ b/sw/inc/dbmgr.hxx
@@ -376,6 +376,9 @@ public:
     /// Load the embedded data source of the document and also register it.
     void LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell);
 
+    /// Unregister a data source.
+    static void RevokeDataSource(const OUString& rName);
+
     /** try to get the data source from the given connection through the XChild interface.
         If this is not possible, the data source will be created through its name.
         @param _xConnection
diff --git a/sw/source/core/doc/docnew.cxx b/sw/source/core/doc/docnew.cxx
index ec44cb5..302ebef 100644
--- a/sw/source/core/doc/docnew.cxx
+++ b/sw/source/core/doc/docnew.cxx
@@ -540,6 +540,11 @@ SwDoc::~SwDoc()
     mpCharFormatTable->erase( mpCharFormatTable->begin() );
 
 #if HAVE_FEATURE_DBCONNECTIVITY
+    // On load, SwDBManager::setEmbeddedName() may register a data source.
+    // If we have an embedded one, then sDataSoure points to the registered name, so revoke it here.
+    if (!mpDBManager->getEmbeddedName().isEmpty() && !maDBData.sDataSource.isEmpty())
+        SwDBManager::RevokeDataSource(maDBData.sDataSource);
+
     DELETEZ( mpDBManager );
 #endif
 
diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx
index b138730..d59d78b 100644
--- a/sw/source/uibase/dbui/dbmgr.cxx
+++ b/sw/source/uibase/dbui/dbmgr.cxx
@@ -2663,6 +2663,13 @@ OUString SwDBManager::LoadAndRegisterDataSource(const OUString &rURI, const OUSt
     return LoadAndRegisterDataSource( type, aURLAny, pSettings, rURI, pPrefix, pDestDir );
 }
 
+void SwDBManager::RevokeDataSource(const OUString& rName)
+{
+    uno::Reference<sdb::XDatabaseContext> xDatabaseContext = sdb::DatabaseContext::create(comphelper::getProcessComponentContext());
+    if (xDatabaseContext->hasByName(rName))
+        xDatabaseContext->revokeObject(rName);
+}
+
 void SwDBManager::LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell)
 {
     uno::Reference<sdb::XDatabaseContext> xDatabaseContext = sdb::DatabaseContext::create(comphelper::getProcessComponentContext());
@@ -2673,8 +2680,7 @@ void SwDBManager::LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const
     if (sDataSource.isEmpty())
         sDataSource = "EmbeddedDatabase";
 
-    if (xDatabaseContext->hasByName(rData.sDataSource))
-        xDatabaseContext->revokeObject(rData.sDataSource);
+    SwDBManager::RevokeDataSource(rData.sDataSource);
 
     // Encode the stream name and the real path into a single URL.
     const INetURLObject& rURLObject = rDocShell.GetMedium()->GetURLObject();
commit b79017f49a487a3981b1cd9f488ed5062eeb880b
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Wed Jun 3 18:54:08 2015 +0200

    sw: move name of the embedded data source def from SwDBData to SwDBManager
    
    The problem is that SwDBData just describes the current data source,
    and when changing that, the embedded name doesn't change, so they should
    be separate.
    
    Change-Id: I2dd7e0a97b9f7d6a3a74a5752467515a0a09171a

diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx
index 27c86a7..e397e57 100644
--- a/sw/inc/dbmgr.hxx
+++ b/sw/inc/dbmgr.hxx
@@ -199,6 +199,8 @@ friend class SwConnectionDisposedListener_Impl;
     SwDSParamArr        aDataSourceParams;
     SwDBManager_Impl*    pImpl;
     const SwXMailMerge* pMergeEvtSrc;   ///< != 0 if mail merge events are to be send
+    /// Name of the embedded database that's included in the current document.
+    OUString     m_sEmbeddedName;
 
     SAL_DLLPRIVATE SwDSParam*          FindDSData(const SwDBData& rData, bool bCreate);
     SAL_DLLPRIVATE SwDSParam*          FindDSConnection(const OUString& rSource, bool bCreate);
@@ -372,7 +374,7 @@ public:
                                                          const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet > *pSettings = 0);
 
     /// Load the embedded data source of the document and also register it.
-    static void LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell);
+    void LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const SwDocShell& rDocShell);
 
     /** try to get the data source from the given connection through the XChild interface.
         If this is not possible, the data source will be created through its name.
@@ -405,6 +407,9 @@ public:
                             sal_Int32 _nCommandType,
                             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection
                             );
+
+    void setEmbeddedName(const OUString& rEmbeddedName, SwDocShell& rDocShell);
+    OUString getEmbeddedName() const;
 };
 
 #endif
diff --git a/sw/inc/swdbdata.hxx b/sw/inc/swdbdata.hxx
index 02ccf68..6d91e2b 100644
--- a/sw/inc/swdbdata.hxx
+++ b/sw/inc/swdbdata.hxx
@@ -28,16 +28,14 @@ struct SwDBData
     OUString     sDataSource;
     OUString     sCommand;       //table, query or statement
     sal_Int32           nCommandType; //com::sun::star::sdb::CommandType
-    /// Name of the embedded database that's included in the current document.
-    OUString     sEmbeddedName;
     SwDBData() :
         nCommandType(0){}
     void dumpAsXml(struct _xmlTextWriter* pWriter) const;
 
     bool operator !=(const SwDBData& rCmp) const
-        {return rCmp.sDataSource != sDataSource || rCmp.sCommand != sCommand || rCmp.nCommandType != nCommandType || rCmp.sEmbeddedName != sEmbeddedName;}
+        {return rCmp.sDataSource != sDataSource || rCmp.sCommand != sCommand || rCmp.nCommandType != nCommandType;}
     bool operator ==(const SwDBData& rCmp) const
-        {return rCmp.sDataSource == sDataSource && rCmp.sCommand == sCommand && rCmp.nCommandType == nCommandType && rCmp.sEmbeddedName == sEmbeddedName;}
+        {return rCmp.sDataSource == sDataSource && rCmp.sCommand == sCommand && rCmp.nCommandType == nCommandType;}
 };
 
 #endif
diff --git a/sw/source/core/doc/doc.cxx b/sw/source/core/doc/doc.cxx
index b427c93..fb43e0c 100644
--- a/sw/source/core/doc/doc.cxx
+++ b/sw/source/core/doc/doc.cxx
@@ -494,15 +494,6 @@ void SwDoc::ChgDBData(const SwDBData& rNewData)
 {
     if( rNewData != maDBData )
     {
-        if (maDBData.sEmbeddedName != rNewData.sEmbeddedName && GetDocShell())
-        {
-            uno::Reference<embed::XStorage> xStorage = GetDocShell()->GetStorage();
-            // It's OK that we don't have the named sub-storage yet, in case
-            // we're in the process of creating it.
-            if (xStorage->hasByName(rNewData.sEmbeddedName))
-                SwDBManager::LoadAndRegisterEmbeddedDataSource(rNewData, *GetDocShell());
-        }
-
         maDBData = rNewData;
         getIDocumentState().SetModified();
     }
diff --git a/sw/source/core/doc/docfmt.cxx b/sw/source/core/doc/docfmt.cxx
index bb1ea87..4acd8e8 100644
--- a/sw/source/core/doc/docfmt.cxx
+++ b/sw/source/core/doc/docfmt.cxx
@@ -1958,7 +1958,6 @@ void SwDBData::dumpAsXml(xmlTextWriterPtr pWriter) const
     xmlTextWriterWriteAttribute(pWriter, BAD_CAST("sDataSource"), BAD_CAST(sDataSource.toUtf8().getStr()));
     xmlTextWriterWriteAttribute(pWriter, BAD_CAST("sCommand"), BAD_CAST(sCommand.toUtf8().getStr()));
     xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nCommandType"), BAD_CAST(OString::number(nCommandType).getStr()));
-    xmlTextWriterWriteAttribute(pWriter, BAD_CAST("sEmbeddedName"), BAD_CAST(sEmbeddedName.toUtf8().getStr()));
 
     xmlTextWriterEndElement(pWriter);
 }
diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx
index fd57164..b138730 100644
--- a/sw/source/uibase/dbui/dbmgr.cxx
+++ b/sw/source/uibase/dbui/dbmgr.cxx
@@ -2667,6 +2667,12 @@ void SwDBManager::LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const
 {
     uno::Reference<sdb::XDatabaseContext> xDatabaseContext = sdb::DatabaseContext::create(comphelper::getProcessComponentContext());
 
+    OUString sDataSource = rData.sDataSource;
+
+    // Fallback, just in case the document would contain an embedded data source, but no DB fields.
+    if (sDataSource.isEmpty())
+        sDataSource = "EmbeddedDatabase";
+
     if (xDatabaseContext->hasByName(rData.sDataSource))
         xDatabaseContext->revokeObject(rData.sDataSource);
 
@@ -2674,7 +2680,7 @@ void SwDBManager::LoadAndRegisterEmbeddedDataSource(const SwDBData& rData, const
     const INetURLObject& rURLObject = rDocShell.GetMedium()->GetURLObject();
     OUString aURL = "vnd.sun.star.pkg://";
     aURL += INetURLObject::encode(rURLObject.GetMainURL(INetURLObject::DECODE_WITH_CHARSET), INetURLObject::PART_AUTHORITY, INetURLObject::ENCODE_ALL);
-    aURL += "/" + INetURLObject::encode(rData.sEmbeddedName, INetURLObject::PART_FPATH, INetURLObject::ENCODE_ALL);
+    aURL += "/" + INetURLObject::encode(m_sEmbeddedName, INetURLObject::PART_FPATH, INetURLObject::ENCODE_ALL);
 
     uno::Reference<uno::XInterface> xDataSource(xDatabaseContext->getByName(aURL), uno::UNO_QUERY);
     xDatabaseContext->registerObject(rData.sDataSource, xDataSource);
@@ -2966,6 +2972,27 @@ uno::Reference<XResultSet> SwDBManager::createCursor(const OUString& _sDataSourc
     return xResultSet;
 }
 
+void SwDBManager::setEmbeddedName(const OUString& rEmbeddedName, SwDocShell& rDocShell)
+{
+    bool bLoad = m_sEmbeddedName != rEmbeddedName && !rEmbeddedName.isEmpty();
+
+    m_sEmbeddedName = rEmbeddedName;
+
+    if (bLoad)
+    {
+        uno::Reference<embed::XStorage> xStorage = rDocShell.GetStorage();
+        // It's OK that we don't have the named sub-storage yet, in case
+        // we're in the process of creating it.
+        if (xStorage->hasByName(rEmbeddedName))
+            LoadAndRegisterEmbeddedDataSource(rDocShell.GetDoc()->GetDBData(), rDocShell);
+    }
+}
+
+OUString SwDBManager::getEmbeddedName() const
+{
+    return m_sEmbeddedName;
+}
+
 SwConnectionDisposedListener_Impl::SwConnectionDisposedListener_Impl(SwDBManager& rManager)
     : m_pDBManager(&rManager)
 {
diff --git a/sw/source/uibase/uno/SwXDocumentSettings.cxx b/sw/source/uibase/uno/SwXDocumentSettings.cxx
index 7d513d4..78ad21e 100644
--- a/sw/source/uibase/uno/SwXDocumentSettings.cxx
+++ b/sw/source/uibase/uno/SwXDocumentSettings.cxx
@@ -46,6 +46,7 @@
 #include "swmodule.hxx"
 #include "cfgitems.hxx"
 #include "prtopt.hxx"
+#include "dbmgr.hxx"
 
 using namespace comphelper;
 using namespace ::com::sun::star;
@@ -508,9 +509,9 @@ void SwXDocumentSettings::_setSingleValue( const comphelper::PropertyInfo & rInf
         break;
         case HANDLE_EMBEDDED_DATABASE_NAME:
         {
-            SwDBData aData = mpDoc->GetDBData();
-            if (rValue >>= aData.sEmbeddedName)
-                mpDoc->ChgDBData(aData);
+            OUString sEmbeddedName;
+            if (rValue >>= sEmbeddedName)
+                mpDoc->GetDBManager()->setEmbeddedName(sEmbeddedName, *mpDocSh);
         }
         break;
         case HANDLE_SAVE_VERSION_ON_CLOSE:
@@ -988,8 +989,7 @@ void SwXDocumentSettings::_getSingleValue( const comphelper::PropertyInfo & rInf
         break;
         case HANDLE_EMBEDDED_DATABASE_NAME:
         {
-            const SwDBData& rData = mpDoc->GetDBDesc();
-            rValue <<= rData.sEmbeddedName;
+            rValue <<= mpDoc->GetDBManager()->getEmbeddedName();
         }
         break;
         case HANDLE_SAVE_VERSION_ON_CLOSE:


More information about the Libreoffice-commits mailing list