LCOV - code coverage report
Current view: top level - framework/inc/helper - mischelper.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 40 82 48.8 %
Date: 2015-06-13 12:38:46 Functions: 12 21 57.1 %
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             : #ifndef INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
      21             : #define INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
      22             : 
      23             : #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
      24             : #include <com/sun/star/container/XNameAccess.hpp>
      25             : #include <com/sun/star/document/XDocumentEventListener.hpp>
      26             : #include <com/sun/star/lang/XEventListener.hpp>
      27             : #include <com/sun/star/util/XChangesListener.hpp>
      28             : #include <com/sun/star/container/XContainerListener.hpp>
      29             : #include <com/sun/star/frame/XFrame.hpp>
      30             : #include <com/sun/star/uno/XComponentContext.hpp>
      31             : 
      32             : #include <cppuhelper/implbase1.hxx>
      33             : 
      34             : #include <i18nlangtag/lang.h>
      35             : #include <svl/languageoptions.hxx>
      36             : #include <rtl/ustring.hxx>
      37             : #include <fwidllapi.h>
      38             : 
      39             : #include <set>
      40             : 
      41             : namespace framework
      42             : {
      43             : 
      44             : // menu ids for language status bar control
      45             : enum LangMenuIDs
      46             : {
      47             :     MID_LANG_SEL_1 = 1,     // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
      48             :     MID_LANG_SEL_2,
      49             :     MID_LANG_SEL_3,
      50             :     MID_LANG_SEL_4,
      51             :     MID_LANG_SEL_5,
      52             :     MID_LANG_SEL_6,
      53             :     MID_LANG_SEL_7,
      54             :     MID_LANG_SEL_8,
      55             :     MID_LANG_SEL_9,
      56             :     MID_LANG_SEL_NONE,
      57             :     MID_LANG_SEL_RESET,
      58             :     MID_LANG_SEL_MORE,
      59             : 
      60             :     MID_LANG_PARA_SEPARATOR,
      61             :     MID_LANG_PARA_STRING,
      62             : 
      63             :     MID_LANG_PARA_1,
      64             :     MID_LANG_PARA_2,
      65             :     MID_LANG_PARA_3,
      66             :     MID_LANG_PARA_4,
      67             :     MID_LANG_PARA_5,
      68             :     MID_LANG_PARA_6,
      69             :     MID_LANG_PARA_7,
      70             :     MID_LANG_PARA_8,
      71             :     MID_LANG_PARA_9,
      72             :     MID_LANG_PARA_NONE,
      73             :     MID_LANG_PARA_RESET,
      74             :     MID_LANG_PARA_MORE,
      75             : };
      76             : 
      77           0 : inline bool IsScriptTypeMatchingToLanguage( SvtScriptType nScriptType, LanguageType nLang )
      78             : {
      79           0 :     return bool(nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ));
      80             : }
      81             : 
      82       24675 : inline void RetrieveTypeNameFromResourceURL( const OUString& aResourceURL, OUString& aType, OUString& aName )
      83             : {
      84       24675 :     const sal_Int32 RESOURCEURL_PREFIX_SIZE = 17;
      85       24675 :     const char      RESOURCEURL_PREFIX[] = "private:resource/";
      86             : 
      87       49350 :     if (( aResourceURL.startsWith( RESOURCEURL_PREFIX ) ) &&
      88       24675 :         ( aResourceURL.getLength() > RESOURCEURL_PREFIX_SIZE ))
      89             :     {
      90       24675 :         OUString aTmpStr( aResourceURL.copy( RESOURCEURL_PREFIX_SIZE ));
      91       24675 :         sal_Int32 nToken = 0;
      92       24675 :         sal_Int32 nPart  = 0;
      93       49350 :         do
      94             :         {
      95       54277 :             OUString sToken = aTmpStr.getToken( 0, '/', nToken);
      96       54277 :             if ( !sToken.isEmpty() )
      97             :             {
      98       54277 :                 if ( nPart == 0 )
      99       24675 :                     aType = sToken;
     100       29602 :                 else if ( nPart == 1 )
     101       24675 :                     aName = sToken;
     102             :                 else
     103        4927 :                     break;
     104       49350 :                 nPart++;
     105       49350 :             }
     106             :         }
     107       74025 :         while( nToken >=0 );
     108             :     }
     109       24675 : }
     110             : 
     111         347 : class FWI_DLLPUBLIC LanguageGuessingHelper
     112             : {
     113             :     mutable ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLanguageGuessing >    m_xLanguageGuesser;
     114             :     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
     115             : 
     116             : public:
     117         347 :     LanguageGuessingHelper(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _xContext) : m_xContext(_xContext){}
     118             : 
     119             :     ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLanguageGuessing >  GetGuesser() const;
     120             : };
     121             : 
     122             : FWI_DLLPUBLIC OUString RetrieveLabelFromCommand( const OUString& aCmdURL
     123             :             ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&    _xContext
     124             :             ,::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >&        _xUICommandLabels
     125             :             ,const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& _xFrame
     126             :             ,OUString& _rModuleIdentifier
     127             :             ,bool& _rIni
     128             :             ,const sal_Char* _pName);
     129             : 
     130             : FWI_DLLPUBLIC void FillLangItems( std::set< OUString > &rLangItems,
     131             :         const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > &rxFrame,
     132             :         const LanguageGuessingHelper & rLangGuessHelper,
     133             :         SvtScriptType    nScriptType,
     134             :         const OUString & rCurLang,
     135             :         const OUString & rKeyboardLang,
     136             :         const OUString & rGuessedTextLang );
     137             : 
     138             : //It's common for an object to want to create and own a Broadcaster and set
     139             : //itself as a Listener on its own Broadcaster member.
     140             : 
     141             : //However, calling addListener on a Broadcaster means that the Broadcaster adds
     142             : //a reference to the Listener leading to an ownership cycle where the Listener
     143             : //owns the Broadcaster which "owns" the Listener.
     144             : 
     145             : //The WeakContainerListener allows breaking this cycle and retrofitting
     146             : //afflicted implentations fairly easily.
     147             : 
     148             : //OriginalListener owns the Broadcaster which "owns" the WeakContainerListener
     149             : //which forwards the events to the OriginalListener without taking ownership of
     150             : //it.
     151             : class WeakContainerListener : public ::cppu::WeakImplHelper1<com::sun::star::container::XContainerListener>
     152             : {
     153             :     private:
     154             :         com::sun::star::uno::WeakReference<com::sun::star::container::XContainerListener> mxOwner;
     155             : 
     156             :     public:
     157         806 :         WeakContainerListener(com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner)
     158         806 :             : mxOwner(xOwner)
     159             :         {
     160         806 :         }
     161             : 
     162        1564 :         virtual ~WeakContainerListener()
     163         782 :         {
     164        1564 :         }
     165             : 
     166             :         // container.XContainerListener
     167           0 :         virtual void SAL_CALL elementInserted(const com::sun::star::container::ContainerEvent& rEvent)
     168             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     169             :         {
     170             :             com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
     171           0 :                 com::sun::star::uno::UNO_QUERY);
     172           0 :             if (xOwner.is())
     173           0 :                 xOwner->elementInserted(rEvent);
     174           0 :         }
     175             : 
     176           0 :         virtual void SAL_CALL elementRemoved(const com::sun::star::container::ContainerEvent& rEvent)
     177             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     178             :         {
     179             :             com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
     180           0 :                 com::sun::star::uno::UNO_QUERY);
     181           0 :             if (xOwner.is())
     182           0 :                 xOwner->elementRemoved(rEvent);
     183           0 :         }
     184             : 
     185           0 :         virtual void SAL_CALL elementReplaced(const com::sun::star::container::ContainerEvent& rEvent)
     186             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     187             :         {
     188             :             com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
     189           0 :                 com::sun::star::uno::UNO_QUERY);
     190           0 :             if (xOwner.is())
     191           0 :                 xOwner->elementReplaced(rEvent);
     192           0 :         }
     193             : 
     194             :         // lang.XEventListener
     195           0 :         virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
     196             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     197             :         {
     198             :             com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
     199           0 :                 com::sun::star::uno::UNO_QUERY);
     200           0 :             if (xOwner.is())
     201           0 :                 xOwner->disposing(rEvent);
     202             : 
     203           0 :         }
     204             : };
     205             : 
     206             : class WeakChangesListener : public ::cppu::WeakImplHelper1<com::sun::star::util::XChangesListener>
     207             : {
     208             :     private:
     209             :         com::sun::star::uno::WeakReference<com::sun::star::util::XChangesListener> mxOwner;
     210             : 
     211             :     public:
     212        1631 :         WeakChangesListener(com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner)
     213        1631 :             : mxOwner(xOwner)
     214             :         {
     215        1631 :         }
     216             : 
     217        3254 :         virtual ~WeakChangesListener()
     218        1627 :         {
     219        3254 :         }
     220             : 
     221             :         // util.XChangesListener
     222           0 :         virtual void SAL_CALL changesOccurred(const com::sun::star::util::ChangesEvent& rEvent)
     223             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     224             :         {
     225             :             com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner(mxOwner.get(),
     226           0 :                 com::sun::star::uno::UNO_QUERY);
     227           0 :             if (xOwner.is())
     228           0 :                 xOwner->changesOccurred(rEvent);
     229           0 :         }
     230             : 
     231             :         // lang.XEventListener
     232           0 :         virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
     233             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     234             :         {
     235             :             com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner(mxOwner.get(),
     236           0 :                 com::sun::star::uno::UNO_QUERY);
     237           0 :             if (xOwner.is())
     238           0 :                 xOwner->disposing(rEvent);
     239             : 
     240           0 :         }
     241             : };
     242             : 
     243             : class WeakEventListener : public ::cppu::WeakImplHelper1<com::sun::star::lang::XEventListener>
     244             : {
     245             :     private:
     246             :         com::sun::star::uno::WeakReference<com::sun::star::lang::XEventListener> mxOwner;
     247             : 
     248             :     public:
     249             :         WeakEventListener(com::sun::star::uno::Reference<com::sun::star::lang::XEventListener> xOwner)
     250             :             : mxOwner(xOwner)
     251             :         {
     252             :         }
     253             : 
     254             :         virtual ~WeakEventListener()
     255             :         {
     256             :         }
     257             : 
     258             :         // lang.XEventListener
     259             :         virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
     260             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     261             :         {
     262             :             com::sun::star::uno::Reference<com::sun::star::lang::XEventListener> xOwner(mxOwner.get(),
     263             :                 com::sun::star::uno::UNO_QUERY);
     264             :             if (xOwner.is())
     265             :                 xOwner->disposing(rEvent);
     266             : 
     267             :         }
     268             : };
     269             : 
     270             : class WeakDocumentEventListener : public ::cppu::WeakImplHelper1<com::sun::star::document::XDocumentEventListener>
     271             : {
     272             :     private:
     273             :         com::sun::star::uno::WeakReference<com::sun::star::document::XDocumentEventListener> mxOwner;
     274             : 
     275             :     public:
     276          65 :         WeakDocumentEventListener(com::sun::star::uno::Reference<com::sun::star::document::XDocumentEventListener> xOwner)
     277          65 :             : mxOwner(xOwner)
     278             :         {
     279          65 :         }
     280             : 
     281         130 :         virtual ~WeakDocumentEventListener()
     282          65 :         {
     283         130 :         }
     284             : 
     285           0 :         virtual void SAL_CALL documentEventOccured(const com::sun::star::document::DocumentEvent& rEvent)
     286             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     287             :         {
     288             :             com::sun::star::uno::Reference<com::sun::star::document::XDocumentEventListener> xOwner(mxOwner.get(),
     289           0 :                 com::sun::star::uno::UNO_QUERY);
     290           0 :             if (xOwner.is())
     291           0 :                 xOwner->documentEventOccured(rEvent);
     292             : 
     293           0 :         }
     294             : 
     295             :         // lang.XEventListener
     296           0 :         virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
     297             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     298             :         {
     299             :             com::sun::star::uno::Reference<com::sun::star::document::XDocumentEventListener> xOwner(mxOwner.get(),
     300           0 :                 com::sun::star::uno::UNO_QUERY);
     301           0 :             if (xOwner.is())
     302           0 :                 xOwner->disposing(rEvent);
     303             : 
     304           0 :         }
     305             : };
     306             : 
     307             : } // namespace framework
     308             : 
     309             : #endif // INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
     310             : 
     311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11