[Libreoffice-commits] core.git: include/svx svx/source

Aditya (via logerrit) logerrit at kemper.freedesktop.org
Fri Jul 17 08:16:54 UTC 2020


 include/svx/gallery1.hxx                    |   11 
 include/svx/gallerybinaryengine.hxx         |   22 +
 include/svx/galmisc.hxx                     |    5 
 include/svx/galtheme.hxx                    |    6 
 svx/source/gallery2/gallery1.cxx            |   31 ++
 svx/source/gallery2/gallerybinaryengine.cxx |  337 +++++++++++++++++++---------
 svx/source/gallery2/galmisc.cxx             |   11 
 svx/source/gallery2/galtheme.cxx            |  333 +++++++++------------------
 8 files changed, 430 insertions(+), 326 deletions(-)

New commits:
commit 457ffe960dfdbd0200292ad9c9835d947dd35c73
Author:     Aditya <adityasahu1511 at gmail.com>
AuthorDate: Fri Jul 10 16:10:05 2020 +0530
Commit:     Tomaž Vajngerl <quikee at gmail.com>
CommitDate: Fri Jul 17 10:16:12 2020 +0200

    svx: refactoring of GalleryTheme GetModel(), InsertModel()
    
    Introduce readModel(), readModelStream(), insertModel(), insertModelStream()
     in Gallery Binary Engine because these deal with file reading stuff.
    Move ImplGetURL() to galmisc for now because
     GalleryTheme and GalleryBinaryEngine both use it.
    Separate InsertModel() into two parts - one dealing with Sot Storage stream,
     the other adding object to gallery list. Move first part to Gallery Binary
     Engine, the second stays.
    Separate InsertObject() into two parts.
    Move CreateThemeEntry() to Binary Engine.
    
    Change-Id: Id3b4091db27b5b88bcf025f9fd94ad86169505d0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/98492
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>

diff --git a/include/svx/gallery1.hxx b/include/svx/gallery1.hxx
index 15fa6e1d6629..013245d91da0 100644
--- a/include/svx/gallery1.hxx
+++ b/include/svx/gallery1.hxx
@@ -30,6 +30,8 @@
 #include <memory>
 #include <vector>
 
+class SgaObjectSvDraw;
+struct GalleryObject;
 class GalleryBinaryEngine;
 
 class GalleryThemeEntry
@@ -55,6 +57,15 @@ public:
 
     void callGalleryThemeInit();
 
+    bool readModel(const GalleryObject* pObject, SdrModel& rModel);
+    bool insertModel(const FmFormModel& rModel, INetURLObject& aURL);
+
+    bool readModelStream(const GalleryObject* pObject, tools::SvRef<SotStorageStream> const& rxModelStream);
+    SgaObjectSvDraw insertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream, INetURLObject& rURL);
+
+    void insertObject(const SgaObject& rObj, GalleryObject* pFoundEntry, OUString& rDestDir,
+        ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList, sal_uInt32& rInsertPos);
+
     const OUString&         GetThemeName() const { return aName; }
 
     const INetURLObject&    GetThmURL() const { return mpGalleryBinaryEngine->GetThmURL(); }
diff --git a/include/svx/gallerybinaryengine.hxx b/include/svx/gallerybinaryengine.hxx
index 20be0b6cdfe0..4582bd1d4b8d 100644
--- a/include/svx/gallerybinaryengine.hxx
+++ b/include/svx/gallerybinaryengine.hxx
@@ -24,6 +24,7 @@
 #include <svx/galtheme.hxx>
 #include <sot/storage.hxx>
 
+class SgaObjectSvDraw;
 class SotStorage;
 struct GalleryObject;
 
@@ -64,10 +65,23 @@ public:
                             OUString& aDestDir,
                             ::std::vector<std::unique_ptr<GalleryObject>>& aObjectList);
     SAL_DLLPRIVATE bool implWrite(const GalleryTheme& rTheme);
-    SAL_DLLPRIVATE static INetURLObject
-    implCreateUniqueURL(SgaObjKind eObjKind, const INetURLObject& rUserURL,
-                        ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList,
-                        ConvertDataFormat nFormat = ConvertDataFormat::Unknown);
+
+    bool readModel(const GalleryObject* pObject, SdrModel& rModel);
+    bool insertModel(const FmFormModel& rModel, INetURLObject& rURL);
+
+    bool readModelStream(const GalleryObject* pObject,
+                         tools::SvRef<SotStorageStream> const& rxModelStream);
+    SgaObjectSvDraw insertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream,
+                                      INetURLObject& rURL);
+
+    void insertObject(const SgaObject& rObj, GalleryObject* pFoundEntry, OUString& rDestDir,
+                      ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList,
+                      sal_uInt32& rInsertPos);
+
+    SAL_DLLPRIVATE static GalleryThemeEntry* CreateThemeEntry(const INetURLObject& rURL,
+                                                              bool bReadOnly);
 };
 
+SvStream& WriteGalleryTheme(SvStream& rOut, const GalleryTheme& rTheme);
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/svx/galmisc.hxx b/include/svx/galmisc.hxx
index 4d7c03757375..dd5a361105de 100644
--- a/include/svx/galmisc.hxx
+++ b/include/svx/galmisc.hxx
@@ -23,6 +23,7 @@
 #include <vcl/imap.hxx>
 #include <svl/hint.hxx>
 #include <vcl/transfer.hxx>
+#include <vcl/salctype.hxx>
 #include <svx/svdobj.hxx>
 #include <com/sun/star/uno/Reference.h>
 #include <svx/svxdllapi.h>
@@ -193,5 +194,9 @@ public:
     void*            GetData1() const { return mnData1; }
 };
 
+struct GalleryObject;
+
+INetURLObject ImplGetURL(const GalleryObject* pObject);
+
 #endif
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/svx/galtheme.hxx b/include/svx/galtheme.hxx
index b262805a0744..7d67fe6f3488 100644
--- a/include/svx/galtheme.hxx
+++ b/include/svx/galtheme.hxx
@@ -98,7 +98,6 @@ private:
                                             return i;
                                     return SAL_MAX_UINT32;
                                 }
-    SAL_DLLPRIVATE static INetURLObject ImplGetURL( const GalleryObject* pObject );
     SAL_DLLPRIVATE void         ImplSetModified( bool bModified );
     SAL_DLLPRIVATE void         ImplBroadcast(sal_uInt32 nUpdatePos);
 
@@ -111,8 +110,6 @@ public:
 
     SAL_DLLPRIVATE              virtual ~GalleryTheme() override;
 
-    SAL_DLLPRIVATE static GalleryThemeEntry* CreateThemeEntry( const INetURLObject& rURL, bool bReadOnly );
-
     SAL_DLLPRIVATE sal_uInt32   GetObjectCount() const { return aObjectList.size(); }
 
     std::unique_ptr<SgaObject>  AcquireObject(sal_uInt32 nPos);
@@ -182,6 +179,8 @@ public:
     SAL_DLLPRIVATE bool         GetModelStream(sal_uInt32 nPos, tools::SvRef<SotStorageStream> const & rModelStreamRef);
     SAL_DLLPRIVATE bool         InsertModelStream(const tools::SvRef<SotStorageStream>& rModelStream, sal_uInt32 nInsertPos);
 
+    INetURLObject implCreateUniqueURL(SgaObjKind eObjKind, ConvertDataFormat nFormat = ConvertDataFormat::Unknown);
+
     SAL_DLLPRIVATE bool         GetURL(sal_uInt32 nPos, INetURLObject& rURL);
     bool                        InsertURL(const INetURLObject& rURL, sal_uInt32 nInsertPos = SAL_MAX_UINT32);
     SAL_DLLPRIVATE bool         InsertFileOrDirURL(const INetURLObject& rFileOrDirURL, sal_uInt32 nInsertPos);
@@ -201,7 +200,6 @@ public:
     SAL_DLLPRIVATE void SetPreviewBitmapExAndStrings(sal_uInt32 nPos, const BitmapEx& rBitmapEx, const Size& rSize, const OUString& rTitle, const OUString& rPath);
 };
 
-SvStream& WriteGalleryTheme( SvStream& rOut, const GalleryTheme& rTheme );
 SvStream& ReadGalleryTheme( SvStream& rIn, GalleryTheme& rTheme );
 
 #endif // INCLUDED_SVX_GALTHEME_HXX
diff --git a/svx/source/gallery2/gallery1.cxx b/svx/source/gallery2/gallery1.cxx
index 73b623a1ce1f..848b593e60f5 100644
--- a/svx/source/gallery2/gallery1.cxx
+++ b/svx/source/gallery2/gallery1.cxx
@@ -42,6 +42,7 @@
 #include <svx/galtheme.hxx>
 #include <svx/gallery1.hxx>
 #include <svx/gallerybinaryengine.hxx>
+#include <galobj.hxx>
 #include <vcl/weld.hxx>
 #include <com/sun/star/sdbc/XResultSet.hpp>
 #include <com/sun/star/ucb/XContentAccess.hpp>
@@ -174,6 +175,34 @@ void GalleryThemeEntry::callGalleryThemeInit()
     getGalleryBinaryEngine()->galleryThemeInit(IsReadOnly());
 }
 
+bool GalleryThemeEntry::readModel(const GalleryObject* pObject, SdrModel& rModel)
+{
+    return mpGalleryBinaryEngine->readModel(pObject, rModel);
+}
+
+bool GalleryThemeEntry::insertModel(const FmFormModel& rModel, INetURLObject& aURL)
+{
+    return mpGalleryBinaryEngine->insertModel(rModel, aURL);
+}
+
+bool GalleryThemeEntry::readModelStream(const GalleryObject* pObject, tools::SvRef<SotStorageStream> const& rxModelStream)
+{
+    return mpGalleryBinaryEngine->readModelStream(pObject, rxModelStream);
+}
+
+SgaObjectSvDraw
+GalleryThemeEntry::insertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream,
+    INetURLObject& rURL)
+{
+    return mpGalleryBinaryEngine->insertModelStream(rxModelStream, rURL);
+}
+
+void GalleryThemeEntry::insertObject(const SgaObject& rObj, GalleryObject* pFoundEntry, OUString& rDestDir,
+    ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList, sal_uInt32& rInsertPos)
+{
+    mpGalleryBinaryEngine->insertObject(rObj, pFoundEntry, rDestDir, rObjectList, rInsertPos);
+}
+
 void GalleryTheme::InsertAllThemes(weld::ComboBox& rListBox)
 {
     for (size_t i = 0; i < SAL_N_ELEMENTS(aUnlocalized); ++i)
@@ -437,7 +466,7 @@ void Gallery::ImplLoadSubDirs( const INetURLObject& rBaseURL, bool& rbDirIsReadO
                                     }
                                 }
 
-                                GalleryThemeEntry* pEntry = GalleryTheme::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
+                                GalleryThemeEntry* pEntry = GalleryBinaryEngine::CreateThemeEntry( aThmURL, rbDirIsReadOnly || bReadOnly );
 
                                 if( pEntry )
                                     aThemeList.emplace_back( pEntry );
diff --git a/svx/source/gallery2/gallerybinaryengine.cxx b/svx/source/gallery2/gallerybinaryengine.cxx
index 57ebc07feff8..494c385908c7 100644
--- a/svx/source/gallery2/gallerybinaryengine.cxx
+++ b/svx/source/gallery2/gallerybinaryengine.cxx
@@ -19,17 +19,24 @@
 
 #include <svx/gallerybinaryengine.hxx>
 #include <svx/galmisc.hxx>
-#include <vcl/salctype.hxx>
+#include <svx/unomodel.hxx>
+#include <svx/fmmodel.hxx>
+#include <svx/gallery1.hxx>
 #include <galobj.hxx>
+#include "codec.hxx"
+#include "gallerydrawmodel.hxx"
 
 #include <sal/log.hxx>
 
 #include <unotools/ucbstreamhelper.hxx>
+#include <unotools/streamwrap.hxx>
 #include <com/sun/star/ucb/ContentCreationException.hpp>
 #include <tools/urlobj.hxx>
 #include <tools/vcompat.hxx>
 #include <tools/diagnose_ex.h>
 
+using namespace ::com::sun::star;
+
 static bool FileExists(const INetURLObject& rURL, const OUString& rExt)
 {
     INetURLObject aURL(rURL);
@@ -231,144 +238,268 @@ bool GalleryBinaryEngine::implWrite(const GalleryTheme& rTheme)
     return true;
 }
 
-INetURLObject
-GalleryBinaryEngine::implCreateUniqueURL(SgaObjKind eObjKind, const INetURLObject& rUserURL,
-                                         ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList,
-                                         ConvertDataFormat nFormat)
+void GalleryBinaryEngine::ImplCreateSvDrawStorage(bool bReadOnly)
 {
-    INetURLObject aDir(rUserURL);
-    INetURLObject aInfoFileURL(rUserURL);
-    INetURLObject aNewURL;
-    sal_uInt32 nNextNumber = 1999;
-    char const* pExt = nullptr;
-    bool bExists;
+    try
+    {
+        aSvDrawStorageRef
+            = new SotStorage(false, GetSdvURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
+                             bReadOnly ? StreamMode::READ : StreamMode::STD_READWRITE);
+        // #i50423# ReadOnly may not been set though the file can't be written (because of security reasons)
+        if ((aSvDrawStorageRef->GetError() != ERRCODE_NONE) && !bReadOnly)
+            aSvDrawStorageRef = new SotStorage(
+                false, GetSdvURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
+                StreamMode::READ);
+    }
+    catch (const css::ucb::ContentCreationException&)
+    {
+        TOOLS_WARN_EXCEPTION("svx", "failed to open: " << GetSdvURL().GetMainURL(
+                                                              INetURLObject::DecodeMechanism::NONE)
+                                                       << "due to");
+    }
+}
 
-    aDir.Append("dragdrop");
-    CreateDir(aDir);
+const tools::SvRef<SotStorage>& GalleryBinaryEngine::GetSvDrawStorage() const
+{
+    return aSvDrawStorageRef;
+}
 
-    aInfoFileURL.Append("sdddndx1");
+bool GalleryBinaryEngine::readModel(const GalleryObject* pObject, SdrModel& rModel)
+{
+    tools::SvRef<SotStorage> xSotStorage(GetSvDrawStorage());
+    bool bRet = false;
+    const INetURLObject aURL(ImplGetURL(pObject));
 
-    // read next possible number
-    if (FileExists(aInfoFileURL))
+    if (xSotStorage.is())
     {
-        std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
-            aInfoFileURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
+        const OUString aStreamName(GetSvDrawStreamNameFromURL(aURL));
+        tools::SvRef<SotStorageStream> xInputStream(
+            xSotStorage->OpenSotStream(aStreamName, StreamMode::READ));
 
-        if (pIStm)
+        if (xInputStream.is() && !xInputStream->GetError())
         {
-            pIStm->ReadUInt32(nNextNumber);
+            xInputStream->SetBufferSize(STREAMBUF_SIZE);
+            bRet = GallerySvDrawImport(*xInputStream, rModel);
+            xInputStream->SetBufferSize(0);
         }
     }
+    return bRet;
+}
+
+bool GalleryBinaryEngine::insertModel(const FmFormModel& rModel, INetURLObject& rURL)
+{
+    tools::SvRef<SotStorage> xSotStorage(GetSvDrawStorage());
+    bool bRet = false;
 
-    // create extension
-    if (nFormat != ConvertDataFormat::Unknown)
+    if (xSotStorage.is())
     {
-        switch (nFormat)
+        const OUString aStreamName(GetSvDrawStreamNameFromURL(rURL));
+        tools::SvRef<SotStorageStream> xOutputStream(
+            xSotStorage->OpenSotStream(aStreamName, StreamMode::WRITE | StreamMode::TRUNC));
+
+        if (xOutputStream.is() && !xOutputStream->GetError())
         {
-            case ConvertDataFormat::BMP:
-                pExt = ".bmp";
-                break;
-            case ConvertDataFormat::GIF:
-                pExt = ".gif";
-                break;
-            case ConvertDataFormat::JPG:
-                pExt = ".jpg";
-                break;
-            case ConvertDataFormat::MET:
-                pExt = ".met";
-                break;
-            case ConvertDataFormat::PCT:
-                pExt = ".pct";
-                break;
-            case ConvertDataFormat::PNG:
-                pExt = ".png";
-                break;
-            case ConvertDataFormat::SVM:
-                pExt = ".svm";
-                break;
-            case ConvertDataFormat::TIF:
-                pExt = ".tif";
-                break;
-            case ConvertDataFormat::WMF:
-                pExt = ".wmf";
-                break;
-            case ConvertDataFormat::EMF:
-                pExt = ".emf";
-                break;
-
-            default:
-                pExt = ".grf";
-                break;
+            SvMemoryStream aMemoryStream(65535, 65535);
+            FmFormModel* pFormModel = const_cast<FmFormModel*>(&rModel);
+
+            pFormModel->BurnInStyleSheetAttributes();
+
+            {
+                uno::Reference<io::XOutputStream> xDocOut(
+                    new utl::OOutputStreamWrapper(aMemoryStream));
+
+                if (xDocOut.is())
+                    (void)SvxDrawingLayerExport(pFormModel, xDocOut);
+            }
+
+            aMemoryStream.Seek(0);
+
+            xOutputStream->SetBufferSize(16348);
+            GalleryCodec aCodec(*xOutputStream);
+            aCodec.Write(aMemoryStream);
+
+            xOutputStream->SetBufferSize(0);
+            xOutputStream->Commit();
+            bRet = !xOutputStream->GetError();
         }
     }
+    return bRet;
+}
+
+bool GalleryBinaryEngine::readModelStream(const GalleryObject* pObject,
+                                          tools::SvRef<SotStorageStream> const& rxModelStream)
+{
+    const INetURLObject aURL(ImplGetURL(pObject));
+    tools::SvRef<SotStorage> xSotStorage(GetSvDrawStorage());
+    bool bRet = false;
 
-    do
+    if (xSotStorage.is())
     {
-        // get URL
-        if (SgaObjKind::SvDraw == eObjKind)
+        const OUString aStreamName(GetSvDrawStreamNameFromURL(aURL));
+        tools::SvRef<SotStorageStream> xInputStream(
+            xSotStorage->OpenSotStream(aStreamName, StreamMode::READ));
+
+        if (xInputStream.is() && !xInputStream->GetError())
         {
-            OUString aFileName = "gallery/svdraw/dd" + OUString::number(++nNextNumber % 99999999);
-            aNewURL = INetURLObject(aFileName, INetProtocol::PrivSoffice);
+            sal_uInt32 nVersion = 0;
+
+            xInputStream->SetBufferSize(16348);
 
-            bExists = false;
+            if (GalleryCodec::IsCoded(*xInputStream, nVersion))
+            {
+                SvxGalleryDrawModel aModel;
 
-            for (auto const& p : rObjectList)
-                if (p->aURL == aNewURL)
+                if (aModel.GetModel())
                 {
-                    bExists = true;
-                    break;
-                }
-        }
-        else
-        {
-            OUString aFileName = "dd" + OUString::number(++nNextNumber % 999999);
+                    if (GallerySvDrawImport(*xInputStream, *aModel.GetModel()))
+                    {
+                        aModel.GetModel()->BurnInStyleSheetAttributes();
 
-            if (pExt)
-                aFileName += OUString(pExt, strlen(pExt), RTL_TEXTENCODING_ASCII_US);
+                        {
+                            uno::Reference<io::XOutputStream> xDocOut(
+                                new utl::OOutputStreamWrapper(*rxModelStream));
 
-            aNewURL = aDir;
-            aNewURL.Append(aFileName);
+                            if (SvxDrawingLayerExport(aModel.GetModel(), xDocOut))
+                                rxModelStream->Commit();
+                        }
+                    }
 
-            bExists = FileExists(aNewURL);
+                    bRet = (rxModelStream->GetError() == ERRCODE_NONE);
+                }
+            }
+
+            xInputStream->SetBufferSize(0);
         }
-    } while (bExists);
+    }
+    return bRet;
+}
 
-    // write updated number
-    std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream(
-        aInfoFileURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE));
+SgaObjectSvDraw
+GalleryBinaryEngine::insertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream,
+                                       INetURLObject& rURL)
+{
+    tools::SvRef<SotStorage> xSotStorage(GetSvDrawStorage());
 
-    if (pOStm)
+    if (xSotStorage.is())
     {
-        pOStm->WriteUInt32(nNextNumber);
-    }
+        const OUString aStreamName(GetSvDrawStreamNameFromURL(rURL));
+        tools::SvRef<SotStorageStream> xOutputStream(
+            xSotStorage->OpenSotStream(aStreamName, StreamMode::WRITE | StreamMode::TRUNC));
+
+        if (xOutputStream.is() && !xOutputStream->GetError())
+        {
+            GalleryCodec aCodec(*xOutputStream);
 
-    return aNewURL;
+            xOutputStream->SetBufferSize(16348);
+            aCodec.Write(*rxModelStream);
+
+            if (!xOutputStream->GetError())
+            {
+                xOutputStream->Seek(0);
+                SgaObjectSvDraw aObjSvDraw(*xOutputStream, rURL);
+                return aObjSvDraw;
+            }
+        }
+    }
+    return SgaObjectSvDraw();
 }
 
-void GalleryBinaryEngine::ImplCreateSvDrawStorage(bool bReadOnly)
+void GalleryBinaryEngine::insertObject(const SgaObject& rObj, GalleryObject* pFoundEntry,
+                                       OUString& rDestDir,
+                                       ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList,
+                                       sal_uInt32& rInsertPos)
 {
-    try
+    if (pFoundEntry)
     {
-        aSvDrawStorageRef
-            = new SotStorage(false, GetSdvURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
-                             bReadOnly ? StreamMode::READ : StreamMode::STD_READWRITE);
-        // #i50423# ReadOnly may not been set though the file can't be written (because of security reasons)
-        if ((aSvDrawStorageRef->GetError() != ERRCODE_NONE) && !bReadOnly)
-            aSvDrawStorageRef = new SotStorage(
-                false, GetSdvURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
-                StreamMode::READ);
-    }
-    catch (const css::ucb::ContentCreationException&)
-    {
-        TOOLS_WARN_EXCEPTION("svx", "failed to open: " << GetSdvURL().GetMainURL(
-                                                              INetURLObject::DecodeMechanism::NONE)
-                                                       << "due to");
+        GalleryObject aNewEntry;
+
+        // update title of new object if necessary
+        if (rObj.GetTitle().isEmpty())
+        {
+            std::unique_ptr<SgaObject> pOldObj(implReadSgaObject(pFoundEntry));
+
+            if (pOldObj)
+            {
+                const_cast<SgaObject&>(rObj).SetTitle(pOldObj->GetTitle());
+            }
+        }
+        else if (rObj.GetTitle() == "__<empty>__")
+            const_cast<SgaObject&>(rObj).SetTitle("");
+
+        implWriteSgaObject(rObj, rInsertPos, &aNewEntry, rDestDir, rObjectList);
+        pFoundEntry->nOffset = aNewEntry.nOffset;
     }
+    else
+        implWriteSgaObject(rObj, rInsertPos, nullptr, rDestDir, rObjectList);
 }
 
-const tools::SvRef<SotStorage>& GalleryBinaryEngine::GetSvDrawStorage() const
+GalleryThemeEntry* GalleryBinaryEngine::CreateThemeEntry(const INetURLObject& rURL, bool bReadOnly)
 {
-    return aSvDrawStorageRef;
+    DBG_ASSERT(rURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
+
+    GalleryThemeEntry* pRet = nullptr;
+
+    if (FileExists(rURL))
+    {
+        std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
+            rURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
+
+        if (pIStm)
+        {
+            OUString aThemeName;
+            sal_uInt16 nVersion;
+
+            pIStm->ReadUInt16(nVersion);
+
+            if (nVersion <= 0x00ff)
+            {
+                bool bThemeNameFromResource = false;
+                sal_uInt32 nThemeId = 0;
+
+                OString aTmpStr = read_uInt16_lenPrefixed_uInt8s_ToOString(*pIStm);
+                aThemeName = OStringToOUString(aTmpStr, RTL_TEXTENCODING_UTF8);
+
+                // execute a character conversion
+                if (nVersion >= 0x0004)
+                {
+                    sal_uInt32 nCount;
+                    sal_uInt16 nTemp16;
+
+                    pIStm->ReadUInt32(nCount).ReadUInt16(nTemp16);
+                    pIStm->Seek(STREAM_SEEK_TO_END);
+
+                    // check whether there is a newer version;
+                    // therefore jump back by 520Bytes (8 bytes ID + 512Bytes reserve buffer)
+                    // if this is at all possible.
+                    if (pIStm->Tell() >= 520)
+                    {
+                        sal_uInt32 nId1, nId2;
+
+                        pIStm->SeekRel(-520);
+                        pIStm->ReadUInt32(nId1).ReadUInt32(nId2);
+
+                        if (nId1 == COMPAT_FORMAT('G', 'A', 'L', 'R')
+                            && nId2 == COMPAT_FORMAT('E', 'S', 'R', 'V'))
+                        {
+                            VersionCompat aCompat(*pIStm, StreamMode::READ);
+
+                            pIStm->ReadUInt32(nThemeId);
+
+                            if (aCompat.GetVersion() >= 2)
+                            {
+                                pIStm->ReadCharAsBool(bThemeNameFromResource);
+                            }
+                        }
+                    }
+                }
+
+                pRet = new GalleryThemeEntry(false, rURL, aThemeName, bReadOnly, false, nThemeId,
+                                             bThemeNameFromResource);
+            }
+        }
+    }
+
+    return pRet;
 }
 
 SvStream& WriteGalleryTheme(SvStream& rOut, const GalleryTheme& rTheme)
diff --git a/svx/source/gallery2/galmisc.cxx b/svx/source/gallery2/galmisc.cxx
index b51cdb1af2b2..ca425f661d17 100644
--- a/svx/source/gallery2/galmisc.cxx
+++ b/svx/source/gallery2/galmisc.cxx
@@ -26,6 +26,7 @@
 #include <ucbhelper/content.hxx>
 #include <com/sun/star/ucb/ContentCreationException.hpp>
 #include <tools/urlobj.hxx>
+#include <unotools/ucbstreamhelper.hxx>
 #include <vcl/graphicfilter.hxx>
 #include <svl/itempool.hxx>
 #include <sfx2/docfile.hxx>
@@ -550,4 +551,14 @@ bool GalleryTransferable::StartDrag()
     return true;
 }
 
+INetURLObject ImplGetURL(const GalleryObject* pObject)
+{
+    INetURLObject aURL;
+
+    if (pObject)
+        aURL = pObject->aURL;
+
+    return aURL;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/gallery2/galtheme.cxx b/svx/source/gallery2/galtheme.cxx
index d53d59d424b0..61c6077eb5b6 100644
--- a/svx/source/gallery2/galtheme.cxx
+++ b/svx/source/gallery2/galtheme.cxx
@@ -59,7 +59,6 @@
 
 using namespace ::com::sun::star;
 
-
 GalleryTheme::GalleryTheme( Gallery* pGallery, GalleryThemeEntry* pThemeEntry )
     : m_bDestDirRelative(false)
     , pParent(pGallery)
@@ -96,14 +95,115 @@ const GalleryObject* GalleryTheme::ImplGetGalleryObject( const INetURLObject& rU
     return nullptr;
 }
 
-INetURLObject GalleryTheme::ImplGetURL( const GalleryObject* pObject )
+INetURLObject GalleryTheme::implCreateUniqueURL(SgaObjKind eObjKind, ConvertDataFormat nFormat)
 {
-    INetURLObject aURL;
+    INetURLObject aDir(GetParent()->GetUserURL());
+    INetURLObject aInfoFileURL(GetParent()->GetUserURL());
+    INetURLObject aNewURL;
+    sal_uInt32 nNextNumber = 1999;
+    char const* pExt = nullptr;
+    bool bExists;
 
-    if( pObject )
-        aURL = pObject->aURL;
+    aDir.Append("dragdrop");
+    CreateDir(aDir);
+
+    aInfoFileURL.Append("sdddndx1");
+
+    // read next possible number
+    if (FileExists(aInfoFileURL))
+    {
+        std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
+            aInfoFileURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
+
+        if (pIStm)
+        {
+            pIStm->ReadUInt32(nNextNumber);
+        }
+    }
+
+    // create extension
+    if (nFormat != ConvertDataFormat::Unknown)
+    {
+        switch (nFormat)
+        {
+        case ConvertDataFormat::BMP:
+            pExt = ".bmp";
+            break;
+        case ConvertDataFormat::GIF:
+            pExt = ".gif";
+            break;
+        case ConvertDataFormat::JPG:
+            pExt = ".jpg";
+            break;
+        case ConvertDataFormat::MET:
+            pExt = ".met";
+            break;
+        case ConvertDataFormat::PCT:
+            pExt = ".pct";
+            break;
+        case ConvertDataFormat::PNG:
+            pExt = ".png";
+            break;
+        case ConvertDataFormat::SVM:
+            pExt = ".svm";
+            break;
+        case ConvertDataFormat::TIF:
+            pExt = ".tif";
+            break;
+        case ConvertDataFormat::WMF:
+            pExt = ".wmf";
+            break;
+        case ConvertDataFormat::EMF:
+            pExt = ".emf";
+            break;
+
+        default:
+            pExt = ".grf";
+            break;
+        }
+    }
+
+    do
+    {
+        // get URL
+        if (SgaObjKind::SvDraw == eObjKind)
+        {
+            OUString aFileName = "gallery/svdraw/dd" + OUString::number(++nNextNumber % 99999999);
+            aNewURL = INetURLObject(aFileName, INetProtocol::PrivSoffice);
+
+            bExists = false;
 
-    return aURL;
+            for (auto const& p : aObjectList)
+                if (p->aURL == aNewURL)
+                {
+                    bExists = true;
+                    break;
+                }
+        }
+        else
+        {
+            OUString aFileName = "dd" + OUString::number(++nNextNumber % 999999);
+
+            if (pExt)
+                aFileName += OUString(pExt, strlen(pExt), RTL_TEXTENCODING_ASCII_US);
+
+            aNewURL = aDir;
+            aNewURL.Append(aFileName);
+
+            bExists = FileExists(aNewURL);
+        }
+    } while (bExists);
+
+    // write updated number
+    std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream(
+        aInfoFileURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE));
+
+    if (pOStm)
+    {
+        pOStm->WriteUInt32(nNextNumber);
+    }
+
+    return aNewURL;
 }
 
 void GalleryTheme::ImplBroadcast(sal_uInt32 nUpdatePos)
@@ -156,28 +256,7 @@ bool GalleryTheme::InsertObject(const SgaObject& rObj, sal_uInt32 nInsertPos)
         }
     }
 
-    if (pFoundEntry)
-    {
-        GalleryObject aNewEntry;
-
-        // update title of new object if necessary
-        if (rObj.GetTitle().isEmpty())
-        {
-            std::unique_ptr<SgaObject> pOldObj(pThm->getGalleryBinaryEngine()->implReadSgaObject(pFoundEntry));
-
-            if (pOldObj)
-            {
-                const_cast<SgaObject&>(rObj).SetTitle( pOldObj->GetTitle() );
-            }
-        }
-        else if (rObj.GetTitle() == "__<empty>__")
-            const_cast<SgaObject&>(rObj).SetTitle("");
-
-        pThm->getGalleryBinaryEngine()->implWriteSgaObject(rObj, nInsertPos, &aNewEntry, m_aDestDir, aObjectList);
-        pFoundEntry->nOffset = aNewEntry.nOffset;
-    }
-    else
-        pThm->getGalleryBinaryEngine()->implWriteSgaObject(rObj, nInsertPos, nullptr, m_aDestDir, aObjectList);
+    pThm->insertObject(rObj, pFoundEntry, m_aDestDir, aObjectList, nInsertPos);
 
     ImplSetModified(true);
     ImplBroadcast(pFoundEntry? iFoundPos: nInsertPos);
@@ -427,75 +506,6 @@ void GalleryTheme::Actualize( const Link<const INetURLObject&, void>& rActualize
     UnlockBroadcaster();
 }
 
-GalleryThemeEntry* GalleryTheme::CreateThemeEntry( const INetURLObject& rURL, bool bReadOnly )
-{
-    DBG_ASSERT( rURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
-
-    GalleryThemeEntry*  pRet = nullptr;
-
-    if( FileExists( rURL ) )
-    {
-        std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( rURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
-
-        if( pIStm )
-        {
-            OUString        aThemeName;
-            sal_uInt16      nVersion;
-
-            pIStm->ReadUInt16( nVersion );
-
-            if( nVersion <= 0x00ff )
-            {
-                bool bThemeNameFromResource = false;
-                sal_uInt32      nThemeId = 0;
-
-                OString aTmpStr = read_uInt16_lenPrefixed_uInt8s_ToOString(*pIStm);
-                aThemeName = OStringToOUString(aTmpStr, RTL_TEXTENCODING_UTF8);
-
-                // execute a character conversion
-                if( nVersion >= 0x0004 )
-                {
-                    sal_uInt32  nCount;
-                    sal_uInt16  nTemp16;
-
-                    pIStm->ReadUInt32( nCount ).ReadUInt16( nTemp16 );
-                    pIStm->Seek( STREAM_SEEK_TO_END );
-
-                    // check whether there is a newer version;
-                    // therefore jump back by 520Bytes (8 bytes ID + 512Bytes reserve buffer)
-                    // if this is at all possible.
-                    if( pIStm->Tell() >= 520 )
-                    {
-                        sal_uInt32 nId1, nId2;
-
-                        pIStm->SeekRel( -520 );
-                        pIStm->ReadUInt32( nId1 ).ReadUInt32( nId2 );
-
-                        if( nId1 == COMPAT_FORMAT( 'G', 'A', 'L', 'R' ) &&
-                            nId2 == COMPAT_FORMAT( 'E', 'S', 'R', 'V' ) )
-                        {
-                            VersionCompat aCompat( *pIStm, StreamMode::READ );
-
-                            pIStm->ReadUInt32( nThemeId );
-
-                            if( aCompat.GetVersion() >= 2 )
-                            {
-                                pIStm->ReadCharAsBool( bThemeNameFromResource );
-                            }
-                        }
-                    }
-                }
-
-                pRet = new GalleryThemeEntry( false, rURL, aThemeName,
-                                              bReadOnly, false, nThemeId,
-                                              bThemeNameFromResource );
-            }
-        }
-    }
-
-    return pRet;
-}
-
 bool GalleryTheme::GetThumb(sal_uInt32 nPos, BitmapEx& rBmp)
 {
     std::unique_ptr<SgaObject> pObj = AcquireObject( nPos );
@@ -626,7 +636,7 @@ bool GalleryTheme::InsertGraphic(const Graphic& rGraphic, sal_uInt32 nInsertPos)
                 nExportFormat = ConvertDataFormat::SVM;
         }
 
-        const INetURLObject aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::Bitmap, GetParent()->GetUserURL(), aObjectList, nExportFormat ) );
+        const INetURLObject aURL( implCreateUniqueURL( SgaObjKind::Bitmap, nExportFormat ) );
         std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::TRUNC ));
 
         if( pOStm )
@@ -671,21 +681,7 @@ bool GalleryTheme::GetModel(sal_uInt32 nPos, SdrModel& rModel)
 
     if( pObject && ( SgaObjKind::SvDraw == pObject->eObjKind ) )
     {
-        const INetURLObject aURL( ImplGetURL( pObject ) );
-        tools::SvRef<SotStorage>        xStor( pThm->getGalleryBinaryEngine()->GetSvDrawStorage() );
-
-        if( xStor.is() )
-        {
-            const OUString        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
-            tools::SvRef<SotStorageStream>  xIStm( xStor->OpenSotStream( aStmName, StreamMode::READ ) );
-
-            if( xIStm.is() && !xIStm->GetError() )
-            {
-                xIStm->SetBufferSize( STREAMBUF_SIZE );
-                bRet = GallerySvDrawImport( *xIStm, rModel );
-                xIStm->SetBufferSize( 0 );
-            }
-        }
+        bRet = pThm->readModel(pObject, rModel);
     }
 
     return bRet;
@@ -693,46 +689,14 @@ bool GalleryTheme::GetModel(sal_uInt32 nPos, SdrModel& rModel)
 
 bool GalleryTheme::InsertModel(const FmFormModel& rModel, sal_uInt32 nInsertPos)
 {
-    INetURLObject   aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::SvDraw, GetParent()->GetUserURL(), aObjectList) );
-    tools::SvRef<SotStorage>    xStor(pThm->getGalleryBinaryEngine()->GetSvDrawStorage() );
-    bool            bRet = false;
+    INetURLObject   aURL( implCreateUniqueURL( SgaObjKind::SvDraw ) );
+    bool bRet = false;
 
-    if( xStor.is() )
+    if(pThm->insertModel(rModel, aURL))
     {
-        const OUString        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
-        tools::SvRef<SotStorageStream>  xOStm( xStor->OpenSotStream( aStmName, StreamMode::WRITE | StreamMode::TRUNC ) );
-
-        if( xOStm.is() && !xOStm->GetError() )
-        {
-            SvMemoryStream  aMemStm( 65535, 65535 );
-            FmFormModel*    pFormModel = const_cast<FmFormModel*>(&rModel);
-
-            pFormModel->BurnInStyleSheetAttributes();
-
-            {
-                uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( aMemStm ) );
-
-                if (xDocOut.is())
-                    (void)SvxDrawingLayerExport( pFormModel, xDocOut );
-            }
-
-            aMemStm.Seek( 0 );
-
-            xOStm->SetBufferSize( 16348 );
-            GalleryCodec aCodec( *xOStm );
-            aCodec.Write( aMemStm );
-
-            if( !xOStm->GetError() )
-            {
-                SgaObjectSvDraw aObjSvDraw( rModel, aURL );
-                bRet = InsertObject( aObjSvDraw, nInsertPos );
-            }
-
-            xOStm->SetBufferSize( 0 );
-            xOStm->Commit();
-        }
+        SgaObjectSvDraw aObjSvDraw(rModel, aURL);
+        bRet = InsertObject( aObjSvDraw, nInsertPos );
     }
-
     return bRet;
 }
 
@@ -743,45 +707,7 @@ bool GalleryTheme::GetModelStream(sal_uInt32 nPos, tools::SvRef<SotStorageStream
 
     if( pObject && ( SgaObjKind::SvDraw == pObject->eObjKind ) )
     {
-        const INetURLObject aURL( ImplGetURL( pObject ) );
-        tools::SvRef<SotStorage>        xStor( pThm->getGalleryBinaryEngine()->GetSvDrawStorage() );
-
-        if( xStor.is() )
-        {
-            const OUString        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
-            tools::SvRef<SotStorageStream>  xIStm( xStor->OpenSotStream( aStmName, StreamMode::READ ) );
-
-            if( xIStm.is() && !xIStm->GetError() )
-            {
-                sal_uInt32 nVersion = 0;
-
-                xIStm->SetBufferSize( 16348 );
-
-                if( GalleryCodec::IsCoded( *xIStm, nVersion ) )
-                {
-                    SvxGalleryDrawModel aModel;
-
-                    if( aModel.GetModel() )
-                    {
-                        if( GallerySvDrawImport( *xIStm, *aModel.GetModel() ) )
-                        {
-                            aModel.GetModel()->BurnInStyleSheetAttributes();
-
-                            {
-                                uno::Reference< io::XOutputStream > xDocOut( new utl::OOutputStreamWrapper( *rxModelStream ) );
-
-                                if( SvxDrawingLayerExport( aModel.GetModel(), xDocOut ) )
-                                    rxModelStream->Commit();
-                            }
-                        }
-
-                        bRet = ( rxModelStream->GetError() == ERRCODE_NONE );
-                    }
-                }
-
-                xIStm->SetBufferSize( 0 );
-            }
-        }
+        bRet = pThm->readModelStream(pObject, rxModelStream);
     }
 
     return bRet;
@@ -789,33 +715,12 @@ bool GalleryTheme::GetModelStream(sal_uInt32 nPos, tools::SvRef<SotStorageStream
 
 bool GalleryTheme::InsertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream, sal_uInt32 nInsertPos)
 {
-    INetURLObject   aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::SvDraw, GetParent()->GetUserURL(), aObjectList ) );
-    tools::SvRef<SotStorage>    xStor( pThm->getGalleryBinaryEngine()->GetSvDrawStorage() );
+    INetURLObject   aURL( implCreateUniqueURL( SgaObjKind::SvDraw ) );
     bool            bRet = false;
 
-    if( xStor.is() )
-    {
-        const OUString        aStmName( GetSvDrawStreamNameFromURL( aURL ) );
-        tools::SvRef<SotStorageStream>  xOStm( xStor->OpenSotStream( aStmName, StreamMode::WRITE | StreamMode::TRUNC ) );
-
-        if( xOStm.is() && !xOStm->GetError() )
-        {
-            GalleryCodec    aCodec( *xOStm );
-
-            xOStm->SetBufferSize( 16348 );
-            aCodec.Write( *rxModelStream );
-
-            if( !xOStm->GetError() )
-            {
-                xOStm->Seek( 0 );
-                SgaObjectSvDraw aObjSvDraw( *xOStm, aURL );
-                bRet = InsertObject( aObjSvDraw, nInsertPos );
-            }
-
-            xOStm->SetBufferSize( 0 );
-            xOStm->Commit();
-        }
-    }
+    const SgaObjectSvDraw aObjSvDraw = pThm->insertModelStream(rxModelStream, aURL);
+    if(aObjSvDraw.IsValid())
+        bRet = InsertObject( aObjSvDraw, nInsertPos );
 
     return bRet;
 }


More information about the Libreoffice-commits mailing list