1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_activepackages.cxx,v $
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"
39 #include "osl/diagnose.h"
40 #include "rtl/strbuf.hxx"
41 #include "rtl/string.hxx"
42 #include "rtl/textenc.h"
44 #include "rtl/uri.hxx"
45 #include "rtl/ustring.hxx"
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)
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
;
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(';');
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
);
92 ::dp_manager::ActivePackages::Data
decodeNewData(::rtl::OString
const & value
) {
93 ::dp_manager::ActivePackages::Data d
;
94 sal_Int32 i1
= value
.indexOf(separator
);
96 d
.temporaryName
= ::rtl::OUString(
97 value
.getStr(), i1
, RTL_TEXTENCODING_UTF8
);
98 sal_Int32 i2
= value
.indexOf(separator
, i1
+ 1);
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
);
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
)
130 if (m_map
.get(&v
, newKey(id
))) {
132 *data
= decodeNewData(v
);
135 } else if (m_map
.get(&v
, oldKey(fileName
))) {
137 *data
= decodeOldData(fileName
, v
);
145 ActivePackages::Entries
ActivePackages::getEntries() const {
147 ::dp_misc::t_string2string_map
m(m_map
.getEntries());
148 for (::dp_misc::t_string2string_map::const_iterator
i(m
.begin());
151 if (i
->first
.getLength() > 0 && i
->first
[0] == separator
) {
155 i
->first
.getStr() + 1, i
->first
.getLength() - 1,
156 RTL_TEXTENCODING_UTF8
),
157 decodeNewData(i
->second
)));
160 ::rtl::OStringToOUString(i
->first
, RTL_TEXTENCODING_UTF8
));
163 ::dp_misc::generateLegacyIdentifier(fn
),
164 decodeOldData(fn
, i
->second
)));
170 void ActivePackages::put(::rtl::OUString
const & id
, Data
const & data
) {
171 ::rtl::OStringBuffer b
;
173 ::rtl::OUStringToOString(data
.temporaryName
, RTL_TEXTENCODING_UTF8
));
175 b
.append(::rtl::OUStringToOString(data
.fileName
, RTL_TEXTENCODING_UTF8
));
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);