1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
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>
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>
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
;
58 // define ----------------------------------------------------------------
60 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
62 // class SvxOnlineUpdateTabPage --------------------------------------------------
64 SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage( Window
* pParent
, const SfxItemSet
& rSet
) :
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
) )
79 m_aNeverChecked
= String( CUI_RES( STR_NEVERCHECKED
) );
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
) );
86 uno::Reference
< lang::XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
88 m_xUpdateAccess
= uno::Reference
< container::XNameReplace
>(
89 xFactory
->createInstance( UNISTRING( "com.sun.star.setup.UpdateCheckConfig" ) ),
90 uno::UNO_QUERY_THROW
);
92 sal_Bool bDownloadSupported
= sal_False
;
93 m_xUpdateAccess
->getByName( UNISTRING( "DownloadSupported") ) >>= bDownloadSupported
;
95 WinBits nStyle
= m_aDestPath
.GetStyle();
96 nStyle
|= WB_PATHELLIPSIS
;
97 m_aDestPath
.SetStyle(nStyle
);
99 m_aAutoDownloadCheckBox
.Show(bDownloadSupported
);
100 m_aDestPathLabel
.Show(bDownloadSupported
);
101 m_aDestPath
.Show(bDownloadSupported
);
102 m_aChangePathButton
.Show(bDownloadSupported
);
104 // dynamical length of the PushButtons
107 m_aLastCheckedTemplate
= m_aLastChecked
.GetText();
109 UpdateLastCheckedText();
112 // -----------------------------------------------------------------------
114 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
118 // -----------------------------------------------------------------------
119 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
121 rtl::OUString aDateStr
;
122 rtl::OUString aTimeStr
;
124 sal_Int64 lastChecked
= 0;
126 m_xUpdateAccess
->getByName( UNISTRING( "LastCheck") ) >>= lastChecked
;
128 if( lastChecked
== 0 ) // never checked
130 aText
= m_aNeverChecked
;
134 TimeValue lastCheckedTV
;
135 oslDateTime lastCheckedDT
;
140 lastCheckedTV
.Seconds
= (sal_uInt32
) lastChecked
;
141 osl_getLocalTimeFromSystemTime( &lastCheckedTV
, &lastCheckedTV
);
143 if ( osl_getDateTimeFromTimeValue( &lastCheckedTV
, &lastCheckedDT
) )
145 aDate
= Date( lastCheckedDT
.Day
, lastCheckedDT
.Month
, lastCheckedDT
.Year
);
146 aTime
= Time( lastCheckedDT
.Hours
, lastCheckedDT
.Minutes
);
149 LanguageType eUILang
= Application::GetSettings().GetUILanguage();
150 SvNumberFormatter
*pNumberFormatter
= new SvNumberFormatter( ::comphelper::getProcessServiceFactory(), eUILang
);
152 Color
* pColor
= NULL
;
153 Date
* pNullDate
= pNumberFormatter
->GetNullDate();
154 sal_uInt32 nFormat
= pNumberFormatter
->GetStandardFormat( NUMBERFORMAT_DATE
, eUILang
);
156 pNumberFormatter
->GetOutputString( aDate
- *pNullDate
, nFormat
, aTmpStr
, &pColor
);
159 nFormat
= pNumberFormatter
->GetStandardFormat( NUMBERFORMAT_TIME
, eUILang
);
160 pNumberFormatter
->GetOutputString( aTime
.GetTimeInDays(), nFormat
, aTmpStr
, &pColor
);
164 delete pNumberFormatter
;
166 aText
= m_aLastCheckedTemplate
;
167 sal_Int32 nIndex
= aText
.indexOf( UNISTRING( "%DATE%" ) );
169 aText
= aText
.replaceAt( nIndex
, 6, aDateStr
);
171 nIndex
= aText
.indexOf( UNISTRING( "%TIME%" ) );
173 aText
= aText
.replaceAt( nIndex
, 6, aTimeStr
);
176 m_aLastChecked
.SetText( aText
);
179 // -----------------------------------------------------------------------
182 SvxOnlineUpdateTabPage::Create( Window
* pParent
, const SfxItemSet
& rAttrSet
)
184 return new SvxOnlineUpdateTabPage( pParent
, rAttrSet
);
187 // -----------------------------------------------------------------------
190 sal_Bool
SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet
& )
192 sal_Bool bModified
= sal_False
;
197 if( m_aAutoCheckCheckBox
.GetSavedValue() != m_aAutoCheckCheckBox
.IsChecked() )
199 bValue
= (sal_True
== m_aAutoCheckCheckBox
.IsChecked());
200 m_xUpdateAccess
->replaceByName( UNISTRING("AutoCheckEnabled"), uno::makeAny( bValue
) );
201 bModified
= sal_True
;
205 if( sal_True
== m_aEveryDayButton
.IsChecked() )
207 if( sal_False
== m_aEveryDayButton
.GetSavedValue() )
210 else if( sal_True
== m_aEveryWeekButton
.IsChecked() )
212 if( sal_False
== m_aEveryWeekButton
.GetSavedValue() )
215 else if( sal_True
== m_aEveryMonthButton
.IsChecked() )
217 if( sal_False
== m_aEveryMonthButton
.GetSavedValue() )
223 m_xUpdateAccess
->replaceByName( UNISTRING("CheckInterval"), uno::makeAny( nValue
) );
224 bModified
= sal_True
;
227 if( m_aAutoDownloadCheckBox
.GetSavedValue() != m_aAutoDownloadCheckBox
.IsChecked() )
229 bValue
= (sal_True
== m_aAutoDownloadCheckBox
.IsChecked());
230 m_xUpdateAccess
->replaceByName( UNISTRING("AutoDownloadEnabled"), uno::makeAny( bValue
) );
231 bModified
= sal_True
;
234 rtl::OUString sValue
, aURL
;
235 m_xUpdateAccess
->getByName( UNISTRING("DownloadDestination") ) >>= sValue
;
237 if( ( osl::FileBase::E_None
== osl::FileBase::getFileURLFromSystemPath(m_aDestPath
.GetText(), aURL
) ) &&
238 ( ! aURL
.equals( sValue
) ) )
240 m_xUpdateAccess
->replaceByName( UNISTRING("DownloadDestination"), uno::makeAny( aURL
) );
241 bModified
= sal_True
;
244 uno::Reference
< util::XChangesBatch
> xChangesBatch(m_xUpdateAccess
, uno::UNO_QUERY
);
245 if( xChangesBatch
.is() && xChangesBatch
->hasPendingChanges() )
246 xChangesBatch
->commitChanges();
251 // -----------------------------------------------------------------------
253 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet
& )
255 sal_Bool bValue
= sal_Bool();
256 m_xUpdateAccess
->getByName( UNISTRING("AutoCheckEnabled") ) >>= bValue
;
258 m_aAutoCheckCheckBox
.Check(bValue
);
259 m_aEveryDayButton
.Enable(bValue
);
260 m_aEveryWeekButton
.Enable(bValue
);
261 m_aEveryMonthButton
.Enable(bValue
);
263 sal_Int64 nValue
= 0;
264 m_xUpdateAccess
->getByName( UNISTRING("CheckInterval") ) >>= nValue
;
266 if( nValue
== 86400 )
267 m_aEveryDayButton
.Check();
268 else if( nValue
== 604800 )
269 m_aEveryWeekButton
.Check();
271 m_aEveryMonthButton
.Check();
273 m_aAutoCheckCheckBox
.SaveValue();
274 m_aEveryDayButton
.SaveValue();
275 m_aEveryWeekButton
.SaveValue();
276 m_aEveryMonthButton
.SaveValue();
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
);
284 rtl::OUString sValue
, aPath
;
285 m_xUpdateAccess
->getByName( UNISTRING("DownloadDestination") ) >>= sValue
;
287 if( osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(sValue
, aPath
) )
288 m_aDestPath
.SetText(aPath
);
290 m_aAutoDownloadCheckBox
.SaveValue();
293 // -----------------------------------------------------------------------
295 void SvxOnlineUpdateTabPage::FillUserData()
299 // -----------------------------------------------------------------------
301 IMPL_LINK( SvxOnlineUpdateTabPage
, AutoCheckHdl_Impl
, CheckBox
*, pBox
)
303 sal_Bool bEnabled
= pBox
->IsChecked();
305 m_aEveryDayButton
.Enable(bEnabled
);
306 m_aEveryWeekButton
.Enable(bEnabled
);
307 m_aEveryMonthButton
.Enable(bEnabled
);
312 // -----------------------------------------------------------------------
314 IMPL_LINK( SvxOnlineUpdateTabPage
, FileDialogHdl_Impl
, PushButton
*, EMPTYARG
)
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
) ) ),
322 if( osl::FileBase::E_None
!= osl::FileBase::getFileURLFromSystemPath(m_aDestPath
.GetText(), aURL
) )
323 osl::Security().getHomeDir(aURL
);
325 xFolderPicker
->setDisplayDirectory( aURL
);
326 sal_Int16 nRet
= xFolderPicker
->execute();
328 if ( dialogs::ExecutableDialogResults::OK
== nRet
)
330 rtl::OUString aFolder
;
331 if( osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(xFolderPicker
->getDirectory(), aFolder
))
332 m_aDestPath
.SetText( aFolder
);
338 // -----------------------------------------------------------------------
340 IMPL_LINK( SvxOnlineUpdateTabPage
, CheckNowHdl_Impl
, PushButton
*, EMPTYARG
)
342 uno::Reference
< lang::XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
346 uno::Reference
< lang::XMultiServiceFactory
> xConfigProvider(
347 xFactory
->createInstance( UNISTRING( "com.sun.star.configuration.ConfigurationProvider" )),
348 uno::UNO_QUERY_THROW
);
350 beans::PropertyValue aProperty
;
351 aProperty
.Name
= UNISTRING( "nodepath" );
352 aProperty
.Value
= uno::makeAny( UNISTRING("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
354 uno::Sequence
< uno::Any
> aArgumentList( 1 );
355 aArgumentList
[0] = uno::makeAny( aProperty
);
357 uno::Reference
< container::XNameAccess
> xNameAccess(
358 xConfigProvider
->createInstanceWithArguments(
359 UNISTRING("com.sun.star.configuration.ConfigurationAccess"), aArgumentList
),
360 uno::UNO_QUERY_THROW
);
363 xNameAccess
->getByName(UNISTRING("URL")) >>= aURL
.Complete
;
365 uno::Reference
< util::XURLTransformer
> xTransformer(
366 xFactory
->createInstance( UNISTRING( "com.sun.star.util.URLTransformer" ) ),
367 uno::UNO_QUERY_THROW
);
369 xTransformer
->parseStrict(aURL
);
371 uno::Reference
< frame::XDesktop
> xDesktop(
372 xFactory
->createInstance( UNISTRING( "com.sun.star.frame.Desktop" ) ),
373 uno::UNO_QUERY_THROW
);
375 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider(
376 xDesktop
->getCurrentFrame(), uno::UNO_QUERY
);
378 uno::Reference
< frame::XDispatch
> xDispatch
= xDispatchProvider
->queryDispatch(aURL
, rtl::OUString(), 0);
382 xDispatch
->dispatch(aURL
, uno::Sequence
< beans::PropertyValue
> ());
383 UpdateLastCheckedText();
386 catch( const uno::Exception
& e
)
388 OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
389 rtl::OUStringToOString(e
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
395 void SvxOnlineUpdateTabPage::CalcButtonWidth()
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() )
407 aSize
.Width() = nTxtWidth
;
408 m_aCheckNowButton
.SetSizePixel( aSize
);
409 m_aChangePathButton
.SetSizePixel( aSize
);