1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_desktop.hxx"
29 
30 #include "sal/config.h"
31 
32 #include <cstddef>
33 #include <utility>
34 #include <vector>
35 
36 #include "osl/diagnose.h"
37 #include "rtl/strbuf.hxx"
38 #include "rtl/string.hxx"
39 #include "rtl/textenc.h"
40 #include "rtl/uri.h"
41 #include "rtl/uri.hxx"
42 #include "rtl/ustring.hxx"
43 #include <hash_map>
44 
45 #include "dp_identifier.hxx"
46 #include "dp_persmap.h"
47 
48 #include "dp_activepackages.hxx"
49 
50 // Old format of database entry:
51 //   key: UTF8(filename)
52 //   value: UTF8(tempname ";" mediatype)
53 // New format of database entry:
54 //   key: 0xFF UTF8(identifier)
55 //   value: UTF8(tempname) 0xFF UTF8(filename) 0xFF UTF8(mediatype)
56 
57 namespace {
58 
59 static char const separator = static_cast< char >(
60     static_cast< unsigned char >(0xFF));
61 
62 static char const legacyPrefix[] = "org.openoffice.legacy.";
63 
64 ::rtl::OString oldKey(::rtl::OUString const & fileName) {
65     return ::rtl::OUStringToOString(fileName, RTL_TEXTENCODING_UTF8);
66 }
67 
68 ::rtl::OString newKey(::rtl::OUString const & id) {
69     ::rtl::OStringBuffer b;
70     b.append(separator);
71     b.append(::rtl::OUStringToOString(id, RTL_TEXTENCODING_UTF8));
72     return b.makeStringAndClear();
73 }
74 
75 ::dp_manager::ActivePackages::Data decodeOldData(
76     ::rtl::OUString const & fileName, ::rtl::OString const & value)
77 {
78     ::dp_manager::ActivePackages::Data d;
79     sal_Int32 i = value.indexOf(';');
80     OSL_ASSERT(i >= 0);
81     d.temporaryName = ::rtl::OUString(value.getStr(), i, RTL_TEXTENCODING_UTF8);
82     d.fileName = fileName;
83     d.mediaType = ::rtl::OUString(
84         value.getStr() + i + 1, value.getLength() - i - 1,
85         RTL_TEXTENCODING_UTF8);
86     return d;
87 }
88 
89 ::dp_manager::ActivePackages::Data decodeNewData(::rtl::OString const & value) {
90     ::dp_manager::ActivePackages::Data d;
91     sal_Int32 i1 = value.indexOf(separator);
92     OSL_ASSERT(i1 >= 0);
93     d.temporaryName = ::rtl::OUString(
94         value.getStr(), i1, RTL_TEXTENCODING_UTF8);
95     sal_Int32 i2 = value.indexOf(separator, i1 + 1);
96     OSL_ASSERT(i2 >= 0);
97     d.fileName = ::rtl::OUString(
98         value.getStr() + i1 + 1, i2 - i1 - 1, RTL_TEXTENCODING_UTF8);
99     sal_Int32 i3 = value.indexOf(separator, i2 + 1);
100 
101     if (i3 < 0)
102     {
103         //Before ActivePackages::Data::version was added
104         d.mediaType = ::rtl::OUString(
105             value.getStr() + i2 + 1, value.getLength() - i2 - 1,
106             RTL_TEXTENCODING_UTF8);
107     }
108     else
109     {
110         sal_Int32 i4 = value.indexOf(separator, i3 + 1);
111         d.mediaType = ::rtl::OUString(
112             value.getStr() + i2 + 1, i3 - i2 -1, RTL_TEXTENCODING_UTF8);
113         d.version = ::rtl::OUString(
114             value.getStr() + i3 + 1, i4 - i3 - 1,
115             RTL_TEXTENCODING_UTF8);
116         d.failedPrerequisites = ::rtl::OUString(
117             value.getStr() + i4 + 1, value.getLength() - i4 - 1,
118             RTL_TEXTENCODING_UTF8);
119     }
120     return d;
121 }
122 
123 }
124 
125 namespace dp_manager {
126 
127 ActivePackages::ActivePackages() {}
128 
129 ActivePackages::ActivePackages(::rtl::OUString const & url, bool readOnly):
130     m_map(url, readOnly) {}
131 
132 ActivePackages::~ActivePackages() {}
133 
134 bool ActivePackages::has(
135     ::rtl::OUString const & id, ::rtl::OUString const & fileName) const
136 {
137     return get(NULL, id, fileName);
138 }
139 
140 bool ActivePackages::get(
141     Data * data, ::rtl::OUString const & id, ::rtl::OUString const & fileName)
142     const
143 {
144     ::rtl::OString v;
145     if (m_map.get(&v, newKey(id))) {
146         if (data != NULL) {
147             *data = decodeNewData(v);
148         }
149         return true;
150     } else if (m_map.get(&v, oldKey(fileName))) {
151         if (data != NULL) {
152             *data = decodeOldData(fileName, v);
153         }
154         return true;
155     } else {
156         return false;
157     }
158 }
159 
160 ActivePackages::Entries ActivePackages::getEntries() const {
161     Entries es;
162     ::dp_misc::t_string2string_map m(m_map.getEntries());
163     for (::dp_misc::t_string2string_map::const_iterator i(m.begin());
164          i != m.end(); ++i)
165     {
166         if (i->first.getLength() > 0 && i->first[0] == separator) {
167             es.push_back(
168                 ::std::make_pair(
169                     ::rtl::OUString(
170                         i->first.getStr() + 1, i->first.getLength() - 1,
171                         RTL_TEXTENCODING_UTF8),
172                     decodeNewData(i->second)));
173         } else {
174             ::rtl::OUString fn(
175                 ::rtl::OStringToOUString(i->first, RTL_TEXTENCODING_UTF8));
176             es.push_back(
177                 ::std::make_pair(
178                     ::dp_misc::generateLegacyIdentifier(fn),
179                     decodeOldData(fn, i->second)));
180         }
181     }
182     return es;
183 }
184 
185 void ActivePackages::put(::rtl::OUString const & id, Data const & data) {
186     ::rtl::OStringBuffer b;
187     b.append(
188         ::rtl::OUStringToOString(data.temporaryName, RTL_TEXTENCODING_UTF8));
189     b.append(separator);
190     b.append(::rtl::OUStringToOString(data.fileName, RTL_TEXTENCODING_UTF8));
191     b.append(separator);
192     b.append(::rtl::OUStringToOString(data.mediaType, RTL_TEXTENCODING_UTF8));
193     b.append(separator);
194     b.append(::rtl::OUStringToOString(data.version, RTL_TEXTENCODING_UTF8));
195     b.append(separator);
196     b.append(::rtl::OUStringToOString(data.failedPrerequisites, RTL_TEXTENCODING_UTF8));
197     m_map.put(newKey(id), b.makeStringAndClear());
198 }
199 
200 void ActivePackages::erase(
201     ::rtl::OUString const & id, ::rtl::OUString const & fileName)
202 {
203     m_map.erase(newKey(id), true) || m_map.erase(oldKey(fileName), true);
204 }
205 
206 }
207