Coverage Report

Created: 2025-07-07 10:01

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