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 <helper/titlebarupdate.hxx>
22 #include <pattern/window.hxx>
23 #include <threadhelp/writeguard.hxx>
24 #include <threadhelp/readguard.hxx>
25 #include <macros/generic.hxx>
27 #include <properties.h>
29 #include <com/sun/star/awt/XWindow.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/frame/ModuleManager.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/beans/XMaterialHolder.hpp>
36 #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
37 #include <com/sun/star/beans/NamedValue.hpp>
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/sequenceashashmap.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <unotools/bootstrap.hxx>
43 #include <vcl/window.hxx>
44 #include <vcl/syswin.hxx>
45 #include <toolkit/unohlp.hxx>
46 #include <vcl/svapp.hxx>
47 #include <vcl/wrkwin.hxx>
48 #include <tools/diagnose_ex.h>
52 static const ::sal_Int32 INVALID_ICON_ID
= -1;
53 static const ::sal_Int32 DEFAULT_ICON_ID
= 0;
56 //*****************************************************************************************************************
57 // XInterface, XTypeProvider
59 DEFINE_XINTERFACE_5(TitleBarUpdate
,
61 DIRECT_INTERFACE (css::lang::XTypeProvider
),
62 DIRECT_INTERFACE (css::lang::XInitialization
),
63 DIRECT_INTERFACE (css::frame::XFrameActionListener
),
64 DIRECT_INTERFACE (css::frame::XTitleChangeListener
),
65 DERIVED_INTERFACE(css::lang::XEventListener
,css::frame::XFrameActionListener
))
67 DEFINE_XTYPEPROVIDER_5(TitleBarUpdate
,
68 css::lang::XTypeProvider
,
69 css::lang::XInitialization
,
70 css::frame::XFrameActionListener
,
71 css::frame::XTitleChangeListener
,
72 css::lang::XEventListener
)
74 //*****************************************************************************************************************
75 TitleBarUpdate::TitleBarUpdate(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
76 : ThreadHelpBase (&Application::GetSolarMutex())
82 //*****************************************************************************************************************
83 TitleBarUpdate::~TitleBarUpdate()
87 //*****************************************************************************************************************
88 void SAL_CALL
TitleBarUpdate::initialize(const css::uno::Sequence
< css::uno::Any
>& lArguments
)
89 throw(css::uno::Exception
,
90 css::uno::RuntimeException
)
93 css::uno::Reference
< css::frame::XFrame
> xFrame
;
94 if (lArguments
.getLength() < 1)
95 throw css::lang::IllegalArgumentException(
96 DECLARE_ASCII("Empty argument list!"),
97 static_cast< ::cppu::OWeakObject
* >(this),
100 lArguments
[0] >>= xFrame
;
102 throw css::lang::IllegalArgumentException(
103 DECLARE_ASCII("No valid frame specified!"),
104 static_cast< ::cppu::OWeakObject
* >(this),
108 WriteGuard
aWriteLock(m_aLock
);
109 // hold the frame as weak reference(!) so it can die everytimes :-)
115 xFrame
->addFrameActionListener(this);
117 css::uno::Reference
< css::frame::XTitleChangeBroadcaster
> xBroadcaster(xFrame
, css::uno::UNO_QUERY
);
118 if (xBroadcaster
.is ())
119 xBroadcaster
->addTitleChangeListener (this);
122 //*****************************************************************************************************************
123 void SAL_CALL
TitleBarUpdate::frameAction(const css::frame::FrameActionEvent
& aEvent
)
124 throw(css::uno::RuntimeException
)
126 // we are interested on events only, which must trigger a title bar update
127 // because component was changed.
129 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_ATTACHED
) ||
130 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_REATTACHED
) ||
131 (aEvent
.Action
== css::frame::FrameAction_COMPONENT_DETACHING
)
138 //*****************************************************************************************************************
139 void SAL_CALL
TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent
& /* aEvent */)
140 throw (css::uno::RuntimeException
)
145 //*****************************************************************************************************************
146 void SAL_CALL
TitleBarUpdate::disposing(const css::lang::EventObject
&)
147 throw(css::uno::RuntimeException
)
149 // nothing todo here - because we hold the frame as weak reference only
152 //http://live.gnome.org/GnomeShell/ApplicationBased
153 //See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
154 //Windows 7 equivalent support
155 void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
157 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
158 if ( ! xWindow
.is() )
161 OUString sApplicationID
;
165 ReadGuard
aReadLock(m_aLock
);
166 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
170 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
171 css::frame::ModuleManager::create( comphelper::getComponentContext(xSMGR
) );
173 OUString aModuleId
= xModuleManager
->identify(xFrame
);
174 OUString sDesktopName
;
175 #if defined(UNX) && !defined(MACOSX)
176 if ( aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextDocument")) ||
177 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.GlobalDocument")) ||
178 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.WebDocument")) ||
179 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.xforms.XMLFormDocument")) )
180 sDesktopName
= OUString("writer");
181 else if ( aModuleId
== "com.sun.star.sheet.SpreadsheetDocument" )
182 sDesktopName
= OUString("calc");
183 else if ( aModuleId
== "com.sun.star.presentation.PresentationDocument" )
184 sDesktopName
= OUString("impress");
185 else if ( aModuleId
== "com.sun.star.drawing.DrawingDocument" )
186 sDesktopName
= OUString("draw");
187 else if ( aModuleId
== "com.sun.star.formula.FormulaProperties" )
188 sDesktopName
= OUString("math");
189 else if ( aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DatabaseDocument")) ||
190 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument")) ||
191 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.RelationDesign")) ||
192 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.QueryDesign")) ||
193 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.TableDesign")) ||
194 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DataSourceBrowser")) )
195 sDesktopName
= OUString("base");
197 sDesktopName
= OUString("startcenter");
198 sApplicationID
= utl::ConfigManager::getProductName().toAsciiLowerCase();
199 sApplicationID
+= OUString(sal_Unicode('-'));
200 sApplicationID
+= sDesktopName
;
202 if ( aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextDocument")) ||
203 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.GlobalDocument")) ||
204 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.WebDocument")) ||
205 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.xforms.XMLFormDocument")) )
206 sDesktopName
= OUString("Writer");
207 else if ( aModuleId
== "com.sun.star.sheet.SpreadsheetDocument" )
208 sDesktopName
= OUString("Calc");
209 else if ( aModuleId
== "com.sun.star.presentation.PresentationDocument" )
210 sDesktopName
= OUString("Impress");
211 else if ( aModuleId
== "com.sun.star.drawing.DrawingDocument" )
212 sDesktopName
= OUString("Draw");
213 else if ( aModuleId
== "com.sun.star.formula.FormulaProperties" )
214 sDesktopName
= OUString("Math");
215 else if ( aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DatabaseDocument")) ||
216 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument")) ||
217 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.RelationDesign")) ||
218 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.QueryDesign")) ||
219 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.TableDesign")) ||
220 aModuleId
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DataSourceBrowser")) )
221 sDesktopName
= OUString("Base");
223 sDesktopName
= OUString("Startcenter");
225 // We use a hardcoded product name matching the registry keys so applications can be associated with file types
226 sApplicationID
= "TheDocumentFoundation.LibreOffice.";
227 sApplicationID
+= sDesktopName
;
230 catch(const css::uno::Exception
&)
234 // VCL SYNCHRONIZED ->
235 SolarMutexGuard aSolarGuard
;
237 Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
240 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
243 WorkWindow
* pWorkWindow
= (WorkWindow
*)pWindow
;
244 pWorkWindow
->SetApplicationID( sApplicationID
);
246 // <- VCL SYNCHRONIZED
250 //*****************************************************************************************************************
251 ::sal_Bool
TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference
< css::frame::XFrame
>& xFrame
,
258 ReadGuard
aReadLock(m_aLock
);
259 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
265 css::uno::Reference
< css::frame::XModuleManager2
> xModuleManager
=
266 css::frame::ModuleManager::create( comphelper::getComponentContext(xSMGR
) );
268 rInfo
.sID
= xModuleManager
->identify(xFrame
);
269 ::comphelper::SequenceAsHashMap lProps
= xModuleManager
->getByName (rInfo
.sID
);
271 rInfo
.sUIName
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME
, OUString());
272 rInfo
.nIcon
= lProps
.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON
, INVALID_ICON_ID
);
274 // Note: If we could retrieve a module id ... everything is OK.
275 // UIName and Icon ID are optional values !
276 ::sal_Bool bSuccess
= !rInfo
.sID
.isEmpty();
279 catch(const css::uno::Exception
&)
285 //*****************************************************************************************************************
286 void TitleBarUpdate::impl_forceUpdate()
289 ReadGuard
aReadLock(m_aLock
);
290 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
291 css::uno::Reference
< css::frame::XFrame
> xFrame(m_xFrame
.get(), css::uno::UNO_QUERY
);
295 // frame already gone ? We hold it weak only ...
299 // no window -> no chance to set/update title and icon
300 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow();
304 impl_updateIcon (xFrame
);
305 impl_updateTitle (xFrame
);
307 impl_updateApplicationID (xFrame
);
311 //*****************************************************************************************************************
312 void TitleBarUpdate::impl_updateIcon(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
314 css::uno::Reference
< css::frame::XController
> xController
= xFrame
->getController ();
315 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
318 ( ! xController
.is() ) ||
323 // a) set default value to an invalid one. So we can start further searches for right icon id, if
324 // first steps failed!
325 sal_Int32 nIcon
= INVALID_ICON_ID
;
327 // b) try to find information on controller property set directly
328 // Don't forget to catch possible exceptions - because these property is an optional one!
329 css::uno::Reference
< css::beans::XPropertySet
> xSet( xController
, css::uno::UNO_QUERY
);
334 css::uno::Reference
< css::beans::XPropertySetInfo
> const xPSI( xSet
->getPropertySetInfo(), css::uno::UNO_SET_THROW
);
335 if ( xPSI
->hasPropertyByName( DECLARE_ASCII("IconId") ) )
336 xSet
->getPropertyValue( DECLARE_ASCII("IconId") ) >>= nIcon
;
338 catch(const css::uno::Exception
&)
340 DBG_UNHANDLED_EXCEPTION();
344 // c) if b) failed ... identify the used module and retrieve set icon from module config.
345 // Tirck :-) Module was already specified outside and aInfo contains all needed information.
346 if ( nIcon
== INVALID_ICON_ID
)
349 if (implst_getModuleInfo(xFrame
, aInfo
))
353 // d) if all steps failed - use fallback :-)
354 // ... means using the global staroffice icon
355 if( nIcon
== INVALID_ICON_ID
)
356 nIcon
= DEFAULT_ICON_ID
;
358 // e) set icon on container window now
359 // Don't forget SolarMutex! We use vcl directly :-(
360 // Check window pointer for right WorkWindow class too!!!
362 // VCL SYNCHRONIZED ->
363 SolarMutexGuard aSolarGuard
;
365 Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
368 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
371 WorkWindow
* pWorkWindow
= (WorkWindow
*)pWindow
;
372 pWorkWindow
->SetIcon( (sal_uInt16
)nIcon
);
374 css::uno::Reference
< css::frame::XModel
> xModel
= xController
->getModel();
377 aURL
= xModel
->getURL();
378 pWorkWindow
->SetRepresentedURL( aURL
);
380 // <- VCL SYNCHRONIZED
383 //*****************************************************************************************************************
384 void TitleBarUpdate::impl_updateTitle(const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
386 // no window ... no chance to set any title -> return
387 css::uno::Reference
< css::awt::XWindow
> xWindow
= xFrame
->getContainerWindow ();
388 if ( ! xWindow
.is() )
391 css::uno::Reference
< css::frame::XTitle
> xTitle(xFrame
, css::uno::UNO_QUERY
);
395 const OUString sTitle
= xTitle
->getTitle ();
397 // VCL SYNCHRONIZED ->
398 SolarMutexGuard aSolarGuard
;
400 Window
* pWindow
= (VCLUnoHelper::GetWindow( xWindow
));
403 ( pWindow
->GetType() == WINDOW_WORKWINDOW
)
406 WorkWindow
* pWorkWindow
= (WorkWindow
*)pWindow
;
407 pWorkWindow
->SetText( sTitle
);
409 // <- VCL SYNCHRONIZED
412 } // namespace framework
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */