Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / desktop / source / deployment / manager / dp_activepackages.cxx
blobab2cbf0d4a9fce566c1fb8e0b8f1839200823775
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 <config_features.h>
22 #include <sal/config.h>
24 #include <cstddef>
25 #include <utility>
26 #include <vector>
28 #include <osl/diagnose.h>
29 #include <rtl/strbuf.hxx>
30 #include <rtl/string.hxx>
31 #include <rtl/textenc.h>
32 #include <rtl/uri.h>
33 #include <rtl/uri.hxx>
34 #include <rtl/ustring.hxx>
36 #include <dp_identifier.hxx>
37 #include "dp_activepackages.hxx"
39 // Old format of database entry:
40 // key: UTF8(filename)
41 // value: UTF8(tempname ";" mediatype)
42 // New format of database entry:
43 // key: 0xFF UTF8(identifier)
44 // value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
46 #if HAVE_FEATURE_EXTENSIONS
48 namespace {
50 constexpr const char separator[] = "\xff";
52 OString oldKey(OUString const & fileName) {
53 return OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
56 OString newKey(OUString const & id) {
57 return separator + OUStringToOString(id, RTL_TEXTENCODING_UTF8);
60 ::dp_manager::ActivePackages::Data decodeOldData(
61 OUString const & fileName, OString const & value)
63 ::dp_manager::ActivePackages::Data d;
64 sal_Int32 i = value.indexOf(';');
65 OSL_ASSERT(i >= 0);
66 d.temporaryName = OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
67 d.fileName = fileName;
68 d.mediaType = OUString(
69 value.getStr() + i + 1, value.getLength() - i - 1,
70 RTL_TEXTENCODING_UTF8);
71 return d;
74 ::dp_manager::ActivePackages::Data decodeNewData(OString const & value) {
75 ::dp_manager::ActivePackages::Data d;
76 sal_Int32 i1 = value.indexOf(separator);
77 OSL_ASSERT(i1 >= 0);
78 d.temporaryName = OUString(
79 value.getStr(), i1, RTL_TEXTENCODING_UTF8);
80 sal_Int32 i2 = value.indexOf(separator, i1 + 1);
81 OSL_ASSERT(i2 >= 0);
82 d.fileName = OUString(
83 value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
84 sal_Int32 i3 = value.indexOf(separator, i2 + 1);
86 if (i3 < 0)
88 //Before ActivePackages::Data::version was added
89 d.mediaType = OUString(
90 value.getStr() + i2 + 1, value.getLength() - i2 - 1,
91 RTL_TEXTENCODING_UTF8);
93 else
95 sal_Int32 i4 = value.indexOf(separator, i3 + 1);
96 d.mediaType = OUString(
97 value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
98 d.version = OUString(
99 value.getStr() + i3 + 1, i4 - i3 - 1,
100 RTL_TEXTENCODING_UTF8);
101 d.failedPrerequisites = OUString(
102 value.getStr() + i4 + 1, value.getLength() - i4 - 1,
103 RTL_TEXTENCODING_UTF8);
105 return d;
109 #endif
111 namespace dp_manager {
113 ActivePackages::ActivePackages() {}
115 ActivePackages::ActivePackages(OUString const & url)
116 #if HAVE_FEATURE_EXTENSIONS
117 : m_map(url)
118 #endif
120 #if !HAVE_FEATURE_EXTENSIONS
121 (void) url;
122 #endif
125 ActivePackages::~ActivePackages() {}
127 bool ActivePackages::has(
128 OUString const & id, OUString const & fileName) const
130 return get(nullptr, id, fileName);
133 bool ActivePackages::get(
134 Data * data, OUString const & id, OUString const & fileName)
135 const
137 #if HAVE_FEATURE_EXTENSIONS
138 OString v;
139 if (m_map.get(&v, newKey(id))) {
140 if (data != nullptr) {
141 *data = decodeNewData(v);
143 return true;
144 } else if (m_map.get(&v, oldKey(fileName))) {
145 if (data != nullptr) {
146 *data = decodeOldData(fileName, v);
148 return true;
149 } else {
150 return false;
152 #else
153 (void) data;
154 (void) id;
155 (void) fileName;
156 (void) this;
157 return false;
158 #endif
161 ActivePackages::Entries ActivePackages::getEntries() const {
162 Entries es;
163 #if HAVE_FEATURE_EXTENSIONS
164 ::dp_misc::t_string2string_map m(m_map.getEntries());
165 for (auto const& elem : m)
167 if (!elem.first.isEmpty() && elem.first[0] == separator[0]) {
168 es.emplace_back(
169 OUString(
170 elem.first.getStr() + 1, elem.first.getLength() - 1,
171 RTL_TEXTENCODING_UTF8),
172 decodeNewData(elem.second));
173 } else {
174 OUString fn(
175 OStringToOUString(elem.first, RTL_TEXTENCODING_UTF8));
176 es.emplace_back(
177 ::dp_misc::generateLegacyIdentifier(fn),
178 decodeOldData(fn, elem.second));
181 #else
182 (void) this;
183 #endif
184 return es;
187 void ActivePackages::put(OUString const & id, Data const & data) {
188 #if HAVE_FEATURE_EXTENSIONS
189 OString b =
190 OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8) +
191 separator +
192 OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8) +
193 separator +
194 OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8) +
195 separator +
196 OUStringToOString(data.version, RTL_TEXTENCODING_UTF8) +
197 separator +
198 OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8);
199 m_map.put(newKey(id), b);
200 #else
201 (void) id;
202 (void) data;
203 (void) this;
204 #endif
207 void ActivePackages::erase(
208 OUString const & id, OUString const & fileName)
210 #if HAVE_FEATURE_EXTENSIONS
211 m_map.erase(newKey(id)) || m_map.erase(oldKey(fileName));
212 #else
213 (void) id;
214 (void) fileName;
215 (void) this;
216 #endif
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */