1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "imestatuswindow.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <sfx2/app.hxx>
34*cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "com/sun/star/beans/PropertyState.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/beans/PropertyValue.hpp"
38*cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
39*cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
41*cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
42*cdf0e10cSrcweir #include "com/sun/star/uno/Exception.hpp"
43*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
44*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
45*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
46*cdf0e10cSrcweir #include "com/sun/star/util/XChangesBatch.hpp"
47*cdf0e10cSrcweir #include "osl/diagnose.h"
48*cdf0e10cSrcweir #include "osl/mutex.hxx"
49*cdf0e10cSrcweir #include "rtl/ustring.h"
50*cdf0e10cSrcweir #include "rtl/ustring.hxx"
51*cdf0e10cSrcweir #include "sal/types.h"
52*cdf0e10cSrcweir #include "vcl/svapp.hxx"
53*cdf0e10cSrcweir #include "vos/mutex.hxx"
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace css = com::sun::star;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir using sfx2::appl::ImeStatusWindow;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir ImeStatusWindow::ImeStatusWindow(
60*cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > const &
61*cdf0e10cSrcweir         rServiceFactory):
62*cdf0e10cSrcweir     m_xServiceFactory(rServiceFactory),
63*cdf0e10cSrcweir     m_bDisposed(false)
64*cdf0e10cSrcweir {}
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir void ImeStatusWindow::init()
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     if (Application::CanToggleImeStatusWindow())
69*cdf0e10cSrcweir         try
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             sal_Bool bShow = sal_Bool();
72*cdf0e10cSrcweir             if (getConfig()->getPropertyValue(
73*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
74*cdf0e10cSrcweir                                       "ShowStatusWindow")))
75*cdf0e10cSrcweir                 >>= bShow)
76*cdf0e10cSrcweir                 Application::ShowImeStatusWindow(bShow);
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir         catch (css::uno::Exception &)
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir             OSL_ENSURE(false, "com.sun.star.uno.Exception");
81*cdf0e10cSrcweir             // Degrade gracefully and use the VCL-supplied default if no
82*cdf0e10cSrcweir             // configuration is available.
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir bool ImeStatusWindow::isShowing()
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     try
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         sal_Bool bShow = sal_Bool();
91*cdf0e10cSrcweir         if (getConfig()->getPropertyValue(
92*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")))
93*cdf0e10cSrcweir             >>= bShow)
94*cdf0e10cSrcweir             return bShow;
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir     catch (css::uno::Exception &)
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         OSL_ENSURE(false, "com.sun.star.uno.Exception");
99*cdf0e10cSrcweir         // Degrade gracefully and use the VCL-supplied default if no
100*cdf0e10cSrcweir         // configuration is available.
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir     return Application::GetShowImeStatusWindowDefault();
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void ImeStatusWindow::show(bool bShow)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     try
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         css::uno::Reference< css::beans::XPropertySet > xConfig(getConfig());
110*cdf0e10cSrcweir         xConfig->setPropertyValue(
111*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
112*cdf0e10cSrcweir             css::uno::makeAny(static_cast< sal_Bool >(bShow)));
113*cdf0e10cSrcweir         css::uno::Reference< css::util::XChangesBatch > xCommit(
114*cdf0e10cSrcweir             xConfig, css::uno::UNO_QUERY);
115*cdf0e10cSrcweir         // Degrade gracefully by not saving the settings permanently:
116*cdf0e10cSrcweir         if (xCommit.is())
117*cdf0e10cSrcweir             xCommit->commitChanges();
118*cdf0e10cSrcweir         // Alternatively, setting the VCL status could be done even if updating
119*cdf0e10cSrcweir         // the configuration failed:
120*cdf0e10cSrcweir         Application::ShowImeStatusWindow(bShow);
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir     catch (css::uno::Exception &)
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         OSL_ENSURE(false, "com.sun.star.uno.Exception");
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir bool ImeStatusWindow::canToggle() const
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir     return Application::CanToggleImeStatusWindow();
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir ImeStatusWindow::~ImeStatusWindow()
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir     if (m_xConfig.is())
136*cdf0e10cSrcweir         // We should never get here, but just in case...
137*cdf0e10cSrcweir         try
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             m_xConfig->removePropertyChangeListener(
140*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
141*cdf0e10cSrcweir                 this);
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir         catch (css::uno::Exception &)
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             OSL_ENSURE(false, "com.sun.star.uno.RuntimeException");
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir void SAL_CALL ImeStatusWindow::disposing(css::lang::EventObject const & )
150*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
153*cdf0e10cSrcweir     m_xConfig = 0;
154*cdf0e10cSrcweir     m_bDisposed = true;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir void SAL_CALL
158*cdf0e10cSrcweir ImeStatusWindow::propertyChange(css::beans::PropertyChangeEvent const & )
159*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir     vos::OGuard aGuard(Application::GetSolarMutex());
162*cdf0e10cSrcweir     SfxApplication* pApp = SfxApplication::Get();
163*cdf0e10cSrcweir     if (pApp)
164*cdf0e10cSrcweir 	pApp->Invalidate(SID_SHOW_IME_STATUS_WINDOW);
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir css::uno::Reference< css::beans::XPropertySet > ImeStatusWindow::getConfig()
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xConfig;
170*cdf0e10cSrcweir     bool bAdd = false;
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         osl::MutexGuard aGuard(m_aMutex);
173*cdf0e10cSrcweir         if (!m_xConfig.is())
174*cdf0e10cSrcweir         {
175*cdf0e10cSrcweir             if (m_bDisposed)
176*cdf0e10cSrcweir                 throw css::lang::DisposedException();
177*cdf0e10cSrcweir             if (!m_xServiceFactory.is())
178*cdf0e10cSrcweir                 throw css::uno::RuntimeException(
179*cdf0e10cSrcweir                     rtl::OUString(
180*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
181*cdf0e10cSrcweir                             "null comphelper::getProcessServiceFactory")),
182*cdf0e10cSrcweir                     0);
183*cdf0e10cSrcweir             css::uno::Reference< css::lang::XMultiServiceFactory > xProvider(
184*cdf0e10cSrcweir                 m_xServiceFactory->createInstance(
185*cdf0e10cSrcweir                     rtl::OUString(
186*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
187*cdf0e10cSrcweir                           "com.sun.star.configuration.ConfigurationProvider"))),
188*cdf0e10cSrcweir                 css::uno::UNO_QUERY);
189*cdf0e10cSrcweir             if (!xProvider.is())
190*cdf0e10cSrcweir                 throw css::uno::RuntimeException(
191*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
192*cdf0e10cSrcweir                                       "null com.sun.star.configuration."
193*cdf0e10cSrcweir                                       "ConfigurationProvider")),
194*cdf0e10cSrcweir                     0);
195*cdf0e10cSrcweir             css::beans::PropertyValue aArg(
196*cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), -1,
197*cdf0e10cSrcweir                 css::uno::makeAny(
198*cdf0e10cSrcweir                     rtl::OUString(
199*cdf0e10cSrcweir                         RTL_CONSTASCII_USTRINGPARAM(
200*cdf0e10cSrcweir                             "/org.openoffice.Office.Common/I18N/InputMethod"))),
201*cdf0e10cSrcweir                 css::beans::PropertyState_DIRECT_VALUE);
202*cdf0e10cSrcweir             css::uno::Sequence< css::uno::Any > aArgs(1);
203*cdf0e10cSrcweir             aArgs[0] <<= aArg;
204*cdf0e10cSrcweir             m_xConfig
205*cdf0e10cSrcweir                 = css::uno::Reference< css::beans::XPropertySet >(
206*cdf0e10cSrcweir                     xProvider->createInstanceWithArguments(
207*cdf0e10cSrcweir                         rtl::OUString(
208*cdf0e10cSrcweir                             RTL_CONSTASCII_USTRINGPARAM(
209*cdf0e10cSrcweir                        "com.sun.star.configuration.ConfigurationUpdateAccess")),
210*cdf0e10cSrcweir                         aArgs),
211*cdf0e10cSrcweir                     css::uno::UNO_QUERY);
212*cdf0e10cSrcweir             if (!m_xConfig.is())
213*cdf0e10cSrcweir                 throw css::uno::RuntimeException(
214*cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
215*cdf0e10cSrcweir                                       "null com.sun.star.configuration."
216*cdf0e10cSrcweir                                       "ConfigurationUpdateAccess")),
217*cdf0e10cSrcweir                     0);
218*cdf0e10cSrcweir             bAdd = true;
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir         xConfig = m_xConfig;
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir     if (bAdd)
223*cdf0e10cSrcweir         // Exceptions here could be handled individually, to support graceful
224*cdf0e10cSrcweir         // degradation (no update notification mechanism in this case---but also
225*cdf0e10cSrcweir         // no dispose notifications):
226*cdf0e10cSrcweir         xConfig->addPropertyChangeListener(
227*cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ShowStatusWindow")),
228*cdf0e10cSrcweir             this);
229*cdf0e10cSrcweir     return xConfig;
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232