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_features.h>
22 #include <sal/config.h>
28 #include <osl/diagnose.h>
29 #include <rtl/strbuf.hxx>
30 #include <rtl/string.hxx>
31 #include <rtl/textenc.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
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(';');
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
);
74 ::dp_manager::ActivePackages::Data
decodeNewData(OString
const & value
) {
75 ::dp_manager::ActivePackages::Data d
;
76 sal_Int32 i1
= value
.indexOf(separator
);
78 d
.temporaryName
= OUString(
79 value
.getStr(), i1
, RTL_TEXTENCODING_UTF8
);
80 sal_Int32 i2
= value
.indexOf(separator
, i1
+ 1);
82 d
.fileName
= OUString(
83 value
.getStr() + i1
+ 1, i2
- i1
- 1, RTL_TEXTENCODING_UTF8
);
84 sal_Int32 i3
= value
.indexOf(separator
, i2
+ 1);
88 //Before ActivePackages::Data::version was added
89 d
.mediaType
= OUString(
90 value
.getStr() + i2
+ 1, value
.getLength() - i2
- 1,
91 RTL_TEXTENCODING_UTF8
);
95 sal_Int32 i4
= value
.indexOf(separator
, i3
+ 1);
96 d
.mediaType
= OUString(
97 value
.getStr() + i2
+ 1, i3
- i2
-1, RTL_TEXTENCODING_UTF8
);
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
);
111 namespace dp_manager
{
113 ActivePackages::ActivePackages() {}
115 ActivePackages::ActivePackages(OUString
const & url
)
116 #if HAVE_FEATURE_EXTENSIONS
120 #if !HAVE_FEATURE_EXTENSIONS
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
)
137 #if HAVE_FEATURE_EXTENSIONS
139 if (m_map
.get(&v
, newKey(id
))) {
140 if (data
!= nullptr) {
141 *data
= decodeNewData(v
);
144 } else if (m_map
.get(&v
, oldKey(fileName
))) {
145 if (data
!= nullptr) {
146 *data
= decodeOldData(fileName
, v
);
161 ActivePackages::Entries
ActivePackages::getEntries() const {
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]) {
170 elem
.first
.getStr() + 1, elem
.first
.getLength() - 1,
171 RTL_TEXTENCODING_UTF8
),
172 decodeNewData(elem
.second
));
175 OStringToOUString(elem
.first
, RTL_TEXTENCODING_UTF8
));
177 ::dp_misc::generateLegacyIdentifier(fn
),
178 decodeOldData(fn
, elem
.second
));
187 void ActivePackages::put(OUString
const & id
, Data
const & data
) {
188 #if HAVE_FEATURE_EXTENSIONS
190 OUStringToOString(data
.temporaryName
, RTL_TEXTENCODING_UTF8
) +
192 OUStringToOString(data
.fileName
, RTL_TEXTENCODING_UTF8
) +
194 OUStringToOString(data
.mediaType
, RTL_TEXTENCODING_UTF8
) +
196 OUStringToOString(data
.version
, RTL_TEXTENCODING_UTF8
) +
198 OUStringToOString(data
.failedPrerequisites
, RTL_TEXTENCODING_UTF8
);
199 m_map
.put(newKey(id
), b
);
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
));
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */