bump product version to 4.1.6.2
[LibreOffice.git] / cui / source / options / optupdt.cxx
blobf08fb83bd31dbaf3b65512f6b0f044de368d9859
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 <dialmgr.hxx>
25 #include <cuires.hrc>
26 #include <comphelper/componentcontext.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/configuration/theDefaultProvider.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
31 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
32 #include <com/sun/star/frame/Desktop.hpp>
33 #include <com/sun/star/frame/XDispatchProvider.hpp>
34 #include <com/sun/star/util/XChangesBatch.hpp>
35 #include <com/sun/star/util/URLTransformer.hpp>
36 #include <com/sun/star/util/XURLTransformer.hpp>
37 #include <com/sun/star/setup/UpdateCheckConfig.hpp>
38 #include <osl/file.hxx>
39 #include <osl/security.hxx>
41 using namespace ::com::sun::star;
43 // class SvxOnlineUpdateTabPage --------------------------------------------------
45 SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage(Window* pParent, const SfxItemSet& rSet)
46 : SfxTabPage(pParent, "OptOnlineUpdatePage", "cui/ui/optonlineupdatepage.ui", rSet)
48 m_aNeverChecked = get<FixedText>("neverchecked")->GetText();
49 get(m_pAutoCheckCheckBox, "autocheck");
50 get(m_pEveryDayButton, "everyday");
51 get(m_pEveryWeekButton, "everyweek");
52 get(m_pEveryMonthButton, "everymonth");
53 get(m_pCheckNowButton, "checknow");
54 get(m_pAutoDownloadCheckBox, "autodownload");
55 get(m_pDestPathLabel, "destpathlabel");
56 get(m_pDestPath, "destpath");
57 get(m_pChangePathButton, "changepath");
58 get(m_pLastChecked, "lastchecked");
60 m_pAutoCheckCheckBox->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
61 m_pCheckNowButton->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
62 m_pChangePathButton->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
64 uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
66 m_xUpdateAccess = setup::UpdateCheckConfig::create( xContext );
68 sal_Bool bDownloadSupported = sal_False;
69 m_xUpdateAccess->getByName( "DownloadSupported" ) >>= bDownloadSupported;
71 WinBits nStyle = m_pDestPath->GetStyle();
72 nStyle |= WB_PATHELLIPSIS;
73 m_pDestPath->SetStyle(nStyle);
75 m_pAutoDownloadCheckBox->Show(bDownloadSupported);
76 m_pDestPathLabel->Show(bDownloadSupported);
77 m_pDestPath->Show(bDownloadSupported);
78 m_pChangePathButton->Show(bDownloadSupported);
80 m_aLastCheckedTemplate = m_pLastChecked->GetText();
82 UpdateLastCheckedText();
85 // -----------------------------------------------------------------------
87 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
91 // -----------------------------------------------------------------------
92 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
94 OUString aDateStr;
95 OUString aTimeStr;
96 OUString aText;
97 sal_Int64 lastChecked = 0;
99 m_xUpdateAccess->getByName("LastCheck") >>= lastChecked;
101 if( lastChecked == 0 ) // never checked
103 aText = m_aNeverChecked;
105 else
107 TimeValue lastCheckedTV;
108 oslDateTime lastCheckedDT;
110 Date aDate( Date::EMPTY );
111 Time aTime( Time::EMPTY );
113 lastCheckedTV.Seconds = (sal_uInt32) lastChecked;
114 osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
116 if ( osl_getDateTimeFromTimeValue( &lastCheckedTV, &lastCheckedDT ) )
118 aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
119 aTime = Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
122 LanguageType eUILang = Application::GetSettings().GetUILanguageTag().getLanguageType();
123 SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( ::comphelper::getProcessComponentContext(), eUILang );
124 String aTmpStr;
125 Color* pColor = NULL;
126 Date* pNullDate = pNumberFormatter->GetNullDate();
127 sal_uInt32 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_DATE, eUILang );
129 pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aTmpStr, &pColor );
130 aDateStr = aTmpStr;
132 nFormat = pNumberFormatter->GetStandardFormat( NUMBERFORMAT_TIME, eUILang );
133 pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTmpStr, &pColor );
134 aTimeStr = aTmpStr;
136 delete pColor;
137 delete pNumberFormatter;
139 aText = m_aLastCheckedTemplate;
140 sal_Int32 nIndex = aText.indexOf( "%DATE%" );
141 if ( nIndex != -1 )
142 aText = aText.replaceAt( nIndex, 6, aDateStr );
144 nIndex = aText.indexOf( "%TIME%" );
145 if ( nIndex != -1 )
146 aText = aText.replaceAt( nIndex, 6, aTimeStr );
149 m_pLastChecked->SetText( aText );
152 // -----------------------------------------------------------------------
154 SfxTabPage*
155 SvxOnlineUpdateTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
157 return new SvxOnlineUpdateTabPage( pParent, rAttrSet );
160 // -----------------------------------------------------------------------
163 sal_Bool SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet& )
165 sal_Bool bModified = sal_False;
167 sal_Bool bValue;
168 sal_Int64 nValue;
170 if( m_pAutoCheckCheckBox->GetSavedValue() != m_pAutoCheckCheckBox->IsChecked() )
172 bValue = (sal_True == m_pAutoCheckCheckBox->IsChecked());
173 m_xUpdateAccess->replaceByName( "AutoCheckEnabled", uno::makeAny( bValue ) );
174 bModified = sal_True;
177 nValue = 0;
178 if( sal_True == m_pEveryDayButton->IsChecked() )
180 if( sal_False == m_pEveryDayButton->GetSavedValue() )
181 nValue = 86400;
183 else if( sal_True == m_pEveryWeekButton->IsChecked() )
185 if( sal_False == m_pEveryWeekButton->GetSavedValue() )
186 nValue = 604800;
188 else if( sal_True == m_pEveryMonthButton->IsChecked() )
190 if( sal_False == m_pEveryMonthButton->GetSavedValue() )
191 nValue = 2592000;
194 if( nValue > 0 )
196 m_xUpdateAccess->replaceByName( "CheckInterval", uno::makeAny( nValue ) );
197 bModified = sal_True;
200 if( m_pAutoDownloadCheckBox->GetSavedValue() != m_pAutoDownloadCheckBox->IsChecked() )
202 bValue = (sal_True == m_pAutoDownloadCheckBox->IsChecked());
203 m_xUpdateAccess->replaceByName( "AutoDownloadEnabled", uno::makeAny( bValue ) );
204 bModified = sal_True;
207 OUString sValue, aURL;
208 m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
210 if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_pDestPath->GetText(), aURL) ) &&
211 ( ! aURL.equals( sValue ) ) )
213 m_xUpdateAccess->replaceByName( "DownloadDestination", uno::makeAny( aURL ) );
214 bModified = sal_True;
217 uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
218 if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
219 xChangesBatch->commitChanges();
221 return bModified;
224 // -----------------------------------------------------------------------
226 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet& )
228 sal_Bool bValue = sal_Bool();
229 m_xUpdateAccess->getByName( "AutoCheckEnabled" ) >>= bValue;
231 m_pAutoCheckCheckBox->Check(bValue);
232 m_pEveryDayButton->Enable(bValue);
233 m_pEveryWeekButton->Enable(bValue);
234 m_pEveryMonthButton->Enable(bValue);
236 sal_Int64 nValue = 0;
237 m_xUpdateAccess->getByName( "CheckInterval" ) >>= nValue;
239 if( nValue == 86400 )
240 m_pEveryDayButton->Check();
241 else if( nValue == 604800 )
242 m_pEveryWeekButton->Check();
243 else
244 m_pEveryMonthButton->Check();
246 m_pAutoCheckCheckBox->SaveValue();
247 m_pEveryDayButton->SaveValue();
248 m_pEveryWeekButton->SaveValue();
249 m_pEveryMonthButton->SaveValue();
251 m_xUpdateAccess->getByName( "AutoDownloadEnabled" ) >>= bValue;
252 m_pAutoDownloadCheckBox->Check(bValue);
253 m_pDestPathLabel->Enable(sal_True);
254 m_pDestPath->Enable(sal_True);
255 m_pChangePathButton->Enable(sal_True);
257 OUString sValue, aPath;
258 m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
260 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
261 m_pDestPath->SetText(aPath);
263 m_pAutoDownloadCheckBox->SaveValue();
266 // -----------------------------------------------------------------------
268 void SvxOnlineUpdateTabPage::FillUserData()
272 // -----------------------------------------------------------------------
274 IMPL_LINK( SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, CheckBox *, pBox )
276 sal_Bool bEnabled = pBox->IsChecked();
278 m_pEveryDayButton->Enable(bEnabled);
279 m_pEveryWeekButton->Enable(bEnabled);
280 m_pEveryMonthButton->Enable(bEnabled);
282 return 0;
285 // -----------------------------------------------------------------------
287 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, FileDialogHdl_Impl)
289 uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
290 uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
292 OUString aURL;
293 if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_pDestPath->GetText(), aURL) )
294 osl::Security().getHomeDir(aURL);
296 xFolderPicker->setDisplayDirectory( aURL );
297 sal_Int16 nRet = xFolderPicker->execute();
299 if ( ui::dialogs::ExecutableDialogResults::OK == nRet )
301 OUString aFolder;
302 if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
303 m_pDestPath->SetText( aFolder );
306 return 0;
309 // -----------------------------------------------------------------------
311 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, CheckNowHdl_Impl)
313 uno::Reference < uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
317 uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
318 com::sun::star::configuration::theDefaultProvider::get( xContext ) );
320 beans::NamedValue aProperty;
321 aProperty.Name = "nodepath";
322 aProperty.Value = uno::makeAny( OUString("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
324 uno::Sequence< uno::Any > aArgumentList( 1 );
325 aArgumentList[0] = uno::makeAny( aProperty );
327 uno::Reference< container::XNameAccess > xNameAccess(
328 xConfigProvider->createInstanceWithArguments(
329 "com.sun.star.configuration.ConfigurationAccess", aArgumentList ),
330 uno::UNO_QUERY_THROW );
332 util::URL aURL;
333 xNameAccess->getByName("URL") >>= aURL.Complete;
335 uno::Reference < util::XURLTransformer > xTransformer( util::URLTransformer::create( xContext ) );
337 xTransformer->parseStrict(aURL);
339 uno::Reference < frame::XDesktop2 > xDesktop = frame::Desktop::create( xContext );
341 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
342 xDesktop->getCurrentFrame(), uno::UNO_QUERY );
344 uno::Reference< frame::XDispatch > xDispatch;
345 if( xDispatchProvider.is() )
346 xDispatch = xDispatchProvider->queryDispatch(aURL, OUString(), 0);
348 if( xDispatch.is() )
349 xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
351 UpdateLastCheckedText();
353 catch( const uno::Exception& e )
355 OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
356 OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
359 return 0;
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */