LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/appl - imestatuswindow.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 9 90 10.0 %
Date: 2012-12-17 Functions: 4 15 26.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           0 :         WeakPropertyChangeListener(com::sun::star::uno::Reference<com::sun::star::beans::XPropertyChangeListener> xOwner)
      53           0 :             : mxOwner(xOwner)
      54             :         {
      55           0 :         }
      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)
      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)
      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          72 : ImeStatusWindow::ImeStatusWindow(
      86             :     css::uno::Reference< css::uno::XComponentContext > const & rxContext):
      87             :     m_xContext(rxContext),
      88          72 :     m_bDisposed(false)
      89          72 : {}
      90             : 
      91          72 : void ImeStatusWindow::init()
      92             : {
      93          72 :     if (Application::CanToggleImeStatusWindow())
      94             :         try
      95             :         {
      96           0 :             sal_Bool bShow = sal_Bool();
      97           0 :             if (getConfig()->getPropertyValue(
      98             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
      99           0 :                                       "ShowStatusWindow")))
     100           0 :                 >>= bShow)
     101           0 :                 Application::ShowImeStatusWindow(bShow);
     102             :         }
     103           0 :         catch (css::uno::Exception &)
     104             :         {
     105             :             OSL_FAIL("com.sun.star.uno.Exception");
     106             :             // Degrade gracefully and use the VCL-supplied default if no
     107             :             // configuration is available.
     108             :         }
     109          72 : }
     110             : 
     111           0 : bool ImeStatusWindow::isShowing()
     112             : {
     113             :     try
     114             :     {
     115           0 :         sal_Bool bShow = sal_Bool();
     116           0 :         if (getConfig()->getPropertyValue(
     117           0 :                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")))
     118           0 :             >>= bShow)
     119           0 :             return bShow;
     120             :     }
     121           0 :     catch (css::uno::Exception &)
     122             :     {
     123             :         OSL_FAIL("com.sun.star.uno.Exception");
     124             :         // Degrade gracefully and use the VCL-supplied default if no
     125             :         // configuration is available.
     126             :     }
     127           0 :     return Application::GetShowImeStatusWindowDefault();
     128             : }
     129             : 
     130           0 : void ImeStatusWindow::show(bool bShow)
     131             : {
     132             :     try
     133             :     {
     134           0 :         css::uno::Reference< css::beans::XPropertySet > xConfig(getConfig());
     135           0 :         xConfig->setPropertyValue(
     136             :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
     137           0 :             css::uno::makeAny(static_cast< sal_Bool >(bShow)));
     138             :         css::uno::Reference< css::util::XChangesBatch > xCommit(
     139           0 :             xConfig, css::uno::UNO_QUERY);
     140             :         // Degrade gracefully by not saving the settings permanently:
     141           0 :         if (xCommit.is())
     142           0 :             xCommit->commitChanges();
     143             :         // Alternatively, setting the VCL status could be done even if updating
     144             :         // the configuration failed:
     145           0 :         Application::ShowImeStatusWindow(bShow);
     146             :     }
     147           0 :     catch (css::uno::Exception &)
     148             :     {
     149             :         OSL_FAIL("com.sun.star.uno.Exception");
     150             :     }
     151           0 : }
     152             : 
     153           0 : bool ImeStatusWindow::canToggle() const
     154             : {
     155           0 :     return Application::CanToggleImeStatusWindow();
     156             : }
     157             : 
     158          24 : ImeStatusWindow::~ImeStatusWindow()
     159             : {
     160           8 :     if (m_xConfig.is() && m_xConfigListener.is())
     161             :         // We should never get here, but just in case...
     162             :         try
     163             :         {
     164           0 :             m_xConfig->removePropertyChangeListener(
     165             :                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
     166           0 :                 m_xConfigListener);
     167             :         }
     168           0 :         catch (css::uno::Exception &)
     169             :         {
     170             :             OSL_FAIL("com.sun.star.uno.RuntimeException");
     171             :         }
     172          16 : }
     173             : 
     174           0 : void SAL_CALL ImeStatusWindow::disposing(css::lang::EventObject const & )
     175             :     throw (css::uno::RuntimeException)
     176             : {
     177           0 :     osl::MutexGuard aGuard(m_aMutex);
     178           0 :     m_xConfig = 0;
     179           0 :     m_bDisposed = true;
     180           0 : }
     181             : 
     182             : void SAL_CALL
     183           0 : ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent const & )
     184             :     throw (css::uno::RuntimeException)
     185             : {
     186           0 :     SolarMutexGuard aGuard;
     187           0 :     SfxApplication* pApp = SfxApplication::Get();
     188           0 :     if (pApp)
     189           0 :     pApp->Invalidate(SID_SHOW_IME_STATUS_WINDOW);
     190           0 : }
     191             : 
     192           0 : css::uno::Reference< css::beans::XPropertySet > ImeStatusWindow::getConfig()
     193             : {
     194           0 :     css::uno::Reference< css::beans::XPropertySet > xConfig;
     195           0 :     bool bAdd = false;
     196             :     {
     197           0 :         osl::MutexGuard aGuard(m_aMutex);
     198           0 :         if (!m_xConfig.is())
     199             :         {
     200           0 :             if (m_bDisposed)
     201           0 :                 throw css::lang::DisposedException();
     202           0 :             if (!m_xContext.is())
     203             :                 throw css::uno::RuntimeException(
     204             :                     rtl::OUString(
     205             :                         RTL_CONSTASCII_USTRINGPARAM(
     206             :                             "null comphelper::getProcessServiceFactory")),
     207           0 :                     0);
     208             :             css::uno::Reference< css::lang::XMultiServiceFactory > xProvider =
     209           0 :                 css::configuration::theDefaultProvider::get( m_xContext );
     210             :             css::beans::PropertyValue aArg(
     211             :                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
     212             :                 css::uno::makeAny(
     213             :                     rtl::OUString(
     214             :                         RTL_CONSTASCII_USTRINGPARAM(
     215             :                             "/org.openoffice.Office.Common/I18N/InputMethod"))),
     216           0 :                 css::beans::PropertyState_DIRECT_VALUE);
     217           0 :             css::uno::Sequence< css::uno::Any > aArgs(1);
     218           0 :             aArgs[0] <<= aArg;
     219             :             m_xConfig
     220             :                 = css::uno::Reference< css::beans::XPropertySet >(
     221           0 :                     xProvider->createInstanceWithArguments(
     222             :                         rtl::OUString(
     223             :                             RTL_CONSTASCII_USTRINGPARAM(
     224             :                        "com.sun.star.configuration.ConfigurationUpdateAccess")),
     225           0 :                         aArgs),
     226           0 :                     css::uno::UNO_QUERY);
     227           0 :             if (!m_xConfig.is())
     228             :                 throw css::uno::RuntimeException(
     229             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     230             :                                       "null com.sun.star.configuration."
     231             :                                       "ConfigurationUpdateAccess")),
     232           0 :                     0);
     233           0 :             bAdd = true;
     234             :         }
     235           0 :         xConfig = m_xConfig;
     236             :     }
     237           0 :     if (bAdd)
     238             :     {
     239             :         // Exceptions here could be handled individually, to support graceful
     240             :         // degradation (no update notification mechanism in this case---but also
     241             :         // no dispose notifications):
     242           0 :         m_xConfigListener = new WeakPropertyChangeListener(this);
     243           0 :         xConfig->addPropertyChangeListener(
     244             :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
     245           0 :             m_xConfigListener);
     246             :     }
     247           0 :     return xConfig;
     248             : }
     249             : 
     250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10