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"
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
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
) {
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(';');
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
);
79 ::dp_manager::ActivePackages::Data
decodeNewData(OString
const & value
) {
80 ::dp_manager::ActivePackages::Data d
;
81 sal_Int32 i1
= value
.indexOf(separator
);
83 d
.temporaryName
= OUString(
84 value
.getStr(), i1
, RTL_TEXTENCODING_UTF8
);
85 sal_Int32 i2
= value
.indexOf(separator
, i1
+ 1);
87 d
.fileName
= OUString(
88 value
.getStr() + i1
+ 1, i2
- i1
- 1, RTL_TEXTENCODING_UTF8
);
89 sal_Int32 i3
= value
.indexOf(separator
, i2
+ 1);
93 //Before ActivePackages::Data::version was added
94 d
.mediaType
= OUString(
95 value
.getStr() + i2
+ 1, value
.getLength() - i2
- 1,
96 RTL_TEXTENCODING_UTF8
);
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
);
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
)
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
)
141 #if HAVE_FEATURE_EXTENSIONS
143 if (m_map
.get(&v
, newKey(id
))) {
145 *data
= decodeNewData(v
);
148 } else if (m_map
.get(&v
, oldKey(fileName
))) {
150 *data
= decodeOldData(fileName
, v
);
164 ActivePackages::Entries
ActivePackages::getEntries() const {
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());
171 if (!i
->first
.isEmpty() && i
->first
[0] == separator
) {
175 i
->first
.getStr() + 1, i
->first
.getLength() - 1,
176 RTL_TEXTENCODING_UTF8
),
177 decodeNewData(i
->second
)));
180 OStringToOUString(i
->first
, RTL_TEXTENCODING_UTF8
));
183 ::dp_misc::generateLegacyIdentifier(fn
),
184 decodeOldData(fn
, i
->second
)));
191 void ActivePackages::put(OUString
const & id
, Data
const & data
) {
192 #if HAVE_FEATURE_EXTENSIONS
195 OUStringToOString(data
.temporaryName
, RTL_TEXTENCODING_UTF8
));
197 b
.append(OUStringToOString(data
.fileName
, RTL_TEXTENCODING_UTF8
));
199 b
.append(OUStringToOString(data
.mediaType
, RTL_TEXTENCODING_UTF8
));
201 b
.append(OUStringToOString(data
.version
, RTL_TEXTENCODING_UTF8
));
203 b
.append(OUStringToOString(data
.failedPrerequisites
, RTL_TEXTENCODING_UTF8
));
204 m_map
.put(newKey(id
), b
.makeStringAndClear());
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);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */