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