1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "imestatuswindow.hxx"
32 #include <sfx2/app.hxx>
33 #include <sfx2/sfxsids.hrc>
35 #include "com/sun/star/beans/PropertyState.hpp"
36 #include "com/sun/star/beans/PropertyValue.hpp"
37 #include "com/sun/star/beans/XPropertySet.hpp"
38 #include "com/sun/star/lang/DisposedException.hpp"
39 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
40 #include "com/sun/star/uno/Any.hxx"
41 #include "com/sun/star/uno/Exception.hpp"
42 #include "com/sun/star/uno/Reference.hxx"
43 #include "com/sun/star/uno/RuntimeException.hpp"
44 #include "com/sun/star/uno/Sequence.hxx"
45 #include "com/sun/star/util/XChangesBatch.hpp"
46 #include "osl/diagnose.h"
47 #include "osl/mutex.hxx"
48 #include "rtl/ustring.h"
49 #include "rtl/ustring.hxx"
50 #include "sal/types.h"
51 #include "vcl/svapp.hxx"
53 //TO-Do, merge into framework/inc/helpers/mischelpers.hxx and deliver
54 class WeakPropertyChangeListener
: public ::cppu::WeakImplHelper1
<com::sun::star::beans::XPropertyChangeListener
>
57 com::sun::star::uno::WeakReference
<com::sun::star::beans::XPropertyChangeListener
> mxOwner
;
60 WeakPropertyChangeListener(com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner
)
65 virtual ~WeakPropertyChangeListener()
69 virtual void SAL_CALL
propertyChange(const com::sun::star::beans::PropertyChangeEvent
&rEvent
)
70 throw(com::sun::star::uno::RuntimeException
)
72 com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner(mxOwner
.get(),
73 com::sun::star::uno::UNO_QUERY
);
75 xOwner
->propertyChange(rEvent
);
79 // lang.XEventListener
80 virtual void SAL_CALL
disposing(const com::sun::star::lang::EventObject
& rEvent
)
81 throw(com::sun::star::uno::RuntimeException
)
83 com::sun::star::uno::Reference
<com::sun::star::beans::XPropertyChangeListener
> xOwner(mxOwner
.get(),
84 com::sun::star::uno::UNO_QUERY
);
86 xOwner
->disposing(rEvent
);
91 namespace css
= com::sun::star
;
93 using sfx2::appl::ImeStatusWindow
;
95 ImeStatusWindow::ImeStatusWindow(
96 css::uno::Reference
< css::lang::XMultiServiceFactory
> const &
98 m_xServiceFactory(rServiceFactory
),
102 void ImeStatusWindow::init()
104 if (Application::CanToggleImeStatusWindow())
107 sal_Bool bShow
= sal_Bool();
108 if (getConfig()->getPropertyValue(
109 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
110 "ShowStatusWindow")))
112 Application::ShowImeStatusWindow(bShow
);
114 catch (css::uno::Exception
&)
116 OSL_FAIL("com.sun.star.uno.Exception");
117 // Degrade gracefully and use the VCL-supplied default if no
118 // configuration is available.
122 bool ImeStatusWindow::isShowing()
126 sal_Bool bShow
= sal_Bool();
127 if (getConfig()->getPropertyValue(
128 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")))
132 catch (css::uno::Exception
&)
134 OSL_FAIL("com.sun.star.uno.Exception");
135 // Degrade gracefully and use the VCL-supplied default if no
136 // configuration is available.
138 return Application::GetShowImeStatusWindowDefault();
141 void ImeStatusWindow::show(bool bShow
)
145 css::uno::Reference
< css::beans::XPropertySet
> xConfig(getConfig());
146 xConfig
->setPropertyValue(
147 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
148 css::uno::makeAny(static_cast< sal_Bool
>(bShow
)));
149 css::uno::Reference
< css::util::XChangesBatch
> xCommit(
150 xConfig
, css::uno::UNO_QUERY
);
151 // Degrade gracefully by not saving the settings permanently:
153 xCommit
->commitChanges();
154 // Alternatively, setting the VCL status could be done even if updating
155 // the configuration failed:
156 Application::ShowImeStatusWindow(bShow
);
158 catch (css::uno::Exception
&)
160 OSL_FAIL("com.sun.star.uno.Exception");
164 bool ImeStatusWindow::canToggle() const
166 return Application::CanToggleImeStatusWindow();
169 ImeStatusWindow::~ImeStatusWindow()
171 if (m_xConfig
.is() && m_xConfigListener
.is())
172 // We should never get here, but just in case...
175 m_xConfig
->removePropertyChangeListener(
176 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
179 catch (css::uno::Exception
&)
181 OSL_FAIL("com.sun.star.uno.RuntimeException");
185 void SAL_CALL
ImeStatusWindow::disposing(css::lang::EventObject
const & )
186 throw (css::uno::RuntimeException
)
188 osl::MutexGuard
aGuard(m_aMutex
);
194 ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent
const & )
195 throw (css::uno::RuntimeException
)
197 SolarMutexGuard aGuard
;
198 SfxApplication
* pApp
= SfxApplication::Get();
200 pApp
->Invalidate(SID_SHOW_IME_STATUS_WINDOW
);
203 css::uno::Reference
< css::beans::XPropertySet
> ImeStatusWindow::getConfig()
205 css::uno::Reference
< css::beans::XPropertySet
> xConfig
;
208 osl::MutexGuard
aGuard(m_aMutex
);
212 throw css::lang::DisposedException();
213 if (!m_xServiceFactory
.is())
214 throw css::uno::RuntimeException(
216 RTL_CONSTASCII_USTRINGPARAM(
217 "null comphelper::getProcessServiceFactory")),
219 css::uno::Reference
< css::lang::XMultiServiceFactory
> xProvider(
220 m_xServiceFactory
->createInstance(
222 RTL_CONSTASCII_USTRINGPARAM(
223 "com.sun.star.configuration.ConfigurationProvider"))),
224 css::uno::UNO_QUERY
);
226 throw css::uno::RuntimeException(
227 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
228 "null com.sun.star.configuration."
229 "ConfigurationProvider")),
231 css::beans::PropertyValue
aArg(
232 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
235 RTL_CONSTASCII_USTRINGPARAM(
236 "/org.openoffice.Office.Common/I18N/InputMethod"))),
237 css::beans::PropertyState_DIRECT_VALUE
);
238 css::uno::Sequence
< css::uno::Any
> aArgs(1);
241 = css::uno::Reference
< css::beans::XPropertySet
>(
242 xProvider
->createInstanceWithArguments(
244 RTL_CONSTASCII_USTRINGPARAM(
245 "com.sun.star.configuration.ConfigurationUpdateAccess")),
247 css::uno::UNO_QUERY
);
249 throw css::uno::RuntimeException(
250 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
251 "null com.sun.star.configuration."
252 "ConfigurationUpdateAccess")),
260 // Exceptions here could be handled individually, to support graceful
261 // degradation (no update notification mechanism in this case---but also
262 // no dispose notifications):
263 m_xConfigListener
= new WeakPropertyChangeListener(this);
264 xConfig
->addPropertyChangeListener(
265 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */