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