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