update credits
[LibreOffice.git] / desktop / source / deployment / manager / dp_activepackages.cxx
blob99e376945799aaf6ddad99701450df1f61c06741
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"
35 #include <boost/unordered_map.hpp>
37 #include "dp_identifier.hxx"
38 #include "dp_activepackages.hxx"
40 // Old format of database entry:
41 // key: UTF8(filename)
42 // value: UTF8(tempname ";" mediatype)
43 // New format of database entry:
44 // key: 0xFF UTF8(identifier)
45 // value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
47 #if HAVE_FEATURE_EXTENSIONS
49 namespace {
51 static char const separator = static_cast< char >(
52 static_cast< unsigned char >(0xFF));
54 OString oldKey(OUString const & fileName) {
55 return OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
58 OString newKey(OUString const & id) {
59 OStringBuffer b;
60 b.append(separator);
61 b.append(OUStringToOString(id, RTL_TEXTENCODING_UTF8));
62 return b.makeStringAndClear();
65 ::dp_manager::ActivePackages::Data decodeOldData(
66 OUString const & fileName, OString const & value)
68 ::dp_manager::ActivePackages::Data d;
69 sal_Int32 i = value.indexOf(';');
70 OSL_ASSERT(i >= 0);
71 d.temporaryName = OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
72 d.fileName = fileName;
73 d.mediaType = OUString(
74 value.getStr() + i + 1, value.getLength() - i - 1,
75 RTL_TEXTENCODING_UTF8);
76 return d;
79 ::dp_manager::ActivePackages::Data decodeNewData(OString const & value) {
80 ::dp_manager::ActivePackages::Data d;
81 sal_Int32 i1 = value.indexOf(separator);
82 OSL_ASSERT(i1 >= 0);
83 d.temporaryName = OUString(
84 value.getStr(), i1, RTL_TEXTENCODING_UTF8);
85 sal_Int32 i2 = value.indexOf(separator, i1 + 1);
86 OSL_ASSERT(i2 >= 0);
87 d.fileName = OUString(
88 value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
89 sal_Int32 i3 = value.indexOf(separator, i2 + 1);
91 if (i3 < 0)
93 //Before ActivePackages::Data::version was added
94 d.mediaType = OUString(
95 value.getStr() + i2 + 1, value.getLength() - i2 - 1,
96 RTL_TEXTENCODING_UTF8);
98 else
100 sal_Int32 i4 = value.indexOf(separator, i3 + 1);
101 d.mediaType = OUString(
102 value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
103 d.version = OUString(
104 value.getStr() + i3 + 1, i4 - i3 - 1,
105 RTL_TEXTENCODING_UTF8);
106 d.failedPrerequisites = OUString(
107 value.getStr() + i4 + 1, value.getLength() - i4 - 1,
108 RTL_TEXTENCODING_UTF8);
110 return d;
114 #endif
116 namespace dp_manager {
118 ActivePackages::ActivePackages() {}
120 ActivePackages::ActivePackages(OUString const & url, bool readOnly)
121 #if HAVE_FEATURE_EXTENSIONS
122 : m_map(url, readOnly)
123 #endif
125 (void) url;
126 (void) readOnly;
129 ActivePackages::~ActivePackages() {}
131 bool ActivePackages::has(
132 OUString const & id, OUString const & fileName) const
134 return get(NULL, id, fileName);
137 bool ActivePackages::get(
138 Data * data, OUString const & id, OUString const & fileName)
139 const
141 #if HAVE_FEATURE_EXTENSIONS
142 OString v;
143 if (m_map.get(&v, newKey(id))) {
144 if (data != NULL) {
145 *data = decodeNewData(v);
147 return true;
148 } else if (m_map.get(&v, oldKey(fileName))) {
149 if (data != NULL) {
150 *data = decodeOldData(fileName, v);
152 return true;
153 } else {
154 return false;
156 #else
157 (void) data;
158 (void) id;
159 (void) fileName;
160 return false;
161 #endif
164 ActivePackages::Entries ActivePackages::getEntries() const {
165 Entries es;
166 #if HAVE_FEATURE_EXTENSIONS
167 ::dp_misc::t_string2string_map m(m_map.getEntries());
168 for (::dp_misc::t_string2string_map::const_iterator i(m.begin());
169 i != m.end(); ++i)
171 if (!i->first.isEmpty() && i->first[0] == separator) {
172 es.push_back(
173 ::std::make_pair(
174 OUString(
175 i->first.getStr() + 1, i->first.getLength() - 1,
176 RTL_TEXTENCODING_UTF8),
177 decodeNewData(i->second)));
178 } else {
179 OUString fn(
180 OStringToOUString(i->first, RTL_TEXTENCODING_UTF8));
181 es.push_back(
182 ::std::make_pair(
183 ::dp_misc::generateLegacyIdentifier(fn),
184 decodeOldData(fn, i->second)));
187 #endif
188 return es;
191 void ActivePackages::put(OUString const & id, Data const & data) {
192 #if HAVE_FEATURE_EXTENSIONS
193 OStringBuffer b;
194 b.append(
195 OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8));
196 b.append(separator);
197 b.append(OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8));
198 b.append(separator);
199 b.append(OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8));
200 b.append(separator);
201 b.append(OUStringToOString(data.version, RTL_TEXTENCODING_UTF8));
202 b.append(separator);
203 b.append(OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8));
204 m_map.put(newKey(id), b.makeStringAndClear());
205 #else
206 (void) id;
207 (void) data;
208 #endif
211 void ActivePackages::erase(
212 OUString const & id, OUString const & fileName)
214 #if HAVE_FEATURE_EXTENSIONS
215 m_map.erase(newKey(id), true) || m_map.erase(oldKey(fileName), true);
216 #else
217 (void) id;
218 (void) fileName;
219 #endif
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */