Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
21 : #define INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
22 :
23 : #include <com/sun/star/linguistic2/XLanguageGuessing.hpp>
24 : #include <com/sun/star/container/XNameAccess.hpp>
25 : #include <com/sun/star/document/XEventListener.hpp>
26 : #include <com/sun/star/lang/XEventListener.hpp>
27 : #include <com/sun/star/util/XChangesListener.hpp>
28 : #include <com/sun/star/container/XContainerListener.hpp>
29 : #include <com/sun/star/frame/XFrame.hpp>
30 : #include <com/sun/star/uno/XComponentContext.hpp>
31 :
32 : #include <cppuhelper/implbase1.hxx>
33 :
34 : #include <i18nlangtag/lang.h>
35 : #include <svl/languageoptions.hxx>
36 : #include <rtl/ustring.hxx>
37 : #include <fwidllapi.h>
38 :
39 : #include <set>
40 :
41 : // flags for script types in use within selection
42 : #define LS_SCRIPT_LATIN 0x0001
43 : #define LS_SCRIPT_ASIAN 0x0002
44 : #define LS_SCRIPT_COMPLEX 0x0004
45 :
46 : namespace framework
47 : {
48 :
49 : // menu ids for language status bar control
50 : enum LangMenuIDs
51 : {
52 : MID_LANG_SEL_1 = 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled
53 : MID_LANG_SEL_2,
54 : MID_LANG_SEL_3,
55 : MID_LANG_SEL_4,
56 : MID_LANG_SEL_5,
57 : MID_LANG_SEL_6,
58 : MID_LANG_SEL_7,
59 : MID_LANG_SEL_8,
60 : MID_LANG_SEL_9,
61 : MID_LANG_SEL_NONE,
62 : MID_LANG_SEL_RESET,
63 : MID_LANG_SEL_MORE,
64 :
65 : MID_LANG_PARA_SEPARATOR,
66 : MID_LANG_PARA_STRING,
67 :
68 : MID_LANG_PARA_1,
69 : MID_LANG_PARA_2,
70 : MID_LANG_PARA_3,
71 : MID_LANG_PARA_4,
72 : MID_LANG_PARA_5,
73 : MID_LANG_PARA_6,
74 : MID_LANG_PARA_7,
75 : MID_LANG_PARA_8,
76 : MID_LANG_PARA_9,
77 : MID_LANG_PARA_NONE,
78 : MID_LANG_PARA_RESET,
79 : MID_LANG_PARA_MORE,
80 : };
81 :
82 0 : inline bool IsScriptTypeMatchingToLanguage( sal_Int16 nScriptType, LanguageType nLang )
83 : {
84 0 : return 0 != (nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang ));
85 : }
86 :
87 40656 : inline void RetrieveTypeNameFromResourceURL( const OUString& aResourceURL, OUString& aType, OUString& aName )
88 : {
89 40656 : const sal_Int32 RESOURCEURL_PREFIX_SIZE = 17;
90 40656 : const char RESOURCEURL_PREFIX[] = "private:resource/";
91 :
92 81312 : if (( aResourceURL.startsWith( RESOURCEURL_PREFIX ) ) &&
93 40656 : ( aResourceURL.getLength() > RESOURCEURL_PREFIX_SIZE ))
94 : {
95 40656 : OUString aTmpStr( aResourceURL.copy( RESOURCEURL_PREFIX_SIZE ));
96 40656 : sal_Int32 nToken = 0;
97 40656 : sal_Int32 nPart = 0;
98 81312 : do
99 : {
100 88262 : OUString sToken = aTmpStr.getToken( 0, '/', nToken);
101 88262 : if ( !sToken.isEmpty() )
102 : {
103 88262 : if ( nPart == 0 )
104 40656 : aType = sToken;
105 47606 : else if ( nPart == 1 )
106 40656 : aName = sToken;
107 : else
108 6950 : break;
109 81312 : nPart++;
110 81312 : }
111 : }
112 121968 : while( nToken >=0 );
113 : }
114 40656 : }
115 :
116 854 : class FWI_DLLPUBLIC LanguageGuessingHelper
117 : {
118 : mutable ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLanguageGuessing > m_xLanguageGuesser;
119 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > m_xContext;
120 :
121 : public:
122 854 : LanguageGuessingHelper(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _xContext) : m_xContext(_xContext){}
123 :
124 : ::com::sun::star::uno::Reference< ::com::sun::star::linguistic2::XLanguageGuessing > GetGuesser() const;
125 : };
126 :
127 : FWI_DLLPUBLIC OUString RetrieveLabelFromCommand( const OUString& aCmdURL
128 : ,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _xContext
129 : ,::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _xUICommandLabels
130 : ,const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& _xFrame
131 : ,OUString& _rModuleIdentifier
132 : ,bool& _rIni
133 : ,const sal_Char* _pName);
134 :
135 : FWI_DLLPUBLIC void FillLangItems( std::set< OUString > &rLangItems,
136 : const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > &rxFrame,
137 : const LanguageGuessingHelper & rLangGuessHelper,
138 : sal_Int16 nScriptType,
139 : const OUString & rCurLang,
140 : const OUString & rKeyboardLang,
141 : const OUString & rGuessedTextLang );
142 :
143 : //It's common for an object to want to create and own a Broadcaster and set
144 : //itself as a Listener on its own Broadcaster member.
145 :
146 : //However, calling addListener on a Broadcaster means that the Broadcaster adds
147 : //a reference to the Listener leading to an ownership cycle where the Listener
148 : //owns the Broadcaster which "owns" the Listener.
149 :
150 : //The WeakContainerListener allows breaking this cycle and retrofitting
151 : //afflicted implentations fairly easily.
152 :
153 : //OriginalListener owns the Broadcaster which "owns" the WeakContainerListener
154 : //which forwards the events to the OriginalListener without taking ownership of
155 : //it.
156 : class WeakContainerListener : public ::cppu::WeakImplHelper1<com::sun::star::container::XContainerListener>
157 : {
158 : private:
159 : com::sun::star::uno::WeakReference<com::sun::star::container::XContainerListener> mxOwner;
160 :
161 : public:
162 1248 : WeakContainerListener(com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner)
163 1248 : : mxOwner(xOwner)
164 : {
165 1248 : }
166 :
167 2420 : virtual ~WeakContainerListener()
168 1210 : {
169 2420 : }
170 :
171 : // container.XContainerListener
172 0 : virtual void SAL_CALL elementInserted(const com::sun::star::container::ContainerEvent& rEvent)
173 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
174 : {
175 : com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
176 0 : com::sun::star::uno::UNO_QUERY);
177 0 : if (xOwner.is())
178 0 : xOwner->elementInserted(rEvent);
179 0 : }
180 :
181 0 : virtual void SAL_CALL elementRemoved(const com::sun::star::container::ContainerEvent& rEvent)
182 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
183 : {
184 : com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
185 0 : com::sun::star::uno::UNO_QUERY);
186 0 : if (xOwner.is())
187 0 : xOwner->elementRemoved(rEvent);
188 0 : }
189 :
190 0 : virtual void SAL_CALL elementReplaced(const com::sun::star::container::ContainerEvent& rEvent)
191 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
192 : {
193 : com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
194 0 : com::sun::star::uno::UNO_QUERY);
195 0 : if (xOwner.is())
196 0 : xOwner->elementReplaced(rEvent);
197 0 : }
198 :
199 : // lang.XEventListener
200 0 : virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
201 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
202 : {
203 : com::sun::star::uno::Reference<com::sun::star::container::XContainerListener> xOwner(mxOwner.get(),
204 0 : com::sun::star::uno::UNO_QUERY);
205 0 : if (xOwner.is())
206 0 : xOwner->disposing(rEvent);
207 :
208 0 : }
209 : };
210 :
211 : class WeakChangesListener : public ::cppu::WeakImplHelper1<com::sun::star::util::XChangesListener>
212 : {
213 : private:
214 : com::sun::star::uno::WeakReference<com::sun::star::util::XChangesListener> mxOwner;
215 :
216 : public:
217 3691 : WeakChangesListener(com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner)
218 3691 : : mxOwner(xOwner)
219 : {
220 3691 : }
221 :
222 7366 : virtual ~WeakChangesListener()
223 3683 : {
224 7366 : }
225 :
226 : // util.XChangesListener
227 0 : virtual void SAL_CALL changesOccurred(const com::sun::star::util::ChangesEvent& rEvent)
228 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
229 : {
230 : com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner(mxOwner.get(),
231 0 : com::sun::star::uno::UNO_QUERY);
232 0 : if (xOwner.is())
233 0 : xOwner->changesOccurred(rEvent);
234 0 : }
235 :
236 : // lang.XEventListener
237 0 : virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
238 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
239 : {
240 : com::sun::star::uno::Reference<com::sun::star::util::XChangesListener> xOwner(mxOwner.get(),
241 0 : com::sun::star::uno::UNO_QUERY);
242 0 : if (xOwner.is())
243 0 : xOwner->disposing(rEvent);
244 :
245 0 : }
246 : };
247 :
248 : class WeakEventListener : public ::cppu::WeakImplHelper1<com::sun::star::lang::XEventListener>
249 : {
250 : private:
251 : com::sun::star::uno::WeakReference<com::sun::star::lang::XEventListener> mxOwner;
252 :
253 : public:
254 : WeakEventListener(com::sun::star::uno::Reference<com::sun::star::lang::XEventListener> xOwner)
255 : : mxOwner(xOwner)
256 : {
257 : }
258 :
259 : virtual ~WeakEventListener()
260 : {
261 : }
262 :
263 : // lang.XEventListener
264 : virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
265 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
266 : {
267 : com::sun::star::uno::Reference<com::sun::star::lang::XEventListener> xOwner(mxOwner.get(),
268 : com::sun::star::uno::UNO_QUERY);
269 : if (xOwner.is())
270 : xOwner->disposing(rEvent);
271 :
272 : }
273 : };
274 :
275 : class WeakDocumentEventListener : public ::cppu::WeakImplHelper1<com::sun::star::document::XEventListener>
276 : {
277 : private:
278 : com::sun::star::uno::WeakReference<com::sun::star::document::XEventListener> mxOwner;
279 :
280 : public:
281 98 : WeakDocumentEventListener(com::sun::star::uno::Reference<com::sun::star::document::XEventListener> xOwner)
282 98 : : mxOwner(xOwner)
283 : {
284 98 : }
285 :
286 196 : virtual ~WeakDocumentEventListener()
287 98 : {
288 196 : }
289 :
290 0 : virtual void SAL_CALL notifyEvent(const com::sun::star::document::EventObject& rEvent)
291 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
292 : {
293 : com::sun::star::uno::Reference<com::sun::star::document::XEventListener> xOwner(mxOwner.get(),
294 0 : com::sun::star::uno::UNO_QUERY);
295 0 : if (xOwner.is())
296 0 : xOwner->notifyEvent(rEvent);
297 :
298 0 : }
299 :
300 : // lang.XEventListener
301 0 : virtual void SAL_CALL disposing(const com::sun::star::lang::EventObject& rEvent)
302 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
303 : {
304 : com::sun::star::uno::Reference<com::sun::star::document::XEventListener> xOwner(mxOwner.get(),
305 0 : com::sun::star::uno::UNO_QUERY);
306 0 : if (xOwner.is())
307 0 : xOwner->disposing(rEvent);
308 :
309 0 : }
310 : };
311 :
312 : } // namespace framework
313 :
314 : #endif // INCLUDED_FRAMEWORK_INC_HELPER_MISCHELPER_HXX
315 :
316 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|