Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/desktop/source/deployment/manager/dp_activepackages.cxx
Line
Count
Source
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 <config_extensions.h>
21
22
#include <sal/config.h>
23
24
#include <string_view>
25
#include <utility>
26
27
#include <osl/diagnose.h>
28
#include <rtl/string.hxx>
29
#include <rtl/textenc.h>
30
#include <rtl/ustring.hxx>
31
32
#include <dp_identifier.hxx>
33
#include "dp_activepackages.hxx"
34
35
// Old format of database entry:
36
//   key: UTF8(filename)
37
//   value: UTF8(tempname ";" mediatype)
38
// New format of database entry:
39
//   key: 0xFF UTF8(identifier)
40
//   value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
41
42
#if HAVE_FEATURE_EXTENSIONS
43
44
namespace {
45
46
constexpr const char separator[] = "\xff";
47
48
OString oldKey(std::u16string_view fileName) {
49
    return OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
50
}
51
52
OString newKey(std::u16string_view id) {
53
    return separator + OUStringToOString(id, RTL_TEXTENCODING_UTF8);
54
}
55
56
::dp_manager::ActivePackages::Data decodeOldData(
57
    OUString const & fileName, OString const & value)
58
{
59
    ::dp_manager::ActivePackages::Data d;
60
    sal_Int32 i = value.indexOf(';');
61
    OSL_ASSERT(i >= 0);
62
    d.temporaryName = OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
63
    d.fileName = fileName;
64
    d.mediaType = OUString(
65
        value.getStr() + i + 1, value.getLength() - i - 1,
66
        RTL_TEXTENCODING_UTF8);
67
    return d;
68
}
69
70
::dp_manager::ActivePackages::Data decodeNewData(OString const & value) {
71
    ::dp_manager::ActivePackages::Data d;
72
    sal_Int32 i1 = value.indexOf(separator);
73
    OSL_ASSERT(i1 >= 0);
74
    d.temporaryName = OUString(
75
        value.getStr(), i1, RTL_TEXTENCODING_UTF8);
76
    sal_Int32 i2 = value.indexOf(separator, i1 + 1);
77
    OSL_ASSERT(i2 >= 0);
78
    d.fileName = OUString(
79
        value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
80
    sal_Int32 i3 = value.indexOf(separator, i2 + 1);
81
82
    if (i3 < 0)
83
    {
84
        //Before ActivePackages::Data::version was added
85
        d.mediaType = OUString(
86
            value.getStr() + i2 + 1, value.getLength() - i2 - 1,
87
            RTL_TEXTENCODING_UTF8);
88
    }
89
    else
90
    {
91
        sal_Int32 i4 = value.indexOf(separator, i3 + 1);
92
        d.mediaType = OUString(
93
            value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
94
        d.version = OUString(
95
            value.getStr() + i3 + 1, i4 - i3 - 1,
96
            RTL_TEXTENCODING_UTF8);
97
        d.failedPrerequisites = OUString(
98
            value.getStr() + i4 + 1, value.getLength() - i4 - 1,
99
            RTL_TEXTENCODING_UTF8);
100
    }
101
    return d;
102
}
103
104
}
105
#endif
106
107
namespace dp_manager {
108
109
0
ActivePackages::ActivePackages() {}
110
111
ActivePackages::ActivePackages(OUString const & url)
112
#if HAVE_FEATURE_EXTENSIONS
113
    : m_map(url)
114
#endif
115
0
{
116
0
#if !HAVE_FEATURE_EXTENSIONS
117
0
    (void) url;
118
0
#endif
119
0
}
120
121
0
ActivePackages::~ActivePackages() {}
122
123
bool ActivePackages::has(
124
    OUString const & id, OUString const & fileName) const
125
0
{
126
0
    return get(nullptr, id, fileName);
127
0
}
128
129
bool ActivePackages::get(
130
    Data * data, OUString const & id, OUString const & fileName)
131
    const
132
0
{
133
#if HAVE_FEATURE_EXTENSIONS
134
    OString v;
135
    if (m_map.get(&v, newKey(id))) {
136
        if (data != nullptr) {
137
            *data = decodeNewData(v);
138
        }
139
        return true;
140
    } else if (m_map.get(&v, oldKey(fileName))) {
141
        if (data != nullptr) {
142
            *data = decodeOldData(fileName, v);
143
        }
144
        return true;
145
    } else {
146
        return false;
147
    }
148
#else
149
0
    (void) data;
150
0
    (void) id;
151
0
    (void) fileName;
152
0
    (void) this;
153
0
    return false;
154
0
#endif
155
0
}
156
157
0
ActivePackages::Entries ActivePackages::getEntries() const {
158
0
    Entries es;
159
#if HAVE_FEATURE_EXTENSIONS
160
    ::dp_misc::t_string2string_map m(m_map.getEntries());
161
    for (auto const& elem : m)
162
    {
163
        if (!elem.first.isEmpty() && elem.first[0] == separator[0]) {
164
            es.emplace_back(
165
                    OUString(
166
                        elem.first.getStr() + 1, elem.first.getLength() - 1,
167
                        RTL_TEXTENCODING_UTF8),
168
                    decodeNewData(elem.second));
169
        } else {
170
            OUString fn(
171
                OStringToOUString(elem.first, RTL_TEXTENCODING_UTF8));
172
            es.emplace_back(
173
                    ::dp_misc::generateLegacyIdentifier(fn),
174
                    decodeOldData(fn, elem.second));
175
        }
176
    }
177
#else
178
0
    (void) this;
179
0
#endif
180
0
    return es;
181
0
}
182
183
0
void ActivePackages::put(OUString const & id, Data const & data) {
184
#if HAVE_FEATURE_EXTENSIONS
185
    OString b =
186
        OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8) +
187
        separator +
188
        OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8) +
189
        separator +
190
        OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8) +
191
        separator +
192
        OUStringToOString(data.version, RTL_TEXTENCODING_UTF8) +
193
        separator +
194
        OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8);
195
    m_map.put(newKey(id), b);
196
#else
197
0
    (void) id;
198
0
    (void) data;
199
0
    (void) this;
200
0
#endif
201
0
}
202
203
void ActivePackages::erase(
204
    OUString const & id, OUString const & fileName)
205
0
{
206
#if HAVE_FEATURE_EXTENSIONS
207
    m_map.erase(newKey(id)) || m_map.erase(oldKey(fileName));
208
#else
209
0
    (void) id;
210
0
    (void) fileName;
211
0
    (void) this;
212
0
#endif
213
0
}
214
215
}
216
217
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */