merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / update / check / updateprotocol.cxx
blob368a9ff80d992103c6646ff0e9dbfe1df3d113e3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: updateprotocol.cxx,v $
10 * $Revision: 1.11.70.1 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
35 #include "updateprotocol.hxx"
36 #include "updatecheckconfig.hxx"
38 #ifndef _COM_SUN_STAR_DEPLOYMENT_UPDATEINFORMATINENTRY_HPP_
39 #include <com/sun/star/deployment/UpdateInformationEntry.hpp>
40 #endif
41 #include <com/sun/star/deployment/XPackageInformationProvider.hpp>
44 #include <rtl/ref.hxx>
45 #include <rtl/uri.hxx>
46 #include <rtl/strbuf.hxx>
47 #include <rtl/ustrbuf.hxx>
48 #include <rtl/bootstrap.hxx>
49 #include <osl/process.h>
51 #include <cppuhelper/implbase1.hxx>
53 namespace css = com::sun::star ;
54 namespace container = css::container ;
55 namespace deployment = css::deployment ;
56 namespace lang = css::lang ;
57 namespace uno = css::uno ;
58 namespace task = css::task ;
59 namespace xml = css::xml ;
61 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
63 //------------------------------------------------------------------------------
65 static bool
66 getBootstrapData(
67 uno::Sequence< ::rtl::OUString > & rRepositoryList,
68 ::rtl::OUString & rBuildID,
69 ::rtl::OUString & rInstallSetID)
71 rBuildID = UNISTRING( "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version") ":ProductBuildid}" );
72 rtl::Bootstrap::expandMacros( rBuildID );
73 if ( ! rBuildID.getLength() )
74 return false;
76 rInstallSetID = UNISTRING( "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateID}" );
77 rtl::Bootstrap::expandMacros( rInstallSetID );
78 if ( ! rInstallSetID.getLength() )
79 return false;
81 rtl::OUString aValue( UNISTRING( "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateURL}" ) );
82 rtl::Bootstrap::expandMacros( aValue );
84 if( aValue.getLength() > 0 )
86 rRepositoryList.realloc(1);
87 rRepositoryList[0] = aValue;
90 return true;
93 //------------------------------------------------------------------------------
95 // Returns 'true' if successfully connected to the update server
96 bool
97 checkForUpdates(
98 UpdateInfo& o_rUpdateInfo,
99 uno::Reference< uno::XComponentContext > const & rxContext,
100 uno::Reference< task::XInteractionHandler > const & rxInteractionHandler,
101 const uno::Reference< deployment::XUpdateInformationProvider >& rUpdateInfoProvider)
103 OSL_TRACE("checking for updates ..\n");
105 ::rtl::OUString myArch;
106 ::rtl::OUString myOS;
108 rtl::Bootstrap::get(UNISTRING("_OS"), myOS);
109 rtl::Bootstrap::get(UNISTRING("_ARCH"), myArch);
111 uno::Sequence< ::rtl::OUString > aRepositoryList;
112 ::rtl::OUString aBuildID;
113 ::rtl::OUString aInstallSetID;
115 if( ! ( getBootstrapData(aRepositoryList, aBuildID, aInstallSetID) && (aRepositoryList.getLength() > 0) ) )
116 return false;
118 if( !rxContext.is() )
119 throw uno::RuntimeException(
120 UNISTRING( "checkForUpdates: empty component context" ), uno::Reference< uno::XInterface >() );
122 OSL_ASSERT( rxContext->getServiceManager().is() );
124 // XPath implementation
125 uno::Reference< xml::xpath::XXPathAPI > xXPath(
126 rxContext->getServiceManager()->createInstanceWithContext( UNISTRING( "com.sun.star.xml.xpath.XPathAPI" ), rxContext ),
127 uno::UNO_QUERY_THROW);
129 xXPath->registerNS( UNISTRING("inst"), UNISTRING("http://installation.openoffice.org/description") );
131 if( rxInteractionHandler.is() )
132 rUpdateInfoProvider->setInteractionHandler(rxInteractionHandler);
136 uno::Reference< container::XEnumeration > aUpdateInfoEnumeration =
137 rUpdateInfoProvider->getUpdateInformationEnumeration( aRepositoryList, aInstallSetID );
139 if ( !aUpdateInfoEnumeration.is() )
140 return false; // something went wrong ..
142 rtl::OUStringBuffer aBuffer;
143 aBuffer.appendAscii("/child::inst:description[inst:os=\'");
144 aBuffer.append( myOS );
145 aBuffer.appendAscii("\' and inst:arch=\'");
146 aBuffer.append( myArch );
147 aBuffer.appendAscii("\' and inst:buildid>");
148 aBuffer.append( aBuildID );
149 aBuffer.appendAscii("]");
151 rtl::OUString aXPathExpression = aBuffer.makeStringAndClear();
153 while( aUpdateInfoEnumeration->hasMoreElements() )
155 deployment::UpdateInformationEntry aEntry;
157 if( aUpdateInfoEnumeration->nextElement() >>= aEntry )
159 uno::Reference< xml::dom::XNode > xNode( aEntry.UpdateDocument.get() );
160 uno::Reference< xml::dom::XNodeList > xNodeList;
161 try {
162 xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
163 + UNISTRING("/inst:update/attribute::src"));
164 } catch (css::xml::xpath::XPathException &) {
165 // ignore
169 o_rUpdateInfo.Sources.push_back( DownloadSource(true,
170 UNISTRING("http://openoffice.bouncer.osuosl.org/?product=OpenOffice.org&os=solarissparcwjre&lang=en-US&version=2.2.1") ) );
173 sal_Int32 i, imax = xNodeList->getLength();
174 for( i = 0; i < imax; ++i )
176 uno::Reference< xml::dom::XNode > xNode2( xNodeList->item(i) );
178 if( xNode2.is() )
180 uno::Reference< xml::dom::XElement > xParent(xNode2->getParentNode(), uno::UNO_QUERY_THROW);
181 rtl::OUString aType = xParent->getAttribute(UNISTRING("type"));
182 bool bIsDirect = ( sal_False == aType.equalsIgnoreAsciiCaseAscii("text/html") );
184 o_rUpdateInfo.Sources.push_back( DownloadSource(bIsDirect, xNode2->getNodeValue()) );
188 uno::Reference< xml::dom::XNode > xNode2;
189 try {
190 xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
191 + UNISTRING("/inst:version/text()"));
192 } catch (css::xml::xpath::XPathException &) {
193 // ignore
196 if( xNode2.is() )
197 o_rUpdateInfo.Version = xNode2->getNodeValue();
199 try {
200 xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
201 + UNISTRING("/inst:buildid/text()"));
202 } catch (css::xml::xpath::XPathException &) {
203 // ignore
206 if( xNode2.is() )
207 o_rUpdateInfo.BuildId = xNode2->getNodeValue();
209 o_rUpdateInfo.Description = aEntry.Description;
211 // Release Notes
212 try {
213 xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
214 + UNISTRING("/inst:relnote"));
215 } catch (css::xml::xpath::XPathException &) {
216 // ignore
218 imax = xNodeList->getLength();
219 for( i = 0; i < imax; ++i )
221 uno::Reference< xml::dom::XElement > xRelNote(xNodeList->item(i), uno::UNO_QUERY);
222 if( xRelNote.is() )
224 sal_Int32 pos = xRelNote->getAttribute(UNISTRING("pos")).toInt32();
226 ReleaseNote aRelNote((sal_uInt8) pos, xRelNote->getAttribute(UNISTRING("src")));
228 if( xRelNote->hasAttribute(UNISTRING("src2")) )
230 pos = xRelNote->getAttribute(UNISTRING("pos2")).toInt32();
231 aRelNote.Pos2 = (sal_Int8) pos;
232 aRelNote.URL2 = xRelNote->getAttribute(UNISTRING("src2"));
235 o_rUpdateInfo.ReleaseNotes.push_back(aRelNote);
239 o_rUpdateInfo.ReleaseNotes.push_back(
240 ReleaseNote(1, UNISTRING("http://qa.openoffice.org/tests/online_update_test.html"))
244 if( o_rUpdateInfo.Sources.size() > 0 )
245 return true;
249 catch( ... )
251 return false;
254 return true;
257 //------------------------------------------------------------------------------
258 bool storeExtensionUpdateInfos( const uno::Reference< uno::XComponentContext > & rxContext,
259 const uno::Sequence< uno::Sequence< rtl::OUString > > &rUpdateInfos )
261 if ( rUpdateInfos.hasElements() )
263 rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
265 for ( sal_Int32 i = rUpdateInfos.getLength() - 1; i >= 0; i-- )
267 aConfig->storeExtensionVersion( rUpdateInfos[i][0], rUpdateInfos[i][1] );
270 return rUpdateInfos.hasElements();
273 //------------------------------------------------------------------------------
274 // Returns 'true' if there are updates for any extension
276 bool checkForExtensionUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
278 uno::Sequence< uno::Sequence< rtl::OUString > > aUpdateList;
280 uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
283 uno::Any aValue( rxContext->getValueByName(
284 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
285 OSL_VERIFY( aValue >>= xInfoProvider );
287 catch( const uno::Exception& )
289 OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
292 if ( !xInfoProvider.is() ) return false;
294 aUpdateList = xInfoProvider->isUpdateAvailable( ::rtl::OUString() );
295 storeExtensionUpdateInfos( rxContext, aUpdateList );
297 return aUpdateList.hasElements();
300 //------------------------------------------------------------------------------
301 // Returns 'true' if there are any pending updates for any extension (offline check)
303 bool checkForPendingUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
305 uno::Sequence< uno::Sequence< rtl::OUString > > aExtensionList;
306 uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
309 uno::Any aValue( rxContext->getValueByName(
310 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
311 OSL_VERIFY( aValue >>= xInfoProvider );
313 catch( const uno::Exception& )
315 OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
318 if ( !xInfoProvider.is() ) return false;
320 bool bPendingUpdateFound = false;
322 aExtensionList = xInfoProvider->getExtensionList();
323 if ( aExtensionList.hasElements() )
325 rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
327 for ( sal_Int32 i = aExtensionList.getLength() - 1; i >= 0; i-- )
329 bPendingUpdateFound = aConfig->checkExtensionVersion( aExtensionList[i][0], aExtensionList[i][1] );
330 if ( bPendingUpdateFound )
331 break;
335 return bPendingUpdateFound;