Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / cui / source / options / optupdt.cxx
blobc1539fccf9d2e076ff8ec938a5ba847ef4ae1f15
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <vcl/svapp.hxx>
21 #include <sfx2/filedlghelper.hxx>
22 #include <svl/zforlist.hxx>
23 #include "optupdt.hxx"
24 #include "optupdt.hrc"
25 #include <dialmgr.hxx>
26 #include <cuires.hrc>
27 #include <comphelper/componentcontext.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/configuration/theDefaultProvider.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
32 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
33 #include <com/sun/star/frame/XDesktop.hpp>
34 #include <com/sun/star/frame/XDispatchProvider.hpp>
35 #include <com/sun/star/util/XChangesBatch.hpp>
36 #include <com/sun/star/util/URLTransformer.hpp>
37 #include <com/sun/star/util/XURLTransformer.hpp>
38 #include <osl/file.hxx>
39 #include <osl/security.hxx>
41 namespace beans = ::com::sun::star::beans;
42 namespace container = ::com::sun::star::container;
43 namespace dialogs = ::com::sun::star::ui::dialogs;
44 namespace frame = ::com::sun::star::frame;
45 namespace lang = ::com::sun::star::lang;
46 namespace uno = ::com::sun::star::uno;
47 namespace util = ::com::sun::star::util;
49 // class SvxOnlineUpdateTabPage --------------------------------------------------
51 SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage( Window* pParent, const SfxItemSet& rSet ) :
53 SfxTabPage( pParent, CUI_RES( RID_SVXPAGE_ONLINEUPDATE ), rSet ),
54 m_aOptionsLine( this, CUI_RES( FL_OPTIONS ) ),
55 m_aAutoCheckCheckBox( this, CUI_RES( CB_AUTOCHECK ) ),
56 m_aEveryDayButton( this, CUI_RES( RB_EVERYDAY ) ),
57 m_aEveryWeekButton( this, CUI_RES( RB_EVERYWEEK ) ),
58 m_aEveryMonthButton( this, CUI_RES( RB_EVERYMONTH ) ),
59 m_aCheckNowButton( this, CUI_RES( PB_CHECKNOW ) ),
60 m_aAutoDownloadCheckBox( this, CUI_RES( CB_AUTODOWNLOAD ) ),
61 m_aDestPathLabel( this, CUI_RES( FT_DESTPATHLABEL ) ),
62 m_aDestPath( this, CUI_RES( FT_DESTPATH ) ),
63 m_aChangePathButton( this, CUI_RES( PB_CHANGEPATH ) ),
64 m_aLastChecked( this, CUI_RES( FT_LASTCHECKED ) )
66 m_aNeverChecked = String( CUI_RES( STR_NEVERCHECKED ) );
67 FreeResource();
69 m_aAutoCheckCheckBox.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
70 m_aCheckNowButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
71 m_aChangePathButton.SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
73 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
75 m_xUpdateAccess = uno::Reference < container::XNameReplace >(
76 xFactory->createInstance( "com.sun.star.setup.UpdateCheckConfig" ),
77 uno::UNO_QUERY_THROW );
79 sal_Bool bDownloadSupported = sal_False;
80 m_xUpdateAccess->getByName( "DownloadSupported" ) >>= bDownloadSupported;
82 WinBits nStyle = m_aDestPath.GetStyle();
83 nStyle |= WB_PATHELLIPSIS;
84 m_aDestPath.SetStyle(nStyle);
86 m_aAutoDownloadCheckBox.Show(bDownloadSupported);
87 m_aDestPathLabel.Show(bDownloadSupported);
88 m_aDestPath.Show(bDownloadSupported);
89 m_aChangePathButton.Show(bDownloadSupported);
91 // dynamical length of the PushButtons
92 CalcButtonWidth();
94 m_aLastCheckedTemplate = m_aLastChecked.GetText();
96 UpdateLastCheckedText();
99 // -----------------------------------------------------------------------
101 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
105 // -----------------------------------------------------------------------
106 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
108 rtl::OUString aDateStr;
109 rtl::OUString aTimeStr;
110 rtl::OUString aText;
111 sal_Int64 lastChecked = 0;
113 m_xUpdateAccess->getByName("LastCheck") >>= lastChecked;
115 if( lastChecked == 0 ) // never checked
117 aText = m_aNeverChecked;
119 else
121 TimeValue lastCheckedTV;
122 oslDateTime lastCheckedDT;
124 Date aDate( Date::EMPTY );
125 Time aTime( Time::EMPTY );
127 lastCheckedTV.Seconds = (sal_uInt32) lastChecked;
128 osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
130 if ( osl_getDateTimeFromTimeValue( &lastCheckedTV, &lastCheckedDT ) )
132 aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
133 aTime = Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
136 LanguageType eUILang = Application::GetSettings().GetUILanguageTag().getLanguageType();
137 SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( ::comphelper::getProcessServiceFactory(), eUILang );
138 String aTmpStr;
139 Color* pColor = NULL;
140 Date* pNullDate = pNumberFormatter->GetNullDate();
141 sal_uInt32 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_DATE, eUILang );
143 pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
144 aDateStr = aTmpStr;
146 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_TIME, eUILang );
147 pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTmpStr, &pColor );
148 aTimeStr = aTmpStr;
150 delete pColor;
151 delete pNumberFormatter;
153 aText = m_aLastCheckedTemplate;
154 sal_Int32 nIndex = aText.indexOf( "%DATE%" );
155 if ( nIndex != -1 )
156 aText = aText.replaceAt( nIndex, 6, aDateStr );
158 nIndex = aText.indexOf( "%TIME%" );
159 if ( nIndex != -1 )
160 aText = aText.replaceAt( nIndex, 6, aTimeStr );
163 m_aLastChecked.SetText( aText );
166 // -----------------------------------------------------------------------
168 SfxTabPage*
169 SvxOnlineUpdateTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
171 return new SvxOnlineUpdateTabPage( pParent, rAttrSet );
174 // -----------------------------------------------------------------------
177 sal_Bool SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet& )
179 sal_Bool bModified = sal_False;
181 sal_Bool bValue;
182 sal_Int64 nValue;
184 if( m_aAutoCheckCheckBox.GetSavedValue() != m_aAutoCheckCheckBox.IsChecked() )
186 bValue = (sal_True == m_aAutoCheckCheckBox.IsChecked());
187 m_xUpdateAccess->replaceByName( "AutoCheckEnabled", uno::makeAny( bValue ) );
188 bModified = sal_True;
191 nValue = 0;
192 if( sal_True == m_aEveryDayButton.IsChecked() )
194 if( sal_False == m_aEveryDayButton.GetSavedValue() )
195 nValue = 86400;
197 else if( sal_True == m_aEveryWeekButton.IsChecked() )
199 if( sal_False == m_aEveryWeekButton.GetSavedValue() )
200 nValue = 604800;
202 else if( sal_True == m_aEveryMonthButton.IsChecked() )
204 if( sal_False == m_aEveryMonthButton.GetSavedValue() )
205 nValue = 2592000;
208 if( nValue > 0 )
210 m_xUpdateAccess->replaceByName( "CheckInterval", uno::makeAny( nValue ) );
211 bModified = sal_True;
214 if( m_aAutoDownloadCheckBox.GetSavedValue() != m_aAutoDownloadCheckBox.IsChecked() )
216 bValue = (sal_True == m_aAutoDownloadCheckBox.IsChecked());
217 m_xUpdateAccess->replaceByName( "AutoDownloadEnabled", uno::makeAny( bValue ) );
218 bModified = sal_True;
221 rtl::OUString sValue, aURL;
222 m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
224 if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) ) &&
225 ( ! aURL.equals( sValue ) ) )
227 m_xUpdateAccess->replaceByName( "DownloadDestination", uno::makeAny( aURL ) );
228 bModified = sal_True;
231 uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
232 if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
233 xChangesBatch->commitChanges();
235 return bModified;
238 // -----------------------------------------------------------------------
240 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet& )
242 sal_Bool bValue = sal_Bool();
243 m_xUpdateAccess->getByName( "AutoCheckEnabled" ) >>= bValue;
245 m_aAutoCheckCheckBox.Check(bValue);
246 m_aEveryDayButton.Enable(bValue);
247 m_aEveryWeekButton.Enable(bValue);
248 m_aEveryMonthButton.Enable(bValue);
250 sal_Int64 nValue = 0;
251 m_xUpdateAccess->getByName( "CheckInterval" ) >>= nValue;
253 if( nValue == 86400 )
254 m_aEveryDayButton.Check();
255 else if( nValue == 604800 )
256 m_aEveryWeekButton.Check();
257 else
258 m_aEveryMonthButton.Check();
260 m_aAutoCheckCheckBox.SaveValue();
261 m_aEveryDayButton.SaveValue();
262 m_aEveryWeekButton.SaveValue();
263 m_aEveryMonthButton.SaveValue();
265 m_xUpdateAccess->getByName( "AutoDownloadEnabled" ) >>= bValue;
266 m_aAutoDownloadCheckBox.Check(bValue);
267 m_aDestPathLabel.Enable(sal_True);
268 m_aDestPath.Enable(sal_True);
269 m_aChangePathButton.Enable(sal_True);
271 rtl::OUString sValue, aPath;
272 m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
274 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
275 m_aDestPath.SetText(aPath);
277 m_aAutoDownloadCheckBox.SaveValue();
280 // -----------------------------------------------------------------------
282 void SvxOnlineUpdateTabPage::FillUserData()
286 // -----------------------------------------------------------------------
288 IMPL_LINK( SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, CheckBox *, pBox )
290 sal_Bool bEnabled = pBox->IsChecked();
292 m_aEveryDayButton.Enable(bEnabled);
293 m_aEveryWeekButton.Enable(bEnabled);
294 m_aEveryMonthButton.Enable(bEnabled);
296 return 0;
299 // -----------------------------------------------------------------------
301 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, FileDialogHdl_Impl)
303 uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
304 uno::Reference < dialogs::XFolderPicker2 > xFolderPicker = dialogs::FolderPicker::create(xContext);
306 rtl::OUString aURL;
307 if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_aDestPath.GetText(), aURL) )
308 osl::Security().getHomeDir(aURL);
310 xFolderPicker->setDisplayDirectory( aURL );
311 sal_Int16 nRet = xFolderPicker->execute();
313 if ( dialogs::ExecutableDialogResults::OK == nRet )
315 rtl::OUString aFolder;
316 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
317 m_aDestPath.SetText( aFolder );
320 return 0;
323 // -----------------------------------------------------------------------
325 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, CheckNowHdl_Impl)
327 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
331 uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
332 com::sun::star::configuration::theDefaultProvider::get(
333 comphelper::getProcessComponentContext() ) );
335 beans::NamedValue aProperty;
336 aProperty.Name = "nodepath";
337 aProperty.Value = uno::makeAny( OUString("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
339 uno::Sequence< uno::Any > aArgumentList( 1 );
340 aArgumentList[0] = uno::makeAny( aProperty );
342 uno::Reference< container::XNameAccess > xNameAccess(
343 xConfigProvider->createInstanceWithArguments(
344 "com.sun.star.configuration.ConfigurationAccess", aArgumentList ),
345 uno::UNO_QUERY_THROW );
347 util::URL aURL;
348 xNameAccess->getByName("URL") >>= aURL.Complete;
350 uno::Reference < util::XURLTransformer > xTransformer(
351 util::URLTransformer::create( comphelper::getProcessComponentContext() ) );
353 xTransformer->parseStrict(aURL);
355 uno::Reference < frame::XDesktop > xDesktop(
356 xFactory->createInstance( "com.sun.star.frame.Desktop" ),
357 uno::UNO_QUERY_THROW );
359 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
360 xDesktop->getCurrentFrame(), uno::UNO_QUERY );
362 uno::Reference< frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch(aURL, rtl::OUString(), 0);
364 if( xDispatch.is() )
366 xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
367 UpdateLastCheckedText();
370 catch( const uno::Exception& e )
372 OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
373 rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
376 return 0;
379 void SvxOnlineUpdateTabPage::CalcButtonWidth()
381 // detect the longest button text
382 long nTxtWidth = ::std::max( m_aCheckNowButton.GetCtrlTextWidth( m_aCheckNowButton.GetText() ),
383 m_aCheckNowButton.GetCtrlTextWidth( m_aChangePathButton.GetText() ) );
384 // add a little offset
385 nTxtWidth = nTxtWidth + 12;
386 // compare with the button width
387 Size aSize = m_aCheckNowButton.GetSizePixel();
388 // and change it if it's necessary
389 if ( nTxtWidth > aSize.Width() )
391 aSize.Width() = nTxtWidth;
392 m_aCheckNowButton.SetSizePixel( aSize );
393 m_aChangePathButton.SetSizePixel( aSize );
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */