1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_desktop.hxx"
26 
27 #include "com/sun/star/ucb/XCommandEnvironment.hpp"
28 #include "com/sun/star/lang/IllegalArgumentException.hpp"
29 #include "xmlscript/xml_helper.hxx"
30 #include "ucbhelper/content.hxx"
31 #include <list>
32 
33 #include "dp_ucb.h"
34 #include "rtl/ustrbuf.hxx"
35 #include "dp_properties.hxx"
36 
37 namespace lang  = com::sun::star::lang;
38 namespace task = com::sun::star::task;
39 namespace ucb = com::sun::star::ucb;
40 namespace uno = com::sun::star::uno;
41 namespace css = com::sun::star;
42 
43 #define OUSTR(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
44 
45 using ::com::sun::star::uno::Reference;
46 using ::rtl::OUString;
47 
48 #define PROP_SUPPRESS_LICENSE "SUPPRESS_LICENSE"
49 #define PROP_EXTENSION_UPDATE "EXTENSION_UPDATE"
50 
51 namespace dp_manager {
52 
53 //Reading the file
ExtensionProperties(OUString const & urlExtension,Reference<ucb::XCommandEnvironment> const & xCmdEnv)54 ExtensionProperties::ExtensionProperties(
55     OUString const & urlExtension,
56     Reference<ucb::XCommandEnvironment> const & xCmdEnv) :
57     m_xCmdEnv(xCmdEnv)
58 {
59     m_propFileUrl = urlExtension + OUSTR("properties");
60 
61     ::std::list< ::std::pair< OUString, OUString> > props;
62     if (! dp_misc::create_ucb_content(NULL, m_propFileUrl, 0, false))
63         return;
64 
65     ::ucbhelper::Content contentProps(m_propFileUrl, m_xCmdEnv);
66     dp_misc::readProperties(props, contentProps);
67 
68     typedef ::std::list< ::std::pair< OUString, OUString> >::const_iterator CI;
69     for (CI i = props.begin(); i != props.end(); i++)
70     {
71         if (i->first.equals(OUSTR(PROP_SUPPRESS_LICENSE)))
72             m_prop_suppress_license = i->second;
73     }
74 }
75 
76 //Writing the file
ExtensionProperties(OUString const & urlExtension,uno::Sequence<css::beans::NamedValue> const & properties,Reference<ucb::XCommandEnvironment> const & xCmdEnv)77 ExtensionProperties::ExtensionProperties(
78     OUString const & urlExtension,
79     uno::Sequence<css::beans::NamedValue> const & properties,
80     Reference<ucb::XCommandEnvironment> const & xCmdEnv) :
81     m_xCmdEnv(xCmdEnv)
82 {
83     m_propFileUrl = urlExtension + OUSTR("properties");
84 
85     for (sal_Int32 i = 0; i < properties.getLength(); i++)
86     {
87         css::beans::NamedValue const & v = properties[i];
88         if (v.Name.equals(OUSTR(PROP_SUPPRESS_LICENSE)))
89         {
90             m_prop_suppress_license = getPropertyValue(v);
91         }
92         else if (v.Name.equals(OUSTR(PROP_EXTENSION_UPDATE)))
93         {
94             m_prop_extension_update = getPropertyValue(v);
95         }
96         else
97         {
98             throw lang::IllegalArgumentException(
99                 OUSTR("Extension Manager: unknown property"), 0, -1);
100         }
101     }
102 }
103 
getPropertyValue(css::beans::NamedValue const & v)104 OUString ExtensionProperties::getPropertyValue(css::beans::NamedValue const & v)
105 {
106     OUString value(OUSTR("0"));
107     if (v.Value >>= value)
108     {
109         if (value.equals(OUSTR("1")))
110             value = OUSTR("1");
111     }
112     else
113     {
114         throw lang::IllegalArgumentException(
115             OUSTR("Extension Manager: wrong property value"), 0, -1);
116     }
117     return value;
118 }
write()119 void ExtensionProperties::write()
120 {
121     ::ucbhelper::Content contentProps(m_propFileUrl, m_xCmdEnv);
122     ::rtl::OUStringBuffer buf;
123 
124     if (m_prop_suppress_license)
125     {
126         buf.append(OUSTR(PROP_SUPPRESS_LICENSE));
127         buf.append(OUSTR("="));
128         buf.append(*m_prop_suppress_license);
129     }
130 
131     ::rtl::OString stamp = ::rtl::OUStringToOString(
132         buf.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
133     Reference<css::io::XInputStream> xData(
134         ::xmlscript::createInputStream(
135             ::rtl::ByteSequence(
136                 reinterpret_cast<sal_Int8 const *>(stamp.getStr()),
137                 stamp.getLength() ) ) );
138     contentProps.writeStream( xData, true /* replace existing */ );
139 }
140 
isSuppressedLicense()141 bool ExtensionProperties::isSuppressedLicense()
142 {
143     bool ret = false;
144     if (m_prop_suppress_license)
145     {
146         if (m_prop_suppress_license->equals(OUSTR("1")))
147             ret = true;
148     }
149     return ret;
150 }
151 
isExtensionUpdate()152 bool ExtensionProperties::isExtensionUpdate()
153 {
154     bool ret = false;
155     if (m_prop_extension_update)
156     {
157         if (m_prop_extension_update->equals(OUSTR("1")))
158             ret = true;
159     }
160     return ret;
161 }
162 
163 } // namespace dp_manager
164 
165 
166