12a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52a97ec55SAndrew Rist  * distributed with this work for additional information
62a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
92a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
102a97ec55SAndrew Rist  *
112a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122a97ec55SAndrew Rist  *
132a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142a97ec55SAndrew Rist  * software distributed under the License is distributed on an
152a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
172a97ec55SAndrew Rist  * specific language governing permissions and limitations
182a97ec55SAndrew Rist  * under the License.
192a97ec55SAndrew Rist  *
202a97ec55SAndrew Rist  *************************************************************/
212a97ec55SAndrew Rist 
222a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "updateprotocol.hxx"
29cdf0e10cSrcweir #include "updatecheckconfig.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #ifndef _COM_SUN_STAR_DEPLOYMENT_UPDATEINFORMATINENTRY_HPP_
32cdf0e10cSrcweir #include <com/sun/star/deployment/UpdateInformationEntry.hpp>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <com/sun/star/deployment/XPackageInformationProvider.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <rtl/ref.hxx>
38cdf0e10cSrcweir #include <rtl/uri.hxx>
39cdf0e10cSrcweir #include <rtl/strbuf.hxx>
40cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
41cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
42cdf0e10cSrcweir #include <osl/process.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace css = com::sun::star ;
47cdf0e10cSrcweir namespace container = css::container ;
48cdf0e10cSrcweir namespace deployment = css::deployment ;
49cdf0e10cSrcweir namespace lang = css::lang ;
50cdf0e10cSrcweir namespace uno = css::uno ;
51cdf0e10cSrcweir namespace task = css::task ;
52cdf0e10cSrcweir namespace xml = css::xml ;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //------------------------------------------------------------------------------
57cdf0e10cSrcweir 
58cdf0e10cSrcweir static bool
getBootstrapData(uno::Sequence<::rtl::OUString> & rRepositoryList,::rtl::OUString & rBuildID,::rtl::OUString & rInstallSetID)59cdf0e10cSrcweir getBootstrapData(
60cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > & rRepositoryList,
61cdf0e10cSrcweir     ::rtl::OUString & rBuildID,
62cdf0e10cSrcweir     ::rtl::OUString & rInstallSetID)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     rBuildID = UNISTRING( "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version") ":ProductBuildid}" );
65cdf0e10cSrcweir     rtl::Bootstrap::expandMacros( rBuildID );
66cdf0e10cSrcweir     if ( ! rBuildID.getLength() )
67cdf0e10cSrcweir         return false;
68cdf0e10cSrcweir 
69*910823aeSJürgen Schmidt     rInstallSetID = UNISTRING( "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateID}" );
70cdf0e10cSrcweir     rtl::Bootstrap::expandMacros( rInstallSetID );
71cdf0e10cSrcweir     if ( ! rInstallSetID.getLength() )
72cdf0e10cSrcweir         return false;
73cdf0e10cSrcweir 
74*910823aeSJürgen Schmidt     rtl::OUString aValue( UNISTRING( "${$OOO_BASE_DIR/program/" SAL_CONFIGFILE("version") ":UpdateURL}" ) );
75cdf0e10cSrcweir     rtl::Bootstrap::expandMacros( aValue );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     if( aValue.getLength() > 0 )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         rRepositoryList.realloc(1);
80cdf0e10cSrcweir         rRepositoryList[0] = aValue;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     return true;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //------------------------------------------------------------------------------
87cdf0e10cSrcweir 
88cdf0e10cSrcweir // Returns 'true' if successfully connected to the update server
89cdf0e10cSrcweir bool
checkForUpdates(UpdateInfo & o_rUpdateInfo,uno::Reference<uno::XComponentContext> const & rxContext,uno::Reference<task::XInteractionHandler> const & rxInteractionHandler,const uno::Reference<deployment::XUpdateInformationProvider> & rUpdateInfoProvider)90cdf0e10cSrcweir checkForUpdates(
91cdf0e10cSrcweir     UpdateInfo& o_rUpdateInfo,
92cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > const & rxContext,
93cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > const & rxInteractionHandler,
94cdf0e10cSrcweir     const uno::Reference< deployment::XUpdateInformationProvider >& rUpdateInfoProvider)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     OSL_TRACE("checking for updates ..\n");
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     ::rtl::OUString myArch;
99cdf0e10cSrcweir     ::rtl::OUString myOS;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     rtl::Bootstrap::get(UNISTRING("_OS"), myOS);
102cdf0e10cSrcweir     rtl::Bootstrap::get(UNISTRING("_ARCH"), myArch);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRepositoryList;
105cdf0e10cSrcweir     ::rtl::OUString aBuildID;
106cdf0e10cSrcweir     ::rtl::OUString aInstallSetID;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     if( ! ( getBootstrapData(aRepositoryList, aBuildID, aInstallSetID) && (aRepositoryList.getLength() > 0) ) )
109cdf0e10cSrcweir         return false;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     if( !rxContext.is() )
112cdf0e10cSrcweir         throw uno::RuntimeException(
113cdf0e10cSrcweir             UNISTRING( "checkForUpdates: empty component context" ), uno::Reference< uno::XInterface >() );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     OSL_ASSERT( rxContext->getServiceManager().is() );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     // XPath implementation
118cdf0e10cSrcweir     uno::Reference< xml::xpath::XXPathAPI > xXPath(
119cdf0e10cSrcweir         rxContext->getServiceManager()->createInstanceWithContext( UNISTRING( "com.sun.star.xml.xpath.XPathAPI" ), rxContext ),
120cdf0e10cSrcweir         uno::UNO_QUERY_THROW);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     xXPath->registerNS( UNISTRING("inst"), UNISTRING("http://installation.openoffice.org/description") );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     if( rxInteractionHandler.is() )
125cdf0e10cSrcweir         rUpdateInfoProvider->setInteractionHandler(rxInteractionHandler);
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     try
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir 		uno::Reference< container::XEnumeration > aUpdateInfoEnumeration =
130cdf0e10cSrcweir             rUpdateInfoProvider->getUpdateInformationEnumeration( aRepositoryList, aInstallSetID );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if ( !aUpdateInfoEnumeration.is() )
133cdf0e10cSrcweir             return false; // something went wrong ..
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         rtl::OUStringBuffer aBuffer;
136cdf0e10cSrcweir         aBuffer.appendAscii("/child::inst:description[inst:os=\'");
137cdf0e10cSrcweir         aBuffer.append( myOS );
138cdf0e10cSrcweir         aBuffer.appendAscii("\' and inst:arch=\'");
139cdf0e10cSrcweir         aBuffer.append( myArch );
140cdf0e10cSrcweir         aBuffer.appendAscii("\' and inst:buildid>");
141cdf0e10cSrcweir         aBuffer.append( aBuildID );
142cdf0e10cSrcweir         aBuffer.appendAscii("]");
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         rtl::OUString aXPathExpression = aBuffer.makeStringAndClear();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         while( aUpdateInfoEnumeration->hasMoreElements() )
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             deployment::UpdateInformationEntry aEntry;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             if( aUpdateInfoEnumeration->nextElement() >>= aEntry )
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 uno::Reference< xml::dom::XNode > xNode( aEntry.UpdateDocument.get() );
153cdf0e10cSrcweir                 uno::Reference< xml::dom::XNodeList > xNodeList;
154cdf0e10cSrcweir                 try {
155cdf0e10cSrcweir                     xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
156cdf0e10cSrcweir                         + UNISTRING("/inst:update/attribute::src"));
157cdf0e10cSrcweir                 } catch (css::xml::xpath::XPathException &) {
158cdf0e10cSrcweir                     // ignore
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir /*
162cdf0e10cSrcweir                 o_rUpdateInfo.Sources.push_back( DownloadSource(true,
163cdf0e10cSrcweir                     UNISTRING("http://openoffice.bouncer.osuosl.org/?product=OpenOffice.org&os=solarissparcwjre&lang=en-US&version=2.2.1") ) );
164cdf0e10cSrcweir */
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                 sal_Int32 i, imax = xNodeList->getLength();
167cdf0e10cSrcweir                 for( i = 0; i < imax; ++i )
168cdf0e10cSrcweir                 {
169cdf0e10cSrcweir                     uno::Reference< xml::dom::XNode > xNode2( xNodeList->item(i) );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir                     if( xNode2.is() )
172cdf0e10cSrcweir                     {
173cdf0e10cSrcweir                         uno::Reference< xml::dom::XElement > xParent(xNode2->getParentNode(), uno::UNO_QUERY_THROW);
174cdf0e10cSrcweir                         rtl::OUString aType = xParent->getAttribute(UNISTRING("type"));
175cdf0e10cSrcweir                         bool bIsDirect = ( sal_False == aType.equalsIgnoreAsciiCaseAscii("text/html") );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir                         o_rUpdateInfo.Sources.push_back( DownloadSource(bIsDirect, xNode2->getNodeValue()) );
178cdf0e10cSrcweir                     }
179cdf0e10cSrcweir                 }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 uno::Reference< xml::dom::XNode > xNode2;
182cdf0e10cSrcweir                 try {
183cdf0e10cSrcweir                     xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
184cdf0e10cSrcweir                         + UNISTRING("/inst:version/text()"));
185cdf0e10cSrcweir                 } catch (css::xml::xpath::XPathException &) {
186cdf0e10cSrcweir                     // ignore
187cdf0e10cSrcweir                 }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                 if( xNode2.is() )
190cdf0e10cSrcweir                     o_rUpdateInfo.Version = xNode2->getNodeValue();
191cdf0e10cSrcweir 
192cdf0e10cSrcweir                 try {
193cdf0e10cSrcweir                     xNode2 = xXPath->selectSingleNode(xNode, aXPathExpression
194cdf0e10cSrcweir                         + UNISTRING("/inst:buildid/text()"));
195cdf0e10cSrcweir                 } catch (css::xml::xpath::XPathException &) {
196cdf0e10cSrcweir                     // ignore
197cdf0e10cSrcweir                 }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir                 if( xNode2.is() )
200cdf0e10cSrcweir                     o_rUpdateInfo.BuildId = xNode2->getNodeValue();
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                 o_rUpdateInfo.Description = aEntry.Description;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir                 // Release Notes
205cdf0e10cSrcweir                 try {
206cdf0e10cSrcweir                     xNodeList = xXPath->selectNodeList(xNode, aXPathExpression
207cdf0e10cSrcweir                         + UNISTRING("/inst:relnote"));
208cdf0e10cSrcweir                 } catch (css::xml::xpath::XPathException &) {
209cdf0e10cSrcweir                     // ignore
210cdf0e10cSrcweir                 }
211cdf0e10cSrcweir                 imax = xNodeList->getLength();
212cdf0e10cSrcweir                 for( i = 0; i < imax; ++i )
213cdf0e10cSrcweir                 {
214cdf0e10cSrcweir                     uno::Reference< xml::dom::XElement > xRelNote(xNodeList->item(i), uno::UNO_QUERY);
215cdf0e10cSrcweir                     if( xRelNote.is() )
216cdf0e10cSrcweir                     {
217cdf0e10cSrcweir                         sal_Int32 pos = xRelNote->getAttribute(UNISTRING("pos")).toInt32();
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                         ReleaseNote aRelNote((sal_uInt8) pos, xRelNote->getAttribute(UNISTRING("src")));
220cdf0e10cSrcweir 
221cdf0e10cSrcweir                         if( xRelNote->hasAttribute(UNISTRING("src2")) )
222cdf0e10cSrcweir                         {
223cdf0e10cSrcweir                             pos = xRelNote->getAttribute(UNISTRING("pos2")).toInt32();
224cdf0e10cSrcweir                             aRelNote.Pos2 = (sal_Int8) pos;
225cdf0e10cSrcweir                             aRelNote.URL2 = xRelNote->getAttribute(UNISTRING("src2"));
226cdf0e10cSrcweir                         }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir                         o_rUpdateInfo.ReleaseNotes.push_back(aRelNote);
229cdf0e10cSrcweir                     }
230cdf0e10cSrcweir                 }
231cdf0e10cSrcweir /*
232cdf0e10cSrcweir                 o_rUpdateInfo.ReleaseNotes.push_back(
233cdf0e10cSrcweir                     ReleaseNote(1, UNISTRING("http://qa.openoffice.org/tests/online_update_test.html"))
234cdf0e10cSrcweir                 );
235cdf0e10cSrcweir */
236cdf0e10cSrcweir 
237cdf0e10cSrcweir                 if( o_rUpdateInfo.Sources.size() > 0 )
238cdf0e10cSrcweir                     return true;
239cdf0e10cSrcweir             }
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir     catch( ... )
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         return false;
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     return true;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir //------------------------------------------------------------------------------
storeExtensionUpdateInfos(const uno::Reference<uno::XComponentContext> & rxContext,const uno::Sequence<uno::Sequence<rtl::OUString>> & rUpdateInfos)251cdf0e10cSrcweir bool storeExtensionUpdateInfos( const uno::Reference< uno::XComponentContext > & rxContext,
252cdf0e10cSrcweir                                 const uno::Sequence< uno::Sequence< rtl::OUString > > &rUpdateInfos )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     bool bNotify = false;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     if ( rUpdateInfos.hasElements() )
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         for ( sal_Int32 i = rUpdateInfos.getLength() - 1; i >= 0; i-- )
261cdf0e10cSrcweir         {
262cdf0e10cSrcweir             bNotify |= aConfig->storeExtensionVersion( rUpdateInfos[i][0], rUpdateInfos[i][1] );
263cdf0e10cSrcweir         }
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir     return bNotify;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir //------------------------------------------------------------------------------
269cdf0e10cSrcweir // Returns 'true' if there are updates for any extension
270cdf0e10cSrcweir 
checkForExtensionUpdates(const uno::Reference<uno::XComponentContext> & rxContext)271cdf0e10cSrcweir bool checkForExtensionUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir     uno::Sequence< uno::Sequence< rtl::OUString > > aUpdateList;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
276cdf0e10cSrcweir     try
277cdf0e10cSrcweir     {
278cdf0e10cSrcweir         uno::Any aValue( rxContext->getValueByName(
279cdf0e10cSrcweir                 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
280cdf0e10cSrcweir         OSL_VERIFY( aValue >>= xInfoProvider );
281cdf0e10cSrcweir     }
282cdf0e10cSrcweir     catch( const uno::Exception& )
283cdf0e10cSrcweir     {
284cdf0e10cSrcweir         OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     if ( !xInfoProvider.is() ) return false;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     aUpdateList = xInfoProvider->isUpdateAvailable( ::rtl::OUString() );
290cdf0e10cSrcweir     bool bNotify = storeExtensionUpdateInfos( rxContext, aUpdateList );
291cdf0e10cSrcweir 
292cdf0e10cSrcweir     return bNotify;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir //------------------------------------------------------------------------------
296cdf0e10cSrcweir // Returns 'true' if there are any pending updates for any extension (offline check)
297cdf0e10cSrcweir 
checkForPendingUpdates(const uno::Reference<uno::XComponentContext> & rxContext)298cdf0e10cSrcweir bool checkForPendingUpdates( const uno::Reference< uno::XComponentContext > & rxContext )
299cdf0e10cSrcweir {
300cdf0e10cSrcweir     uno::Sequence< uno::Sequence< rtl::OUString > > aExtensionList;
301cdf0e10cSrcweir     uno::Reference< deployment::XPackageInformationProvider > xInfoProvider;
302cdf0e10cSrcweir     try
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         uno::Any aValue( rxContext->getValueByName(
305cdf0e10cSrcweir                 UNISTRING( "/singletons/com.sun.star.deployment.PackageInformationProvider" ) ) );
306cdf0e10cSrcweir         OSL_VERIFY( aValue >>= xInfoProvider );
307cdf0e10cSrcweir     }
308cdf0e10cSrcweir     catch( const uno::Exception& )
309cdf0e10cSrcweir     {
310cdf0e10cSrcweir         OSL_ENSURE( false, "checkForExtensionUpdates: could not create the PackageInformationProvider!" );
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     if ( !xInfoProvider.is() ) return false;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir     bool bPendingUpdateFound = false;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     aExtensionList = xInfoProvider->getExtensionList();
318cdf0e10cSrcweir     if ( aExtensionList.hasElements() )
319cdf0e10cSrcweir     {
320cdf0e10cSrcweir         rtl::Reference< UpdateCheckConfig > aConfig = UpdateCheckConfig::get( rxContext );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         for ( sal_Int32 i = aExtensionList.getLength() - 1; i >= 0; i-- )
323cdf0e10cSrcweir         {
324cdf0e10cSrcweir             bPendingUpdateFound = aConfig->checkExtensionVersion( aExtensionList[i][0], aExtensionList[i][1] );
325cdf0e10cSrcweir             if ( bPendingUpdateFound )
326cdf0e10cSrcweir                 break;
327cdf0e10cSrcweir         }
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     return bPendingUpdateFound;
331cdf0e10cSrcweir }
332