merge the formfield patch from ooo-build
[ooovba.git] / desktop / source / deployment / manager / dp_activepackages.cxx
blob02dccabff59d1507ecd0b282116140360c94ba5a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_activepackages.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_desktop.hxx"
33 #include "sal/config.h"
35 #include <cstddef>
36 #include <utility>
37 #include <vector>
39 #include "osl/diagnose.h"
40 #include "rtl/strbuf.hxx"
41 #include "rtl/string.hxx"
42 #include "rtl/textenc.h"
43 #include "rtl/uri.h"
44 #include "rtl/uri.hxx"
45 #include "rtl/ustring.hxx"
46 #include <hash_map>
48 #include "dp_identifier.hxx"
49 #include "dp_persmap.h"
51 #include "dp_activepackages.hxx"
53 // Old format of database entry:
54 // key: UTF8(filename)
55 // value: UTF8(tempname ";" mediatype)
56 // New format of database entry:
57 // key: 0xFF UTF8(identifier)
58 // value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
60 namespace {
62 static char const separator = static_cast< char >(
63 static_cast< unsigned char >(0xFF));
65 static char const legacyPrefix[] = "org.openoffice.legacy.";
67 ::rtl::OString oldKey(::rtl::OUString const & fileName) {
68 return ::rtl::OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
71 ::rtl::OString newKey(::rtl::OUString const & id) {
72 ::rtl::OStringBuffer b;
73 b.append(separator);
74 b.append(::rtl::OUStringToOString(id, RTL_TEXTENCODING_UTF8));
75 return b.makeStringAndClear();
78 ::dp_manager::ActivePackages::Data decodeOldData(
79 ::rtl::OUString const & fileName, ::rtl::OString const & value)
81 ::dp_manager::ActivePackages::Data d;
82 sal_Int32 i = value.indexOf(';');
83 OSL_ASSERT(i >= 0);
84 d.temporaryName = ::rtl::OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
85 d.fileName = fileName;
86 d.mediaType = ::rtl::OUString(
87 value.getStr() + i + 1, value.getLength() - i - 1,
88 RTL_TEXTENCODING_UTF8);
89 return d;
92 ::dp_manager::ActivePackages::Data decodeNewData(::rtl::OString const & value) {
93 ::dp_manager::ActivePackages::Data d;
94 sal_Int32 i1 = value.indexOf(separator);
95 OSL_ASSERT(i1 >= 0);
96 d.temporaryName = ::rtl::OUString(
97 value.getStr(), i1, RTL_TEXTENCODING_UTF8);
98 sal_Int32 i2 = value.indexOf(separator, i1 + 1);
99 OSL_ASSERT(i2 >= 0);
100 d.fileName = ::rtl::OUString(
101 value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
102 d.mediaType = ::rtl::OUString(
103 value.getStr() + i2 + 1, value.getLength() - i2 - 1,
104 RTL_TEXTENCODING_UTF8);
105 return d;
110 namespace dp_manager {
112 ActivePackages::ActivePackages() {}
114 ActivePackages::ActivePackages(::rtl::OUString const & url, bool readOnly):
115 m_map(url, readOnly) {}
117 ActivePackages::~ActivePackages() {}
119 bool ActivePackages::has(
120 ::rtl::OUString const & id, ::rtl::OUString const & fileName) const
122 return get(NULL, id, fileName);
125 bool ActivePackages::get(
126 Data * data, ::rtl::OUString const & id, ::rtl::OUString const & fileName)
127 const
129 ::rtl::OString v;
130 if (m_map.get(&v, newKey(id))) {
131 if (data != NULL) {
132 *data = decodeNewData(v);
134 return true;
135 } else if (m_map.get(&v, oldKey(fileName))) {
136 if (data != NULL) {
137 *data = decodeOldData(fileName, v);
139 return true;
140 } else {
141 return false;
145 ActivePackages::Entries ActivePackages::getEntries() const {
146 Entries es;
147 ::dp_misc::t_string2string_map m(m_map.getEntries());
148 for (::dp_misc::t_string2string_map::const_iterator i(m.begin());
149 i != m.end(); ++i)
151 if (i->first.getLength() > 0 && i->first[0] == separator) {
152 es.push_back(
153 ::std::make_pair(
154 ::rtl::OUString(
155 i->first.getStr() + 1, i->first.getLength() - 1,
156 RTL_TEXTENCODING_UTF8),
157 decodeNewData(i->second)));
158 } else {
159 ::rtl::OUString fn(
160 ::rtl::OStringToOUString(i->first, RTL_TEXTENCODING_UTF8));
161 es.push_back(
162 ::std::make_pair(
163 ::dp_misc::generateLegacyIdentifier(fn),
164 decodeOldData(fn, i->second)));
167 return es;
170 void ActivePackages::put(::rtl::OUString const & id, Data const & data) {
171 ::rtl::OStringBuffer b;
172 b.append(
173 ::rtl::OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8));
174 b.append(separator);
175 b.append(::rtl::OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8));
176 b.append(separator);
177 b.append(::rtl::OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8));
178 m_map.put(newKey(id), b.makeStringAndClear());
181 void ActivePackages::erase(
182 ::rtl::OUString const & id, ::rtl::OUString const & fileName)
184 m_map.erase(newKey(id), true) || m_map.erase(oldKey(fileName), true);