1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/settings.hxx>
22 #include <sfx2/filedlghelper.hxx>
23 #include <svl/zforlist.hxx>
24 #include "optupdt.hxx"
25 #include <dialmgr.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(vcl::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 bool bDownloadSupported
= 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 SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
90 void SvxOnlineUpdateTabPage::dispose()
92 m_pAutoCheckCheckBox
.clear();
93 m_pEveryDayButton
.clear();
94 m_pEveryWeekButton
.clear();
95 m_pEveryMonthButton
.clear();
96 m_pCheckNowButton
.clear();
97 m_pAutoDownloadCheckBox
.clear();
98 m_pDestPathLabel
.clear();
100 m_pChangePathButton
.clear();
101 m_pLastChecked
.clear();
102 SfxTabPage::dispose();
106 void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
111 sal_Int64 lastChecked
= 0;
113 m_xUpdateAccess
->getByName("LastCheck") >>= lastChecked
;
115 if( lastChecked
== 0 ) // never checked
117 aText
= m_aNeverChecked
;
121 TimeValue lastCheckedTV
;
122 oslDateTime lastCheckedDT
;
124 Date
aDate( Date::EMPTY
);
125 tools::Time
aTime( tools::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
= ::tools::Time( lastCheckedDT
.Hours
, lastCheckedDT
.Minutes
);
136 LanguageType eUILang
= Application::GetSettings().GetUILanguageTag().getLanguageType();
137 SvNumberFormatter
*pNumberFormatter
= new SvNumberFormatter( ::comphelper::getProcessComponentContext(), eUILang
);
138 Color
* pColor
= NULL
;
139 Date
* pNullDate
= pNumberFormatter
->GetNullDate();
140 sal_uInt32 nFormat
= pNumberFormatter
->GetStandardFormat( css::util::NumberFormat::DATE
, eUILang
);
142 pNumberFormatter
->GetOutputString( aDate
- *pNullDate
, nFormat
, aDateStr
, &pColor
);
144 nFormat
= pNumberFormatter
->GetStandardFormat( css::util::NumberFormat::TIME
, eUILang
);
145 pNumberFormatter
->GetOutputString( aTime
.GetTimeInDays(), nFormat
, aTimeStr
, &pColor
);
148 delete pNumberFormatter
;
150 aText
= m_aLastCheckedTemplate
;
151 sal_Int32 nIndex
= aText
.indexOf( "%DATE%" );
153 aText
= aText
.replaceAt( nIndex
, 6, aDateStr
);
155 nIndex
= aText
.indexOf( "%TIME%" );
157 aText
= aText
.replaceAt( nIndex
, 6, aTimeStr
);
160 m_pLastChecked
->SetText( aText
);
164 SvxOnlineUpdateTabPage::Create( vcl::Window
* pParent
, const SfxItemSet
* rAttrSet
)
166 return VclPtr
<SvxOnlineUpdateTabPage
>::Create( pParent
, *rAttrSet
);
169 bool SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet
* )
171 bool bModified
= false;
176 if( m_pAutoCheckCheckBox
->IsValueChangedFromSaved() )
178 bValue
= m_pAutoCheckCheckBox
->IsChecked();
179 m_xUpdateAccess
->replaceByName( "AutoCheckEnabled", uno::makeAny( bValue
) );
184 if( m_pEveryDayButton
->IsChecked() )
186 if( !m_pEveryDayButton
->GetSavedValue() )
189 else if( m_pEveryWeekButton
->IsChecked() )
191 if( !m_pEveryWeekButton
->GetSavedValue() )
194 else if( m_pEveryMonthButton
->IsChecked() )
196 if( !m_pEveryMonthButton
->GetSavedValue() )
202 m_xUpdateAccess
->replaceByName( "CheckInterval", uno::makeAny( nValue
) );
206 if( m_pAutoDownloadCheckBox
->IsValueChangedFromSaved() )
208 bValue
= m_pAutoDownloadCheckBox
->IsChecked();
209 m_xUpdateAccess
->replaceByName( "AutoDownloadEnabled", uno::makeAny( bValue
) );
213 OUString sValue
, aURL
;
214 m_xUpdateAccess
->getByName( "DownloadDestination" ) >>= sValue
;
216 if( ( osl::FileBase::E_None
== osl::FileBase::getFileURLFromSystemPath(m_pDestPath
->GetText(), aURL
) ) &&
217 ( ! aURL
.equals( sValue
) ) )
219 m_xUpdateAccess
->replaceByName( "DownloadDestination", uno::makeAny( aURL
) );
223 uno::Reference
< util::XChangesBatch
> xChangesBatch(m_xUpdateAccess
, uno::UNO_QUERY
);
224 if( xChangesBatch
.is() && xChangesBatch
->hasPendingChanges() )
225 xChangesBatch
->commitChanges();
232 void SvxOnlineUpdateTabPage::Reset( const SfxItemSet
* )
235 m_xUpdateAccess
->getByName( "AutoCheckEnabled" ) >>= bValue
;
237 m_pAutoCheckCheckBox
->Check(bValue
);
238 m_pEveryDayButton
->Enable(bValue
);
239 m_pEveryWeekButton
->Enable(bValue
);
240 m_pEveryMonthButton
->Enable(bValue
);
242 sal_Int64 nValue
= 0;
243 m_xUpdateAccess
->getByName( "CheckInterval" ) >>= nValue
;
245 if( nValue
== 86400 )
246 m_pEveryDayButton
->Check();
247 else if( nValue
== 604800 )
248 m_pEveryWeekButton
->Check();
250 m_pEveryMonthButton
->Check();
252 m_pAutoCheckCheckBox
->SaveValue();
253 m_pEveryDayButton
->SaveValue();
254 m_pEveryWeekButton
->SaveValue();
255 m_pEveryMonthButton
->SaveValue();
257 m_xUpdateAccess
->getByName( "AutoDownloadEnabled" ) >>= bValue
;
258 m_pAutoDownloadCheckBox
->Check(bValue
);
259 m_pDestPathLabel
->Enable(true);
260 m_pDestPath
->Enable(true);
261 m_pChangePathButton
->Enable(true);
263 OUString sValue
, aPath
;
264 m_xUpdateAccess
->getByName( "DownloadDestination" ) >>= sValue
;
266 if( osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(sValue
, aPath
) )
267 m_pDestPath
->SetText(aPath
);
269 m_pAutoDownloadCheckBox
->SaveValue();
274 void SvxOnlineUpdateTabPage::FillUserData()
280 IMPL_LINK( SvxOnlineUpdateTabPage
, AutoCheckHdl_Impl
, CheckBox
*, pBox
)
282 bool bEnabled
= pBox
->IsChecked();
284 m_pEveryDayButton
->Enable(bEnabled
);
285 m_pEveryWeekButton
->Enable(bEnabled
);
286 m_pEveryMonthButton
->Enable(bEnabled
);
293 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage
, FileDialogHdl_Impl
)
295 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
296 uno::Reference
< ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(xContext
);
299 if( osl::FileBase::E_None
!= osl::FileBase::getFileURLFromSystemPath(m_pDestPath
->GetText(), aURL
) )
300 osl::Security().getHomeDir(aURL
);
302 xFolderPicker
->setDisplayDirectory( aURL
);
303 sal_Int16 nRet
= xFolderPicker
->execute();
305 if ( ui::dialogs::ExecutableDialogResults::OK
== nRet
)
308 if( osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(xFolderPicker
->getDirectory(), aFolder
))
309 m_pDestPath
->SetText( aFolder
);
317 IMPL_LINK_NOARG(SvxOnlineUpdateTabPage
, CheckNowHdl_Impl
)
319 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
323 uno::Reference
< lang::XMultiServiceFactory
> xConfigProvider(
324 com::sun::star::configuration::theDefaultProvider::get( xContext
) );
326 beans::NamedValue aProperty
;
327 aProperty
.Name
= "nodepath";
328 aProperty
.Value
= uno::makeAny( OUString("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
330 uno::Sequence
< uno::Any
> aArgumentList( 1 );
331 aArgumentList
[0] = uno::makeAny( aProperty
);
333 uno::Reference
< container::XNameAccess
> xNameAccess(
334 xConfigProvider
->createInstanceWithArguments(
335 "com.sun.star.configuration.ConfigurationAccess", aArgumentList
),
336 uno::UNO_QUERY_THROW
);
339 xNameAccess
->getByName("URL") >>= aURL
.Complete
;
341 uno::Reference
< util::XURLTransformer
> xTransformer( util::URLTransformer::create( xContext
) );
343 xTransformer
->parseStrict(aURL
);
345 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create( xContext
);
347 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider(
348 xDesktop
->getCurrentFrame(), uno::UNO_QUERY
);
350 uno::Reference
< frame::XDispatch
> xDispatch
;
351 if( xDispatchProvider
.is() )
352 xDispatch
= xDispatchProvider
->queryDispatch(aURL
, OUString(), 0);
355 xDispatch
->dispatch(aURL
, uno::Sequence
< beans::PropertyValue
> ());
357 UpdateLastCheckedText();
359 catch( const uno::Exception
& e
)
361 OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
362 OUStringToOString(e
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */