1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "imestatuswindow.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <sfx2/app.hxx>
30cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "com/sun/star/beans/PropertyState.hpp"
33cdf0e10cSrcweir #include "com/sun/star/beans/PropertyValue.hpp"
34cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
35cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
36cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
37cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
38cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
39cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
40cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
41cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
42cdf0e10cSrcweir #include "com/sun/star/util/XChangesBatch.hpp"
43cdf0e10cSrcweir #include "osl/diagnose.h"
44cdf0e10cSrcweir #include "osl/mutex.hxx"
45cdf0e10cSrcweir #include "rtl/ustring.h"
46cdf0e10cSrcweir #include "rtl/ustring.hxx"
47cdf0e10cSrcweir #include "sal/types.h"
48cdf0e10cSrcweir #include "vcl/svapp.hxx"
49cdf0e10cSrcweir #include "vos/mutex.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace css = com::sun::star;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir using sfx2::appl::ImeStatusWindow;
54cdf0e10cSrcweir 
ImeStatusWindow(css::uno::Reference<css::lang::XMultiServiceFactory> const & rServiceFactory)55cdf0e10cSrcweir ImeStatusWindow::ImeStatusWindow(
56cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > const &
57cdf0e10cSrcweir         rServiceFactory):
58cdf0e10cSrcweir     m_xServiceFactory(rServiceFactory),
59cdf0e10cSrcweir     m_bDisposed(false)
60cdf0e10cSrcweir {}
61cdf0e10cSrcweir 
init()62cdf0e10cSrcweir void ImeStatusWindow::init()
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     if (Application::CanToggleImeStatusWindow())
65cdf0e10cSrcweir         try
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir             sal_Bool bShow = sal_Bool();
68cdf0e10cSrcweir             if (getConfig()->getPropertyValue(
69cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
70cdf0e10cSrcweir                                       "ShowStatusWindow")))
71cdf0e10cSrcweir                 >>= bShow)
72cdf0e10cSrcweir                 Application::ShowImeStatusWindow(bShow);
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         catch (css::uno::Exception &)
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             OSL_ENSURE(false, "com.sun.star.uno.Exception");
77cdf0e10cSrcweir             // Degrade gracefully and use the VCL-supplied default if no
78cdf0e10cSrcweir             // configuration is available.
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
isShowing()82cdf0e10cSrcweir bool ImeStatusWindow::isShowing()
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     try
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         sal_Bool bShow = sal_Bool();
87cdf0e10cSrcweir         if (getConfig()->getPropertyValue(
88cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")))
89cdf0e10cSrcweir             >>= bShow)
90cdf0e10cSrcweir             return bShow;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir     catch (css::uno::Exception &)
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         OSL_ENSURE(false, "com.sun.star.uno.Exception");
95cdf0e10cSrcweir         // Degrade gracefully and use the VCL-supplied default if no
96cdf0e10cSrcweir         // configuration is available.
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir     return Application::GetShowImeStatusWindowDefault();
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
show(bool bShow)101cdf0e10cSrcweir void ImeStatusWindow::show(bool bShow)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     try
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         css::uno::Reference< css::beans::XPropertySet > xConfig(getConfig());
106cdf0e10cSrcweir         xConfig->setPropertyValue(
107cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
108cdf0e10cSrcweir             css::uno::makeAny(static_cast< sal_Bool >(bShow)));
109cdf0e10cSrcweir         css::uno::Reference< css::util::XChangesBatch > xCommit(
110cdf0e10cSrcweir             xConfig, css::uno::UNO_QUERY);
111cdf0e10cSrcweir         // Degrade gracefully by not saving the settings permanently:
112cdf0e10cSrcweir         if (xCommit.is())
113cdf0e10cSrcweir             xCommit->commitChanges();
114cdf0e10cSrcweir         // Alternatively, setting the VCL status could be done even if updating
115cdf0e10cSrcweir         // the configuration failed:
116cdf0e10cSrcweir         Application::ShowImeStatusWindow(bShow);
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir     catch (css::uno::Exception &)
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         OSL_ENSURE(false, "com.sun.star.uno.Exception");
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
canToggle() const124cdf0e10cSrcweir bool ImeStatusWindow::canToggle() const
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     return Application::CanToggleImeStatusWindow();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
~ImeStatusWindow()129cdf0e10cSrcweir ImeStatusWindow::~ImeStatusWindow()
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     if (m_xConfig.is())
132cdf0e10cSrcweir         // We should never get here, but just in case...
133cdf0e10cSrcweir         try
134cdf0e10cSrcweir         {
135cdf0e10cSrcweir             m_xConfig->removePropertyChangeListener(
136cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
137cdf0e10cSrcweir                 this);
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         catch (css::uno::Exception &)
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             OSL_ENSURE(false, "com.sun.star.uno.RuntimeException");
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
disposing(css::lang::EventObject const &)145cdf0e10cSrcweir void SAL_CALL ImeStatusWindow::disposing(css::lang::EventObject const & )
146cdf0e10cSrcweir     throw (css::uno::RuntimeException)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
149cdf0e10cSrcweir     m_xConfig = 0;
150cdf0e10cSrcweir     m_bDisposed = true;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir void SAL_CALL
propertyChange(css::beans::PropertyChangeEvent const &)154cdf0e10cSrcweir ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent const & )
155cdf0e10cSrcweir     throw (css::uno::RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     vos::OGuard aGuard(Application::GetSolarMutex());
158cdf0e10cSrcweir     SfxApplication* pApp = SfxApplication::Get();
159cdf0e10cSrcweir     if (pApp)
160cdf0e10cSrcweir 	pApp->Invalidate(SID_SHOW_IME_STATUS_WINDOW);
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
getConfig()163cdf0e10cSrcweir css::uno::Reference< css::beans::XPropertySet > ImeStatusWindow::getConfig()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xConfig;
166cdf0e10cSrcweir     bool bAdd = false;
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         osl::MutexGuard aGuard(m_aMutex);
169cdf0e10cSrcweir         if (!m_xConfig.is())
170cdf0e10cSrcweir         {
171cdf0e10cSrcweir             if (m_bDisposed)
172cdf0e10cSrcweir                 throw css::lang::DisposedException();
173cdf0e10cSrcweir             if (!m_xServiceFactory.is())
174cdf0e10cSrcweir                 throw css::uno::RuntimeException(
175cdf0e10cSrcweir                     rtl::OUString(
176cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
177cdf0e10cSrcweir                             "null comphelper::getProcessServiceFactory")),
178cdf0e10cSrcweir                     0);
179cdf0e10cSrcweir             css::uno::Reference< css::lang::XMultiServiceFactory > xProvider(
180cdf0e10cSrcweir                 m_xServiceFactory->createInstance(
181cdf0e10cSrcweir                     rtl::OUString(
182cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
183cdf0e10cSrcweir                           "com.sun.star.configuration.ConfigurationProvider"))),
184cdf0e10cSrcweir                 css::uno::UNO_QUERY);
185cdf0e10cSrcweir             if (!xProvider.is())
186cdf0e10cSrcweir                 throw css::uno::RuntimeException(
187cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
188cdf0e10cSrcweir                                       "null com.sun.star.configuration."
189cdf0e10cSrcweir                                       "ConfigurationProvider")),
190cdf0e10cSrcweir                     0);
191cdf0e10cSrcweir             css::beans::PropertyValue aArg(
192cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
193cdf0e10cSrcweir                 css::uno::makeAny(
194cdf0e10cSrcweir                     rtl::OUString(
195cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
196cdf0e10cSrcweir                             "/org.openoffice.Office.Common/I18N/InputMethod"))),
197cdf0e10cSrcweir                 css::beans::PropertyState_DIRECT_VALUE);
198cdf0e10cSrcweir             css::uno::Sequence< css::uno::Any > aArgs(1);
199cdf0e10cSrcweir             aArgs[0] <<= aArg;
200cdf0e10cSrcweir             m_xConfig
201cdf0e10cSrcweir                 = css::uno::Reference< css::beans::XPropertySet >(
202cdf0e10cSrcweir                     xProvider->createInstanceWithArguments(
203cdf0e10cSrcweir                         rtl::OUString(
204cdf0e10cSrcweir                             RTL_CONSTASCII_USTRINGPARAM(
205cdf0e10cSrcweir                        "com.sun.star.configuration.ConfigurationUpdateAccess")),
206cdf0e10cSrcweir                         aArgs),
207cdf0e10cSrcweir                     css::uno::UNO_QUERY);
208cdf0e10cSrcweir             if (!m_xConfig.is())
209cdf0e10cSrcweir                 throw css::uno::RuntimeException(
210cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
211cdf0e10cSrcweir                                       "null com.sun.star.configuration."
212cdf0e10cSrcweir                                       "ConfigurationUpdateAccess")),
213cdf0e10cSrcweir                     0);
214cdf0e10cSrcweir             bAdd = true;
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir         xConfig = m_xConfig;
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir     if (bAdd)
219cdf0e10cSrcweir         // Exceptions here could be handled individually, to support graceful
220cdf0e10cSrcweir         // degradation (no update notification mechanism in this case---but also
221cdf0e10cSrcweir         // no dispose notifications):
222cdf0e10cSrcweir         xConfig->addPropertyChangeListener(
223cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
224cdf0e10cSrcweir             this);
225cdf0e10cSrcweir     return xConfig;
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
228