summaryrefslogtreecommitdiff
path: root/svx/source/gallery2
diff options
context:
space:
mode:
authorAditya <adityasahu1511@gmail.com>2020-06-17 19:41:59 +0530
committerTomaž Vajngerl <quikee@gmail.com>2020-06-23 00:19:56 +0200
commitaadcd6f90916bd2b9734ae793141d0c77cc5b46c (patch)
tree350ac0c49761296a923b80a7119273a5d1eae503 /svx/source/gallery2
parent339bfdbd81bd412d02e97a3c43a20f6fba465bee (diff)
svx:Refactor members of GalleryTheme class to GalleryBinaryEngine
Move those data members and functions in GalleryTheme that deal with reading and writing of binary files to the new class GalleryBinaryEngine. These internal changes must not reflect any change in the behaviour. Functions moved in this patch: ImplReadSgaObject(), ImplWrite(), ImplCreateUniqueURL() Change-Id: Ifd30bc7920f31007dfe7fe2ffdc539dfc43976ac Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96541 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'svx/source/gallery2')
-rw-r--r--svx/source/gallery2/gallery1.cxx4
-rw-r--r--svx/source/gallery2/gallerybinaryengine.cxx219
-rw-r--r--svx/source/gallery2/galtheme.cxx191
3 files changed, 230 insertions, 184 deletions
diff --git a/svx/source/gallery2/gallery1.cxx b/svx/source/gallery2/gallery1.cxx
index 3bd04dbeb527..7f0c524fda65 100644
--- a/svx/source/gallery2/gallery1.cxx
+++ b/svx/source/gallery2/gallery1.cxx
@@ -593,7 +593,9 @@ void Gallery::RenameTheme( const OUString& rOldName, const OUString& rNewName )
if( pThm )
{
pThemeEntry->SetName( rNewName );
- pThm->ImplWrite();
+ if (pThm->pThm->IsModified())
+ if (!pThm->pThm->getGalleryBinaryEngine()->implWrite(*pThm))
+ pThm->ImplSetModified(false);
Broadcast( GalleryHint( GalleryHintType::THEME_RENAMED, rOldName, pThm->GetName() ) );
ReleaseTheme( pThm, aListener );
diff --git a/svx/source/gallery2/gallerybinaryengine.cxx b/svx/source/gallery2/gallerybinaryengine.cxx
index c5d2f018e1d4..f72247ba038b 100644
--- a/svx/source/gallery2/gallerybinaryengine.cxx
+++ b/svx/source/gallery2/gallerybinaryengine.cxx
@@ -19,10 +19,12 @@
#include <svx/gallerybinaryengine.hxx>
#include <svx/galmisc.hxx>
+#include <vcl/salctype.hxx>
#include <galobj.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <tools/urlobj.hxx>
+#include <tools/vcompat.hxx>
static bool FileExists(const INetURLObject& rURL, const OUString& rExt)
{
@@ -88,9 +90,64 @@ void GalleryBinaryEngine::SetStrExtension(INetURLObject aURL)
aStrURL = ImplGetURLIgnoreCase(aURL);
}
-bool GalleryBinaryEngine::ImplWriteSgaObject(
+std::unique_ptr<SgaObject> GalleryBinaryEngine::implReadSgaObject(GalleryObject const* pEntry)
+{
+ std::unique_ptr<SgaObject> pSgaObj;
+
+ if (pEntry)
+ {
+ std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
+ GetSdgURL().GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
+
+ if (pIStm)
+ {
+ sal_uInt32 nInventor;
+
+ // Check to ensure that the file is a valid SGA file
+ pIStm->Seek(pEntry->nOffset);
+ pIStm->ReadUInt32(nInventor);
+
+ if (nInventor == COMPAT_FORMAT('S', 'G', 'A', '3'))
+ {
+ pIStm->Seek(pEntry->nOffset);
+
+ switch (pEntry->eObjKind)
+ {
+ case SgaObjKind::Bitmap:
+ pSgaObj.reset(new SgaObjectBmp());
+ break;
+ case SgaObjKind::Animation:
+ pSgaObj.reset(new SgaObjectAnim());
+ break;
+ case SgaObjKind::Inet:
+ pSgaObj.reset(new SgaObjectINet());
+ break;
+ case SgaObjKind::SvDraw:
+ pSgaObj.reset(new SgaObjectSvDraw());
+ break;
+ case SgaObjKind::Sound:
+ pSgaObj.reset(new SgaObjectSound());
+ break;
+
+ default:
+ break;
+ }
+
+ if (pSgaObj)
+ {
+ ReadSgaObject(*pIStm, *pSgaObj);
+ pSgaObj->ImplUpdateURL(pEntry->aURL);
+ }
+ }
+ }
+ }
+
+ return pSgaObj;
+}
+
+bool GalleryBinaryEngine::implWriteSgaObject(
const SgaObject& rObj, sal_uInt32 nPos, GalleryObject* pExistentEntry, OUString& aDestDir,
- ::std::vector<std::unique_ptr<GalleryObject>>& aObjectList)
+ ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList)
{
std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream(
GetSdgURL().GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::WRITE));
@@ -109,12 +166,12 @@ bool GalleryBinaryEngine::ImplWriteSgaObject(
if (!pExistentEntry)
{
pEntry = new GalleryObject;
- if (nPos < aObjectList.size())
+ if (nPos < rObjectList.size())
{
- aObjectList.emplace(aObjectList.begin() + nPos, pEntry);
+ rObjectList.emplace(rObjectList.begin() + nPos, pEntry);
}
else
- aObjectList.emplace_back(pEntry);
+ rObjectList.emplace_back(pEntry);
}
else
pEntry = pExistentEntry;
@@ -128,3 +185,155 @@ bool GalleryBinaryEngine::ImplWriteSgaObject(
return bRet;
}
+
+bool GalleryBinaryEngine::implWrite(const GalleryTheme& rTheme)
+{
+ INetURLObject aPathURL(GetThmURL());
+
+ aPathURL.removeSegment();
+ aPathURL.removeFinalSlash();
+
+ DBG_ASSERT(aPathURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
+
+ if (FileExists(aPathURL) || CreateDir(aPathURL))
+ {
+#ifdef UNX
+ std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream(
+ GetThmURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
+ StreamMode::WRITE | StreamMode::COPY_ON_SYMLINK | StreamMode::TRUNC));
+#else
+ std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream(
+ GetThmURL().GetMainURL(INetURLObject::DecodeMechanism::NONE),
+ StreamMode::WRITE | StreamMode::TRUNC));
+#endif
+
+ if (pOStm)
+ {
+ WriteGalleryTheme(*pOStm, rTheme);
+ pOStm.reset();
+ return true;
+ }
+
+ return false;
+ }
+ return true;
+}
+
+INetURLObject
+GalleryBinaryEngine::implCreateUniqueURL(SgaObjKind eObjKind, const INetURLObject& rUserURL,
+ ::std::vector<std::unique_ptr<GalleryObject>>& rObjectList,
+ ConvertDataFormat nFormat)
+{
+ INetURLObject aDir(rUserURL);
+ INetURLObject aInfoFileURL(rUserURL);
+ INetURLObject aNewURL;
+ sal_uInt32 nNextNumber = 1999;
+ char const* pExt = nullptr;
+ bool bExists;
+
+ 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;
+
+ for (auto const& p : rObjectList)
+ 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;
+}
+
+SvStream& WriteGalleryTheme(SvStream& rOut, const GalleryTheme& rTheme)
+{
+ return rTheme.WriteData(rOut);
+}
diff --git a/svx/source/gallery2/galtheme.cxx b/svx/source/gallery2/galtheme.cxx
index e849542e4ef2..ec52c09b56e5 100644
--- a/svx/source/gallery2/galtheme.cxx
+++ b/svx/source/gallery2/galtheme.cxx
@@ -75,7 +75,9 @@ GalleryTheme::GalleryTheme( Gallery* pGallery, GalleryThemeEntry* pThemeEntry )
GalleryTheme::~GalleryTheme()
{
- ImplWrite();
+ if(pThm->IsModified())
+ if(!pThm->getGalleryBinaryEngine()->implWrite(*this))
+ ImplSetModified(false);
for (auto & pEntry : aObjectList)
{
@@ -103,80 +105,6 @@ void GalleryTheme::ImplCreateSvDrawStorage()
}
}
-std::unique_ptr<SgaObject> GalleryTheme::ImplReadSgaObject( GalleryObject const * pEntry )
-{
- std::unique_ptr<SgaObject> pSgaObj;
-
- if( pEntry )
- {
- std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream( GetSdgURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ ));
-
- if( pIStm )
- {
- sal_uInt32 nInventor;
-
- // Check to ensure that the file is a valid SGA file
- pIStm->Seek( pEntry->nOffset );
- pIStm->ReadUInt32( nInventor );
-
- if( nInventor == COMPAT_FORMAT( 'S', 'G', 'A', '3' ) )
- {
- pIStm->Seek( pEntry->nOffset );
-
- switch( pEntry->eObjKind )
- {
- case SgaObjKind::Bitmap: pSgaObj.reset(new SgaObjectBmp()); break;
- case SgaObjKind::Animation: pSgaObj.reset(new SgaObjectAnim()); break;
- case SgaObjKind::Inet: pSgaObj.reset(new SgaObjectINet()); break;
- case SgaObjKind::SvDraw: pSgaObj.reset(new SgaObjectSvDraw()); break;
- case SgaObjKind::Sound: pSgaObj.reset(new SgaObjectSound()); break;
-
- default:
- break;
- }
-
- if( pSgaObj )
- {
- ReadSgaObject( *pIStm, *pSgaObj );
- pSgaObj->ImplUpdateURL( pEntry->aURL );
- }
- }
- }
- }
-
- return pSgaObj;
-}
-
-void GalleryTheme::ImplWrite()
-{
- if( pThm->IsModified() )
- {
- INetURLObject aPathURL( GetThmURL() );
-
- aPathURL.removeSegment();
- aPathURL.removeFinalSlash();
-
- DBG_ASSERT( aPathURL.GetProtocol() != INetProtocol::NotValid, "invalid URL" );
-
- if( FileExists( aPathURL ) || CreateDir( aPathURL ) )
- {
-#ifdef UNX
- std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream( GetThmURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::COPY_ON_SYMLINK | StreamMode::TRUNC ));
-#else
- std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream( GetThmURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::TRUNC ));
-#endif
-
- if( pOStm )
- {
- WriteGalleryTheme( *pOStm, *this );
- pOStm.reset();
- }
-
- ImplSetModified( false );
- }
- }
-}
-
const GalleryObject* GalleryTheme::ImplGetGalleryObject( const INetURLObject& rURL )
{
for (auto const & i : aObjectList)
@@ -195,96 +123,6 @@ INetURLObject GalleryTheme::ImplGetURL( const GalleryObject* pObject )
return aURL;
}
-INetURLObject GalleryTheme::ImplCreateUniqueURL( SgaObjKind eObjKind, ConvertDataFormat nFormat )
-{
- INetURLObject aDir( GetParent()->GetUserURL() );
- INetURLObject aInfoFileURL( GetParent()->GetUserURL() );
- INetURLObject aNewURL;
- sal_uInt32 nNextNumber = 1999;
- char const* pExt = nullptr;
- bool bExists;
-
- 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;
-
- 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)
{
if( !IsBroadcasterLocked() )
@@ -342,7 +180,7 @@ bool GalleryTheme::InsertObject(const SgaObject& rObj, sal_uInt32 nInsertPos)
// update title of new object if necessary
if (rObj.GetTitle().isEmpty())
{
- std::unique_ptr<SgaObject> pOldObj(ImplReadSgaObject(pFoundEntry));
+ std::unique_ptr<SgaObject> pOldObj(pThm->getGalleryBinaryEngine()->implReadSgaObject(pFoundEntry));
if (pOldObj)
{
@@ -352,11 +190,11 @@ bool GalleryTheme::InsertObject(const SgaObject& rObj, sal_uInt32 nInsertPos)
else if (rObj.GetTitle() == "__<empty>__")
const_cast<SgaObject&>(rObj).SetTitle("");
- pThm->getGalleryBinaryEngine()->ImplWriteSgaObject(rObj, nInsertPos, &aNewEntry, m_aDestDir, aObjectList);
+ pThm->getGalleryBinaryEngine()->implWriteSgaObject(rObj, nInsertPos, &aNewEntry, m_aDestDir, aObjectList);
pFoundEntry->nOffset = aNewEntry.nOffset;
}
else
- pThm->getGalleryBinaryEngine()->ImplWriteSgaObject(rObj, nInsertPos, nullptr, m_aDestDir, aObjectList);
+ pThm->getGalleryBinaryEngine()->implWriteSgaObject(rObj, nInsertPos, nullptr, m_aDestDir, aObjectList);
ImplSetModified(true);
ImplBroadcast(pFoundEntry? iFoundPos: nInsertPos);
@@ -366,7 +204,7 @@ bool GalleryTheme::InsertObject(const SgaObject& rObj, sal_uInt32 nInsertPos)
std::unique_ptr<SgaObject> GalleryTheme::AcquireObject(sal_uInt32 nPos)
{
- return ImplReadSgaObject(ImplGetGalleryObject(nPos));
+ return pThm->getGalleryBinaryEngine()->implReadSgaObject(ImplGetGalleryObject(nPos));
}
void GalleryTheme::GetPreviewBitmapExAndStrings(sal_uInt32 nPos, BitmapEx& rBitmapEx, Size& rSize, OUString& rTitle, OUString& rPath) const
@@ -599,7 +437,9 @@ void GalleryTheme::Actualize( const Link<const INetURLObject&, void>& rActualize
KillFile( aTmpURL );
ImplSetModified( true );
- ImplWrite();
+ if (pThm->IsModified())
+ if (!pThm->getGalleryBinaryEngine()->implWrite(*this))
+ ImplSetModified(false);
UnlockBroadcaster();
}
@@ -802,7 +642,7 @@ bool GalleryTheme::InsertGraphic(const Graphic& rGraphic, sal_uInt32 nInsertPos)
nExportFormat = ConvertDataFormat::SVM;
}
- const INetURLObject aURL( ImplCreateUniqueURL( SgaObjKind::Bitmap, nExportFormat ) );
+ const INetURLObject aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::Bitmap, GetParent()->GetUserURL(), aObjectList, nExportFormat ) );
std::unique_ptr<SvStream> pOStm(::utl::UcbStreamHelper::CreateStream( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::WRITE | StreamMode::TRUNC ));
if( pOStm )
@@ -869,7 +709,7 @@ bool GalleryTheme::GetModel(sal_uInt32 nPos, SdrModel& rModel)
bool GalleryTheme::InsertModel(const FmFormModel& rModel, sal_uInt32 nInsertPos)
{
- INetURLObject aURL( ImplCreateUniqueURL( SgaObjKind::SvDraw ) );
+ INetURLObject aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::SvDraw, GetParent()->GetUserURL(), aObjectList) );
tools::SvRef<SotStorage> xStor( GetSvDrawStorage() );
bool bRet = false;
@@ -965,7 +805,7 @@ bool GalleryTheme::GetModelStream(sal_uInt32 nPos, tools::SvRef<SotStorageStream
bool GalleryTheme::InsertModelStream(const tools::SvRef<SotStorageStream>& rxModelStream, sal_uInt32 nInsertPos)
{
- INetURLObject aURL( ImplCreateUniqueURL( SgaObjKind::SvDraw ) );
+ INetURLObject aURL( GalleryBinaryEngine::implCreateUniqueURL( SgaObjKind::SvDraw, GetParent()->GetUserURL(), aObjectList ) );
tools::SvRef<SotStorage> xStor( GetSvDrawStorage() );
bool bRet = false;
@@ -1396,11 +1236,6 @@ SvStream& GalleryTheme::ReadData( SvStream& rIStm )
return rIStm;
}
-SvStream& WriteGalleryTheme( SvStream& rOut, const GalleryTheme& rTheme )
-{
- return rTheme.WriteData( rOut );
-}
-
SvStream& ReadGalleryTheme( SvStream& rIn, GalleryTheme& rTheme )
{
return rTheme.ReadData( rIn );