1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "imestatuswindow.hxx"
23 #include <sfx2/app.hxx>
24 #include <sfx2/sfxsids.hrc>
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>
45 //TO-Do, merge into framework/inc/helpers/mischelpers.hxx and deliver
46 class WeakPropertyChangeListener
: public ::cppu::WeakImplHelper1
<com::sun::star::beans::XPropertyChangeListener
>
49 com::sun::star::uno::WeakReference
<com::sun::star::beans::XPropertyChangeListener
> mxOwner
;
52 WeakPropertyChangeListener(com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner
)
57 virtual ~WeakPropertyChangeListener()
61 virtual void SAL_CALL
propertyChange(const com::sun::star::beans::PropertyChangeEvent
&rEvent
)
62 throw(com::sun::star::uno::RuntimeException
)
64 com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner(mxOwner
.get(),
65 com::sun::star::uno::UNO_QUERY
);
67 xOwner
->propertyChange(rEvent
);
71 // lang.XEventListener
72 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
73 throw(com::sun::star::uno::RuntimeException
)
75 com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner(mxOwner
.get(),
76 com::sun::star::uno::UNO_QUERY
);
78 xOwner
->disposing(rEvent
);
83 using sfx2::appl::ImeStatusWindow
;
85 ImeStatusWindow::ImeStatusWindow(
86 css::uno::Reference
< css::uno::XComponentContext
> const & rxContext
):
87 m_xContext(rxContext
),
91 void ImeStatusWindow::init()
93 if (Application::CanToggleImeStatusWindow())
96 sal_Bool bShow
= sal_Bool();
97 if (getConfig()->getPropertyValue(
101 Application::ShowImeStatusWindow(bShow
);
103 catch (css::uno::Exception
&)
105 OSL_FAIL("com.sun.star.uno.Exception");
106 // Degrade gracefully and use the VCL-supplied default if no
107 // configuration is available.
111 bool ImeStatusWindow::isShowing()
115 sal_Bool bShow
= sal_Bool();
116 if (getConfig()->getPropertyValue(
117 OUString("ShowStatusWindow"))
121 catch (css::uno::Exception
&)
123 OSL_FAIL("com.sun.star.uno.Exception");
124 // Degrade gracefully and use the VCL-supplied default if no
125 // configuration is available.
127 return Application::GetShowImeStatusWindowDefault();
130 void ImeStatusWindow::show(bool bShow
)
134 css::uno::Reference
< css::beans::XPropertySet
> xConfig(getConfig());
135 xConfig
->setPropertyValue(
136 OUString("ShowStatusWindow"),
137 css::uno::makeAny(static_cast< sal_Bool
>(bShow
)));
138 css::uno::Reference
< css::util::XChangesBatch
> xCommit(
139 xConfig
, css::uno::UNO_QUERY
);
140 // Degrade gracefully by not saving the settings permanently:
142 xCommit
->commitChanges();
143 // Alternatively, setting the VCL status could be done even if updating
144 // the configuration failed:
145 Application::ShowImeStatusWindow(bShow
);
147 catch (css::uno::Exception
&)
149 OSL_FAIL("com.sun.star.uno.Exception");
153 bool ImeStatusWindow::canToggle() const
155 return Application::CanToggleImeStatusWindow();
158 ImeStatusWindow::~ImeStatusWindow()
160 if (m_xConfig
.is() && m_xConfigListener
.is())
161 // We should never get here, but just in case...
164 m_xConfig
->removePropertyChangeListener(
165 OUString("ShowStatusWindow"),
168 catch (css::uno::Exception
&)
170 OSL_FAIL("com.sun.star.uno.RuntimeException");
174 void SAL_CALL
ImeStatusWindow::disposing(css::lang::EventObject
const & )
175 throw (css::uno::RuntimeException
)
177 osl::MutexGuard
aGuard(m_aMutex
);
183 ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent
const & )
184 throw (css::uno::RuntimeException
)
186 SolarMutexGuard aGuard
;
187 SfxApplication
* pApp
= SfxApplication::Get();
189 pApp
->Invalidate(SID_SHOW_IME_STATUS_WINDOW
);
192 css::uno::Reference
< css::beans::XPropertySet
> ImeStatusWindow::getConfig()
194 css::uno::Reference
< css::beans::XPropertySet
> xConfig
;
197 osl::MutexGuard
aGuard(m_aMutex
);
201 throw css::lang::DisposedException();
202 if (!m_xContext
.is())
203 throw css::uno::RuntimeException(
205 "null comphelper::getProcessServiceFactory"),
207 css::uno::Reference
< css::lang::XMultiServiceFactory
> xProvider
=
208 css::configuration::theDefaultProvider::get( m_xContext
);
209 css::beans::PropertyValue
aArg(
210 OUString("nodepath"), -1,
213 "/org.openoffice.Office.Common/I18N/InputMethod")),
214 css::beans::PropertyState_DIRECT_VALUE
);
215 css::uno::Sequence
< css::uno::Any
> aArgs(1);
218 = css::uno::Reference
< css::beans::XPropertySet
>(
219 xProvider
->createInstanceWithArguments(
221 "com.sun.star.configuration.ConfigurationUpdateAccess"),
223 css::uno::UNO_QUERY
);
225 throw css::uno::RuntimeException(
227 "null com.sun.star.configuration."
228 "ConfigurationUpdateAccess"),
236 // Exceptions here could be handled individually, to support graceful
237 // degradation (no update notification mechanism in this case---but also
238 // no dispose notifications):
239 m_xConfigListener
= new WeakPropertyChangeListener(this);
240 xConfig
->addPropertyChangeListener(
241 OUString("ShowStatusWindow"),
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */