LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/helper - titlebarupdate.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 145 156 92.9 %
Date: 2013-07-09 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          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 <helper/titlebarupdate.hxx>
      21             : 
      22             : #include <pattern/window.hxx>
      23             : #include <threadhelp/writeguard.hxx>
      24             : #include <threadhelp/readguard.hxx>
      25             : #include <macros/generic.hxx>
      26             : #include <services.h>
      27             : #include <properties.h>
      28             : 
      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>
      38             : 
      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/helper/vclunohelper.hxx>
      46             : #include <vcl/svapp.hxx>
      47             : #include <vcl/wrkwin.hxx>
      48             : #include <tools/diagnose_ex.h>
      49             : 
      50             : namespace framework{
      51             : 
      52             : static const ::sal_Int32 INVALID_ICON_ID = -1;
      53             : static const ::sal_Int32 DEFAULT_ICON_ID =  0;
      54             : 
      55             : 
      56             : //*****************************************************************************************************************
      57             : //  XInterface, XTypeProvider
      58             : 
      59       82659 : DEFINE_XINTERFACE_5(TitleBarUpdate                                                              ,
      60             :                     OWeakObject                                                                 ,
      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))
      66             : 
      67           0 : DEFINE_XTYPEPROVIDER_5(TitleBarUpdate                  ,
      68             :                        css::lang::XTypeProvider        ,
      69             :                        css::lang::XInitialization      ,
      70             :                        css::frame::XFrameActionListener,
      71             :                        css::frame::XTitleChangeListener,
      72             :                        css::lang::XEventListener       )
      73             : 
      74             : //*****************************************************************************************************************
      75        1103 : TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::uno::XComponentContext >& xContext)
      76        1103 :     : ThreadHelpBase          (&Application::GetSolarMutex())
      77             :     , m_xContext              (xContext                     )
      78        2206 :     , m_xFrame                (                             )
      79             : {
      80        1103 : }
      81             : 
      82             : //*****************************************************************************************************************
      83        1976 : TitleBarUpdate::~TitleBarUpdate()
      84             : {
      85        1976 : }
      86             : 
      87             : //*****************************************************************************************************************
      88        1103 : void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
      89             :     throw(css::uno::Exception       ,
      90             :           css::uno::RuntimeException)
      91             : {
      92             :     // check arguments
      93        1103 :     css::uno::Reference< css::frame::XFrame > xFrame;
      94        1103 :     if (lArguments.getLength() < 1)
      95             :         throw css::lang::IllegalArgumentException(
      96             :                 DECLARE_ASCII("Empty argument list!"),
      97             :                 static_cast< ::cppu::OWeakObject* >(this),
      98           0 :                 1);
      99             : 
     100        1103 :     lArguments[0] >>= xFrame;
     101        1103 :     if (!xFrame.is())
     102             :         throw css::lang::IllegalArgumentException(
     103             :                 DECLARE_ASCII("No valid frame specified!"),
     104             :                 static_cast< ::cppu::OWeakObject* >(this),
     105           0 :                 1);
     106             : 
     107             :     // SYNCHRONIZED ->
     108        2206 :     WriteGuard aWriteLock(m_aLock);
     109             :     // hold the frame as weak reference(!) so it can die everytimes :-)
     110        1103 :     m_xFrame = xFrame;
     111        1103 :     aWriteLock.unlock();
     112             :     // <- SYNCHRONIZED
     113             : 
     114             :     // start listening
     115        1103 :     xFrame->addFrameActionListener(this);
     116             : 
     117        2206 :     css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
     118        1103 :     if (xBroadcaster.is ())
     119        2206 :         xBroadcaster->addTitleChangeListener (this);
     120        1103 : }
     121             : 
     122             : //*****************************************************************************************************************
     123        5885 : void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
     124             :     throw(css::uno::RuntimeException)
     125             : {
     126             :     // we are interested on events only, which must trigger a title bar update
     127             :     // because component was changed.
     128        5885 :     if (
     129       10671 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED  ) ||
     130        9563 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
     131        4777 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
     132             :        )
     133             :     {
     134        2209 :         impl_forceUpdate ();
     135             :     }
     136        5885 : }
     137             : 
     138             : //*****************************************************************************************************************
     139        1133 : void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
     140             :     throw (css::uno::RuntimeException)
     141             : {
     142        1133 :     impl_forceUpdate ();
     143        1133 : }
     144             : 
     145             : //*****************************************************************************************************************
     146        1096 : void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
     147             :     throw(css::uno::RuntimeException)
     148             : {
     149             :     // nothing todo here - because we hold the frame as weak reference only
     150        1096 : }
     151             : 
     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        3342 : void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
     156             : {
     157        3342 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
     158        3342 :     if ( ! xWindow.is() )
     159        3342 :         return;
     160             : 
     161        6684 :     OUString sApplicationID;
     162             :     try
     163             :     {
     164             :         // SYNCHRONIZED ->
     165        3342 :         ReadGuard aReadLock(m_aLock);
     166        6684 :         css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
     167        3342 :         aReadLock.unlock();
     168             :         // <- SYNCHRONIZED
     169             : 
     170             :         css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     171        6684 :             css::frame::ModuleManager::create( xContext );
     172             : 
     173        5556 :         OUString aModuleId = xModuleManager->identify(xFrame);
     174        4428 :         OUString sDesktopName;
     175             : 
     176        5058 :         if ( aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextDocument")) ||
     177        1260 :              aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.GlobalDocument")) ||
     178        3469 :              aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.WebDocument")) ||
     179         625 :              aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.xforms.XMLFormDocument")) )
     180        1589 :             sDesktopName = OUString("writer");
     181         625 :         else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
     182         432 :             sDesktopName = OUString("calc");
     183         193 :         else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
     184          28 :             sDesktopName = OUString("impress");
     185         165 :         else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
     186         106 :             sDesktopName = OUString("draw");
     187          59 :         else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
     188          38 :             sDesktopName = OUString("math");
     189          63 :         else if ( aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DatabaseDocument")) ||
     190          42 :                   aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument")) ||
     191          42 :                   aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.RelationDesign")) ||
     192          42 :                   aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.QueryDesign")) ||
     193          63 :                   aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.TableDesign")) ||
     194          21 :                   aModuleId.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("com.sun.star.sdb.DataSourceBrowser")) )
     195           0 :             sDesktopName = OUString("base");
     196             :         else
     197          21 :             sDesktopName = OUString("startcenter");
     198        2214 :         sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase();
     199        2214 :         sApplicationID += OUString(sal_Unicode('-'));
     200        5556 :         sApplicationID += sDesktopName;
     201             :     }
     202        1128 :     catch(const css::uno::Exception&)
     203             :     {
     204             :     }
     205             : 
     206             :     // VCL SYNCHRONIZED ->
     207        6684 :     SolarMutexGuard aSolarGuard;
     208             : 
     209        3342 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     210        3342 :     if (
     211        6684 :         ( pWindow                                 ) &&
     212        3342 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     213             :        )
     214             :     {
     215        3339 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     216        3339 :         pWorkWindow->SetApplicationID( sApplicationID );
     217        3342 :     }
     218             :     // <- VCL SYNCHRONIZED
     219             : }
     220             : 
     221             : 
     222             : //*****************************************************************************************************************
     223        2246 : ::sal_Bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame >& xFrame,
     224             :                                                       TModuleInfo&                               rInfo )
     225             : {
     226        2246 :     if ( ! xFrame.is ())
     227           0 :         return sal_False;
     228             : 
     229             :     // SYNCHRONIZED ->
     230        2246 :     ReadGuard aReadLock(m_aLock);
     231        4492 :     css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
     232        2246 :     aReadLock.unlock();
     233             :     // <- SYNCHRONIZED
     234             : 
     235             :     try
     236             :     {
     237             :         css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     238        2246 :             css::frame::ModuleManager::create( xContext );
     239             : 
     240        2246 :         rInfo.sID = xModuleManager->identify(xFrame);
     241        4424 :         ::comphelper::SequenceAsHashMap lProps    = xModuleManager->getByName (rInfo.sID);
     242             : 
     243        2210 :         rInfo.sUIName = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME, OUString());
     244        2210 :         rInfo.nIcon   = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON  , INVALID_ICON_ID  );
     245             : 
     246             :         // Note: If we could retrieve a module id ... everything is OK.
     247             :         // UIName and Icon ID are optional values !
     248        2210 :         ::sal_Bool bSuccess = !rInfo.sID.isEmpty();
     249        4456 :         return bSuccess;
     250             :     }
     251          36 :     catch(const css::uno::Exception&)
     252             :         {}
     253             : 
     254        2282 :     return sal_False;
     255             : }
     256             : 
     257             : //*****************************************************************************************************************
     258        3342 : void TitleBarUpdate::impl_forceUpdate()
     259             : {
     260             :     // SYNCHRONIZED ->
     261        3342 :     ReadGuard aReadLock(m_aLock);
     262        6684 :     css::uno::Reference< css::frame::XFrame >              xFrame(m_xFrame.get(), css::uno::UNO_QUERY);
     263        3342 :     aReadLock.unlock();
     264             :     // <- SYNCHRONIZED
     265             : 
     266             :     // frame already gone ? We hold it weak only ...
     267        3342 :     if ( ! xFrame.is())
     268           0 :         return;
     269             : 
     270             :     // no window -> no chance to set/update title and icon
     271        6684 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
     272        3342 :     if ( ! xWindow.is())
     273           0 :         return;
     274             : 
     275        3342 :     impl_updateIcon  (xFrame);
     276        3342 :     impl_updateTitle (xFrame);
     277             : #if defined(UNX) && !defined(MACOSX)
     278        6684 :     impl_updateApplicationID (xFrame);
     279             : #endif
     280             : }
     281             : 
     282             : //*****************************************************************************************************************
     283        3342 : void TitleBarUpdate::impl_updateIcon(const css::uno::Reference< css::frame::XFrame >& xFrame)
     284             : {
     285        3342 :     css::uno::Reference< css::frame::XController > xController = xFrame->getController      ();
     286        5588 :     css::uno::Reference< css::awt::XWindow >       xWindow     = xFrame->getContainerWindow ();
     287             : 
     288        3342 :     if (
     289        5588 :         ( ! xController.is() ) ||
     290        2246 :         ( ! xWindow.is()     )
     291             :        )
     292        4438 :         return;
     293             : 
     294             :     // a) set default value to an invalid one. So we can start further searches for right icon id, if
     295             :     //    first steps failed!
     296        2246 :     sal_Int32 nIcon = INVALID_ICON_ID;
     297             : 
     298             :     // b) try to find information on controller property set directly
     299             :     //    Don't forget to catch possible exceptions - because these property is an optional one!
     300        4492 :     css::uno::Reference< css::beans::XPropertySet > xSet( xController, css::uno::UNO_QUERY );
     301        2246 :     if ( xSet.is() )
     302             :     {
     303             :         try
     304             :         {
     305        2131 :             css::uno::Reference< css::beans::XPropertySetInfo > const xPSI( xSet->getPropertySetInfo(), css::uno::UNO_SET_THROW );
     306        2131 :             if ( xPSI->hasPropertyByName( DECLARE_ASCII("IconId") ) )
     307           0 :                 xSet->getPropertyValue( DECLARE_ASCII("IconId") ) >>= nIcon;
     308             :         }
     309           0 :         catch(const css::uno::Exception&)
     310             :         {
     311             :             DBG_UNHANDLED_EXCEPTION();
     312             :         }
     313             :     }
     314             : 
     315             :     // c) if b) failed ... identify the used module and retrieve set icon from module config.
     316             :     //    Tirck :-) Module was already specified outside and aInfo contains all needed information.
     317        2246 :     if ( nIcon == INVALID_ICON_ID )
     318             :     {
     319        2246 :         TModuleInfo aInfo;
     320        2246 :         if (implst_getModuleInfo(xFrame, aInfo))
     321        2210 :             nIcon = aInfo.nIcon;
     322             :     }
     323             : 
     324             :     // d) if all steps failed - use fallback :-)
     325             :     //    ... means using the global staroffice icon
     326        2246 :     if( nIcon == INVALID_ICON_ID )
     327          36 :         nIcon = DEFAULT_ICON_ID;
     328             : 
     329             :     // e) set icon on container window now
     330             :     //    Don't forget SolarMutex! We use vcl directly :-(
     331             :     //    Check window pointer for right WorkWindow class too!!!
     332             : 
     333             :     // VCL SYNCHRONIZED ->
     334        4492 :     SolarMutexGuard aSolarGuard;
     335             : 
     336        2246 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     337        2246 :     if (
     338        4492 :         ( pWindow                                 ) &&
     339        2246 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     340             :        )
     341             :     {
     342        2244 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     343        2244 :         pWorkWindow->SetIcon( (sal_uInt16)nIcon );
     344             : 
     345        2244 :         css::uno::Reference< css::frame::XModel > xModel = xController->getModel();
     346        4488 :         OUString aURL;
     347        2244 :         if( xModel.is() )
     348        2209 :             aURL = xModel->getURL();
     349        4488 :         pWorkWindow->SetRepresentedURL( aURL );
     350        2246 :     }
     351             :     // <- VCL SYNCHRONIZED
     352             : }
     353             : 
     354             : //*****************************************************************************************************************
     355        3342 : void TitleBarUpdate::impl_updateTitle(const css::uno::Reference< css::frame::XFrame >& xFrame)
     356             : {
     357             :     // no window ... no chance to set any title -> return
     358        3342 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
     359        3342 :     if ( ! xWindow.is() )
     360           0 :         return;
     361             : 
     362        6684 :     css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
     363        3342 :     if ( ! xTitle.is() )
     364           0 :         return;
     365             : 
     366        6684 :     const OUString sTitle = xTitle->getTitle ();
     367             : 
     368             :     // VCL SYNCHRONIZED ->
     369        6684 :     SolarMutexGuard aSolarGuard;
     370             : 
     371        3342 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     372        3342 :     if (
     373        6684 :         ( pWindow                                 ) &&
     374        3342 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     375             :        )
     376             :     {
     377        3339 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     378        3339 :         pWorkWindow->SetText( sTitle );
     379        3342 :     }
     380             :     // <- VCL SYNCHRONIZED
     381             : }
     382             : 
     383         402 : } // namespace framework
     384             : 
     385             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10