LibreOffice Module cui (master)  1
optupdt.cxx
Go to the documentation of this file.
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 <svl/zforlist.hxx>
23 #include "optupdt.hxx"
25 #include <com/sun/star/configuration/theDefaultProvider.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
28 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
29 #include <com/sun/star/deployment/UpdateInformationProvider.hpp>
30 #include <com/sun/star/ucb/XWebDAVCommandEnvironment.hpp>
31 #include <com/sun/star/frame/Desktop.hpp>
32 #include <com/sun/star/frame/XDispatchProvider.hpp>
33 #include <com/sun/star/util/XChangesBatch.hpp>
34 #include <com/sun/star/util/URLTransformer.hpp>
35 #include <com/sun/star/util/XURLTransformer.hpp>
36 #include <com/sun/star/setup/UpdateCheckConfig.hpp>
37 #include <com/sun/star/configuration/ReadWriteAccess.hpp>
38 #include <com/sun/star/beans/PropertyAttribute.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
40 #include <osl/file.hxx>
41 #include <osl/security.hxx>
42 #include <tools/diagnose_ex.h>
43 
44 using namespace ::css;
45 
47  : SfxTabPage(pPage, pController, "cui/ui/optonlineupdatepage.ui", "OptOnlineUpdatePage", &rSet)
48  , m_xNeverChecked(m_xBuilder->weld_label("neverchecked"))
49  , m_xAutoCheckCheckBox(m_xBuilder->weld_check_button("autocheck"))
50  , m_xEveryDayButton(m_xBuilder->weld_radio_button("everyday"))
51  , m_xEveryWeekButton(m_xBuilder->weld_radio_button("everyweek"))
52  , m_xEveryMonthButton(m_xBuilder->weld_radio_button("everymonth"))
53  , m_xCheckNowButton(m_xBuilder->weld_button("checknow"))
54  , m_xAutoDownloadCheckBox(m_xBuilder->weld_check_button("autodownload"))
55  , m_xDestPathLabel(m_xBuilder->weld_label("destpathlabel"))
56  , m_xDestPath(m_xBuilder->weld_label("destpath"))
57  , m_xChangePathButton(m_xBuilder->weld_button("changepath"))
58  , m_xLastChecked(m_xBuilder->weld_label("lastchecked"))
59  , m_xExtrasCheckBox(m_xBuilder->weld_check_button("extrabits"))
60  , m_xUserAgentLabel(m_xBuilder->weld_label("useragent"))
61 {
62  m_aNeverChecked = m_xNeverChecked->get_label();
63 
64  m_xAutoCheckCheckBox->connect_toggled( LINK( this, SvxOnlineUpdateTabPage, AutoCheckHdl_Impl ) );
65  m_xExtrasCheckBox->connect_clicked( LINK( this, SvxOnlineUpdateTabPage, ExtrasCheckHdl_Impl ) );
66  m_xCheckNowButton->connect_clicked( LINK( this, SvxOnlineUpdateTabPage, CheckNowHdl_Impl ) );
67  m_xChangePathButton->connect_clicked( LINK( this, SvxOnlineUpdateTabPage, FileDialogHdl_Impl ) );
68 
69  uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
70 
71  m_xUpdateAccess = setup::UpdateCheckConfig::create( xContext );
72  m_xReadWriteAccess = css::configuration::ReadWriteAccess::create(xContext, "*");
73 
74  bool bDownloadSupported = false;
75  m_xUpdateAccess->getByName( "DownloadSupported" ) >>= bDownloadSupported;
76 
77  m_xAutoDownloadCheckBox->set_visible(bDownloadSupported);
78  m_xDestPathLabel->set_visible(bDownloadSupported);
79  m_xDestPath->set_visible(bDownloadSupported);
80  m_xChangePathButton->set_visible(bDownloadSupported);
81 
83 
86 }
87 
89 {
90 }
91 
93 {
94  OUString aDateStr;
95  OUString aTimeStr;
96  OUString aText;
97  sal_Int64 lastChecked = 0;
98 
99  m_xUpdateAccess->getByName("LastCheck") >>= lastChecked;
100 
101  if( lastChecked == 0 ) // never checked
102  {
103  aText = m_aNeverChecked;
104  }
105  else
106  {
107  TimeValue lastCheckedTV;
108  oslDateTime lastCheckedDT;
109 
110  Date aDate( Date::EMPTY );
112 
113  lastCheckedTV.Seconds = static_cast<sal_uInt32>(lastChecked);
114  osl_getLocalTimeFromSystemTime( &lastCheckedTV, &lastCheckedTV );
115 
116  if ( osl_getDateTimeFromTimeValue( &lastCheckedTV, &lastCheckedDT ) )
117  {
118  aDate = Date( lastCheckedDT.Day, lastCheckedDT.Month, lastCheckedDT.Year );
119  aTime = ::tools::Time( lastCheckedDT.Hours, lastCheckedDT.Minutes );
120  }
121 
123  std::unique_ptr<SvNumberFormatter> pNumberFormatter(new SvNumberFormatter( ::comphelper::getProcessComponentContext(), eUILang ));
124  Color* pColor = nullptr;
125  const Date& rNullDate = pNumberFormatter->GetNullDate();
126  sal_uInt32 nFormat = pNumberFormatter->GetStandardFormat( SvNumFormatType::DATE, eUILang );
127 
128  pNumberFormatter->GetOutputString( aDate - rNullDate, nFormat, aDateStr, &pColor );
129 
130  nFormat = pNumberFormatter->GetStandardFormat( SvNumFormatType::TIME, eUILang );
131  pNumberFormatter->GetOutputString( aTime.GetTimeInDays(), nFormat, aTimeStr, &pColor );
132 
133  pNumberFormatter.reset();
134 
135  aText = m_aLastCheckedTemplate;
136  sal_Int32 nIndex = aText.indexOf( "%DATE%" );
137  if ( nIndex != -1 )
138  aText = aText.replaceAt( nIndex, 6, aDateStr );
139 
140  nIndex = aText.indexOf( "%TIME%" );
141  if ( nIndex != -1 )
142  aText = aText.replaceAt( nIndex, 6, aTimeStr );
143  }
144 
145  m_xLastChecked->set_label(aText);
146 }
147 
149 {
150  try {
151  uno::Reference< ucb::XWebDAVCommandEnvironment > xDav(
152  css::deployment::UpdateInformationProvider::create(
154  css::uno::UNO_QUERY_THROW );
155 
156  OUString aPseudoURL = "useragent:normal";
157  if( m_xExtrasCheckBox->get_active() )
158  aPseudoURL = "useragent:extended";
159  const uno::Sequence< beans::StringPair > aHeaders
160  = xDav->getUserRequestHeaders( aPseudoURL, ucb::WebDAVHTTPMethod(0) );
161 
162  for (const css::beans::StringPair & aHeader : aHeaders)
163  {
164  if ( aHeader.First == "User-Agent" )
165  {
166  OUString aText = aHeader.Second;
167  aText = aText.replaceAll(";", ";\n");
168  aText = aText.replaceAll("(", "\n(");
169  m_xUserAgentLabel->set_label(aText);
170  break;
171  }
172  }
173  } catch (const uno::Exception &) {
174  TOOLS_WARN_EXCEPTION( "cui.options", "Unexpected exception fetching User Agent" );
175  }
176 }
177 
178 std::unique_ptr<SfxTabPage> SvxOnlineUpdateTabPage::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet )
179 {
180  return std::make_unique<SvxOnlineUpdateTabPage>( pPage, pController, *rAttrSet );
181 }
182 
184 {
185  bool bModified = false;
186 
187  bool bValue;
188  sal_Int64 nValue;
189 
190  if( m_xAutoCheckCheckBox->get_state_changed_from_saved() )
191  {
192  bValue = m_xAutoCheckCheckBox->get_active();
193  m_xUpdateAccess->replaceByName( "AutoCheckEnabled", uno::Any( bValue ) );
194  bModified = true;
195  }
196 
197  nValue = 0;
198  if( m_xEveryDayButton->get_active() )
199  {
200  if( !m_xEveryDayButton->get_saved_state() )
201  nValue = 86400;
202  }
203  else if( m_xEveryWeekButton->get_active() )
204  {
205  if( !m_xEveryWeekButton->get_saved_state() )
206  nValue = 604800;
207  }
208  else if( m_xEveryMonthButton->get_active() )
209  {
210  if( !m_xEveryMonthButton->get_saved_state() )
211  nValue = 2592000;
212  }
213 
214  if( nValue > 0 )
215  {
216  m_xUpdateAccess->replaceByName( "CheckInterval", uno::Any( nValue ) );
217  bModified = true;
218  }
219 
220  if( m_xAutoDownloadCheckBox->get_state_changed_from_saved() )
221  {
222  bValue = m_xAutoDownloadCheckBox->get_active();
223  m_xUpdateAccess->replaceByName( "AutoDownloadEnabled", uno::Any( bValue ) );
224  bModified = true;
225  }
226 
227  OUString sValue, aURL;
228  m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
229 
230  if( ( osl::FileBase::E_None == osl::FileBase::getFileURLFromSystemPath(m_xDestPath->get_label(), aURL) ) &&
231  ( aURL != sValue ) )
232  {
233  m_xUpdateAccess->replaceByName( "DownloadDestination", uno::Any( aURL ) );
234  bModified = true;
235  }
236 
237  if( m_xExtrasCheckBox->get_state_changed_from_saved() )
238  {
239  bValue = m_xExtrasCheckBox->get_active();
240  m_xUpdateAccess->replaceByName( "ExtendedUserAgent", uno::Any( bValue ) );
241  bModified = true;
242  }
243 
244  uno::Reference< util::XChangesBatch > xChangesBatch(m_xUpdateAccess, uno::UNO_QUERY);
245  if( xChangesBatch.is() && xChangesBatch->hasPendingChanges() )
246  xChangesBatch->commitChanges();
247 
248  return bModified;
249 }
250 
252 {
253  bool bValue = false;
254  m_xUpdateAccess->getByName( "AutoCheckEnabled" ) >>= bValue;
255  beans::Property aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/AutoCheckEnabled");
256  bool bReadOnly = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
257 
258  m_xAutoCheckCheckBox->set_active(bValue);
259  m_xAutoCheckCheckBox->set_sensitive(!bReadOnly);
260 
261  sal_Int64 nValue = 0;
262  m_xUpdateAccess->getByName( "CheckInterval" ) >>= nValue;
263  aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/CheckInterval");
264  bool bReadOnly2 = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
265  m_xEveryDayButton->set_sensitive(bValue && !(bReadOnly || bReadOnly2));
266  m_xEveryWeekButton->set_sensitive(bValue && !(bReadOnly || bReadOnly2));
267  m_xEveryMonthButton->set_sensitive(bValue && !(bReadOnly || bReadOnly2));
268 
269  if( nValue == 86400 )
270  m_xEveryDayButton->set_active(true);
271  else if( nValue == 604800 )
272  m_xEveryWeekButton->set_active(true);
273  else
274  m_xEveryMonthButton->set_active(true);
275 
276  m_xAutoCheckCheckBox->save_state();
277  m_xEveryDayButton->save_state();
278  m_xEveryWeekButton->save_state();
279  m_xEveryMonthButton->save_state();
280 
281  m_xUpdateAccess->getByName( "AutoDownloadEnabled" ) >>= bValue;
282  aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/AutoDownloadEnabled");
283  bReadOnly = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
284  m_xAutoDownloadCheckBox->set_active(bValue);
285  m_xAutoDownloadCheckBox->set_sensitive(!bReadOnly);
286  m_xDestPathLabel->set_sensitive(true);
287  m_xDestPath->set_sensitive(true);
288 
289  OUString sValue, aPath;
290  m_xUpdateAccess->getByName( "DownloadDestination" ) >>= sValue;
291  aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/DownloadDestination");
292  bReadOnly = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
293  m_xChangePathButton->set_sensitive(!bReadOnly);
294 
295  if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sValue, aPath) )
296  m_xDestPath->set_label(aPath);
297 
298  m_xUpdateAccess->getByName( "ExtendedUserAgent" ) >>= bValue;
299  aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/ExtendedUserAgent");
300  bReadOnly = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
301  m_xExtrasCheckBox->set_active(bValue);
302  m_xExtrasCheckBox->set_sensitive(!bReadOnly);
303  m_xExtrasCheckBox->save_state();
304  UpdateUserAgent();
305 
306  m_xAutoDownloadCheckBox->save_state();
307 }
308 
310 {
311 }
312 
313 IMPL_LINK(SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, weld::ToggleButton&, rBox, void)
314 {
315  bool bEnabled = rBox.get_active();
316  beans::Property aProperty = m_xReadWriteAccess->getPropertyByHierarchicalName("/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments/CheckInterval");
317  bool bReadOnly = (aProperty.Attributes & beans::PropertyAttribute::READONLY) != 0;
318  m_xEveryDayButton->set_sensitive(bEnabled && !bReadOnly);
319  m_xEveryWeekButton->set_sensitive(bEnabled && !bReadOnly);
320  m_xEveryMonthButton->set_sensitive(bEnabled && !bReadOnly);
321 }
322 
324 {
325  UpdateUserAgent();
326 }
327 
329 {
330  uno::Reference < uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
331  uno::Reference < ui::dialogs::XFolderPicker2 > xFolderPicker = ui::dialogs::FolderPicker::create(xContext);
332 
333  OUString aURL;
334  if( osl::FileBase::E_None != osl::FileBase::getFileURLFromSystemPath(m_xDestPath->get_label(), aURL) )
335  osl::Security().getHomeDir(aURL);
336 
337  xFolderPicker->setDisplayDirectory( aURL );
338  sal_Int16 nRet = xFolderPicker->execute();
339 
340  if ( ui::dialogs::ExecutableDialogResults::OK == nRet )
341  {
342  OUString aFolder;
343  if( osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(xFolderPicker->getDirectory(), aFolder))
344  m_xDestPath->set_label(aFolder);
345  }
346 }
347 
349 {
350  uno::Reference < uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
351 
352  try
353  {
354  uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
355  css::configuration::theDefaultProvider::get( xContext ) );
356 
357  beans::NamedValue aProperty;
358  aProperty.Name = "nodepath";
359  aProperty.Value <<= OUString("org.openoffice.Office.Addons/AddonUI/OfficeHelp/UpdateCheckJob");
360 
361  uno::Sequence< uno::Any > aArgumentList( 1 );
362  aArgumentList[0] <<= aProperty;
363 
364  uno::Reference< container::XNameAccess > xNameAccess(
365  xConfigProvider->createInstanceWithArguments(
366  "com.sun.star.configuration.ConfigurationAccess", aArgumentList ),
367  uno::UNO_QUERY_THROW );
368 
369  util::URL aURL;
370  xNameAccess->getByName("URL") >>= aURL.Complete;
371 
372  uno::Reference < util::XURLTransformer > xTransformer( util::URLTransformer::create( xContext ) );
373 
374  xTransformer->parseStrict(aURL);
375 
376  uno::Reference < frame::XDesktop2 > xDesktop = frame::Desktop::create( xContext );
377 
378  uno::Reference< frame::XDispatchProvider > xDispatchProvider(
379  xDesktop->getCurrentFrame(), uno::UNO_QUERY );
380 
381  uno::Reference< frame::XDispatch > xDispatch;
382  if( xDispatchProvider.is() )
383  xDispatch = xDispatchProvider->queryDispatch(aURL, OUString(), 0);
384 
385  if( xDispatch.is() )
386  xDispatch->dispatch(aURL, uno::Sequence< beans::PropertyValue > ());
387 
388  UpdateLastCheckedText();
389  }
390  catch( const uno::Exception& )
391  {
392  TOOLS_WARN_EXCEPTION("cui.options", "Caught exception, thread terminated");
393  }
394 }
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::CheckButton > m_xAutoCheckCheckBox
Definition: optupdt.hxx:39
SvxOnlineUpdateTabPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: optupdt.cxx:46
const LanguageTag & GetUILanguageTag() const
LanguageType getLanguageType(bool bResolveSystem=true) const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rSet)
Definition: optupdt.cxx:178
static const AllSettings & GetSettings()
std::unique_ptr< weld::RadioButton > m_xEveryMonthButton
Definition: optupdt.hxx:42
css::uno::Reference< css::container::XNameReplace > m_xUpdateAccess
Definition: optupdt.hxx:35
css::uno::Reference< css::configuration::XReadWriteAccess > m_xReadWriteAccess
Definition: optupdt.hxx:36
std::unique_ptr< weld::Button > m_xChangePathButton
Definition: optupdt.hxx:47
std::unique_ptr< weld::Label > m_xDestPathLabel
Definition: optupdt.hxx:45
IMPL_LINK(SvxOnlineUpdateTabPage, AutoCheckHdl_Impl, weld::ToggleButton &, rBox, void)
Definition: optupdt.cxx:313
OUString m_aLastCheckedTemplate
Definition: optupdt.hxx:33
std::unique_ptr< weld::Label > m_xLastChecked
Definition: optupdt.hxx:48
virtual void Reset(const SfxItemSet *rSet) override
Definition: optupdt.cxx:251
double GetTimeInDays() const
IMPL_LINK_NOARG(SvxOnlineUpdateTabPage, ExtrasCheckHdl_Impl, weld::Button &, void)
Definition: optupdt.cxx:323
std::unique_ptr< weld::CheckButton > m_xExtrasCheckBox
Definition: optupdt.hxx:49
void UpdateLastCheckedText()
Definition: optupdt.cxx:92
std::unique_ptr< weld::Label > m_xUserAgentLabel
Definition: optupdt.hxx:50
#define TOOLS_WARN_EXCEPTION(area, stream)
std::unique_ptr< weld::CheckButton > m_xAutoDownloadCheckBox
Definition: optupdt.hxx:44
std::unique_ptr< weld::RadioButton > m_xEveryWeekButton
Definition: optupdt.hxx:41
OUString m_aNeverChecked
Definition: optupdt.hxx:32
virtual void FillUserData() override
Definition: optupdt.cxx:309
std::unique_ptr< weld::Label > m_xNeverChecked
Definition: optupdt.hxx:38
std::unique_ptr< weld::RadioButton > m_xEveryDayButton
Definition: optupdt.hxx:40
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: optupdt.cxx:183
virtual ~SvxOnlineUpdateTabPage() override
Definition: optupdt.cxx:88
Reference< XComponentContext > getProcessComponentContext()
std::unique_ptr< weld::Button > m_xCheckNowButton
Definition: optupdt.hxx:43
std::unique_ptr< weld::Label > m_xDestPath
Definition: optupdt.hxx:46