LCOV - code coverage report
Current view: top level - sfx2/source/appl - imestatuswindow.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 38 86 44.2 %
Date: 2015-06-13 12:38:46 Functions: 7 15 46.7 %
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             : 
      21             : #include "imestatuswindow.hxx"
      22             : 
      23             : #include <sfx2/app.hxx>
      24             : #include <sfx2/sfxsids.hrc>
      25             : 
      26             : #include <com/sun/star/beans/PropertyState.hpp>
      27             : #include <com/sun/star/beans/PropertyValue.hpp>
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      30             : #include <com/sun/star/lang/DisposedException.hpp>
      31             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      32             : #include <com/sun/star/uno/Any.hxx>
      33             : #include <com/sun/star/uno/Exception.hpp>
      34             : #include <com/sun/star/uno/Reference.hxx>
      35             : #include <com/sun/star/uno/RuntimeException.hpp>
      36             : #include <com/sun/star/uno/Sequence.hxx>
      37             : #include <com/sun/star/util/XChangesBatch.hpp>
      38             : #include <osl/diagnose.h>
      39             : #include <osl/mutex.hxx>
      40             : #include <rtl/ustring.h>
      41             : #include <rtl/ustring.hxx>
      42             : #include <sal/types.h>
      43             : #include <vcl/svapp.hxx>
      44             : 
      45             : //TO-Do, merge into framework/inc/helpers/mischelpers.hxx and deliver
      46             : class WeakPropertyChangeListener : public ::cppu::WeakImplHelper1<com::sun::star::beans::XPropertyChangeListener>
      47             : {
      48             :     private:
      49             :         com::sun::star::uno::WeakReference<com::sun::star::beans::XPropertyChangeListener> mxOwner;
      50             : 
      51             :     public:
      52           3 :         WeakPropertyChangeListener(com::sun::star::uno::Reference<com::sun::star::beans::XPropertyChangeListener> xOwner)
      53           3 :             : mxOwner(xOwner)
      54             :         {
      55           3 :         }
      56             : 
      57           0 :         virtual ~WeakPropertyChangeListener()
      58           0 :         {
      59           0 :         }
      60             : 
      61           0 :         virtual void SAL_CALL propertyChange(const com::sun::star::beans::PropertyChangeEvent &rEvent )
      62             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      63             :         {
      64             :             com::sun::star::uno::Reference<com::sun::star::beans::XPropertyChangeListener> xOwner(mxOwner.get(),
      65           0 :                 com::sun::star::uno::UNO_QUERY);
      66           0 :             if (xOwner.is())
      67           0 :                 xOwner->propertyChange(rEvent);
      68             : 
      69           0 :         }
      70             : 
      71             :         // lang.XEventListener
      72           0 :         virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
      73             :             throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
      74             :         {
      75             :             com::sun::star::uno::Reference<com::sun::star::beans::XPropertyChangeListener> xOwner(mxOwner.get(),
      76           0 :                 com::sun::star::uno::UNO_QUERY);
      77           0 :             if (xOwner.is())
      78           0 :                 xOwner->disposing(rEvent);
      79             : 
      80           0 :         }
      81             : };
      82             : 
      83             : using sfx2::appl::ImeStatusWindow;
      84             : 
      85         208 : ImeStatusWindow::ImeStatusWindow(
      86             :     css::uno::Reference< css::uno::XComponentContext > const & rxContext):
      87             :     m_xContext(rxContext),
      88         208 :     m_bDisposed(false)
      89         208 : {}
      90             : 
      91         208 : void ImeStatusWindow::init()
      92             : {
      93         208 :     if (Application::CanToggleImeStatusWindow())
      94             :         try
      95             :         {
      96             :             bool bShow;
      97           3 :             if (getConfig()->getPropertyValue(OUString("ShowStatusWindow")) >>= bShow)
      98           0 :                 Application::ShowImeStatusWindow(bShow);
      99             :         }
     100           0 :         catch (css::uno::Exception &)
     101             :         {
     102             :             OSL_FAIL("com.sun.star.uno.Exception");
     103             :             // Degrade gracefully and use the VCL-supplied default if no
     104             :             // configuration is available.
     105             :         }
     106         208 : }
     107             : 
     108           0 : bool ImeStatusWindow::isShowing()
     109             : {
     110             :     try
     111             :     {
     112           0 :         bool bShow(false);
     113           0 :         if (getConfig()->getPropertyValue(OUString("ShowStatusWindow")) >>= bShow)
     114           0 :             return bShow;
     115             :     }
     116           0 :     catch (css::uno::Exception &)
     117             :     {
     118             :         OSL_FAIL("com.sun.star.uno.Exception");
     119             :         // Degrade gracefully and use the VCL-supplied default if no
     120             :         // configuration is available.
     121             :     }
     122           0 :     return Application::GetShowImeStatusWindowDefault();
     123             : }
     124             : 
     125           0 : void ImeStatusWindow::show(bool bShow)
     126             : {
     127             :     try
     128             :     {
     129           0 :         css::uno::Reference< css::beans::XPropertySet > xConfig(getConfig());
     130           0 :         xConfig->setPropertyValue(
     131             :             OUString("ShowStatusWindow"),
     132           0 :             css::uno::makeAny(bShow));
     133             :         css::uno::Reference< css::util::XChangesBatch > xCommit(
     134           0 :             xConfig, css::uno::UNO_QUERY);
     135             :         // Degrade gracefully by not saving the settings permanently:
     136           0 :         if (xCommit.is())
     137           0 :             xCommit->commitChanges();
     138             :         // Alternatively, setting the VCL status could be done even if updating
     139             :         // the configuration failed:
     140           0 :         Application::ShowImeStatusWindow(bShow);
     141             :     }
     142           0 :     catch (css::uno::Exception &)
     143             :     {
     144             :         OSL_FAIL("com.sun.star.uno.Exception");
     145             :     }
     146           0 : }
     147             : 
     148           5 : bool ImeStatusWindow::canToggle()
     149             : {
     150           5 :     return Application::CanToggleImeStatusWindow();
     151             : }
     152             : 
     153         357 : ImeStatusWindow::~ImeStatusWindow()
     154             : {
     155         119 :     if (m_xConfig.is() && m_xConfigListener.is())
     156             :         // We should never get here, but just in case...
     157             :         try
     158             :         {
     159           0 :             m_xConfig->removePropertyChangeListener(
     160             :                 OUString("ShowStatusWindow"),
     161           0 :                 m_xConfigListener);
     162             :         }
     163           0 :         catch (css::uno::Exception &)
     164             :         {
     165             :             OSL_FAIL("com.sun.star.uno.RuntimeException");
     166             :         }
     167         238 : }
     168             : 
     169           0 : void SAL_CALL ImeStatusWindow::disposing(css::lang::EventObject const & )
     170             :     throw (css::uno::RuntimeException, std::exception)
     171             : {
     172           0 :     osl::MutexGuard aGuard(m_aMutex);
     173           0 :     m_xConfig = 0;
     174           0 :     m_bDisposed = true;
     175           0 : }
     176             : 
     177             : void SAL_CALL
     178           0 : ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent const & )
     179             :     throw (css::uno::RuntimeException, std::exception)
     180             : {
     181           0 :     SolarMutexGuard aGuard;
     182           0 :     SfxApplication* pApp = SfxApplication::Get();
     183           0 :     if (pApp)
     184           0 :     pApp->Invalidate(SID_SHOW_IME_STATUS_WINDOW);
     185           0 : }
     186             : 
     187           3 : css::uno::Reference< css::beans::XPropertySet > ImeStatusWindow::getConfig()
     188             : {
     189           3 :     css::uno::Reference< css::beans::XPropertySet > xConfig;
     190           3 :     bool bAdd = false;
     191             :     {
     192           3 :         osl::MutexGuard aGuard(m_aMutex);
     193           3 :         if (!m_xConfig.is())
     194             :         {
     195           3 :             if (m_bDisposed)
     196           0 :                 throw css::lang::DisposedException();
     197           3 :             if (!m_xContext.is())
     198             :                 throw css::uno::RuntimeException(
     199             :                     OUString(
     200             :                             "null comphelper::getProcessServiceFactory"),
     201           0 :                     0);
     202             :             css::uno::Reference< css::lang::XMultiServiceFactory > xProvider =
     203           3 :                 css::configuration::theDefaultProvider::get( m_xContext );
     204             :             css::beans::PropertyValue aArg(
     205             :                 OUString("nodepath"), -1,
     206             :                 css::uno::makeAny(
     207             :                     OUString(
     208             :                             "/org.openoffice.Office.Common/I18N/InputMethod")),
     209           6 :                 css::beans::PropertyState_DIRECT_VALUE);
     210           6 :             css::uno::Sequence< css::uno::Any > aArgs(1);
     211           3 :             aArgs[0] <<= aArg;
     212             :             m_xConfig
     213           6 :                 = css::uno::Reference< css::beans::XPropertySet >(
     214           3 :                     xProvider->createInstanceWithArguments(
     215             :                         OUString(
     216             :                        "com.sun.star.configuration.ConfigurationUpdateAccess"),
     217           3 :                         aArgs),
     218           3 :                     css::uno::UNO_QUERY);
     219           3 :             if (!m_xConfig.is())
     220             :                 throw css::uno::RuntimeException(
     221             :                     OUString(
     222             :                                       "null com.sun.star.configuration."
     223             :                                       "ConfigurationUpdateAccess"),
     224           0 :                     0);
     225           6 :             bAdd = true;
     226             :         }
     227           3 :         xConfig = m_xConfig;
     228             :     }
     229           3 :     if (bAdd)
     230             :     {
     231             :         // Exceptions here could be handled individually, to support graceful
     232             :         // degradation (no update notification mechanism in this case---but also
     233             :         // no dispose notifications):
     234           3 :         m_xConfigListener = new WeakPropertyChangeListener(this);
     235           3 :         xConfig->addPropertyChangeListener(
     236             :             OUString("ShowStatusWindow"),
     237           3 :             m_xConfigListener);
     238             :     }
     239           3 :     return xConfig;
     240             : }
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11