Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
26 : #include <cuires.hrc>
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>
40 :
41 : using namespace ::com::sun::star;
42 :
43 : // class SvxOnlineUpdateTabPage --------------------------------------------------
44 :
45 0 : SvxOnlineUpdateTabPage::SvxOnlineUpdateTabPage(vcl::Window* pParent, const SfxItemSet& rSet)
46 0 : : SfxTabPage(pParent, "OptOnlineUpdatePage", "cui/ui/optonlineupdatepage.ui", &rSet)
47 : {
48 0 : m_aNeverChecked = get<FixedText>("neverchecked")->GetText();
49 0 : get(m_pAutoCheckCheckBox, "autocheck");
50 0 : get(m_pEveryDayButton, "everyday");
51 0 : get(m_pEveryWeekButton, "everyweek");
52 0 : get(m_pEveryMonthButton, "everymonth");
53 0 : get(m_pCheckNowButton, "checknow");
54 0 : get(m_pAutoDownloadCheckBox, "autodownload");
55 0 : get(m_pDestPathLabel, "destpathlabel");
56 0 : get(m_pDestPath, "destpath");
57 0 : get(m_pChangePathButton, "changepath");
58 0 : get(m_pLastChecked, "lastchecked");
59 :
60 0 : m_pAutoCheckCheckBox->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
61 0 : m_pCheckNowButton->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
62 0 : m_pChangePathButton->SetClickHdl( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
63 :
64 0 : uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
65 :
66 0 : m_xUpdateAccess = setup::UpdateCheckConfig::create( xContext );
67 :
68 0 : bool bDownloadSupported = false;
69 0 : m_xUpdateAccess->getByName( "DownloadSupported" ) >>= bDownloadSupported;
70 :
71 0 : WinBits nStyle = m_pDestPath->GetStyle();
72 0 : nStyle |= WB_PATHELLIPSIS;
73 0 : m_pDestPath->SetStyle(nStyle);
74 :
75 0 : m_pAutoDownloadCheckBox->Show(bDownloadSupported);
76 0 : m_pDestPathLabel->Show(bDownloadSupported);
77 0 : m_pDestPath->Show(bDownloadSupported);
78 0 : m_pChangePathButton->Show(bDownloadSupported);
79 :
80 0 : m_aLastCheckedTemplate = m_pLastChecked->GetText();
81 :
82 0 : UpdateLastCheckedText();
83 0 : }
84 :
85 0 : SvxOnlineUpdateTabPage::~SvxOnlineUpdateTabPage()
86 : {
87 0 : disposeOnce();
88 0 : }
89 :
90 0 : void SvxOnlineUpdateTabPage::dispose()
91 : {
92 0 : m_pAutoCheckCheckBox.clear();
93 0 : m_pEveryDayButton.clear();
94 0 : m_pEveryWeekButton.clear();
95 0 : m_pEveryMonthButton.clear();
96 0 : m_pCheckNowButton.clear();
97 0 : m_pAutoDownloadCheckBox.clear();
98 0 : m_pDestPathLabel.clear();
99 0 : m_pDestPath.clear();
100 0 : m_pChangePathButton.clear();
101 0 : m_pLastChecked.clear();
102 0 : SfxTabPage::dispose();
103 0 : }
104 :
105 :
106 0 : void SvxOnlineUpdateTabPage::UpdateLastCheckedText()
107 : {
108 0 : OUString aDateStr;
109 0 : OUString aTimeStr;
110 0 : OUString aText;
111 0 : sal_Int64 lastChecked = 0;
112 :
113 0 : m_xUpdateAccess->getByName("LastCheck") >>= lastChecked;
114 :
115 0 : if( lastChecked == 0 ) // never checked
116 : {
117 0 : aText = m_aNeverChecked;
118 : }
119 : else
120 : {
121 : TimeValue lastCheckedTV;
122 : oslDateTime lastCheckedDT;
123 :
124 0 : Date aDate( Date::EMPTY );
125 0 : tools::Time aTime( tools::Time::EMPTY );
126 :
127 0 : lastCheckedTV.Seconds = (sal_uInt32) lastChecked;
128 0 : osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
129 :
130 0 : if ( osl_getDateTimeFromTimeValue( &lastCheckedTV, &lastCheckedDT ) )
131 : {
132 0 : aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
133 0 : aTime = ::tools::Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
134 : }
135 :
136 0 : LanguageType eUILang = Application::GetSettings().GetUILanguageTag().getLanguageType();
137 0 : SvNumberFormatter *pNumberFormatter = new SvNumberFormatter( ::comphelper::getProcessComponentContext(), eUILang );
138 0 : Color* pColor = NULL;
139 0 : Date* pNullDate = pNumberFormatter->GetNullDate();
140 0 : sal_uInt32 nFormat = pNumberFormatter->GetStandardFormat( css::util::NumberFormat::DATE, eUILang );
141 :
142 0 : pNumberFormatter->GetOutputString( aDate - *pNullDate, nFormat, aDateStr, &pColor );
143 :
144 0 : nFormat = pNumberFormatter->GetStandardFormat( css::util::NumberFormat::TIME, eUILang );
145 0 : pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTimeStr, &pColor );
146 :
147 0 : delete pColor;
148 0 : delete pNumberFormatter;
149 :
150 0 : aText = m_aLastCheckedTemplate;
151 0 : sal_Int32 nIndex = aText.indexOf( "%DATE%" );
152 0 : if ( nIndex != -1 )
153 0 : aText = aText.replaceAt( nIndex, 6, aDateStr );
154 :
155 0 : nIndex = aText.indexOf( "%TIME%" );
156 0 : if ( nIndex != -1 )
157 0 : aText = aText.replaceAt( nIndex, 6, aTimeStr );
158 : }
159 :
160 0 : m_pLastChecked->SetText( aText );
161 0 : }
162 :
163 : VclPtr<SfxTabPage>
164 0 : SvxOnlineUpdateTabPage::Create( vcl::Window* pParent, const SfxItemSet* rAttrSet )
165 : {
166 0 : return VclPtr<SvxOnlineUpdateTabPage>::Create( pParent, *rAttrSet );
167 : }
168 :
169 0 : bool SvxOnlineUpdateTabPage::FillItemSet( SfxItemSet* )
170 : {
171 0 : bool bModified = false;
172 :
173 : bool bValue;
174 : sal_Int64 nValue;
175 :
176 0 : if( m_pAutoCheckCheckBox->IsValueChangedFromSaved() )
177 : {
178 0 : bValue = m_pAutoCheckCheckBox->IsChecked();
179 0 : m_xUpdateAccess->replaceByName( "AutoCheckEnabled", uno::makeAny( bValue ) );
180 0 : bModified = true;
181 : }
182 :
183 0 : nValue = 0;
184 0 : if( m_pEveryDayButton->IsChecked() )
185 : {
186 0 : if( !m_pEveryDayButton->GetSavedValue() )
187 0 : nValue = 86400;
188 : }
189 0 : else if( m_pEveryWeekButton->IsChecked() )
190 : {
191 0 : if( !m_pEveryWeekButton->GetSavedValue() )
192 0 : nValue = 604800;
193 : }
194 0 : else if( m_pEveryMonthButton->IsChecked() )
195 : {
196 0 : if( !m_pEveryMonthButton->GetSavedValue() )
197 0 : nValue = 2592000;
198 : }
199 :
200 0 : if( nValue > 0 )
201 : {
202 0 : m_xUpdateAccess->replaceByName( "CheckInterval", uno::makeAny( nValue ) );
203 0 : bModified = true;
204 : }
205 :
206 0 : if( m_pAutoDownloadCheckBox->IsValueChangedFromSaved() )
207 : {
208 0 : bValue = m_pAutoDownloadCheckBox->IsChecked();
209 0 : m_xUpdateAccess->replaceByName( "AutoDownloadEnabled", uno::makeAny( bValue ) );
210 0 : bModified = true;
211 : }
212 :
213 0 : OUString sValue, aURL;
214 0 : m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
215 :
216 0 : if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_pDestPath->GetText(), aURL) ) &&
217 0 : ( ! aURL.equals( sValue ) ) )
218 : {
219 0 : m_xUpdateAccess->replaceByName( "DownloadDestination", uno::makeAny( aURL ) );
220 0 : bModified = true;
221 : }
222 :
223 0 : uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
224 0 : if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
225 0 : xChangesBatch->commitChanges();
226 :
227 0 : return bModified;
228 : }
229 :
230 :
231 :
232 0 : void SvxOnlineUpdateTabPage::Reset( const SfxItemSet* )
233 : {
234 0 : bool bValue = false;
235 0 : m_xUpdateAccess->getByName( "AutoCheckEnabled" ) >>= bValue;
236 :
237 0 : m_pAutoCheckCheckBox->Check(bValue);
238 0 : m_pEveryDayButton->Enable(bValue);
239 0 : m_pEveryWeekButton->Enable(bValue);
240 0 : m_pEveryMonthButton->Enable(bValue);
241 :
242 0 : sal_Int64 nValue = 0;
243 0 : m_xUpdateAccess->getByName( "CheckInterval" ) >>= nValue;
244 :
245 0 : if( nValue == 86400 )
246 0 : m_pEveryDayButton->Check();
247 0 : else if( nValue == 604800 )
248 0 : m_pEveryWeekButton->Check();
249 : else
250 0 : m_pEveryMonthButton->Check();
251 :
252 0 : m_pAutoCheckCheckBox->SaveValue();
253 0 : m_pEveryDayButton->SaveValue();
254 0 : m_pEveryWeekButton->SaveValue();
255 0 : m_pEveryMonthButton->SaveValue();
256 :
257 0 : m_xUpdateAccess->getByName( "AutoDownloadEnabled" ) >>= bValue;
258 0 : m_pAutoDownloadCheckBox->Check(bValue);
259 0 : m_pDestPathLabel->Enable(true);
260 0 : m_pDestPath->Enable(true);
261 0 : m_pChangePathButton->Enable(true);
262 :
263 0 : OUString sValue, aPath;
264 0 : m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
265 :
266 0 : if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
267 0 : m_pDestPath->SetText(aPath);
268 :
269 0 : m_pAutoDownloadCheckBox->SaveValue();
270 0 : }
271 :
272 :
273 :
274 0 : void SvxOnlineUpdateTabPage::FillUserData()
275 : {
276 0 : }
277 :
278 :
279 :
280 0 : IMPL_LINK( SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, CheckBox *, pBox )
281 : {
282 0 : bool bEnabled = pBox->IsChecked();
283 :
284 0 : m_pEveryDayButton->Enable(bEnabled);
285 0 : m_pEveryWeekButton->Enable(bEnabled);
286 0 : m_pEveryMonthButton->Enable(bEnabled);
287 :
288 0 : return 0;
289 : }
290 :
291 :
292 :
293 0 : IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, FileDialogHdl_Impl)
294 : {
295 0 : uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
296 0 : uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
297 :
298 0 : OUString aURL;
299 0 : if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_pDestPath->GetText(), aURL) )
300 0 : osl::Security().getHomeDir(aURL);
301 :
302 0 : xFolderPicker->setDisplayDirectory( aURL );
303 0 : sal_Int16 nRet = xFolderPicker->execute();
304 :
305 0 : if ( ui::dialogs::ExecutableDialogResults::OK == nRet )
306 : {
307 0 : OUString aFolder;
308 0 : if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
309 0 : m_pDestPath->SetText( aFolder );
310 : }
311 :
312 0 : return 0;
313 : }
314 :
315 :
316 :
317 0 : IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, CheckNowHdl_Impl)
318 : {
319 0 : uno::Reference < uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
320 :
321 : try
322 : {
323 : uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
324 0 : com::sun::star::configuration::theDefaultProvider::get( xContext ) );
325 :
326 0 : beans::NamedValue aProperty;
327 0 : aProperty.Name = "nodepath";
328 0 : aProperty.Value = uno::makeAny( OUString("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob") );
329 :
330 0 : uno::Sequence< uno::Any > aArgumentList( 1 );
331 0 : aArgumentList[0] = uno::makeAny( aProperty );
332 :
333 : uno::Reference< container::XNameAccess > xNameAccess(
334 0 : xConfigProvider->createInstanceWithArguments(
335 0 : "com.sun.star.configuration.ConfigurationAccess", aArgumentList ),
336 0 : uno::UNO_QUERY_THROW );
337 :
338 0 : util::URL aURL;
339 0 : xNameAccess->getByName("URL") >>= aURL.Complete;
340 :
341 0 : uno::Reference < util::XURLTransformer > xTransformer( util::URLTransformer::create( xContext ) );
342 :
343 0 : xTransformer->parseStrict(aURL);
344 :
345 0 : uno::Reference < frame::XDesktop2 > xDesktop = frame::Desktop::create( xContext );
346 :
347 : uno::Reference< frame::XDispatchProvider > xDispatchProvider(
348 0 : xDesktop->getCurrentFrame(), uno::UNO_QUERY );
349 :
350 0 : uno::Reference< frame::XDispatch > xDispatch;
351 0 : if( xDispatchProvider.is() )
352 0 : xDispatch = xDispatchProvider->queryDispatch(aURL, OUString(), 0);
353 :
354 0 : if( xDispatch.is() )
355 0 : xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
356 :
357 0 : UpdateLastCheckedText();
358 : }
359 0 : catch( const uno::Exception& e )
360 : {
361 : OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
362 : OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
363 : }
364 :
365 0 : return 0;
366 0 : }
367 :
368 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|