LCOV - code coverage report
Current view: top level - sfx2/source/sidebar - CommandInfoProvider.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 126 0.0 %
Date: 2014-04-11 Functions: 0 18 0.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 <sfx2/sidebar/CommandInfoProvider.hxx>
      21             : 
      22             : #include <comphelper/processfactory.hxx>
      23             : #include <svtools/acceleratorexecute.hxx>
      24             : #include <cppuhelper/compbase1.hxx>
      25             : #include <cppuhelper/basemutex.hxx>
      26             : 
      27             : #include <com/sun/star/frame/ModuleManager.hpp>
      28             : #include <com/sun/star/frame/theUICommandDescription.hpp>
      29             : #include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
      30             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      31             : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
      32             : 
      33             : using namespace css;
      34             : using namespace cssu;
      35             : using ::rtl::OUString;
      36             : 
      37             : 
      38             : 
      39             : namespace
      40             : {
      41             :     typedef ::cppu::WeakComponentImplHelper1 <
      42             :         css::lang::XEventListener
      43             :         > FrameListenerInterfaceBase;
      44             :     class FrameListener
      45             :         : public ::cppu::BaseMutex,
      46             :           public FrameListenerInterfaceBase
      47             :     {
      48             :     public:
      49           0 :         FrameListener (sfx2::sidebar::CommandInfoProvider& rInfoProvider, const Reference<frame::XFrame>& rxFrame)
      50             :             : FrameListenerInterfaceBase(m_aMutex),
      51             :               mrInfoProvider(rInfoProvider),
      52           0 :               mxFrame(rxFrame)
      53             :         {
      54           0 :             if (mxFrame.is())
      55           0 :                 mxFrame->addEventListener(this);
      56           0 :         }
      57           0 :         virtual ~FrameListener (void)
      58           0 :         {
      59           0 :         }
      60           0 :         virtual void SAL_CALL disposing (void) SAL_OVERRIDE
      61             :         {
      62           0 :             if (mxFrame.is())
      63           0 :                 mxFrame->removeEventListener(this);
      64           0 :         }
      65           0 :         virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
      66             :             throw (cssu::RuntimeException, std::exception) SAL_OVERRIDE
      67             :         {
      68             :             (void)rEvent;
      69           0 :             mrInfoProvider.SetFrame(NULL);
      70           0 :             mxFrame = NULL;
      71           0 :         }
      72             : 
      73             :     private:
      74             :         sfx2::sidebar::CommandInfoProvider& mrInfoProvider;
      75             :         Reference<frame::XFrame> mxFrame;
      76             :     };
      77             : }
      78             : 
      79             : 
      80             : 
      81             : namespace sfx2 { namespace sidebar {
      82             : 
      83           0 : CommandInfoProvider& CommandInfoProvider::Instance (void)
      84             : {
      85           0 :     static CommandInfoProvider aProvider;
      86           0 :     return aProvider;
      87             : }
      88             : 
      89             : 
      90             : 
      91             : 
      92           0 : CommandInfoProvider::CommandInfoProvider (void)
      93             :     : mxContext(comphelper::getProcessComponentContext()),
      94             :       mxCachedDataFrame(),
      95             :       mxCachedDocumentAcceleratorConfiguration(),
      96             :       mxCachedModuleAcceleratorConfiguration(),
      97             :       mxCachedGlobalAcceleratorConfiguration(),
      98             :       msCachedModuleIdentifier(),
      99           0 :       mxFrameListener()
     100             : {
     101           0 : }
     102             : 
     103             : 
     104             : 
     105             : 
     106           0 : CommandInfoProvider::~CommandInfoProvider (void)
     107             : {
     108           0 :     if (mxFrameListener.is())
     109             :     {
     110           0 :         mxFrameListener->dispose();
     111           0 :         mxFrameListener = NULL;
     112             :     }
     113           0 : }
     114             : 
     115             : 
     116             : 
     117             : 
     118           0 : OUString CommandInfoProvider::GetLabelForCommand (
     119             :     const OUString& rsCommandName,
     120             :     const Reference<frame::XFrame>& rxFrame)
     121             : {
     122           0 :     SetFrame(rxFrame);
     123             : 
     124           0 :     const OUString sLabel (GetCommandLabel(rsCommandName));
     125           0 :     const OUString sShortCut (GetCommandShortcut(rsCommandName));
     126           0 :     if (sShortCut.getLength() > 0)
     127           0 :         return sLabel + " (" + sShortCut + ")";
     128             :     else
     129           0 :         return sLabel;
     130             : }
     131             : 
     132             : 
     133             : 
     134             : 
     135           0 : void CommandInfoProvider::SetFrame (const Reference<frame::XFrame>& rxFrame)
     136             : {
     137           0 :     if (rxFrame != mxCachedDataFrame)
     138             :     {
     139             :         // Detach from the old frame.
     140           0 :         if (mxFrameListener.is())
     141             :         {
     142           0 :             mxFrameListener->dispose();
     143           0 :             mxFrameListener = NULL;
     144             :         }
     145             : 
     146             :         // Release objects that are tied to the old frame.
     147           0 :         mxCachedDocumentAcceleratorConfiguration = NULL;
     148           0 :         mxCachedModuleAcceleratorConfiguration = NULL;
     149           0 :         msCachedModuleIdentifier = OUString();
     150           0 :         mxCachedDataFrame = rxFrame;
     151             : 
     152             :         // Connect to the new frame.
     153           0 :         if (rxFrame.is())
     154           0 :             mxFrameListener = new FrameListener(*this, rxFrame);
     155             :     }
     156           0 : }
     157             : 
     158             : 
     159             : 
     160             : 
     161           0 : Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetDocumentAcceleratorConfiguration (void)
     162             : {
     163           0 :     if ( ! mxCachedDocumentAcceleratorConfiguration.is())
     164             :     {
     165             :         // Get the accelerator configuration for the document.
     166           0 :         if (mxCachedDataFrame.is())
     167             :         {
     168           0 :             Reference<frame::XController> xController = mxCachedDataFrame->getController();
     169           0 :             if (xController.is())
     170             :             {
     171           0 :                 Reference<frame::XModel> xModel (xController->getModel());
     172           0 :                 if (xModel.is())
     173             :                 {
     174           0 :                     Reference<ui::XUIConfigurationManagerSupplier> xSupplier (xModel, UNO_QUERY);
     175           0 :                     if (xSupplier.is())
     176             :                     {
     177             :                         Reference<ui::XUIConfigurationManager> xConfigurationManager(
     178           0 :                             xSupplier->getUIConfigurationManager(),
     179           0 :                             UNO_QUERY);
     180           0 :                         if (xConfigurationManager.is())
     181             :                         {
     182           0 :                             mxCachedDocumentAcceleratorConfiguration = xConfigurationManager->getShortCutManager();
     183           0 :                         }
     184           0 :                     }
     185           0 :                 }
     186           0 :             }
     187             :         }
     188             :     }
     189           0 :     return mxCachedDocumentAcceleratorConfiguration;
     190             : }
     191             : 
     192             : 
     193             : 
     194             : 
     195           0 : Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetModuleAcceleratorConfiguration (void)
     196             : {
     197           0 :     if ( ! mxCachedModuleAcceleratorConfiguration.is())
     198             :     {
     199             :         try
     200             :         {
     201           0 :             Reference<ui::XModuleUIConfigurationManagerSupplier> xSupplier  = ui::theModuleUIConfigurationManagerSupplier::get(mxContext);
     202             :             Reference<ui::XUIConfigurationManager> xManager (
     203           0 :                 xSupplier->getUIConfigurationManager(GetModuleIdentifier()));
     204           0 :             if (xManager.is())
     205             :             {
     206           0 :                 mxCachedModuleAcceleratorConfiguration = xManager->getShortCutManager();
     207           0 :             }
     208             :         }
     209           0 :         catch (Exception&)
     210             :         {
     211             :         }
     212             :     }
     213           0 :     return mxCachedModuleAcceleratorConfiguration;
     214             : }
     215             : 
     216             : 
     217             : 
     218             : 
     219           0 : Reference<ui::XAcceleratorConfiguration> CommandInfoProvider::GetGlobalAcceleratorConfiguration (void)
     220             : {
     221             :     // Get the global accelerator configuration.
     222           0 :     if ( ! mxCachedGlobalAcceleratorConfiguration.is())
     223             :     {
     224           0 :         mxCachedGlobalAcceleratorConfiguration = ui::GlobalAcceleratorConfiguration::create(mxContext);
     225             :     }
     226             : 
     227           0 :     return mxCachedGlobalAcceleratorConfiguration;
     228             : }
     229             : 
     230             : 
     231             : 
     232             : 
     233           0 : OUString CommandInfoProvider::GetModuleIdentifier (void)
     234             : {
     235           0 :     if (msCachedModuleIdentifier.getLength() == 0)
     236             :     {
     237           0 :         Reference<frame::XModuleManager2> xModuleManager = frame::ModuleManager::create(mxContext);
     238           0 :         msCachedModuleIdentifier = xModuleManager->identify(mxCachedDataFrame);
     239             :     }
     240           0 :     return msCachedModuleIdentifier;
     241             : }
     242             : 
     243             : 
     244             : 
     245             : 
     246           0 : OUString CommandInfoProvider::GetCommandShortcut (const OUString& rsCommandName)
     247             : {
     248           0 :     OUString sShortcut;
     249             : 
     250           0 :     sShortcut = RetrieveShortcutsFromConfiguration(GetDocumentAcceleratorConfiguration(), rsCommandName);
     251           0 :     if (sShortcut.getLength() > 0)
     252           0 :         return sShortcut;
     253             : 
     254           0 :     sShortcut = RetrieveShortcutsFromConfiguration(GetModuleAcceleratorConfiguration(), rsCommandName);
     255           0 :     if (sShortcut.getLength() > 0)
     256           0 :         return sShortcut;
     257             : 
     258           0 :     sShortcut = RetrieveShortcutsFromConfiguration(GetGlobalAcceleratorConfiguration(), rsCommandName);
     259           0 :     if (sShortcut.getLength() > 0)
     260           0 :         return sShortcut;
     261             : 
     262           0 :     return OUString();
     263             : }
     264             : 
     265             : 
     266             : 
     267             : 
     268           0 : OUString CommandInfoProvider::RetrieveShortcutsFromConfiguration(
     269             :     const Reference<ui::XAcceleratorConfiguration>& rxConfiguration,
     270             :     const OUString& rsCommandName)
     271             : {
     272           0 :     if (rxConfiguration.is())
     273             :     {
     274             :         try
     275             :         {
     276           0 :             Sequence<OUString> aCommands(1);
     277           0 :             aCommands[0] = rsCommandName;
     278             : 
     279           0 :             Sequence<Any> aKeyCodes (rxConfiguration->getPreferredKeyEventsForCommandList(aCommands));
     280           0 :             if (aCommands.getLength() == 1)
     281             :             {
     282           0 :                 css::awt::KeyEvent aKeyEvent;
     283           0 :                 if (aKeyCodes[0] >>= aKeyEvent)
     284             :                 {
     285           0 :                     return svt::AcceleratorExecute::st_AWTKey2VCLKey(aKeyEvent).GetName();
     286           0 :                 }
     287           0 :             }
     288             :         }
     289           0 :         catch (lang::IllegalArgumentException&)
     290             :         {
     291             :         }
     292             :     }
     293           0 :     return OUString();
     294             : }
     295             : 
     296             : 
     297             : 
     298             : 
     299           0 : Sequence<beans::PropertyValue> CommandInfoProvider::GetCommandProperties (const OUString& rsCommandName)
     300             : {
     301           0 :     Sequence<beans::PropertyValue> aProperties;
     302             : 
     303             :     try
     304             :     {
     305           0 :         const OUString sModuleIdentifier (GetModuleIdentifier());
     306           0 :         if (sModuleIdentifier.getLength() > 0)
     307             :         {
     308           0 :             Reference<container::XNameAccess> xNameAccess  = frame::theUICommandDescription::get(mxContext);
     309           0 :             Reference<container::XNameAccess> xUICommandLabels;
     310           0 :             if (xNameAccess->getByName(sModuleIdentifier) >>= xUICommandLabels)
     311           0 :                 xUICommandLabels->getByName(rsCommandName) >>= aProperties;
     312           0 :         }
     313             :     }
     314           0 :     catch (Exception&)
     315             :     {
     316             :     }
     317             : 
     318           0 :     return aProperties;
     319             : }
     320             : 
     321             : 
     322             : 
     323             : 
     324           0 : OUString CommandInfoProvider::GetCommandLabel (const OUString& rsCommandName)
     325             : {
     326           0 :     const Sequence<beans::PropertyValue> aProperties (GetCommandProperties(rsCommandName));
     327           0 :     for (sal_Int32 nIndex=0; nIndex<aProperties.getLength(); ++nIndex)
     328             :     {
     329           0 :         if (aProperties[nIndex].Name.equalsAscii("Name"))
     330             :         {
     331           0 :             OUString sLabel;
     332           0 :             aProperties[nIndex].Value >>= sLabel;
     333           0 :             return sLabel;
     334             :         }
     335             :     }
     336           0 :     return OUString();
     337             : }
     338             : 
     339             : 
     340             : } } // end of namespace sfx2/framework
     341             : 
     342             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10