tdf#161412 - UI: fix warning in PDF password dialog didn't disappear
[LibreOffice.git] / svx / source / gallery2 / galleryfilestorageentry.cxx
blob593396aaed376a76e7a2d6c0f4346cf37479a113
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <galleryfilestorageentry.hxx>
21 #include <gallerystoragelocations.hxx>
22 #include <svx/galmisc.hxx>
23 #include <svx/gallery1.hxx>
25 #include <unotools/ucbstreamhelper.hxx>
26 #include <tools/urlobj.hxx>
27 #include <tools/vcompat.hxx>
29 #include <com/sun/star/ucb/ContentCreationException.hpp>
31 static bool FileExists(const INetURLObject& rURL, std::u16string_view rExt)
33 INetURLObject aURL(rURL);
34 aURL.setExtension(rExt);
35 return FileExists(aURL);
38 GalleryFileStorageEntry::GalleryFileStorageEntry() {}
40 void GalleryFileStorageEntry::setStorageLocations(INetURLObject& rURL)
42 maGalleryStorageLocations.SetStorageLocations(rURL);
45 std::unique_ptr<GalleryFileStorage> GalleryFileStorageEntry::createGalleryStorageEngine(
46 GalleryObjectCollection& mrGalleryObjectCollection, bool& bReadOnly)
48 return std::make_unique<GalleryFileStorage>(maGalleryStorageLocations,
49 mrGalleryObjectCollection, bReadOnly);
52 void GalleryFileStorageEntry::CreateUniqueURL(const INetURLObject& rBaseURL, INetURLObject& aURL)
54 INetURLObject aBaseNoCase(GalleryStorageLocations::ImplGetURLIgnoreCase(rBaseURL));
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)));
65 GalleryThemeEntry* GalleryFileStorageEntry::CreateThemeEntry(const INetURLObject& rURL,
66 bool bReadOnly)
68 DBG_ASSERT(rURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
70 GalleryThemeEntry* pRet = nullptr;
72 if (FileExists(rURL))
74 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
75 rURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
77 if (pIStm)
79 sal_uInt16 nVersion;
81 pIStm->ReadUInt16(nVersion);
83 if (nVersion <= 0x00ff)
85 bool bThemeNameFromResource = false;
86 sal_uInt32 nThemeId = 0;
88 OString aTmpStr = read_uInt16_lenPrefixed_uInt8s_ToOString(*pIStm);
89 OUString aThemeName = OStringToOUString(aTmpStr, RTL_TEXTENCODING_UTF8);
91 // execute a character conversion
92 if (nVersion >= 0x0004)
94 sal_uInt32 nCount;
95 sal_uInt16 nTemp16;
97 pIStm->ReadUInt32(nCount).ReadUInt16(nTemp16);
98 pIStm->Seek(STREAM_SEEK_TO_END);
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)
105 sal_uInt32 nId1, nId2;
107 pIStm->SeekRel(-520);
108 pIStm->ReadUInt32(nId1).ReadUInt32(nId2);
110 if (nId1 == COMPAT_FORMAT('G', 'A', 'L', 'R')
111 && nId2 == COMPAT_FORMAT('E', 'S', 'R', 'V'))
113 VersionCompatRead aCompat(*pIStm);
115 pIStm->ReadUInt32(nThemeId);
117 if (aCompat.GetVersion() >= 2)
119 pIStm->ReadCharAsBool(bThemeNameFromResource);
125 pRet = new GalleryThemeEntry(false, rURL, aThemeName, bReadOnly, false, nThemeId,
126 bThemeNameFromResource);
131 return pRet;
134 void GalleryFileStorageEntry::removeTheme()
136 INetURLObject aThmURL(GetThmURL());
137 INetURLObject aSdgURL(GetSdgURL());
138 INetURLObject aSdvURL(GetSdvURL());
139 INetURLObject aStrURL(GetStrURL());
141 KillFile(aThmURL);
142 KillFile(aSdgURL);
143 KillFile(aSdvURL);
144 KillFile(aStrURL);
147 std::unique_ptr<GalleryTheme>&
148 GalleryFileStorageEntry::getCachedTheme(std::unique_ptr<GalleryTheme>& pNewTheme)
150 INetURLObject aURL = GetThmURL();
152 DBG_ASSERT(aURL.GetProtocol() != INetProtocol::NotValid, "invalid URL");
154 if (FileExists(aURL))
156 std::unique_ptr<SvStream> pIStm(::utl::UcbStreamHelper::CreateStream(
157 aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE), StreamMode::READ));
159 if (pIStm)
163 ReadGalleryTheme(*pIStm, *pNewTheme);
165 if (pIStm->GetError())
166 pNewTheme.reset();
168 catch (const css::ucb::ContentCreationException&)
173 return pNewTheme;
176 SvStream& ReadGalleryTheme(SvStream& rIn, GalleryTheme& rTheme) { return rTheme.ReadData(rIn); }
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */