Update ooo320-m1
[ooovba.git] / svx / source / cui / optupdt.cxx
blob25668f414900ecaeaf797803f9c1d471a14a2b20
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: optupdt.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
36 #endif
38 // include ---------------------------------------------------------------
39 #include <vcl/svapp.hxx>
40 #include <sfx2/filedlghelper.hxx>
41 #include <svtools/zforlist.hxx>
43 #include "optupdt.hxx"
44 #include "optupdt.hrc"
46 #include <svx/dialmgr.hxx>
47 #include <svx/dialogs.hrc>
50 #include <comphelper/processfactory.hxx>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
53 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
54 #include <com/sun/star/frame/XDesktop.hpp>
55 #include <com/sun/star/frame/XDispatchProvider.hpp>
56 #include <com/sun/star/util/XChangesBatch.hpp>
57 #include <com/sun/star/util/XURLTransformer.hpp>
59 #include <osl/file.hxx>
60 #include <osl/security.hxx>
62 namespace beans = ::com::sun::star::beans;
63 namespace container = ::com::sun::star::container;
64 namespace dialogs = ::com::sun::star::ui::dialogs;
65 namespace frame = ::com::sun::star::frame;
66 namespace lang = ::com::sun::star::lang;
67 namespace uno = ::com::sun::star::uno;
68 namespace util = ::com::sun::star::util;
70 // define ----------------------------------------------------------------
72 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
74 // class SvxOnlineUpdateTabPage --------------------------------------------------
76 SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage( Window* pParent, const SfxItemSet& rSet ) :
78 SfxTabPage( pParent, SVX_RES( RID_SVXPAGE_ONLINEUPDATE ), rSet ),
79 m_aOptionsLine( this, SVX_RES( FL_OPTIONS ) ),
80 m_aAutoCheckCheckBox( this, SVX_RES( CB_AUTOCHECK ) ),
81 m_aEveryDayButton( this, SVX_RES( RB_EVERYDAY ) ),
82 m_aEveryWeekButton( this, SVX_RES( RB_EVERYWEEK ) ),
83 m_aEveryMonthButton( this, SVX_RES( RB_EVERYMONTH ) ),
84 m_aCheckNowButton( this, SVX_RES( PB_CHECKNOW ) ),
85 m_aAutoDownloadCheckBox( this, SVX_RES( CB_AUTODOWNLOAD ) ),
86 m_aDestPathLabel( this, SVX_RES( FT_DESTPATHLABEL ) ),
87 m_aDestPath( this, SVX_RES( FT_DESTPATH ) ),
88 m_aChangePathButton( this, SVX_RES( PB_CHANGEPATH ) ),
89 m_aLastChecked( this, SVX_RES( FT_LASTCHECKED ) )
91 m_aNeverChecked = String( SVX_RES( STR_NEVERCHECKED ) );
92 FreeResource();
94 m_aAutoCheckCheckBox.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
95 m_aCheckNowButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
96 m_aChangePathButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
98 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
100 m_xUpdateAccess = uno::Reference < container::XNameReplace >(
101 xFactory->createInstance( UNISTRING( "com.sun.star.setup.UpdateCheckConfig" ) ),
102 uno::UNO_QUERY_THROW );
104 sal_Bool bDownloadSupported = sal_False;
105 m_xUpdateAccess->getByName( UNISTRING( "DownloadSupported") ) >>= bDownloadSupported;
107 WinBits nStyle = m_aDestPath.GetStyle();
108 nStyle |= WB_PATHELLIPSIS;
109 m_aDestPath.SetStyle(nStyle);
111 m_aAutoDownloadCheckBox.Show(bDownloadSupported);
112 m_aDestPathLabel.Show(bDownloadSupported);
113 m_aDestPath.Show(bDownloadSupported);
114 m_aChangePathButton.Show(bDownloadSupported);
116 // dynamical length of the PushButtons
117 CalcButtonWidth();
119 m_aLastCheckedTemplate = m_aLastChecked.GetText();
121 UpdateLastCheckedText();
124 // -----------------------------------------------------------------------
126 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
130 // -----------------------------------------------------------------------
131 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
133 rtl::OUString aDateStr;
134 rtl::OUString aTimeStr;
135 rtl::OUString aText;
136 sal_Int64 lastChecked = 0;
138 m_xUpdateAccess->getByName( UNISTRING( "LastCheck") ) >>= lastChecked;
140 if( lastChecked == 0 ) // never checked
142 aText = m_aNeverChecked;
144 else
146 TimeValue lastCheckedTV;
147 oslDateTime lastCheckedDT;
149 Date aDate;
150 Time aTime;
152 lastCheckedTV.Seconds = (sal_uInt32) lastChecked;
153 osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
155 if ( osl_getDateTimeFromTimeValue( &lastCheckedTV, &lastCheckedDT ) )
157 aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
158 aTime = Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
161 LanguageType eUILang = Application::GetSettings().GetUILanguage();
162 SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( ::comphelper::getProcessServiceFactory(), eUILang );
163 String aTmpStr;
164 Color* pColor = NULL;
165 Date* pNullDate = pNumberFormatter->GetNullDate();
166 sal_uInt32 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_DATE, eUILang );
168 pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
169 aDateStr = aTmpStr;
171 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_TIME, eUILang );
172 pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTmpStr, &pColor );
173 aTimeStr = aTmpStr;
175 delete pColor;
176 delete pNumberFormatter;
178 aText = m_aLastCheckedTemplate;
179 sal_Int32 nIndex = aText.indexOf( UNISTRING( "%DATE%" ) );
180 if ( nIndex != -1 )
181 aText = aText.replaceAt( nIndex, 6, aDateStr );
183 nIndex = aText.indexOf( UNISTRING( "%TIME%" ) );
184 if ( nIndex != -1 )
185 aText = aText.replaceAt( nIndex, 6, aTimeStr );
188 m_aLastChecked.SetText( aText );
191 // -----------------------------------------------------------------------
193 SfxTabPage*
194 SvxOnlineUpdateTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
196 return new SvxOnlineUpdateTabPage( pParent, rAttrSet );
199 // -----------------------------------------------------------------------
202 BOOL SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet& )
204 BOOL bModified = FALSE;
206 sal_Bool bValue;
207 sal_Int64 nValue;
209 if( m_aAutoCheckCheckBox.GetSavedValue() != m_aAutoCheckCheckBox.IsChecked() )
211 bValue = (TRUE == m_aAutoCheckCheckBox.IsChecked());
212 m_xUpdateAccess->replaceByName( UNISTRING("AutoCheckEnabled"), uno::makeAny( bValue ) );
213 bModified = TRUE;
216 nValue = 0;
217 if( TRUE == m_aEveryDayButton.IsChecked() )
219 if( FALSE == m_aEveryDayButton.GetSavedValue() )
220 nValue = 86400;
222 else if( TRUE == m_aEveryWeekButton.IsChecked() )
224 if( FALSE == m_aEveryWeekButton.GetSavedValue() )
225 nValue = 604800;
227 else if( TRUE == m_aEveryMonthButton.IsChecked() )
229 if( FALSE == m_aEveryMonthButton.GetSavedValue() )
230 nValue = 2592000;
233 if( nValue > 0 )
235 m_xUpdateAccess->replaceByName( UNISTRING("CheckInterval"), uno::makeAny( nValue ) );
236 bModified = TRUE;
239 if( m_aAutoDownloadCheckBox.GetSavedValue() != m_aAutoDownloadCheckBox.IsChecked() )
241 bValue = (TRUE == m_aAutoDownloadCheckBox.IsChecked());
242 m_xUpdateAccess->replaceByName( UNISTRING("AutoDownloadEnabled"), uno::makeAny( bValue ) );
243 bModified = TRUE;
246 rtl::OUString sValue, aURL;
247 m_xUpdateAccess->getByName( UNISTRING("DownloadDestination") ) >>= sValue;
249 if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) ) &&
250 ( ! aURL.equals( sValue ) ) )
252 m_xUpdateAccess->replaceByName( UNISTRING("DownloadDestination"), uno::makeAny( aURL ) );
253 bModified = TRUE;
256 uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
257 if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
258 xChangesBatch->commitChanges();
260 return bModified;
263 // -----------------------------------------------------------------------
265 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet& )
267 sal_Bool bValue = sal_Bool();
268 m_xUpdateAccess->getByName( UNISTRING("AutoCheckEnabled") ) >>= bValue;
270 m_aAutoCheckCheckBox.Check(bValue);
271 m_aEveryDayButton.Enable(bValue);
272 m_aEveryWeekButton.Enable(bValue);
273 m_aEveryMonthButton.Enable(bValue);
275 sal_Int64 nValue = 0;
276 m_xUpdateAccess->getByName( UNISTRING("CheckInterval") ) >>= nValue;
278 if( nValue == 86400 )
279 m_aEveryDayButton.Check();
280 else if( nValue == 604800 )
281 m_aEveryWeekButton.Check();
282 else
283 m_aEveryMonthButton.Check();
285 m_aAutoCheckCheckBox.SaveValue();
286 m_aEveryDayButton.SaveValue();
287 m_aEveryWeekButton.SaveValue();
288 m_aEveryMonthButton.SaveValue();
290 m_xUpdateAccess->getByName( UNISTRING("AutoDownloadEnabled") ) >>= bValue;
291 m_aAutoDownloadCheckBox.Check(bValue);
292 m_aDestPathLabel.Enable(TRUE);
293 m_aDestPath.Enable(TRUE);
294 m_aChangePathButton.Enable(TRUE);
296 rtl::OUString sValue, aPath;
297 m_xUpdateAccess->getByName( UNISTRING("DownloadDestination") ) >>= sValue;
299 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
300 m_aDestPath.SetText(aPath);
302 m_aAutoDownloadCheckBox.SaveValue();
305 // -----------------------------------------------------------------------
307 void SvxOnlineUpdateTabPage::FillUserData()
311 // -----------------------------------------------------------------------
313 IMPL_LINK( SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, CheckBox *, pBox )
315 BOOL bEnabled = pBox->IsChecked();
317 m_aEveryDayButton.Enable(bEnabled);
318 m_aEveryWeekButton.Enable(bEnabled);
319 m_aEveryMonthButton.Enable(bEnabled);
321 return 0;
324 // -----------------------------------------------------------------------
326 IMPL_LINK( SvxOnlineUpdateTabPage, FileDialogHdl_Impl, PushButton *, EMPTYARG )
328 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
329 uno::Reference < dialogs::XFolderPicker > xFolderPicker(
330 xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME ) ) ),
331 uno::UNO_QUERY );
333 rtl::OUString aURL;
334 if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) )
335 osl::Security().getHomeDir(aURL);
337 xFolderPicker->setDisplayDirectory( aURL );
338 sal_Int16 nRet = xFolderPicker->execute();
340 if ( dialogs::ExecutableDialogResults::OK == nRet )
342 rtl::OUString aFolder;
343 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
344 m_aDestPath.SetText( aFolder );
347 return 0;
350 // -----------------------------------------------------------------------
352 IMPL_LINK( SvxOnlineUpdateTabPage, CheckNowHdl_Impl, PushButton *, EMPTYARG )
354 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
358 uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
359 xFactory->createInstance( UNISTRING( "com.sun.star.configuration.ConfigurationProvider" )),
360 uno::UNO_QUERY_THROW);
362 beans::PropertyValue aProperty;
363 aProperty.Name = UNISTRING( "nodepath" );
364 aProperty.Value = uno::makeAny( UNISTRING("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
366 uno::Sequence< uno::Any > aArgumentList( 1 );
367 aArgumentList[0] = uno::makeAny( aProperty );
369 uno::Reference< container::XNameAccess > xNameAccess(
370 xConfigProvider->createInstanceWithArguments(
371 UNISTRING("com.sun.star.configuration.ConfigurationAccess"), aArgumentList ),
372 uno::UNO_QUERY_THROW );
374 util::URL aURL;
375 xNameAccess->getByName(UNISTRING("URL")) >>= aURL.Complete;
377 uno::Reference < util::XURLTransformer > xTransformer(
378 xFactory->createInstance( UNISTRING( "com.sun.star.util.URLTransformer" ) ),
379 uno::UNO_QUERY_THROW );
381 xTransformer->parseStrict(aURL);
383 uno::Reference < frame::XDesktop > xDesktop(
384 xFactory->createInstance( UNISTRING( "com.sun.star.frame.Desktop" ) ),
385 uno::UNO_QUERY_THROW );
387 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
388 xDesktop->getCurrentFrame(), uno::UNO_QUERY );
390 uno::Reference< frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL, rtl::OUString(), 0);
392 if( xDispatch.is() )
394 xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
395 UpdateLastCheckedText();
398 catch( const uno::Exception& e )
400 OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
401 rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
404 return 0;
407 void SvxOnlineUpdateTabPage::CalcButtonWidth()
409 // detect the longest button text
410 long nTxtWidth = ::std::max( m_aCheckNowButton.GetCtrlTextWidth( m_aCheckNowButton.GetText() ),
411 m_aCheckNowButton.GetCtrlTextWidth( m_aChangePathButton.GetText() ) );
412 // add a little offset
413 nTxtWidth = nTxtWidth + 12;
414 // compare with the button width
415 Size aSize = m_aCheckNowButton.GetSizePixel();
416 // and change it if it's necessary
417 if ( nTxtWidth > aSize.Width() )
419 aSize.Width() = nTxtWidth;
420 m_aCheckNowButton.SetSizePixel( aSize );
421 m_aChangePathButton.SetSizePixel( aSize );