tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / desktop / source / deployment / manager / dp_activepackages.cxx
blobc1c5f2b28dba62a697723bc9b9ee24c977d40c3c
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_extensions.h>
22 #include <sal/config.h>
24 #include <string_view>
25 #include <utility>
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
44 namespace {
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(';');
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;
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);
82 if (i3 < 0)
84 //Before ActivePackages::Data::version was added
85 d.mediaType = OUString(
86 value.getStr() + i2 + 1, value.getLength() - i2 - 1,
87 RTL_TEXTENCODING_UTF8);
89 else
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);
101 return d;
105 #endif
107 namespace dp_manager {
109 ActivePackages::ActivePackages() {}
111 ActivePackages::ActivePackages(OUString const & url)
112 #if HAVE_FEATURE_EXTENSIONS
113 : m_map(url)
114 #endif
116 #if !HAVE_FEATURE_EXTENSIONS
117 (void) url;
118 #endif
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)
131 const
133 #if HAVE_FEATURE_EXTENSIONS
134 OString v;
135 if (m_map.get(&v, newKey(id))) {
136 if (data != nullptr) {
137 *data = decodeNewData(v);
139 return true;
140 } else if (m_map.get(&v, oldKey(fileName))) {
141 if (data != nullptr) {
142 *data = decodeOldData(fileName, v);
144 return true;
145 } else {
146 return false;
148 #else
149 (void) data;
150 (void) id;
151 (void) fileName;
152 (void) this;
153 return false;
154 #endif
157 ActivePackages::Entries ActivePackages::getEntries() const {
158 Entries es;
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]) {
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));
177 #else
178 (void) this;
179 #endif
180 return es;
183 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 (void) id;
198 (void) data;
199 (void) this;
200 #endif
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));
208 #else
209 (void) id;
210 (void) fileName;
211 (void) this;
212 #endif
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */