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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_desktop.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "com/sun/star/uno/Reference.hxx"
34 #include "com/sun/star/uno/Sequence.hxx"
35 #include "com/sun/star/xml/dom/XElement.hpp"
36 #include "com/sun/star/xml/dom/XNode.hpp"
37 #include "com/sun/star/xml/dom/XNodeList.hpp"
38 #include "rtl/bootstrap.hxx"
39 #include "rtl/string.h"
40 #include "rtl/ustring.h"
41 #include "rtl/ustring.hxx"
42 #include "sal/types.h"
43 #include "tools/string.hxx"
44 
45 #include "deployment.hrc"
46 #include "dp_resource.h"
47 
48 #include "dp_dependencies.hxx"
49 #include "dp_descriptioninfoset.hxx"
50 #include "dp_version.hxx"
51 
52 namespace {
53 
54 namespace css = ::com::sun::star;
55 
56 static char const xmlNamespace[] =
57     "http://openoffice.org/extensions/description/2006";
58 
59 bool satisfiesMinimalVersion(::rtl::OUString const & version) {
60     ::rtl::OUString v(
61         RTL_CONSTASCII_USTRINGPARAM(
62             "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version")
63             ":Version:OOOPackageVersion}"));
64     ::rtl::Bootstrap::expandMacros(v);
65     return ::dp_misc::compareVersions(v, version) != ::dp_misc::LESS;
66 }
67 
68 }
69 
70 namespace dp_misc {
71 
72 namespace Dependencies {
73 
74 css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > >
75 check(::dp_misc::DescriptionInfoset const & infoset) {
76     css::uno::Reference< css::xml::dom::XNodeList > deps(
77         infoset.getDependencies());
78     ::sal_Int32 n = deps->getLength();
79     css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > >
80         unsatisfied(n);
81     ::sal_Int32 unsat = 0;
82     for (::sal_Int32 i = 0; i < n; ++i) {
83         static char const minimalVersion[] = "OpenOffice.org-minimal-version";
84         css::uno::Reference< css::xml::dom::XElement > e(
85             deps->item(i), css::uno::UNO_QUERY_THROW);
86         bool sat = false;
87         if (e->getNamespaceURI().equalsAsciiL(
88                 RTL_CONSTASCII_STRINGPARAM(xmlNamespace))
89             && e->getTagName().equalsAsciiL(
90                 RTL_CONSTASCII_STRINGPARAM(minimalVersion)))
91         {
92             sat = satisfiesMinimalVersion(
93                 e->getAttribute(
94                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("value"))));
95         } else if (e->getNamespaceURI().equalsAsciiL(
96                        RTL_CONSTASCII_STRINGPARAM(xmlNamespace))
97                    && e->getTagName().equalsAsciiL(
98                        RTL_CONSTASCII_STRINGPARAM(
99                            "OpenOffice.org-maximal-version")))
100         {
101             ::rtl::OUString v(
102                 RTL_CONSTASCII_USTRINGPARAM(
103                     "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version")
104                     ":Version:OOOBaseVersion}"));
105             ::rtl::Bootstrap::expandMacros(v);
106             sat =
107                 ::dp_misc::compareVersions(
108                     v,
109                     e->getAttribute(
110                         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("value"))))
111                 != ::dp_misc::GREATER;
112         } else if (e->hasAttributeNS(
113                        ::rtl::OUString(
114                            RTL_CONSTASCII_USTRINGPARAM(xmlNamespace)),
115                        ::rtl::OUString(
116                            RTL_CONSTASCII_USTRINGPARAM(minimalVersion))))
117         {
118             sat = satisfiesMinimalVersion(
119                 e->getAttributeNS(
120                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(xmlNamespace)),
121                     ::rtl::OUString(
122                         RTL_CONSTASCII_USTRINGPARAM(minimalVersion))));
123         }
124         if (!sat) {
125             unsatisfied[unsat++] = e;
126         }
127     }
128     unsatisfied.realloc(unsat);
129     return unsatisfied;
130 }
131 
132 ::rtl::OUString getErrorText( css::uno::Reference< css::xml::dom::XElement > const & dependency )
133 {
134     ::rtl::OUString sReason;
135     ::rtl::OUString sValue;
136     ::rtl::OUString sVersion(RTL_CONSTASCII_USTRINGPARAM("%VERSION"));
137 
138     if ( dependency->getNamespaceURI().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( xmlNamespace ) )
139          && dependency->getTagName().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "OpenOffice.org-minimal-version" ) ) )
140     {
141         sValue = dependency->getAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "value" ) ) );
142         sReason = ::rtl::OUString( ::String(::dp_misc::getResId(RID_DEPLYOMENT_DEPENDENCIES_MIN)) );
143     }
144     else if ( dependency->getNamespaceURI().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( xmlNamespace ) )
145               && dependency->getTagName().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "OpenOffice.org-maximal-version" ) ) )
146     {
147         sValue = dependency->getAttribute( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("value") ) );
148         sReason = ::rtl::OUString( ::String(::dp_misc::getResId(RID_DEPLYOMENT_DEPENDENCIES_MAX)) );
149     }
150     else if ( dependency->hasAttributeNS( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( xmlNamespace ) ),
151                                           ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenOffice.org-minimal-version" ))))
152     {
153         sValue = dependency->getAttributeNS( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( xmlNamespace ) ),
154                                              ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenOffice.org-minimal-version" ) ) );
155         sReason = ::rtl::OUString( ::String(::dp_misc::getResId(RID_DEPLYOMENT_DEPENDENCIES_MIN)) );
156     }
157     else
158         return ::rtl::OUString( ::String(::dp_misc::getResId(RID_DEPLYOMENT_DEPENDENCIES_UNKNOWN)) );
159 
160     if ( sValue.getLength() == 0 )
161         sValue = ::rtl::OUString( ::String(::dp_misc::getResId(RID_DEPLYOMENT_DEPENDENCIES_UNKNOWN)) );
162 
163     sal_Int32 nPos = sReason.indexOf( sVersion );
164     if ( nPos >= 0 )
165         sReason = sReason.replaceAt( nPos, sVersion.getLength(), sValue );
166     return sReason;
167 }
168 
169 }
170 
171 }
172