Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / update / ui / updatecheckui.cxx
blob492cb92669ab51a1abd1a0f5538514a66d8696cd
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 .
21 #include <cppuhelper/implbase.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/document/XDocumentEventListener.hpp>
25 #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
28 #include <com/sun/star/graphic/GraphicProvider.hpp>
29 #include <com/sun/star/graphic/XGraphicProvider.hpp>
30 #include <com/sun/star/task/XJob.hpp>
31 #include <comphelper/processfactory.hxx>
32 #include <comphelper/propertyvalue.hxx>
33 #include <unotools/resmgr.hxx>
34 #include <vcl/image.hxx>
35 #include <vcl/menubarupdateicon.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <sfx2/strings.hrc>
40 #include <bitmaps.hlst>
42 constexpr OUString PROPERTY_TITLE = u"BubbleHeading"_ustr;
43 constexpr OUString PROPERTY_TEXT = u"BubbleText"_ustr;
44 constexpr OUString PROPERTY_IMAGE = u"BubbleImageURL"_ustr;
45 constexpr OUString PROPERTY_SHOW_BUBBLE = u"BubbleVisible"_ustr;
46 constexpr OUString PROPERTY_CLICK_HDL = u"MenuClickHDL"_ustr;
47 constexpr OUString PROPERTY_SHOW_MENUICON = u"MenuIconVisible"_ustr;
49 using namespace ::com::sun::star;
52 namespace
55 class UpdateCheckUI : public ::cppu::WeakImplHelper
56 < lang::XServiceInfo, document::XDocumentEventListener, beans::XPropertySet >
58 uno::Reference< uno::XComponentContext > m_xContext;
59 uno::Reference< task::XJob > mrJob;
60 OUString maBubbleImageURL;
61 MenuBarUpdateIconManager maBubbleManager;
62 std::locale maSfxLocale;
64 private:
65 DECL_LINK(ClickHdl, LinkParamNone*, void);
67 Image GetBubbleImage( OUString const &rURL );
69 public:
70 explicit UpdateCheckUI(const uno::Reference<uno::XComponentContext>&);
72 // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() override;
74 virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName) override;
75 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
77 // XDocumentEventListener
78 virtual void SAL_CALL documentEventOccured(const document::DocumentEvent& Event) override;
79 virtual void SAL_CALL disposing(const lang::EventObject& Event) override;
81 //XPropertySet
82 virtual uno::Reference< beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override;
83 virtual void SAL_CALL setPropertyValue(const OUString& PropertyName, const uno::Any& aValue) override;
84 virtual uno::Any SAL_CALL getPropertyValue(const OUString& PropertyName) override;
85 virtual void SAL_CALL addPropertyChangeListener(const OUString& PropertyName,
86 const uno::Reference< beans::XPropertyChangeListener > & aListener) override;
87 virtual void SAL_CALL removePropertyChangeListener(const OUString& PropertyName,
88 const uno::Reference< beans::XPropertyChangeListener > & aListener) override;
89 virtual void SAL_CALL addVetoableChangeListener(const OUString& PropertyName,
90 const uno::Reference< beans::XVetoableChangeListener > & aListener) override;
91 virtual void SAL_CALL removeVetoableChangeListener(const OUString& PropertyName,
92 const uno::Reference< beans::XVetoableChangeListener > & aListener) override;
95 UpdateCheckUI::UpdateCheckUI(const uno::Reference<uno::XComponentContext>& xContext)
96 : m_xContext(xContext)
98 maSfxLocale = Translate::Create("sfx");
100 uno::Reference< document::XDocumentEventBroadcaster > xBroadcaster( frame::theGlobalEventBroadcaster::get(m_xContext) );
101 xBroadcaster->addDocumentEventListener( this );
103 SolarMutexGuard aGuard;
105 maBubbleManager.SetBubbleImage(GetBubbleImage(maBubbleImageURL));
106 maBubbleManager.SetClickHdl(LINK(this, UpdateCheckUI, ClickHdl));
109 OUString SAL_CALL
110 UpdateCheckUI::getImplementationName()
112 return "vnd.sun.UpdateCheckUI";
115 uno::Sequence< OUString > SAL_CALL
116 UpdateCheckUI::getSupportedServiceNames()
118 return { "com.sun.star.setup.UpdateCheckUI" };
121 sal_Bool SAL_CALL
122 UpdateCheckUI::supportsService( OUString const & serviceName )
124 return cppu::supportsService(this, serviceName);
127 Image UpdateCheckUI::GetBubbleImage( OUString const &rURL )
129 Image aImage;
131 if ( !maBubbleImageURL.isEmpty() )
133 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
135 if( !xContext.is() )
136 throw uno::RuntimeException(
137 "UpdateCheckUI: unable to obtain service manager from component context" );
141 uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
142 uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue("URL",
143 rURL) };
144 uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
145 if ( xGraphic.is() )
147 aImage = Image( xGraphic );
150 catch( const uno::Exception& )
155 if ( aImage.GetSizePixel().Width() == 0 )
156 aImage = Image(StockImage::Yes, SV_RESID_BITMAP_INFOBOX);
158 return aImage;
161 void SAL_CALL UpdateCheckUI::documentEventOccured(const document::DocumentEvent& rEvent)
163 SolarMutexGuard aGuard;
165 if( rEvent.EventName == "OnPrepareViewClosing" )
166 maBubbleManager.RemoveBubbleWindow();
169 void SAL_CALL UpdateCheckUI::disposing(const lang::EventObject&)
173 uno::Reference< beans::XPropertySetInfo > UpdateCheckUI::getPropertySetInfo()
175 return nullptr;
178 void UpdateCheckUI::setPropertyValue(const OUString& rPropertyName,
179 const uno::Any& rValue)
181 SolarMutexGuard aGuard;
183 OUString aString;
185 if( rPropertyName == PROPERTY_TITLE ) {
186 rValue >>= aString;
187 maBubbleManager.SetBubbleTitle(aString);
189 else if( rPropertyName == PROPERTY_TEXT ) {
190 rValue >>= aString;
191 maBubbleManager.SetBubbleText(aString);
193 else if( rPropertyName == PROPERTY_IMAGE ) {
194 rValue >>= aString;
195 if ( aString != maBubbleImageURL ) {
196 maBubbleImageURL = aString;
197 maBubbleManager.SetBubbleImage(GetBubbleImage(maBubbleImageURL));
200 else if( rPropertyName == PROPERTY_SHOW_BUBBLE ) {
201 bool bShowBubble= false;
202 rValue >>= bShowBubble;
203 maBubbleManager.SetShowBubble(bShowBubble);
205 else if( rPropertyName == PROPERTY_CLICK_HDL ) {
206 uno::Reference< task::XJob > aJob;
207 rValue >>= aJob;
208 if ( !aJob.is() )
209 throw lang::IllegalArgumentException();
210 mrJob = aJob;
212 else if (rPropertyName == PROPERTY_SHOW_MENUICON ) {
213 bool bShowMenuIcon = false;
214 rValue >>= bShowMenuIcon;
215 maBubbleManager.SetShowMenuIcon(bShowMenuIcon);
217 else
218 throw beans::UnknownPropertyException(rPropertyName);
221 uno::Any UpdateCheckUI::getPropertyValue(const OUString& rPropertyName)
223 SolarMutexGuard aGuard;
225 uno::Any aRet;
227 if( rPropertyName == PROPERTY_TITLE )
228 aRet <<= maBubbleManager.GetBubbleTitle();
229 else if( rPropertyName == PROPERTY_TEXT )
230 aRet <<= maBubbleManager.GetBubbleText();
231 else if( rPropertyName == PROPERTY_SHOW_BUBBLE )
232 aRet <<= maBubbleManager.GetShowBubble();
233 else if( rPropertyName == PROPERTY_IMAGE )
234 aRet <<= maBubbleImageURL;
235 else if( rPropertyName == PROPERTY_CLICK_HDL )
236 aRet <<= mrJob;
237 else if( rPropertyName == PROPERTY_SHOW_MENUICON )
238 aRet <<= maBubbleManager.GetShowMenuIcon();
239 else
240 throw beans::UnknownPropertyException(rPropertyName);
242 return aRet;
246 void UpdateCheckUI::addPropertyChangeListener( const OUString& /*aPropertyName*/,
247 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
249 //no bound properties
253 void UpdateCheckUI::removePropertyChangeListener( const OUString& /*aPropertyName*/,
254 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
256 //no bound properties
259 void UpdateCheckUI::addVetoableChangeListener( const OUString& /*aPropertyName*/,
260 const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
262 //no vetoable properties
265 void UpdateCheckUI::removeVetoableChangeListener( const OUString& /*aPropertyName*/,
266 const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
268 //no vetoable properties
271 IMPL_LINK_NOARG(UpdateCheckUI, ClickHdl, LinkParamNone*, void)
273 SolarMutexGuard aGuard;
275 if ( mrJob.is() )
277 try {
278 uno::Sequence<beans::NamedValue> aEmpty;
279 mrJob->execute( aEmpty );
281 catch(const uno::Exception&) {
282 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(nullptr,
283 VclMessageType::Warning, VclButtonsType::Ok,
284 Translate::get(STR_NO_WEBBROWSER_FOUND, maSfxLocale)));
285 xErrorBox->run();
290 } // anonymous namespace
293 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
294 extensions_update_UpdateCheckUI_get_implementation(
295 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
297 SolarMutexGuard aGuard;
298 return cppu::acquire(new UpdateCheckUI(context));
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */