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_features.h>
21 
22 #include <sal/config.h>
23 
24 #include <cstddef>
25 #include <utility>
26 #include <vector>
27 
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 
36 #include <dp_identifier.hxx>
37 #include "dp_activepackages.hxx"
38 
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)
45 
46 #if HAVE_FEATURE_EXTENSIONS
47 
48 namespace {
49 
50 constexpr const char separator[] = "\xff";
51 
52 OString oldKey(OUString const & fileName) {
53  return OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
54 }
55 
56 OString newKey(OUString const & id) {
57  return separator + OUStringToOString(id, RTL_TEXTENCODING_UTF8);
58 }
59 
61  OUString const & fileName, OString const & value)
62 {
64  sal_Int32 i = value.indexOf(';');
65  OSL_ASSERT(i >= 0);
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);
71  return d;
72 }
73 
74 ::dp_manager::ActivePackages::Data decodeNewData(OString const & value) {
76  sal_Int32 i1 = value.indexOf(separator);
77  OSL_ASSERT(i1 >= 0);
78  d.temporaryName = OUString(
79  value.getStr(), i1, RTL_TEXTENCODING_UTF8);
80  sal_Int32 i2 = value.indexOf(separator, i1 + 1);
81  OSL_ASSERT(i2 >= 0);
82  d.fileName = OUString(
83  value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
84  sal_Int32 i3 = value.indexOf(separator, i2 + 1);
85 
86  if (i3 < 0)
87  {
88  //Before ActivePackages::Data::version was added
89  d.mediaType = OUString(
90  value.getStr() + i2 + 1, value.getLength() - i2 - 1,
91  RTL_TEXTENCODING_UTF8);
92  }
93  else
94  {
95  sal_Int32 i4 = value.indexOf(separator, i3 + 1);
96  d.mediaType = OUString(
97  value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
98  d.version = OUString(
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);
104  }
105  return d;
106 }
107 
108 }
109 #endif
110 
111 namespace dp_manager {
112 
114 
115 ActivePackages::ActivePackages(OUString const & url)
116 #if HAVE_FEATURE_EXTENSIONS
117  : m_map(url)
118 #endif
119 {
120 #if !HAVE_FEATURE_EXTENSIONS
121  (void) url;
122 #endif
123 }
124 
126 
128  OUString const & id, OUString const & fileName) const
129 {
130  return get(nullptr, id, fileName);
131 }
132 
134  Data * data, OUString const & id, OUString const & fileName)
135  const
136 {
137 #if HAVE_FEATURE_EXTENSIONS
138  OString v;
139  if (m_map.get(&v, newKey(id))) {
140  if (data != nullptr) {
141  *data = decodeNewData(v);
142  }
143  return true;
144  } else if (m_map.get(&v, oldKey(fileName))) {
145  if (data != nullptr) {
146  *data = decodeOldData(fileName, v);
147  }
148  return true;
149  } else {
150  return false;
151  }
152 #else
153  (void) data;
154  (void) id;
155  (void) fileName;
156  (void) this;
157  return false;
158 #endif
159 }
160 
162  Entries es;
163 #if HAVE_FEATURE_EXTENSIONS
164  ::dp_misc::t_string2string_map m(m_map.getEntries());
165  for (auto const& elem : m)
166  {
167  if (!elem.first.isEmpty() && elem.first[0] == separator[0]) {
168  es.emplace_back(
169  OUString(
170  elem.first.getStr() + 1, elem.first.getLength() - 1,
171  RTL_TEXTENCODING_UTF8),
172  decodeNewData(elem.second));
173  } else {
174  OUString fn(
175  OStringToOUString(elem.first, RTL_TEXTENCODING_UTF8));
176  es.emplace_back(
178  decodeOldData(fn, elem.second));
179  }
180  }
181 #else
182  (void) this;
183 #endif
184  return es;
185 }
186 
187 void ActivePackages::put(OUString const & id, Data const & data) {
188 #if HAVE_FEATURE_EXTENSIONS
189  OString b =
190  OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8) +
191  separator +
192  OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8) +
193  separator +
194  OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8) +
195  separator +
196  OUStringToOString(data.version, RTL_TEXTENCODING_UTF8) +
197  separator +
198  OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8);
199  m_map.put(newKey(id), b);
200 #else
201  (void) id;
202  (void) data;
203  (void) this;
204 #endif
205 }
206 
208  OUString const & id, OUString const & fileName)
209 {
210 #if HAVE_FEATURE_EXTENSIONS
211  m_map.erase(newKey(id)) || m_map.erase(oldKey(fileName));
212 #else
213  (void) id;
214  (void) fileName;
215  (void) this;
216 #endif
217 }
218 
219 }
220 
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void erase(OUString const &id, OUString const &fileName)
void put(OUString const &id, Data const &value)
std::unordered_map< OString, OString > t_string2string_map
Definition: dp_persmap.h:31
std::vector< std::pair< OUString, Data > > Entries
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