1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <config_extensions.h>
22 #include <sal/config.h>
24 #include <string_view>
27 #include <osl/diagnose.h>
28 #include <rtl/string.hxx>
29 #include <rtl/textenc.h>
30 #include <rtl/ustring.hxx>
32 #include <dp_identifier.hxx>
33 #include "dp_activepackages.hxx"
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)
42 #if HAVE_FEATURE_EXTENSIONS
46 constexpr const char separator
[] = "\xff";
48 OString
oldKey(std::u16string_view fileName
) {
49 return OUStringToOString(fileName
, RTL_TEXTENCODING_UTF8
);
52 OString
newKey(std::u16string_view id
) {
53 return separator
+ OUStringToOString(id
, RTL_TEXTENCODING_UTF8
);
56 ::dp_manager::ActivePackages::Data
decodeOldData(
57 OUString
const & fileName
, OString
const & value
)
59 ::dp_manager::ActivePackages::Data d
;
60 sal_Int32 i
= value
.indexOf(';');
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
);
70 ::dp_manager::ActivePackages::Data
decodeNewData(OString
const & value
) {
71 ::dp_manager::ActivePackages::Data d
;
72 sal_Int32 i1
= value
.indexOf(separator
);
74 d
.temporaryName
= OUString(
75 value
.getStr(), i1
, RTL_TEXTENCODING_UTF8
);
76 sal_Int32 i2
= value
.indexOf(separator
, i1
+ 1);
78 d
.fileName
= OUString(
79 value
.getStr() + i1
+ 1, i2
- i1
- 1, RTL_TEXTENCODING_UTF8
);
80 sal_Int32 i3
= value
.indexOf(separator
, i2
+ 1);
84 //Before ActivePackages::Data::version was added
85 d
.mediaType
= OUString(
86 value
.getStr() + i2
+ 1, value
.getLength() - i2
- 1,
87 RTL_TEXTENCODING_UTF8
);
91 sal_Int32 i4
= value
.indexOf(separator
, i3
+ 1);
92 d
.mediaType
= OUString(
93 value
.getStr() + i2
+ 1, i3
- i2
-1, RTL_TEXTENCODING_UTF8
);
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
);
107 namespace dp_manager
{
109 ActivePackages::ActivePackages() {}
111 ActivePackages::ActivePackages(OUString
const & url
)
112 #if HAVE_FEATURE_EXTENSIONS
116 #if !HAVE_FEATURE_EXTENSIONS
121 ActivePackages::~ActivePackages() {}
123 bool ActivePackages::has(
124 OUString
const & id
, OUString
const & fileName
) const
126 return get(nullptr, id
, fileName
);
129 bool ActivePackages::get(
130 Data
* data
, OUString
const & id
, OUString
const & fileName
)
133 #if HAVE_FEATURE_EXTENSIONS
135 if (m_map
.get(&v
, newKey(id
))) {
136 if (data
!= nullptr) {
137 *data
= decodeNewData(v
);
140 } else if (m_map
.get(&v
, oldKey(fileName
))) {
141 if (data
!= nullptr) {
142 *data
= decodeOldData(fileName
, v
);
157 ActivePackages::Entries
ActivePackages::getEntries() const {
159 #if HAVE_FEATURE_EXTENSIONS
160 ::dp_misc::t_string2string_map
m(m_map
.getEntries());
161 for (auto const& elem
: m
)
163 if (!elem
.first
.isEmpty() && elem
.first
[0] == separator
[0]) {
166 elem
.first
.getStr() + 1, elem
.first
.getLength() - 1,
167 RTL_TEXTENCODING_UTF8
),
168 decodeNewData(elem
.second
));
171 OStringToOUString(elem
.first
, RTL_TEXTENCODING_UTF8
));
173 ::dp_misc::generateLegacyIdentifier(fn
),
174 decodeOldData(fn
, elem
.second
));
183 void ActivePackages::put(OUString
const & id
, Data
const & data
) {
184 #if HAVE_FEATURE_EXTENSIONS
186 OUStringToOString(data
.temporaryName
, RTL_TEXTENCODING_UTF8
) +
188 OUStringToOString(data
.fileName
, RTL_TEXTENCODING_UTF8
) +
190 OUStringToOString(data
.mediaType
, RTL_TEXTENCODING_UTF8
) +
192 OUStringToOString(data
.version
, RTL_TEXTENCODING_UTF8
) +
194 OUStringToOString(data
.failedPrerequisites
, RTL_TEXTENCODING_UTF8
);
195 m_map
.put(newKey(id
), b
);
203 void ActivePackages::erase(
204 OUString
const & id
, OUString
const & fileName
)
206 #if HAVE_FEATURE_EXTENSIONS
207 m_map
.erase(newKey(id
)) || m_map
.erase(oldKey(fileName
));
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */