/src/libreoffice/framework/inc/helper/mischelper.hxx
Line | Count | Source |
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 | | #pragma once |
21 | | |
22 | | #include <com/sun/star/linguistic2/XLanguageGuessing.hpp> |
23 | | #include <com/sun/star/document/XDocumentEventListener.hpp> |
24 | | #include <com/sun/star/util/XChangesListener.hpp> |
25 | | #include <com/sun/star/container/XContainerListener.hpp> |
26 | | #include <com/sun/star/frame/XFrame.hpp> |
27 | | #include <com/sun/star/uno/XComponentContext.hpp> |
28 | | #include <com/sun/star/ui/XContextChangeEventListener.hpp> |
29 | | |
30 | | #include <cppuhelper/implbase.hxx> |
31 | | #include <cppuhelper/weakref.hxx> |
32 | | |
33 | | #include <i18nlangtag/lang.h> |
34 | | #include <o3tl/string_view.hxx> |
35 | | #include <svl/languageoptions.hxx> |
36 | | #include <rtl/ustring.hxx> |
37 | | |
38 | | #include <functional> |
39 | | #include <set> |
40 | | #include <utility> |
41 | | |
42 | | namespace framework |
43 | | { |
44 | | |
45 | | // menu ids for language status bar control |
46 | | enum LangMenuIDs |
47 | | { |
48 | | MID_LANG_SEL_1 = 1, // need to start with 1 since xPopupMenu->execute will return 0 if the menu is cancelled |
49 | | MID_LANG_SEL_2, |
50 | | MID_LANG_SEL_3, |
51 | | MID_LANG_SEL_4, |
52 | | MID_LANG_SEL_5, |
53 | | MID_LANG_SEL_6, |
54 | | MID_LANG_SEL_7, |
55 | | MID_LANG_SEL_8, |
56 | | MID_LANG_SEL_9, |
57 | | MID_LANG_SEL_NONE, |
58 | | MID_LANG_SEL_RESET, |
59 | | MID_LANG_SEL_MORE, |
60 | | MID_LANG_DEF_NONE, |
61 | | MID_LANG_DEF_RESET, |
62 | | MID_LANG_DEF_MORE, |
63 | | |
64 | | MID_LANG_PARA_SEPARATOR, |
65 | | MID_LANG_PARA_STRING, |
66 | | |
67 | | MID_LANG_PARA_1, |
68 | | MID_LANG_PARA_2, |
69 | | MID_LANG_PARA_3, |
70 | | MID_LANG_PARA_4, |
71 | | MID_LANG_PARA_5, |
72 | | MID_LANG_PARA_6, |
73 | | MID_LANG_PARA_7, |
74 | | MID_LANG_PARA_8, |
75 | | MID_LANG_PARA_9, |
76 | | MID_LANG_PARA_NONE, |
77 | | MID_LANG_PARA_RESET, |
78 | | MID_LANG_PARA_MORE, |
79 | | }; |
80 | | |
81 | | inline bool IsScriptTypeMatchingToLanguage( SvtScriptType nScriptType, LanguageType nLang ) |
82 | 0 | { |
83 | 0 | return bool(nScriptType & SvtLanguageOptions::GetScriptTypeOfLanguage( nLang )); |
84 | 0 | } |
85 | | |
86 | | inline void RetrieveTypeNameFromResourceURL( std::u16string_view aResourceURL, OUString& aType, OUString& aName ) |
87 | 0 | { |
88 | 0 | static constexpr std::u16string_view RESOURCEURL_PREFIX = u"private:resource/"; |
89 | |
|
90 | 0 | if (o3tl::starts_with(aResourceURL, RESOURCEURL_PREFIX )) |
91 | 0 | { |
92 | 0 | size_t nIdx = RESOURCEURL_PREFIX.size(); |
93 | 0 | while (nIdx < aResourceURL.size() && aResourceURL[nIdx]=='/') |
94 | 0 | ++nIdx; |
95 | 0 | if (nIdx >= aResourceURL.size()) |
96 | 0 | return; |
97 | 0 | aType = o3tl::getToken(aResourceURL, u'/', nIdx); |
98 | 0 | if (nIdx == std::u16string_view::npos) |
99 | 0 | return; |
100 | 0 | while (nIdx < aResourceURL.size() && aResourceURL[nIdx]=='/') |
101 | 0 | ++nIdx; |
102 | 0 | if (nIdx >= aResourceURL.size()) |
103 | 0 | return; |
104 | 0 | aName = o3tl::getToken(aResourceURL, u'/', nIdx); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | class LanguageGuessingHelper |
109 | | { |
110 | | mutable css::uno::Reference< css::linguistic2::XLanguageGuessing > m_xLanguageGuesser; |
111 | | css::uno::Reference< css::uno::XComponentContext > m_xContext; |
112 | | |
113 | | public: |
114 | 0 | LanguageGuessingHelper(css::uno::Reference< css::uno::XComponentContext > _xContext) : m_xContext(std::move(_xContext)){} |
115 | | |
116 | | css::uno::Reference< css::linguistic2::XLanguageGuessing > const & GetGuesser() const; |
117 | | }; |
118 | | |
119 | | void FillLangItems( std::set< OUString > &rLangItems, |
120 | | const css::uno::Reference< css::frame::XFrame > &rxFrame, |
121 | | const LanguageGuessingHelper & rLangGuessHelper, |
122 | | SvtScriptType nScriptType, |
123 | | const OUString & rCurLang, |
124 | | const OUString & rKeyboardLang, |
125 | | const OUString & rGuessedTextLang ); |
126 | | |
127 | | //It's common for an object to want to create and own a Broadcaster and set |
128 | | //itself as a Listener on its own Broadcaster member. |
129 | | |
130 | | //However, calling addListener on a Broadcaster means that the Broadcaster adds |
131 | | //a reference to the Listener leading to an ownership cycle where the Listener |
132 | | //owns the Broadcaster which "owns" the Listener. |
133 | | |
134 | | //The WeakContainerListener allows breaking this cycle and retrofitting |
135 | | //afflicted implementations fairly easily. |
136 | | |
137 | | //OriginalListener owns the Broadcaster which "owns" the WeakContainerListener |
138 | | //which forwards the events to the OriginalListener without taking ownership of |
139 | | //it. |
140 | | class WeakContainerListener final : public ::cppu::WeakImplHelper<css::container::XContainerListener> |
141 | | { |
142 | | private: |
143 | | css::uno::WeakReference<css::container::XContainerListener> mxOwner; |
144 | | |
145 | | public: |
146 | | WeakContainerListener(css::uno::Reference<css::container::XContainerListener> const & xOwner) |
147 | 0 | : mxOwner(xOwner) |
148 | 0 | { |
149 | 0 | } |
150 | | |
151 | | // container.XContainerListener |
152 | | virtual void SAL_CALL elementInserted(const css::container::ContainerEvent& rEvent) override |
153 | 0 | { |
154 | 0 | css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(), |
155 | 0 | css::uno::UNO_QUERY); |
156 | 0 | if (xOwner.is()) |
157 | 0 | xOwner->elementInserted(rEvent); |
158 | 0 | } |
159 | | |
160 | | virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent& rEvent) override |
161 | 0 | { |
162 | 0 | css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(), |
163 | 0 | css::uno::UNO_QUERY); |
164 | 0 | if (xOwner.is()) |
165 | 0 | xOwner->elementRemoved(rEvent); |
166 | 0 | } |
167 | | |
168 | | virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent& rEvent) override |
169 | 0 | { |
170 | 0 | css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(), |
171 | 0 | css::uno::UNO_QUERY); |
172 | 0 | if (xOwner.is()) |
173 | 0 | xOwner->elementReplaced(rEvent); |
174 | 0 | } |
175 | | |
176 | | // lang.XEventListener |
177 | | virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override |
178 | 0 | { |
179 | 0 | css::uno::Reference<css::container::XContainerListener> xOwner(mxOwner.get(), |
180 | 0 | css::uno::UNO_QUERY); |
181 | 0 | if (xOwner.is()) |
182 | 0 | xOwner->disposing(rEvent); |
183 | |
|
184 | 0 | } |
185 | | }; |
186 | | |
187 | | class WeakChangesListener final : public ::cppu::WeakImplHelper<css::util::XChangesListener> |
188 | | { |
189 | | private: |
190 | | css::uno::WeakReference<css::util::XChangesListener> mxOwner; |
191 | | |
192 | | public: |
193 | | WeakChangesListener(css::uno::Reference<css::util::XChangesListener> const & xOwner) |
194 | 0 | : mxOwner(xOwner) |
195 | 0 | { |
196 | 0 | } |
197 | | |
198 | | // util.XChangesListener |
199 | | virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent& rEvent) override |
200 | 0 | { |
201 | 0 | css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(), |
202 | 0 | css::uno::UNO_QUERY); |
203 | 0 | if (xOwner.is()) |
204 | 0 | xOwner->changesOccurred(rEvent); |
205 | 0 | } |
206 | | |
207 | | // lang.XEventListener |
208 | | virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override |
209 | 0 | { |
210 | 0 | css::uno::Reference<css::util::XChangesListener> xOwner(mxOwner.get(), |
211 | 0 | css::uno::UNO_QUERY); |
212 | 0 | if (xOwner.is()) |
213 | 0 | xOwner->disposing(rEvent); |
214 | |
|
215 | 0 | } |
216 | | }; |
217 | | |
218 | | class WeakDocumentEventListener final : public ::cppu::WeakImplHelper<css::document::XDocumentEventListener> |
219 | | { |
220 | | private: |
221 | | css::uno::WeakReference<css::document::XDocumentEventListener> mxOwner; |
222 | | |
223 | | public: |
224 | | WeakDocumentEventListener(css::uno::Reference<css::document::XDocumentEventListener> const & xOwner) |
225 | 0 | : mxOwner(xOwner) |
226 | 0 | { |
227 | 0 | } |
228 | | |
229 | | virtual void SAL_CALL documentEventOccured(const css::document::DocumentEvent& rEvent) override |
230 | 0 | { |
231 | 0 | css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(), |
232 | 0 | css::uno::UNO_QUERY); |
233 | 0 | if (xOwner.is()) |
234 | 0 | xOwner->documentEventOccured(rEvent); |
235 | |
|
236 | 0 | } |
237 | | |
238 | | // lang.XEventListener |
239 | | virtual void SAL_CALL disposing(const css::lang::EventObject& rEvent) override |
240 | 0 | { |
241 | 0 | css::uno::Reference<css::document::XDocumentEventListener> xOwner(mxOwner.get(), |
242 | 0 | css::uno::UNO_QUERY); |
243 | 0 | if (xOwner.is()) |
244 | 0 | xOwner->disposing(rEvent); |
245 | |
|
246 | 0 | } |
247 | | }; |
248 | | |
249 | | css::uno::Reference<css::ui::XContextChangeEventListener> |
250 | | GetFirstListenerWith_Impl( |
251 | | css::uno::Reference<css::uno::XComponentContext> const & xComponentContext, |
252 | | css::uno::Reference<css::uno::XInterface> const& xEventFocus, |
253 | | std::function<bool (css::uno::Reference<css::ui::XContextChangeEventListener> const&)> const& rPredicate); |
254 | | |
255 | | extern auto (*g_pGetMultiplexerListener)( |
256 | | css::uno::Reference<css::uno::XComponentContext> const & xComponentContext, |
257 | | css::uno::Reference<css::uno::XInterface> const&, |
258 | | std::function<bool (css::uno::Reference<css::ui::XContextChangeEventListener> const&)> const&) |
259 | | -> css::uno::Reference<css::ui::XContextChangeEventListener>; |
260 | | |
261 | | } // namespace framework |
262 | | |
263 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |