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 #include <cppuhelper/implbase3.hxx>
29 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/util/XChangesBatch.hpp>
34 #include <rtl/ref.hxx>
35 
36 #include "updatecheckconfiglistener.hxx"
37 #include "updateinfo.hxx"
38 
39 /* Interface to acess configuration data read-only */
40 struct IByNameAccess
41 {
42     virtual ::com::sun::star::uno::Any getValue(const sal_Char * pName) = 0;
43 };
44 
45 /* This helper class provides by name access to a sequence of named values */
46 class NamedValueByNameAccess : public IByNameAccess
47 {
48     const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& m_rValues;
49 
50 public:
51     NamedValueByNameAccess(
52         const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rValues) :
53         m_rValues(rValues) {} ;
54 
55     virtual ~NamedValueByNameAccess();
56 
57     virtual ::com::sun::star::uno::Any getValue(const sal_Char * pName);
58 };
59 
60 
61 /* This class encapsulates the configuration item actually used for storing the state
62  * the update check is actually in.
63  */
64 class UpdateCheckROModel
65 {
66 public:
67     UpdateCheckROModel(IByNameAccess& aNameAccess) : m_aNameAccess(aNameAccess) {};
68 
69     bool isAutoCheckEnabled() const;
70     bool isDownloadPaused() const;
71     rtl::OUString getLocalFileName() const;
72     sal_Int64 getDownloadSize() const;
73 
74     rtl::OUString getUpdateEntryVersion() const;
75     void getUpdateEntry(UpdateInfo& rInfo) const;
76 
77 private:
78 
79     rtl::OUString getStringValue(const sal_Char *) const;
80 
81     IByNameAccess& m_aNameAccess;
82 };
83 
84 
85 
86 /* This class implements the non published UNO service com.sun.star.setup.UpdateCheckConfig,
87  * which primary use is to be able to track changes done in the Toos -> Options page of this
88  * component, as this is not supported by the OOo configuration for extendable groups.
89  */
90 
91 class UpdateCheckConfig : public ::cppu::WeakImplHelper3<
92     ::com::sun::star::container::XNameReplace,
93     ::com::sun::star::util::XChangesBatch,
94     ::com::sun::star::lang::XServiceInfo >
95 {
96     UpdateCheckConfig( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& xContainer,
97                        const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& xAvailableUpdates,
98                        const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& xIgnoredUpdates,
99                        const ::rtl::Reference< UpdateCheckConfigListener >& rListener );
100 
101     virtual ~UpdateCheckConfig();
102 
103 public:
104 
105     static ::com::sun::star::uno::Sequence< rtl::OUString > getServiceNames();
106     static rtl::OUString getImplName();
107 
108     static ::rtl::Reference< UpdateCheckConfig > get(
109         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext,
110         const ::rtl::Reference< UpdateCheckConfigListener >& rListener = ::rtl::Reference< UpdateCheckConfigListener >());
111 
112     // Should really implement ROModel ..
113     bool isAutoCheckEnabled() const;
114     bool isAutoDownloadEnabled() const;
115     rtl::OUString getUpdateEntryVersion() const;
116 
117     /* Updates the timestamp of last check, but does not commit the change
118      * as either clearUpdateFound() or setUpdateFound() are expected to get
119      * called next.
120      */
121     void updateLastChecked();
122 
123     /* Returns the date of the last successful check in seconds since 1970 */
124     sal_Int64 getLastChecked() const;
125 
126     /* Returns configured check interval in seconds */
127     sal_Int64 getCheckInterval() const;
128 
129     /* Reset values of previously remembered update
130      */
131     void clearUpdateFound();
132 
133     /* Stores the specified data of an available update
134      */
135     void storeUpdateFound(const UpdateInfo& rInfo, const rtl::OUString& aCurrentBuild);
136 
137     // Returns the local file name of a started download
138     rtl::OUString getLocalFileName() const;
139 
140     // Returns the local file name of a started download
141     rtl::OUString getDownloadDestination() const;
142 
143     // stores the local file name of a just started download
144     void storeLocalFileName(const rtl::OUString& rFileName, sal_Int64 nFileSize);
145 
146     // Removes the local file name of a download
147     void clearLocalFileName();
148 
149     // Stores the bool value for manually paused downloads
150     void storeDownloadPaused(bool paused);
151 
152     // Returns the directory that acts as the user's desktop
153     static rtl::OUString getDesktopDirectory();
154 
155     // Returns a directory accessible for all users
156     static rtl::OUString getAllUsersDirectory();
157 
158     // store and retrieve information about extensions
159     bool storeExtensionVersion( const rtl::OUString& rExtensionName,
160                                 const rtl::OUString& rVersion );
161     bool checkExtensionVersion( const rtl::OUString& rExtensionName,
162                                 const rtl::OUString& rVersion );
163 
164     // XElementAccess
165     virtual ::com::sun::star::uno::Type SAL_CALL getElementType(  )
166         throw (::com::sun::star::uno::RuntimeException);
167     virtual sal_Bool SAL_CALL hasElements(  )
168         throw (::com::sun::star::uno::RuntimeException);
169 
170     // XNameAccess
171     virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
172         throw (::com::sun::star::container::NoSuchElementException,
173                ::com::sun::star::lang::WrappedTargetException,
174                ::com::sun::star::uno::RuntimeException);
175     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  )
176         throw (::com::sun::star::uno::RuntimeException);
177     virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
178         throw (::com::sun::star::uno::RuntimeException);
179 
180     // XNameReplace
181     virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
182         throw (::com::sun::star::lang::IllegalArgumentException,
183                ::com::sun::star::container::NoSuchElementException,
184                ::com::sun::star::lang::WrappedTargetException,
185                ::com::sun::star::uno::RuntimeException);
186 
187     // XChangesBatch
188     virtual void SAL_CALL commitChanges(  )
189         throw (::com::sun::star::lang::WrappedTargetException,
190                ::com::sun::star::uno::RuntimeException);
191     virtual ::sal_Bool SAL_CALL hasPendingChanges(  )
192         throw (::com::sun::star::uno::RuntimeException);
193     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::util::ElementChange > SAL_CALL getPendingChanges(  )
194         throw (::com::sun::star::uno::RuntimeException);
195 
196     // XServiceInfo
197     virtual rtl::OUString SAL_CALL getImplementationName()
198         throw (::com::sun::star::uno::RuntimeException);
199     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
200         throw (::com::sun::star::uno::RuntimeException);
201     virtual ::com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames()
202         throw (::com::sun::star::uno::RuntimeException);
203 
204 private:
205 
206     static rtl::OUString getSubVersion( const rtl::OUString& rVersion, sal_Int32 *nIndex );
207     static bool isVersionGreater( const rtl::OUString& rVersion1, const rtl::OUString& rVersion2 );
208 
209     const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xContainer;
210     const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xAvailableUpdates;
211     const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer > m_xIgnoredUpdates;
212     const ::rtl::Reference< UpdateCheckConfigListener > m_rListener;
213 };
214 
215 //------------------------------------------------------------------------------
216 
217 
218 template <typename T>
219 T getValue( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rNamedValues, const sal_Char * pszName )
220     throw (::com::sun::star::uno::RuntimeException)
221 {
222     for( sal_Int32 n=0; n < rNamedValues.getLength(); n++ )
223     {
224 	    // Unfortunatly gcc-3.3 does not like Any.get<T>();
225         if( rNamedValues[n].Name.equalsAscii( pszName ) )
226         {
227             T value = T();
228             if( ! (rNamedValues[n].Value >>= value) )
229                 throw ::com::sun::star::uno::RuntimeException(
230                     ::rtl::OUString(
231                         cppu_Any_extraction_failure_msg(
232                             &rNamedValues[n].Value,
233                             ::cppu::getTypeFavourUnsigned(&value).getTypeLibType() ),
234                             SAL_NO_ACQUIRE ),
235                     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
236 
237             return value;
238         }
239     }
240 
241     return T();
242 }
243