LCOV - code coverage report
Current view: top level - framework/source/helper - titlebarupdate.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 136 145 93.8 %
Date: 2014-04-11 Functions: 12 12 100.0 %
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 <macros/generic.hxx>
      24             : #include <services.h>
      25             : #include <properties.h>
      26             : 
      27             : #include <com/sun/star/awt/XWindow.hpp>
      28             : #include <com/sun/star/lang/XServiceInfo.hpp>
      29             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      30             : #include <com/sun/star/frame/ModuleManager.hpp>
      31             : #include <com/sun/star/container/XNameAccess.hpp>
      32             : #include <com/sun/star/beans/XPropertySet.hpp>
      33             : #include <com/sun/star/beans/XMaterialHolder.hpp>
      34             : #include <com/sun/star/frame/XTitleChangeBroadcaster.hpp>
      35             : #include <com/sun/star/beans/NamedValue.hpp>
      36             : 
      37             : #include <comphelper/processfactory.hxx>
      38             : #include <comphelper/sequenceashashmap.hxx>
      39             : #include <unotools/configmgr.hxx>
      40             : #include <unotools/bootstrap.hxx>
      41             : #include <vcl/window.hxx>
      42             : #include <vcl/syswin.hxx>
      43             : #include <toolkit/helper/vclunohelper.hxx>
      44             : #include <vcl/svapp.hxx>
      45             : #include <vcl/wrkwin.hxx>
      46             : #include <tools/diagnose_ex.h>
      47             : 
      48             : namespace framework{
      49             : 
      50             : static const ::sal_Int32 INVALID_ICON_ID = -1;
      51             : static const ::sal_Int32 DEFAULT_ICON_ID =  0;
      52             : 
      53        2074 : TitleBarUpdate::TitleBarUpdate(const css::uno::Reference< css::uno::XComponentContext >& xContext)
      54             :     : m_xContext              (xContext                     )
      55        2074 :     , m_xFrame                (                             )
      56             : {
      57        2074 : }
      58             : 
      59        3906 : TitleBarUpdate::~TitleBarUpdate()
      60             : {
      61        3906 : }
      62             : 
      63        2074 : void SAL_CALL TitleBarUpdate::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
      64             :     throw(css::uno::Exception       ,
      65             :           css::uno::RuntimeException, std::exception)
      66             : {
      67             :     // check arguments
      68        2074 :     css::uno::Reference< css::frame::XFrame > xFrame;
      69        2074 :     if (lArguments.getLength() < 1)
      70             :         throw css::lang::IllegalArgumentException(
      71             :                 "Empty argument list!",
      72             :                 static_cast< ::cppu::OWeakObject* >(this),
      73           0 :                 1);
      74             : 
      75        2074 :     lArguments[0] >>= xFrame;
      76        2074 :     if (!xFrame.is())
      77             :         throw css::lang::IllegalArgumentException(
      78             :                 "No valid frame specified!",
      79             :                 static_cast< ::cppu::OWeakObject* >(this),
      80           0 :                 1);
      81             : 
      82             :     {
      83        2074 :         SolarMutexGuard g;
      84             :         // hold the frame as weak reference(!) so it can die everytimes :-)
      85        2074 :         m_xFrame = xFrame;
      86             :     }
      87             : 
      88             :     // start listening
      89        2074 :     xFrame->addFrameActionListener(this);
      90             : 
      91        4148 :     css::uno::Reference< css::frame::XTitleChangeBroadcaster > xBroadcaster(xFrame, css::uno::UNO_QUERY);
      92        2074 :     if (xBroadcaster.is ())
      93        4148 :         xBroadcaster->addTitleChangeListener (this);
      94        2074 : }
      95             : 
      96       11614 : void SAL_CALL TitleBarUpdate::frameAction(const css::frame::FrameActionEvent& aEvent)
      97             :     throw(css::uno::RuntimeException, std::exception)
      98             : {
      99             :     // we are interested on events only, which must trigger a title bar update
     100             :     // because component was changed.
     101       11614 :     if (
     102       21158 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_ATTACHED  ) ||
     103       19079 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED) ||
     104        9535 :         (aEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING )
     105             :        )
     106             :     {
     107        4145 :         impl_forceUpdate ();
     108             :     }
     109       11614 : }
     110             : 
     111        2094 : void SAL_CALL TitleBarUpdate::titleChanged(const css::frame::TitleChangedEvent& /* aEvent */)
     112             :     throw (css::uno::RuntimeException, std::exception)
     113             : {
     114        2094 :     impl_forceUpdate ();
     115        2094 : }
     116             : 
     117        2061 : void SAL_CALL TitleBarUpdate::disposing(const css::lang::EventObject&)
     118             :     throw(css::uno::RuntimeException, std::exception)
     119             : {
     120             :     // nothing todo here - because we hold the frame as weak reference only
     121        2061 : }
     122             : 
     123             : //http://live.gnome.org/GnomeShell/ApplicationBased
     124             : //See http://msdn.microsoft.com/en-us/library/dd378459(v=VS.85).aspx for future
     125             : //Windows 7 equivalent support
     126        6239 : void TitleBarUpdate::impl_updateApplicationID(const css::uno::Reference< css::frame::XFrame >& xFrame)
     127             : {
     128        6239 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
     129        6239 :     if ( ! xWindow.is() )
     130        6239 :         return;
     131             : 
     132       12478 :     OUString sApplicationID;
     133             : 
     134             : #if !defined(MACOSX)
     135             :     try
     136             :     {
     137             :         css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     138        6239 :             css::frame::ModuleManager::create( m_xContext );
     139             : 
     140       12478 :         OUString sDesktopName;
     141       10385 :         OUString aModuleId = xModuleManager->identify(xFrame);
     142        8977 :         if ( aModuleId == "com.sun.star.text.TextDocument" ||
     143        1370 :              aModuleId == "com.sun.star.text.GlobalDocument" ||
     144        5511 :              aModuleId == "com.sun.star.text.WebDocument" ||
     145         680 :              aModuleId == "com.sun.star.xforms.XMLFormDocument" )
     146        3466 :             sDesktopName = "Writer";
     147         680 :         else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
     148         467 :             sDesktopName = "Calc";
     149         213 :         else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
     150          43 :             sDesktopName = "Impress";
     151         170 :         else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
     152         105 :             sDesktopName = "Draw";
     153          65 :         else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
     154          38 :             sDesktopName = "Math";
     155          81 :         else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" ||
     156          48 :                   aModuleId == "com.sun.star.sdb.OfficeDatabaseDocument" ||
     157          42 :                   aModuleId == "com.sun.star.sdb.RelationDesign" ||
     158          42 :                   aModuleId == "com.sun.star.sdb.QueryDesign" ||
     159          69 :                   aModuleId == "com.sun.star.sdb.TableDesign" ||
     160          21 :                   aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
     161           6 :             sDesktopName = "Base";
     162             :         else
     163          21 :             sDesktopName = "Startcenter";
     164             : #if defined(WNT)
     165             :         // We use a hardcoded product name matching the registry keys so applications can be associated with file types
     166             :         sApplicationID = "TheDocumentFoundation.LibreOffice.";
     167             :         sApplicationID += sDesktopName;
     168             : #else
     169        4146 :         sApplicationID = utl::ConfigManager::getProductName().toAsciiLowerCase();
     170        4146 :         sApplicationID += "-";
     171       10385 :         sApplicationID += sDesktopName.toAsciiLowerCase();
     172             : #endif
     173             :     }
     174        2093 :     catch(const css::uno::Exception&)
     175             :     {
     176             :     }
     177             : #endif
     178             : 
     179             :     // VCL SYNCHRONIZED ->
     180       12478 :     SolarMutexGuard aSolarGuard;
     181             : 
     182        6239 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     183        6239 :     if (
     184       12478 :         ( pWindow                                 ) &&
     185        6239 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     186             :        )
     187             :     {
     188        6236 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     189        6236 :         pWorkWindow->SetApplicationID( sApplicationID );
     190        6239 :     }
     191             :     // <- VCL SYNCHRONIZED
     192             : }
     193             : 
     194        4178 : bool TitleBarUpdate::implst_getModuleInfo(const css::uno::Reference< css::frame::XFrame >& xFrame,
     195             :                                                 TModuleInfo&                               rInfo )
     196             : {
     197        4178 :     if ( ! xFrame.is ())
     198           0 :         return false;
     199             : 
     200             :     try
     201             :     {
     202             :         css::uno::Reference< css::frame::XModuleManager2 > xModuleManager =
     203        4178 :             css::frame::ModuleManager::create( m_xContext );
     204             : 
     205        4178 :         rInfo.sID = xModuleManager->identify(xFrame);
     206        8292 :         ::comphelper::SequenceAsHashMap lProps    = xModuleManager->getByName (rInfo.sID);
     207             : 
     208        4146 :         rInfo.sUIName = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_UINAME, OUString());
     209        4146 :         rInfo.nIcon   = lProps.getUnpackedValueOrDefault (OFFICEFACTORY_PROPNAME_ICON  , INVALID_ICON_ID  );
     210             : 
     211             :         // Note: If we could retrieve a module id ... everything is OK.
     212             :         // UIName and Icon ID are optional values !
     213        4146 :         bool bSuccess = !rInfo.sID.isEmpty();
     214        8324 :         return bSuccess;
     215             :     }
     216          32 :     catch(const css::uno::Exception&)
     217             :         {}
     218             : 
     219          32 :     return false;
     220             : }
     221             : 
     222        6239 : void TitleBarUpdate::impl_forceUpdate()
     223             : {
     224        6239 :     css::uno::Reference< css::frame::XFrame > xFrame;
     225             :     {
     226        6239 :         SolarMutexGuard g;
     227        6239 :         xFrame.set(m_xFrame.get(), css::uno::UNO_QUERY);
     228             :     }
     229             : 
     230             :     // frame already gone ? We hold it weak only ...
     231        6239 :     if ( ! xFrame.is())
     232           0 :         return;
     233             : 
     234             :     // no window -> no chance to set/update title and icon
     235       12478 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow();
     236        6239 :     if ( ! xWindow.is())
     237           0 :         return;
     238             : 
     239        6239 :     impl_updateIcon  (xFrame);
     240        6239 :     impl_updateTitle (xFrame);
     241             : #if !defined(MACOSX)
     242       12478 :     impl_updateApplicationID (xFrame);
     243             : #endif
     244             : }
     245             : 
     246        6239 : void TitleBarUpdate::impl_updateIcon(const css::uno::Reference< css::frame::XFrame >& xFrame)
     247             : {
     248        6239 :     css::uno::Reference< css::frame::XController > xController = xFrame->getController      ();
     249       10417 :     css::uno::Reference< css::awt::XWindow >       xWindow     = xFrame->getContainerWindow ();
     250             : 
     251        6239 :     if (
     252       10417 :         ( ! xController.is() ) ||
     253        4178 :         ( ! xWindow.is()     )
     254             :        )
     255        8300 :         return;
     256             : 
     257             :     // a) set default value to an invalid one. So we can start further searches for right icon id, if
     258             :     //    first steps failed!
     259        4178 :     sal_Int32 nIcon = INVALID_ICON_ID;
     260             : 
     261             :     // b) try to find information on controller property set directly
     262             :     //    Don't forget to catch possible exceptions - because these property is an optional one!
     263        8356 :     css::uno::Reference< css::beans::XPropertySet > xSet( xController, css::uno::UNO_QUERY );
     264        4178 :     if ( xSet.is() )
     265             :     {
     266             :         try
     267             :         {
     268        4057 :             css::uno::Reference< css::beans::XPropertySetInfo > const xPSI( xSet->getPropertySetInfo(), css::uno::UNO_SET_THROW );
     269        4057 :             if ( xPSI->hasPropertyByName( "IconId" ) )
     270           0 :                 xSet->getPropertyValue( "IconId" ) >>= nIcon;
     271             :         }
     272           0 :         catch(const css::uno::Exception&)
     273             :         {
     274             :             DBG_UNHANDLED_EXCEPTION();
     275             :         }
     276             :     }
     277             : 
     278             :     // c) if b) failed ... identify the used module and retrieve set icon from module config.
     279             :     //    Tirck :-) Module was already specified outside and aInfo contains all needed information.
     280        4178 :     if ( nIcon == INVALID_ICON_ID )
     281             :     {
     282        4178 :         TModuleInfo aInfo;
     283        4178 :         if (implst_getModuleInfo(xFrame, aInfo))
     284        4146 :             nIcon = aInfo.nIcon;
     285             :     }
     286             : 
     287             :     // d) if all steps failed - use fallback :-)
     288             :     //    ... means using the global staroffice icon
     289        4178 :     if( nIcon == INVALID_ICON_ID )
     290          32 :         nIcon = DEFAULT_ICON_ID;
     291             : 
     292             :     // e) set icon on container window now
     293             :     //    Don't forget SolarMutex! We use vcl directly :-(
     294             :     //    Check window pointer for right WorkWindow class too!!!
     295             : 
     296             :     // VCL SYNCHRONIZED ->
     297        8356 :     SolarMutexGuard aSolarGuard;
     298             : 
     299        4178 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     300        4178 :     if (
     301        8356 :         ( pWindow                                 ) &&
     302        4178 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     303             :        )
     304             :     {
     305        4176 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     306        4176 :         pWorkWindow->SetIcon( (sal_uInt16)nIcon );
     307             : 
     308        4176 :         css::uno::Reference< css::frame::XModel > xModel = xController->getModel();
     309        8352 :         OUString aURL;
     310        4176 :         if( xModel.is() )
     311        4141 :             aURL = xModel->getURL();
     312        8352 :         pWorkWindow->SetRepresentedURL( aURL );
     313        4178 :     }
     314             :     // <- VCL SYNCHRONIZED
     315             : }
     316             : 
     317        6239 : void TitleBarUpdate::impl_updateTitle(const css::uno::Reference< css::frame::XFrame >& xFrame)
     318             : {
     319             :     // no window ... no chance to set any title -> return
     320        6239 :     css::uno::Reference< css::awt::XWindow > xWindow = xFrame->getContainerWindow ();
     321        6239 :     if ( ! xWindow.is() )
     322           0 :         return;
     323             : 
     324       12478 :     css::uno::Reference< css::frame::XTitle > xTitle(xFrame, css::uno::UNO_QUERY);
     325        6239 :     if ( ! xTitle.is() )
     326           0 :         return;
     327             : 
     328       12478 :     const OUString sTitle = xTitle->getTitle ();
     329             : 
     330             :     // VCL SYNCHRONIZED ->
     331       12478 :     SolarMutexGuard aSolarGuard;
     332             : 
     333        6239 :     Window* pWindow = (VCLUnoHelper::GetWindow( xWindow ));
     334        6239 :     if (
     335       12478 :         ( pWindow                                 ) &&
     336        6239 :         ( pWindow->GetType() == WINDOW_WORKWINDOW )
     337             :        )
     338             :     {
     339        6236 :         WorkWindow* pWorkWindow = (WorkWindow*)pWindow;
     340        6236 :         pWorkWindow->SetText( sTitle );
     341        6239 :     }
     342             :     // <- VCL SYNCHRONIZED
     343             : }
     344             : 
     345             : } // namespace framework
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10