xref: /aoo41x/main/cui/source/options/optupdt.cxx (revision 2ee96f1c)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cui.hxx"
26 
27 // include ---------------------------------------------------------------
28 #include <vcl/svapp.hxx>
29 #include <sfx2/filedlghelper.hxx>
30 #include <svl/zforlist.hxx>
31 #include "optupdt.hxx"
32 #include "optupdt.hrc"
33 #include <dialmgr.hxx>
34 #include <cuires.hrc>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <com/sun/star/frame/XDesktop.hpp>
40 #include <com/sun/star/frame/XDispatchProvider.hpp>
41 #include <com/sun/star/util/XChangesBatch.hpp>
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <osl/file.hxx>
44 #include <osl/security.hxx>
45 
46 namespace beans = ::com::sun::star::beans;
47 namespace container = ::com::sun::star::container;
48 namespace dialogs = ::com::sun::star::ui::dialogs;
49 namespace frame = ::com::sun::star::frame;
50 namespace lang = ::com::sun::star::lang;
51 namespace uno = ::com::sun::star::uno;
52 namespace util = ::com::sun::star::util;
53 
54 // define ----------------------------------------------------------------
55 
56 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
57 
58 // class SvxOnlineUpdateTabPage --------------------------------------------------
59 
60 SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage( Window* pParent, const SfxItemSet& rSet ) :
61 
62 	SfxTabPage( pParent, CUI_RES( RID_SVXPAGE_ONLINEUPDATE ), rSet ),
63         m_aOptionsLine( this, CUI_RES( FL_OPTIONS ) ),
64         m_aAutoCheckCheckBox( this, CUI_RES( CB_AUTOCHECK ) ),
65         m_aEveryDayButton( this, CUI_RES( RB_EVERYDAY ) ),
66         m_aEveryWeekButton( this, CUI_RES( RB_EVERYWEEK ) ),
67         m_aEveryMonthButton( this, CUI_RES( RB_EVERYMONTH ) ),
68         m_aCheckNowButton( this, CUI_RES( PB_CHECKNOW ) ),
69         m_aAutoDownloadCheckBox( this, CUI_RES( CB_AUTODOWNLOAD ) ),
70         m_aDestPathLabel( this, CUI_RES( FT_DESTPATHLABEL ) ),
71         m_aDestPath( this, CUI_RES( FT_DESTPATH ) ),
72         m_aChangePathButton( this, CUI_RES( PB_CHANGEPATH ) ),
73         m_aLastChecked( this, CUI_RES( FT_LASTCHECKED ) )
74 {
75     m_aNeverChecked = String( CUI_RES( STR_NEVERCHECKED ) );
76     FreeResource();
77 
78     m_aAutoCheckCheckBox.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
79     m_aCheckNowButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
80     m_aChangePathButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
81 
82     uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
83 
84     m_xUpdateAccess = uno::Reference < container::XNameReplace >(
85         xFactory->createInstance( UNISTRING( "com.sun.star.setup.UpdateCheckConfig" ) ),
86         uno::UNO_QUERY_THROW );
87 
88     sal_Bool bDownloadSupported = sal_False;
89     m_xUpdateAccess->getByName( UNISTRING( "DownloadSupported") ) >>= bDownloadSupported;
90 
91     WinBits nStyle = m_aDestPath.GetStyle();
92     nStyle |= WB_PATHELLIPSIS;
93     m_aDestPath.SetStyle(nStyle);
94 
95     m_aAutoDownloadCheckBox.Show(bDownloadSupported);
96     m_aDestPathLabel.Show(bDownloadSupported);
97     m_aDestPath.Show(bDownloadSupported);
98     m_aChangePathButton.Show(bDownloadSupported);
99 
100     // dynamical length of the PushButtons
101     CalcButtonWidth();
102 
103     m_aLastCheckedTemplate = m_aLastChecked.GetText();
104 
105     UpdateLastCheckedText();
106 }
107 
108 // -----------------------------------------------------------------------
109 
110 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
111 {
112 }
113 
114 // -----------------------------------------------------------------------
115 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
116 {
117     rtl::OUString aDateStr;
118     rtl::OUString aTimeStr;
119     rtl::OUString aText;
120     sal_Int64 lastChecked = 0;
121 
122     m_xUpdateAccess->getByName( UNISTRING( "LastCheck") ) >>= lastChecked;
123 
124     if( lastChecked == 0 ) // never checked
125     {
126         aText = m_aNeverChecked;
127     }
128     else
129     {
130         TimeValue   lastCheckedTV;
131         oslDateTime lastCheckedDT;
132 
133         Date  aDate;
134         Time  aTime;
135 
136         lastCheckedTV.Seconds = (sal_uInt32) lastChecked;
137         osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
138 
139         if ( osl_getDateTimeFromTimeValue(  &lastCheckedTV, &lastCheckedDT ) )
140         {
141             aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
142             aTime = Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
143         }
144 
145         LanguageType eUILang = Application::GetSettings().GetUILanguage();
146         SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( ::comphelper::getProcessServiceFactory(), eUILang );
147         String      aTmpStr;
148         Color*      pColor = NULL;
149         Date*       pNullDate = pNumberFormatter->GetNullDate();
150         sal_uInt32  nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_DATE, eUILang );
151 
152         pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
153         aDateStr = aTmpStr;
154 
155         nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_TIME, eUILang );
156         pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTmpStr, &pColor );
157         aTimeStr = aTmpStr;
158 
159         delete pColor;
160         delete pNumberFormatter;
161 
162         aText = m_aLastCheckedTemplate;
163         sal_Int32 nIndex = aText.indexOf( UNISTRING( "%DATE%" ) );
164         if ( nIndex != -1 )
165             aText = aText.replaceAt( nIndex, 6, aDateStr );
166 
167         nIndex = aText.indexOf( UNISTRING( "%TIME%" ) );
168         if ( nIndex != -1 )
169             aText = aText.replaceAt( nIndex, 6, aTimeStr );
170     }
171 
172     m_aLastChecked.SetText( aText );
173 }
174 
175 // -----------------------------------------------------------------------
176 
177 SfxTabPage*
178 SvxOnlineUpdateTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
179 {
180     return new SvxOnlineUpdateTabPage( pParent, rAttrSet );
181 }
182 
183 // -----------------------------------------------------------------------
184 
185 
186 sal_Bool SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet& )
187 {
188     sal_Bool bModified = sal_False;
189 
190     sal_Bool bValue;
191     sal_Int64 nValue;
192 
193     if( m_aAutoCheckCheckBox.GetSavedValue() != m_aAutoCheckCheckBox.IsChecked() )
194     {
195         bValue = (sal_True == m_aAutoCheckCheckBox.IsChecked());
196         m_xUpdateAccess->replaceByName( UNISTRING("AutoCheckEnabled"), uno::makeAny( bValue ) );
197         bModified = sal_True;
198     }
199 
200     nValue = 0;
201     if( sal_True == m_aEveryDayButton.IsChecked() )
202     {
203         if( sal_False == m_aEveryDayButton.GetSavedValue() )
204             nValue = 86400;
205     }
206     else if( sal_True == m_aEveryWeekButton.IsChecked() )
207     {
208         if( sal_False == m_aEveryWeekButton.GetSavedValue() )
209             nValue = 604800;
210     }
211     else if( sal_True == m_aEveryMonthButton.IsChecked() )
212     {
213         if( sal_False == m_aEveryMonthButton.GetSavedValue() )
214             nValue = 2592000;
215     }
216 
217     if( nValue > 0 )
218     {
219         m_xUpdateAccess->replaceByName( UNISTRING("CheckInterval"), uno::makeAny( nValue ) );
220         bModified = sal_True;
221     }
222 
223     if( m_aAutoDownloadCheckBox.GetSavedValue() != m_aAutoDownloadCheckBox.IsChecked() )
224     {
225         bValue = (sal_True == m_aAutoDownloadCheckBox.IsChecked());
226         m_xUpdateAccess->replaceByName( UNISTRING("AutoDownloadEnabled"), uno::makeAny( bValue ) );
227         bModified = sal_True;
228     }
229 
230     rtl::OUString sValue, aURL;
231     m_xUpdateAccess->getByName( UNISTRING("DownloadDestination") ) >>= sValue;
232 
233     if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) ) &&
234         ( ! aURL.equals( sValue ) ) )
235     {
236         m_xUpdateAccess->replaceByName( UNISTRING("DownloadDestination"), uno::makeAny( aURL ) );
237         bModified = sal_True;
238     }
239 
240     uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
241     if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
242         xChangesBatch->commitChanges();
243 
244     return bModified;
245 }
246 
247 // -----------------------------------------------------------------------
248 
249 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet& )
250 {
251     sal_Bool bValue = sal_Bool();
252     m_xUpdateAccess->getByName( UNISTRING("AutoCheckEnabled") ) >>= bValue;
253 
254     m_aAutoCheckCheckBox.Check(bValue);
255     m_aEveryDayButton.Enable(bValue);
256     m_aEveryWeekButton.Enable(bValue);
257     m_aEveryMonthButton.Enable(bValue);
258 
259     sal_Int64 nValue = 0;
260     m_xUpdateAccess->getByName( UNISTRING("CheckInterval") ) >>= nValue;
261 
262     if( nValue == 86400 )
263         m_aEveryDayButton.Check();
264     else if( nValue == 604800 )
265         m_aEveryWeekButton.Check();
266     else
267         m_aEveryMonthButton.Check();
268 
269     m_aAutoCheckCheckBox.SaveValue();
270     m_aEveryDayButton.SaveValue();
271     m_aEveryWeekButton.SaveValue();
272     m_aEveryMonthButton.SaveValue();
273 
274     m_xUpdateAccess->getByName( UNISTRING("AutoDownloadEnabled") ) >>= bValue;
275     m_aAutoDownloadCheckBox.Check(bValue);
276     m_aDestPathLabel.Enable(sal_True);
277     m_aDestPath.Enable(sal_True);
278     m_aChangePathButton.Enable(sal_True);
279 
280     rtl::OUString sValue, aPath;
281     m_xUpdateAccess->getByName( UNISTRING("DownloadDestination") ) >>= sValue;
282 
283     if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
284         m_aDestPath.SetText(aPath);
285 
286     m_aAutoDownloadCheckBox.SaveValue();
287 }
288 
289 // -----------------------------------------------------------------------
290 
291 void SvxOnlineUpdateTabPage::FillUserData()
292 {
293 }
294 
295 // -----------------------------------------------------------------------
296 
297 IMPL_LINK( SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, CheckBox *, pBox )
298 {
299     sal_Bool bEnabled = pBox->IsChecked();
300 
301     m_aEveryDayButton.Enable(bEnabled);
302     m_aEveryWeekButton.Enable(bEnabled);
303     m_aEveryMonthButton.Enable(bEnabled);
304 
305     return 0;
306 }
307 
308 // -----------------------------------------------------------------------
309 
310 IMPL_LINK( SvxOnlineUpdateTabPage, FileDialogHdl_Impl, PushButton *, EMPTYARG )
311 {
312     uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
313     uno::Reference < dialogs::XFolderPicker > xFolderPicker(
314         xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME ) ) ),
315         uno::UNO_QUERY );
316 
317     rtl::OUString aURL;
318     if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) )
319         osl::Security().getHomeDir(aURL);
320 
321     xFolderPicker->setDisplayDirectory( aURL );
322     sal_Int16 nRet = xFolderPicker->execute();
323 
324     if ( dialogs::ExecutableDialogResults::OK == nRet )
325     {
326         rtl::OUString aFolder;
327         if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
328             m_aDestPath.SetText( aFolder );
329     }
330 
331     return 0;
332 }
333 
334 // -----------------------------------------------------------------------
335 
336 IMPL_LINK( SvxOnlineUpdateTabPage, CheckNowHdl_Impl, PushButton *, EMPTYARG )
337 {
338     uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
339 
340     try
341     {
342         uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
343             xFactory->createInstance( UNISTRING( "com.sun.star.configuration.ConfigurationProvider" )),
344             uno::UNO_QUERY_THROW);
345 
346         beans::PropertyValue aProperty;
347         aProperty.Name  = UNISTRING( "nodepath" );
348         aProperty.Value = uno::makeAny( UNISTRING("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
349 
350         uno::Sequence< uno::Any > aArgumentList( 1 );
351         aArgumentList[0] = uno::makeAny( aProperty );
352 
353         uno::Reference< container::XNameAccess > xNameAccess(
354             xConfigProvider->createInstanceWithArguments(
355                 UNISTRING("com.sun.star.configuration.ConfigurationAccess"), aArgumentList ),
356             uno::UNO_QUERY_THROW );
357 
358         util::URL aURL;
359         xNameAccess->getByName(UNISTRING("URL")) >>= aURL.Complete;
360 
361         uno::Reference < util::XURLTransformer > xTransformer(
362             xFactory->createInstance(  UNISTRING( "com.sun.star.util.URLTransformer" ) ),
363             uno::UNO_QUERY_THROW );
364 
365         xTransformer->parseStrict(aURL);
366 
367         uno::Reference < frame::XDesktop > xDesktop(
368             xFactory->createInstance(  UNISTRING( "com.sun.star.frame.Desktop" ) ),
369             uno::UNO_QUERY_THROW );
370 
371         uno::Reference< frame::XDispatchProvider > xDispatchProvider(
372             xDesktop->getCurrentFrame(), uno::UNO_QUERY );
373 
374         uno::Reference< frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL, rtl::OUString(), 0);
375 
376         if( xDispatch.is() )
377         {
378             xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
379             UpdateLastCheckedText();
380         }
381     }
382     catch( const uno::Exception& e )
383     {
384          OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
385             rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
386     }
387 
388     return 0;
389 }
390 
391 void SvxOnlineUpdateTabPage::CalcButtonWidth()
392 {
393     // detect the longest button text
394     long nTxtWidth = ::std::max( m_aCheckNowButton.GetCtrlTextWidth( m_aCheckNowButton.GetText() ),
395                                  m_aCheckNowButton.GetCtrlTextWidth( m_aChangePathButton.GetText() ) );
396     // add a little offset
397     nTxtWidth = nTxtWidth + 12;
398     // compare with the button width
399     Size aSize = m_aCheckNowButton.GetSizePixel();
400     // and change it if it's necessary
401     if ( nTxtWidth > aSize.Width() )
402     {
403         aSize.Width() = nTxtWidth;
404         m_aCheckNowButton.SetSizePixel( aSize );
405         m_aChangePathButton.SetSizePixel( aSize );
406     }
407 }
408 
409