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/gallery1.hxx>
23
25#include <tools/urlobj.hxx>
26#include <tools/vcompat.hxx>
27
28#include <com/sun/star/ucb/ContentCreationException.hpp>
29
30static bool FileExists(const INetURLObject& rURL, std::u16string_view rExt)
31{
32 INetURLObject aURL(rURL);
33 aURL.setExtension(rExt);
34 return FileExists(aURL);
35}
36
38{
39 mpGalleryStorageLocations = std::make_unique<GalleryBinaryStorageLocations>();
40}
41
43{
44 mpGalleryStorageLocations->SetStorageLocations(rURL);
45}
46
47std::unique_ptr<GalleryBinaryEngine> GalleryBinaryEngineEntry::createGalleryStorageEngine(
48 GalleryObjectCollection& mrGalleryObjectCollection, bool& bReadOnly)
49{
50 return std::make_unique<GalleryBinaryEngine>(*mpGalleryStorageLocations,
51 mrGalleryObjectCollection, bReadOnly);
52}
53
55{
57 aURL = aBaseNoCase;
58 static sal_Int32 nIdx = 0;
59 while (FileExists(aURL, u"thm"))
60 { // create new URLs
61 nIdx++;
62 aURL = aBaseNoCase;
63 aURL.setName(OUStringConcatenation(aURL.getName() + OUString::number(nIdx)));
64 }
65}
66
68 bool bReadOnly)
69{
70 DBG_ASSERT(rURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
71
72 GalleryThemeEntry* pRet = nullptr;
73
74 if (FileExists(rURL))
75 {
76 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
77 rURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
78
79 if (pIStm)
80 {
81 sal_uInt16 nVersion;
82
83 pIStm->ReadUInt16(nVersion);
84
85 if (nVersion <= 0x00ff)
86 {
87 bool bThemeNameFromResource = false;
88 sal_uInt32 nThemeId = 0;
89
90 OString aTmpStr = read_uInt16_lenPrefixed_uInt8s_ToOString(*pIStm);
91 OUString aThemeName = OStringToOUString(aTmpStr, RTL_TEXTENCODING_UTF8);
92
93 // execute a character conversion
94 if (nVersion >= 0x0004)
95 {
96 sal_uInt32 nCount;
97 sal_uInt16 nTemp16;
98
99 pIStm->ReadUInt32(nCount).ReadUInt16(nTemp16);
100 pIStm->Seek(STREAM_SEEK_TO_END);
101
102 // check whether there is a newer version;
103 // therefore jump back by 520Bytes (8 bytes ID + 512Bytes reserve buffer)
104 // if this is at all possible.
105 if (pIStm->Tell() >= 520)
106 {
107 sal_uInt32 nId1, nId2;
108
109 pIStm->SeekRel(-520);
110 pIStm->ReadUInt32(nId1).ReadUInt32(nId2);
111
112 if (nId1 == COMPAT_FORMAT('G', 'A', 'L', 'R')
113 && nId2 == COMPAT_FORMAT('E', 'S', 'R', 'V'))
114 {
115 VersionCompatRead aCompat(*pIStm);
116
117 pIStm->ReadUInt32(nThemeId);
118
119 if (aCompat.GetVersion() >= 2)
120 {
121 pIStm->ReadCharAsBool(bThemeNameFromResource);
122 }
123 }
124 }
125 }
126
127 pRet = new GalleryThemeEntry(false, rURL, aThemeName, bReadOnly, false, nThemeId,
128 bThemeNameFromResource);
129 }
130 }
131 }
132
133 return pRet;
134}
135
137{
138 INetURLObject aThmURL(GetThmURL());
139 INetURLObject aSdgURL(GetSdgURL());
140 INetURLObject aSdvURL(GetSdvURL());
141 INetURLObject aStrURL(GetStrURL());
142
143 KillFile(aThmURL);
144 KillFile(aSdgURL);
145 KillFile(aSdvURL);
146 KillFile(aStrURL);
147}
148
149std::unique_ptr<GalleryTheme>&
150GalleryBinaryEngineEntry::getCachedTheme(std::unique_ptr<GalleryTheme>& pNewTheme)
151{
153
154 DBG_ASSERT(aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
155
156 if (FileExists(aURL))
157 {
158 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
159 aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
160
161 if (pIStm)
162 {
163 try
164 {
165 ReadGalleryTheme(*pIStm, *pNewTheme);
166
167 if (pIStm->GetError())
168 pNewTheme.reset();
169 }
170 catch (const css::ucb::ContentCreationException&)
171 {
172 }
173 }
174 }
175 return pNewTheme;
176}
177
178SvStream& ReadGalleryTheme(SvStream& rIn, GalleryTheme& rTheme) { return rTheme.ReadData(rIn); }
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static GalleryThemeEntry * CreateThemeEntry(const INetURLObject &rURL, bool bReadOnly)
static void CreateUniqueURL(const INetURLObject &rBaseURL, INetURLObject &aURL)
std::unique_ptr< GalleryBinaryStorageLocations > mpGalleryStorageLocations
const INetURLObject & GetSdvURL() const
const INetURLObject & GetSdgURL() const
std::unique_ptr< GalleryTheme > & getCachedTheme(std::unique_ptr< GalleryTheme > &pNewTheme)
const INetURLObject & GetThmURL() const
void setStorageLocations(INetURLObject &rURL)
const INetURLObject & GetStrURL() const
std::unique_ptr< GalleryBinaryEngine > createGalleryStorageEngine(GalleryObjectCollection &mrGalleryObjectCollection, bool &bReadOnly)
static INetURLObject ImplGetURLIgnoreCase(const INetURLObject &rURL)
SAL_DLLPRIVATE SvStream & ReadData(SvStream &rIn)
Definition: galtheme.cxx:642
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
INetProtocol GetProtocol() const
sal_uInt16 GetVersion() const
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
int nCount
#define DBG_ASSERT(sCon, aError)
URL aURL
float u
sal_Int16 nVersion
bool bReadOnly
static bool FileExists(const INetURLObject &rURL, std::u16string_view rExt)
SvStream & ReadGalleryTheme(SvStream &rIn, GalleryTheme &rTheme)
bool KillFile(const INetURLObject &rURL)
Definition: galmisc.cxx:287
OString read_uInt16_lenPrefixed_uInt8s_ToOString(SvStream &rStrm)
#define STREAM_SEEK_TO_END
sal_uInt32 COMPAT_FORMAT(char char1, char char2, char char3, char char4)