merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / update / check / updateprotocol.cxx
blob5d51ea14cc6f6542ce1f44b5c9fb4f4b2c3a82f9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
32 #include "updateprotocol.hxx"
33 #include "updatecheckconfig.hxx"
35 #ifndef _COM_SUN_STAR_DEPLOYMENT_UPDATEINFORMATINENTRY_HPP_
36 #include <com/sun/star/deployment/UpdateInformationEntry.hpp>
37 #endif
38 #include <com/sun/star/deployment/XPackageInformationProvider.hpp>
41 #include <rtl/ref.hxx>
42 #include <rtl/uri.hxx>
43 #include <rtl/strbuf.hxx>
44 #include <rtl/ustrbuf.hxx>
45 #include <rtl/bootstrap.hxx>
46 #include <osl/process.h>
48 #include <cppuhelper/implbase1.hxx>
50 namespace css = com::sun::star ;
51 namespace container = css::container ;
52 namespace deployment = css::deployment ;
53 namespace lang = css::lang ;
54 namespace uno = css::uno ;
55 namespace task = css::task ;
56 namespace xml = css::xml ;
58 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
60 //------------------------------------------------------------------------------
62 static bool
63 getBootstrapData(
64 uno::Sequence< ::rtl::OUString > & rRepositoryList,
65 ::rtl::OUString & rBuildID,
66 ::rtl::OUString & rInstallSetID)
68 rBuildID = UNISTRING( "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version") ":ProductBuildid}" );
69 rtl::Bootstrap::expandMacros( rBuildID );
70 if ( ! rBuildID.getLength() )
71 return false;
73 rInstallSetID = UNISTRING( "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateID}" );
74 rtl::Bootstrap::expandMacros( rInstallSetID );
75 if ( ! rInstallSetID.getLength() )
76 return false;
78 rtl::OUString aValue( UNISTRING( "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateURL}" ) );
79 rtl::Bootstrap::expandMacros( aValue );
81 if( aValue.getLength() > 0 )
83 rRepositoryList.realloc(1);
84 rRepositoryList[0] = aValue;
87 return true;
90 //------------------------------------------------------------------------------
92 // Returns 'true' if successfully connected to the update server
93 bool
94 checkForUpdates(
95 UpdateInfo& o_rUpdateInfo,
96 uno::Reference< uno::XComponentContext > const & rxContext,
97 uno::Reference< task::XInteractionHandler > const & rxInteractionHandler,
98 const uno::Reference< deployment::XUpdateInformationProvider >& rUpdateInfoProvider)
100 OSL_TRACE("checking for updates ..\n");
102 ::rtl::OUString myArch;
103 ::rtl::OUString myOS;
105 rtl::Bootstrap::get(UNISTRING("_OS"), myOS);
106 rtl::Bootstrap::get(UNISTRING("_ARCH"), myArch);
108 uno::Sequence< ::rtl::OUString > aRepositoryList;
109 ::rtl::OUString aBuildID;
110 ::rtl::OUString aInstallSetID;
112 if( ! ( getBootstrapData(aRepositoryList, aBuildID, aInstallSetID) && (aRepositoryList.getLength() > 0) ) )
113 return false;
115 if( !rxContext.is() )
116 throw uno::RuntimeException(
117 UNISTRING( "checkForUpdates: empty component context" ), uno::Reference< uno::XInterface >() );
119 OSL_ASSERT( rxContext->getServiceManager().is() );
121 // XPath implementation
122 uno::Reference< xml::xpath::XXPathAPI > xXPath(
123 rxContext->getServiceManager()->createInstanceWithContext( UNISTRING( "com.sun.star.xml.xpath.XPathAPI" ), rxContext ),
124 uno::UNO_QUERY_THROW);
126 xXPath->registerNS( UNISTRING("inst"), UNISTRING("http://installation.openoffice.org/description") );
128 if( rxInteractionHandler.is() )
129 rUpdateInfoProvider->setInteractionHandler(rxInteractionHandler);
133 uno::Reference< container::XEnumeration > aUpdateInfoEnumeration =
134 rUpdateInfoProvider->getUpdateInformationEnumeration( aRepositoryList, aInstallSetID );
136 if ( !aUpdateInfoEnumeration.is() )
137 return false; // something went wrong ..
139 rtl::OUStringBuffer aBuffer;
140 aBuffer.appendAscii("/child::inst:description[inst:os=\'");
141 aBuffer.append( myOS );
142 aBuffer.appendAscii("\' and inst:arch=\'");
143 aBuffer.append( myArch );
144 aBuffer.appendAscii("\' and inst:buildid>");
145 aBuffer.append( aBuildID );
146 aBuffer.appendAscii("]");
148 rtl::OUString aXPathExpression = aBuffer.makeStringAndClear();
150 while( aUpdateInfoEnumeration->hasMoreElements() )
152 deployment::UpdateInformationEntry aEntry;
154 if( aUpdateInfoEnumeration->nextElement() >>= aEntry )
156 uno::Reference< xml::dom::XNode > xNode( aEntry.UpdateDocument.get() );
157 uno::Reference< xml::dom::XNodeList > xNodeList;
158 try {
159 xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
160 + UNISTRING("/inst:update/attribute::src"));
161 } catch (css::xml::xpath::XPathException &) {
162 // ignore
166 o_rUpdateInfo.Sources.push_back( DownloadSource(true,
167 UNISTRING("http://openoffice.bouncer.osuosl.org/?product=OpenOffice.org&os=solarissparcwjre&lang=en-US&version=2.2.1") ) );
170 sal_Int32 i, imax = xNodeList->getLength();
171 for( i = 0; i < imax; ++i )
173 uno::Reference< xml::dom::XNode > xNode2( xNodeList->item(i) );
175 if( xNode2.is() )
177 uno::Reference< xml::dom::XElement > xParent(xNode2->getParentNode(), uno::UNO_QUERY_THROW);
178 rtl::OUString aType = xParent->getAttribute(UNISTRING("type"));
179 bool bIsDirect = ( sal_False == aType.equalsIgnoreAsciiCaseAscii("text/html") );
181 o_rUpdateInfo.Sources.push_back( DownloadSource(bIsDirect, xNode2->getNodeValue()) );
185 uno::Reference< xml::dom::XNode > xNode2;
186 try {
187 xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
188 + UNISTRING("/inst:version/text()"));
189 } catch (css::xml::xpath::XPathException &) {
190 // ignore
193 if( xNode2.is() )
194 o_rUpdateInfo.Version = xNode2->getNodeValue();
196 try {
197 xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
198 + UNISTRING("/inst:buildid/text()"));
199 } catch (css::xml::xpath::XPathException &) {
200 // ignore
203 if( xNode2.is() )
204 o_rUpdateInfo.BuildId = xNode2->getNodeValue();
206 o_rUpdateInfo.Description = aEntry.Description;
208 // Release Notes
209 try {
210 xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
211 + UNISTRING("/inst:relnote"));
212 } catch (css::xml::xpath::XPathException &) {
213 // ignore
215 imax = xNodeList->getLength();
216 for( i = 0; i < imax; ++i )
218 uno::Reference< xml::dom::XElement > xRelNote(xNodeList->item(i), uno::UNO_QUERY);
219 if( xRelNote.is() )
221 sal_Int32 pos = xRelNote->getAttribute(UNISTRING("pos")).toInt32();
223 ReleaseNote aRelNote((sal_uInt8) pos, xRelNote->getAttribute(UNISTRING("src")));
225 if( xRelNote->hasAttribute(UNISTRING("src2")) )
227 pos = xRelNote->getAttribute(UNISTRING("pos2")).toInt32();
228 aRelNote.Pos2 = (sal_Int8) pos;
229 aRelNote.URL2 = xRelNote->getAttribute(UNISTRING("src2"));
232 o_rUpdateInfo.ReleaseNotes.push_back(aRelNote);
236 o_rUpdateInfo.ReleaseNotes.push_back(
237 ReleaseNote(1, UNISTRING("http://qa.openoffice.org/tests/online_update_test.html"))
241 if( o_rUpdateInfo.Sources.size() > 0 )
242 return true;
246 catch( ... )
248 return false;
251 return true;
254 //------------------------------------------------------------------------------
255 bool storeExtensionUpdateInfos( const uno::Reference< uno::XComponentContext > & rxContext,
256 const uno::Sequence< uno::Sequence< rtl::OUString > > &rUpdateInfos )
258 if ( rUpdateInfos.hasElements() )
260 rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
262 for ( sal_Int32 i = rUpdateInfos.getLength() - 1; i >= 0; i-- )
264 aConfig->storeExtensionVersion( rUpdateInfos[i][0], rUpdateInfos[i][1] );
267 return rUpdateInfos.hasElements();
270 //------------------------------------------------------------------------------
271 // Returns 'true' if there are updates for any extension
273 bool checkForExtensionUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
275 uno::Sequence< uno::Sequence< rtl::OUString > > aUpdateList;
277 uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
280 uno::Any aValue( rxContext->getValueByName(
281 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
282 OSL_VERIFY( aValue >>= xInfoProvider );
284 catch( const uno::Exception& )
286 OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
289 if ( !xInfoProvider.is() ) return false;
291 aUpdateList = xInfoProvider->isUpdateAvailable( ::rtl::OUString() );
292 storeExtensionUpdateInfos( rxContext, aUpdateList );
294 return aUpdateList.hasElements();
297 //------------------------------------------------------------------------------
298 // Returns 'true' if there are any pending updates for any extension (offline check)
300 bool checkForPendingUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
302 uno::Sequence< uno::Sequence< rtl::OUString > > aExtensionList;
303 uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
306 uno::Any aValue( rxContext->getValueByName(
307 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
308 OSL_VERIFY( aValue >>= xInfoProvider );
310 catch( const uno::Exception& )
312 OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
315 if ( !xInfoProvider.is() ) return false;
317 bool bPendingUpdateFound = false;
319 aExtensionList = xInfoProvider->getExtensionList();
320 if ( aExtensionList.hasElements() )
322 rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
324 for ( sal_Int32 i = aExtensionList.getLength() - 1; i >= 0; i-- )
326 bPendingUpdateFound = aConfig->checkExtensionVersion( aExtensionList[i][0], aExtensionList[i][1] );
327 if ( bPendingUpdateFound )
328 break;
332 return bPendingUpdateFound;