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
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>
39
40 #include <bitmaps.hlst>
41
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;
48
49 using namespace ::com::sun::star;
50
51
52 namespace
53 {
54
55 class UpdateCheckUI : public ::cppu::WeakImplHelper
56 < lang::XServiceInfo, document::XDocumentEventListener, beans::XPropertySet >
57 {
58 uno::Reference< uno::XComponentContext > m_xContext;
59 uno::Reference< task::XJob > mrJob;
60 OUString maBubbleImageURL;
61 MenuBarUpdateIconManager maBubbleManager;
62 std::locale maSfxLocale;
63
64 private:
65 DECL_LINK(ClickHdl, LinkParamNone*, void);
66
67 Image GetBubbleImage( OUString const &rURL );
68
69 public:
70 explicit UpdateCheckUI(const uno::Reference<uno::XComponentContext>&);
71
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;
76
77 // XDocumentEventListener
78 virtual void SAL_CALL documentEventOccured(const document::DocumentEvent& Event) override;
79 virtual void SAL_CALL disposing(const lang::EventObject& Event) override;
80
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;
93 };
94
UpdateCheckUI(const uno::Reference<uno::XComponentContext> & xContext)95 UpdateCheckUI::UpdateCheckUI(const uno::Reference<uno::XComponentContext>& xContext)
96 : m_xContext(xContext)
97 {
98 maSfxLocale = Translate::Create("sfx");
99
100 uno::Reference< document::XDocumentEventBroadcaster > xBroadcaster( frame::theGlobalEventBroadcaster::get(m_xContext) );
101 xBroadcaster->addDocumentEventListener( this );
102
103 SolarMutexGuard aGuard;
104
105 maBubbleManager.SetBubbleImage(GetBubbleImage(maBubbleImageURL));
106 maBubbleManager.SetClickHdl(LINK(this, UpdateCheckUI, ClickHdl));
107 }
108
109 OUString SAL_CALL
getImplementationName()110 UpdateCheckUI::getImplementationName()
111 {
112 return u"vnd.sun.UpdateCheckUI"_ustr;
113 }
114
115 uno::Sequence< OUString > SAL_CALL
getSupportedServiceNames()116 UpdateCheckUI::getSupportedServiceNames()
117 {
118 return { u"com.sun.star.setup.UpdateCheckUI"_ustr };
119 }
120
121 sal_Bool SAL_CALL
supportsService(OUString const & serviceName)122 UpdateCheckUI::supportsService( OUString const & serviceName )
123 {
124 return cppu::supportsService(this, serviceName);
125 }
126
GetBubbleImage(OUString const & rURL)127 Image UpdateCheckUI::GetBubbleImage( OUString const &rURL )
128 {
129 Image aImage;
130
131 if ( !maBubbleImageURL.isEmpty() )
132 {
133 uno::Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
134
135 if( !xContext.is() )
136 throw uno::RuntimeException(
137 u"UpdateCheckUI: unable to obtain service manager from component context"_ustr );
138
139 try
140 {
141 uno::Reference< graphic::XGraphicProvider > xGraphProvider(graphic::GraphicProvider::create(xContext));
142 uno::Sequence< beans::PropertyValue > aMediaProps{ comphelper::makePropertyValue(u"URL"_ustr,
143 rURL) };
144 uno::Reference< graphic::XGraphic > xGraphic = xGraphProvider->queryGraphic( aMediaProps );
145 if ( xGraphic.is() )
146 {
147 aImage = Image( xGraphic );
148 }
149 }
150 catch( const uno::Exception& )
151 {
152 }
153 }
154
155 if ( aImage.GetSizePixel().Width() == 0 )
156 aImage = Image(StockImage::Yes, u"" SV_RESID_BITMAP_INFOBOX ""_ustr);
157
158 return aImage;
159 }
160
documentEventOccured(const document::DocumentEvent & rEvent)161 void SAL_CALL UpdateCheckUI::documentEventOccured(const document::DocumentEvent& rEvent)
162 {
163 SolarMutexGuard aGuard;
164
165 if( rEvent.EventName == "OnPrepareViewClosing" )
166 maBubbleManager.RemoveBubbleWindow();
167 }
168
disposing(const lang::EventObject &)169 void SAL_CALL UpdateCheckUI::disposing(const lang::EventObject&)
170 {
171 }
172
getPropertySetInfo()173 uno::Reference< beans::XPropertySetInfo > UpdateCheckUI::getPropertySetInfo()
174 {
175 return nullptr;
176 }
177
setPropertyValue(const OUString & rPropertyName,const uno::Any & rValue)178 void UpdateCheckUI::setPropertyValue(const OUString& rPropertyName,
179 const uno::Any& rValue)
180 {
181 SolarMutexGuard aGuard;
182
183 OUString aString;
184
185 if( rPropertyName == PROPERTY_TITLE ) {
186 rValue >>= aString;
187 maBubbleManager.SetBubbleTitle(aString);
188 }
189 else if( rPropertyName == PROPERTY_TEXT ) {
190 rValue >>= aString;
191 maBubbleManager.SetBubbleText(aString);
192 }
193 else if( rPropertyName == PROPERTY_IMAGE ) {
194 rValue >>= aString;
195 if ( aString != maBubbleImageURL ) {
196 maBubbleImageURL = aString;
197 maBubbleManager.SetBubbleImage(GetBubbleImage(maBubbleImageURL));
198 }
199 }
200 else if( rPropertyName == PROPERTY_SHOW_BUBBLE ) {
201 bool bShowBubble= false;
202 rValue >>= bShowBubble;
203 maBubbleManager.SetShowBubble(bShowBubble);
204 }
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 = std::move(aJob);
211 }
212 else if (rPropertyName == PROPERTY_SHOW_MENUICON ) {
213 bool bShowMenuIcon = false;
214 rValue >>= bShowMenuIcon;
215 maBubbleManager.SetShowMenuIcon(bShowMenuIcon);
216 }
217 else
218 throw beans::UnknownPropertyException(rPropertyName);
219 }
220
getPropertyValue(const OUString & rPropertyName)221 uno::Any UpdateCheckUI::getPropertyValue(const OUString& rPropertyName)
222 {
223 SolarMutexGuard aGuard;
224
225 uno::Any aRet;
226
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);
241
242 return aRet;
243 }
244
245
addPropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)246 void UpdateCheckUI::addPropertyChangeListener( const OUString& /*aPropertyName*/,
247 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
248 {
249 //no bound properties
250 }
251
252
removePropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)253 void UpdateCheckUI::removePropertyChangeListener( const OUString& /*aPropertyName*/,
254 const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
255 {
256 //no bound properties
257 }
258
addVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)259 void UpdateCheckUI::addVetoableChangeListener( const OUString& /*aPropertyName*/,
260 const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
261 {
262 //no vetoable properties
263 }
264
removeVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)265 void UpdateCheckUI::removeVetoableChangeListener( const OUString& /*aPropertyName*/,
266 const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
267 {
268 //no vetoable properties
269 }
270
IMPL_LINK_NOARG(UpdateCheckUI,ClickHdl,LinkParamNone *,void)271 IMPL_LINK_NOARG(UpdateCheckUI, ClickHdl, LinkParamNone*, void)
272 {
273 SolarMutexGuard aGuard;
274
275 if ( mrJob.is() )
276 {
277 try {
278 uno::Sequence<beans::NamedValue> aEmpty;
279 mrJob->execute( aEmpty );
280 }
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();
286 }
287 }
288 }
289
290 } // anonymous namespace
291
292
293 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
extensions_update_UpdateCheckUI_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)294 extensions_update_UpdateCheckUI_get_implementation(
295 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
296 {
297 SolarMutexGuard aGuard;
298 return cppu::acquire(new UpdateCheckUI(context));
299 }
300
301
302
303
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
305