LibreOffice Module svx (master)  1
gallerybinaryengineentry.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
21 #include <svx/galmisc.hxx>
22 #include <svx/unomodel.hxx>
23 #include <svx/fmmodel.hxx>
24 #include <svx/gallery1.hxx>
25 #include "codec.hxx"
26 #include "gallerydrawmodel.hxx"
27 
28 #include <sal/log.hxx>
29 
31 #include <unotools/streamwrap.hxx>
32 #include <tools/urlobj.hxx>
33 #include <tools/vcompat.hxx>
34 
35 #include <com/sun/star/ucb/ContentCreationException.hpp>
36 
37 static bool FileExists(const INetURLObject& rURL, const OUString& rExt)
38 {
39  INetURLObject aURL(rURL);
40  aURL.setExtension(rExt);
41  return FileExists(aURL);
42 }
43 
45 {
46  mpGalleryStorageLocations = std::make_unique<GalleryBinaryStorageLocations>();
47 }
48 
50 {
51  mpGalleryStorageLocations->SetStorageLocations(rURL);
52 }
53 
54 std::unique_ptr<GalleryBinaryEngine> GalleryBinaryEngineEntry::createGalleryStorageEngine(
55  GalleryObjectCollection& mrGalleryObjectCollection, bool& bReadOnly)
56 {
57  return std::make_unique<GalleryBinaryEngine>(*mpGalleryStorageLocations,
58  mrGalleryObjectCollection, bReadOnly);
59 }
60 
62 {
64  aURL = aBaseNoCase;
65  static sal_Int32 nIdx = 0;
66  while (FileExists(aURL, "thm"))
67  { // create new URLs
68  nIdx++;
69  aURL = aBaseNoCase;
70  aURL.setName(aURL.getName() + OUString::number(nIdx));
71  }
72 }
73 
75  bool bReadOnly)
76 {
77  DBG_ASSERT(rURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
78 
79  GalleryThemeEntry* pRet = nullptr;
80 
81  if (FileExists(rURL))
82  {
83  std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
84  rURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
85 
86  if (pIStm)
87  {
88  sal_uInt16 nVersion;
89 
90  pIStm->ReadUInt16(nVersion);
91 
92  if (nVersion <= 0x00ff)
93  {
94  bool bThemeNameFromResource = false;
95  sal_uInt32 nThemeId = 0;
96 
97  OString aTmpStr = read_uInt16_lenPrefixed_uInt8s_ToOString(*pIStm);
98  OUString aThemeName = OStringToOUString(aTmpStr, RTL_TEXTENCODING_UTF8);
99 
100  // execute a character conversion
101  if (nVersion >= 0x0004)
102  {
103  sal_uInt32 nCount;
104  sal_uInt16 nTemp16;
105 
106  pIStm->ReadUInt32(nCount).ReadUInt16(nTemp16);
107  pIStm->Seek(STREAM_SEEK_TO_END);
108 
109  // check whether there is a newer version;
110  // therefore jump back by 520Bytes (8 bytes ID + 512Bytes reserve buffer)
111  // if this is at all possible.
112  if (pIStm->Tell() >= 520)
113  {
114  sal_uInt32 nId1, nId2;
115 
116  pIStm->SeekRel(-520);
117  pIStm->ReadUInt32(nId1).ReadUInt32(nId2);
118 
119  if (nId1 == COMPAT_FORMAT('G', 'A', 'L', 'R')
120  && nId2 == COMPAT_FORMAT('E', 'S', 'R', 'V'))
121  {
122  VersionCompat aCompat(*pIStm, StreamMode::READ);
123 
124  pIStm->ReadUInt32(nThemeId);
125 
126  if (aCompat.GetVersion() >= 2)
127  {
128  pIStm->ReadCharAsBool(bThemeNameFromResource);
129  }
130  }
131  }
132  }
133 
134  pRet = new GalleryThemeEntry(false, rURL, aThemeName, bReadOnly, false, nThemeId,
135  bThemeNameFromResource);
136  }
137  }
138  }
139 
140  return pRet;
141 }
142 
144 {
145  INetURLObject aThmURL(GetThmURL());
146  INetURLObject aSdgURL(GetSdgURL());
147  INetURLObject aSdvURL(GetSdvURL());
148  INetURLObject aStrURL(GetStrURL());
149 
150  KillFile(aThmURL);
151  KillFile(aSdgURL);
152  KillFile(aSdvURL);
153  KillFile(aStrURL);
154 }
155 
156 std::unique_ptr<GalleryTheme>&
157 GalleryBinaryEngineEntry::getCachedTheme(std::unique_ptr<GalleryTheme>& pNewTheme)
158 {
160 
161  DBG_ASSERT(aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
162 
163  if (FileExists(aURL))
164  {
165  std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
166  aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
167 
168  if (pIStm)
169  {
170  try
171  {
172  ReadGalleryTheme(*pIStm, *pNewTheme);
173 
174  if (pIStm->GetError())
175  pNewTheme.reset();
176  }
177  catch (const css::ucb::ContentCreationException&)
178  {
179  }
180  }
181  }
182  return pNewTheme;
183 }
184 
185 SvStream& ReadGalleryTheme(SvStream& rIn, GalleryTheme& rTheme) { return rTheme.ReadData(rIn); }
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL aURL
std::unique_ptr< GalleryBinaryStorageLocations > mpGalleryStorageLocations
#define STREAM_SEEK_TO_END
bool bReadOnly
OString read_uInt16_lenPrefixed_uInt8s_ToOString(SvStream &rStrm)
const INetURLObject & GetSdgURL() const
bool setName(OUString const &rTheName, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
const INetURLObject & GetStrURL() const
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
bool setExtension(OUString const &rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
std::unique_ptr< GalleryTheme > & getCachedTheme(std::unique_ptr< GalleryTheme > &pNewTheme)
SvStream & ReadGalleryTheme(SvStream &rIn, GalleryTheme &rTheme)
int nCount
static GalleryThemeEntry * CreateThemeEntry(const INetURLObject &rURL, bool bReadOnly)
SAL_DLLPRIVATE SvStream & ReadData(SvStream &rIn)
Definition: galtheme.cxx:654
const INetURLObject & GetThmURL() const
#define DBG_ASSERT(sCon, aError)
sal_uInt32 COMPAT_FORMAT(char char1, char char2, char char3, char char4)
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
sal_Int16 nVersion
bool KillFile(const INetURLObject &rURL)
Definition: galmisc.cxx:288
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static bool FileExists(const INetURLObject &rURL, const OUString &rExt)
static void CreateUniqueURL(const INetURLObject &rBaseURL, INetURLObject &aURL)
INetProtocol GetProtocol() const
const INetURLObject & GetSdvURL() const
static INetURLObject ImplGetURLIgnoreCase(const INetURLObject &rURL)
std::unique_ptr< GalleryBinaryEngine > createGalleryStorageEngine(GalleryObjectCollection &mrGalleryObjectCollection, bool &bReadOnly)
void setStorageLocations(INetURLObject &rURL)