LibreOffice Module desktop (master)  1
dp_activepackages.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <config_extensions.h>
21 
22 #include <sal/config.h>
23 
24 #include <utility>
25 
26 #include <osl/diagnose.h>
27 #include <rtl/string.hxx>
28 #include <rtl/textenc.h>
29 #include <rtl/ustring.hxx>
30 
31 #include <dp_identifier.hxx>
32 #include "dp_activepackages.hxx"
33 
34 // Old format of database entry:
35 // key: UTF8(filename)
36 // value: UTF8(tempname ";" mediatype)
37 // New format of database entry:
38 // key: 0xFF UTF8(identifier)
39 // value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
40 
41 #if HAVE_FEATURE_EXTENSIONS
42 
43 namespace {
44 
45 constexpr const char separator[] = "\xff";
46 
47 OString oldKey(OUString const & fileName) {
48  return OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
49 }
50 
51 OString newKey(OUString const & id) {
52  return separator + OUStringToOString(id, RTL_TEXTENCODING_UTF8);
53 }
54 
56  OUString const & fileName, OString const & value)
57 {
59  sal_Int32 i = value.indexOf(';');
60  OSL_ASSERT(i >= 0);
61  d.temporaryName = OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
62  d.fileName = fileName;
63  d.mediaType = OUString(
64  value.getStr() + i + 1, value.getLength() - i - 1,
65  RTL_TEXTENCODING_UTF8);
66  return d;
67 }
68 
69 ::dp_manager::ActivePackages::Data decodeNewData(OString const & value) {
71  sal_Int32 i1 = value.indexOf(separator);
72  OSL_ASSERT(i1 >= 0);
73  d.temporaryName = OUString(
74  value.getStr(), i1, RTL_TEXTENCODING_UTF8);
75  sal_Int32 i2 = value.indexOf(separator, i1 + 1);
76  OSL_ASSERT(i2 >= 0);
77  d.fileName = OUString(
78  value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
79  sal_Int32 i3 = value.indexOf(separator, i2 + 1);
80 
81  if (i3 < 0)
82  {
83  //Before ActivePackages::Data::version was added
84  d.mediaType = OUString(
85  value.getStr() + i2 + 1, value.getLength() - i2 - 1,
86  RTL_TEXTENCODING_UTF8);
87  }
88  else
89  {
90  sal_Int32 i4 = value.indexOf(separator, i3 + 1);
91  d.mediaType = OUString(
92  value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
93  d.version = OUString(
94  value.getStr() + i3 + 1, i4 - i3 - 1,
95  RTL_TEXTENCODING_UTF8);
96  d.failedPrerequisites = OUString(
97  value.getStr() + i4 + 1, value.getLength() - i4 - 1,
98  RTL_TEXTENCODING_UTF8);
99  }
100  return d;
101 }
102 
103 }
104 #endif
105 
106 namespace dp_manager {
107 
109 
110 ActivePackages::ActivePackages(OUString const & url)
111 #if HAVE_FEATURE_EXTENSIONS
112  : m_map(url)
113 #endif
114 {
115 #if !HAVE_FEATURE_EXTENSIONS
116  (void) url;
117 #endif
118 }
119 
121 
123  OUString const & id, OUString const & fileName) const
124 {
125  return get(nullptr, id, fileName);
126 }
127 
129  Data * data, OUString const & id, OUString const & fileName)
130  const
131 {
132 #if HAVE_FEATURE_EXTENSIONS
133  OString v;
134  if (m_map.get(&v, newKey(id))) {
135  if (data != nullptr) {
136  *data = decodeNewData(v);
137  }
138  return true;
139  } else if (m_map.get(&v, oldKey(fileName))) {
140  if (data != nullptr) {
141  *data = decodeOldData(fileName, v);
142  }
143  return true;
144  } else {
145  return false;
146  }
147 #else
148  (void) data;
149  (void) id;
150  (void) fileName;
151  (void) this;
152  return false;
153 #endif
154 }
155 
157  Entries es;
158 #if HAVE_FEATURE_EXTENSIONS
159  ::dp_misc::t_string2string_map m(m_map.getEntries());
160  for (auto const& elem : m)
161  {
162  if (!elem.first.isEmpty() && elem.first[0] == separator[0]) {
163  es.emplace_back(
164  OUString(
165  elem.first.getStr() + 1, elem.first.getLength() - 1,
166  RTL_TEXTENCODING_UTF8),
167  decodeNewData(elem.second));
168  } else {
169  OUString fn(
170  OStringToOUString(elem.first, RTL_TEXTENCODING_UTF8));
171  es.emplace_back(
173  decodeOldData(fn, elem.second));
174  }
175  }
176 #else
177  (void) this;
178 #endif
179  return es;
180 }
181 
182 void ActivePackages::put(OUString const & id, Data const & data) {
183 #if HAVE_FEATURE_EXTENSIONS
184  OString b =
185  OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8) +
186  separator +
187  OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8) +
188  separator +
189  OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8) +
190  separator +
191  OUStringToOString(data.version, RTL_TEXTENCODING_UTF8) +
192  separator +
193  OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8);
194  m_map.put(newKey(id), b);
195 #else
196  (void) id;
197  (void) data;
198  (void) this;
199 #endif
200 }
201 
203  OUString const & id, OUString const & fileName)
204 {
205 #if HAVE_FEATURE_EXTENSIONS
206  m_map.erase(newKey(id)) || m_map.erase(oldKey(fileName));
207 #else
208  (void) id;
209  (void) fileName;
210  (void) this;
211 #endif
212 }
213 
214 }
215 
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void erase(OUString const &id, OUString const &fileName)
void put(OUString const &id, Data const &value)
t_map m_map
std::unordered_map< OString, OString > t_string2string_map
Definition: dp_persmap.h:31
std::vector< std::pair< OUString, Data > > Entries
double d
bool has(OUString const &id, OUString const &fileName) const
int i
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
float v
bool get(Data *data, OUString const &id, OUString const &fileName) const
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC OUString generateLegacyIdentifier(OUString const &fileName)
Generates a legacy identifier based on a file name.
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo