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