xref: /aoo41x/main/sfx2/source/menu/thessubmenu.cxx (revision 79aad27f)
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 <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/linguistic2/XThesaurus.hpp>
29 #include <com/sun/star/linguistic2/XMeaning.hpp>
30 #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
31 
32 #include <comphelper/processfactory.hxx>
33 #include <svl/stritem.hxx>
34 #include <tools/debug.hxx>
35 #include <vcl/graph.hxx>
36 #include <svtools/filter.hxx>
37 
38 
39 #include <vector>
40 
41 #include <sfx2/bindings.hxx>
42 #include <sfx2/dispatch.hxx>
43 #include <sfx2/viewsh.hxx>
44 #include "thessubmenu.hxx"
45 
46 
47 using namespace ::com::sun::star;
48 using ::rtl::OUString;
49 
50 
51 // STATIC DATA -----------------------------------------------------------
52 
53 SFX_IMPL_MENU_CONTROL(SfxThesSubMenuControl, SfxStringItem);
54 
55 ////////////////////////////////////////////////////////////
56 
57 
58 /*
59 	Ctor; setzt Select-Handler am Menu und traegt Menu
60 	in seinen Parent ein.
61  */
SfxThesSubMenuControl(sal_uInt16 nSlotId,Menu & rMenu,SfxBindings & rBindings)62 SfxThesSubMenuControl::SfxThesSubMenuControl( sal_uInt16 nSlotId, Menu &rMenu, SfxBindings &rBindings )
63     : SfxMenuControl( nSlotId, rBindings ),
64     pMenu(new PopupMenu),
65     rParent(rMenu)
66 {
67 	rMenu.SetPopupMenu(nSlotId, pMenu);
68 	pMenu->SetSelectHdl(LINK(this, SfxThesSubMenuControl, MenuSelect));
69 	pMenu->Clear();
70 	rParent.EnableItem( GetId(), sal_False );
71 }
72 
73 
~SfxThesSubMenuControl()74 SfxThesSubMenuControl::~SfxThesSubMenuControl()
75 {
76 	delete pMenu;
77 }
78 
79 
80 /*
81 	Statusbenachrichtigung;
82 	Ist die Funktionalit"at disabled, wird der entsprechende
83 	Menueeintrag im Parentmenu disabled, andernfalls wird er enabled.
84  */
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem *)85 void SfxThesSubMenuControl::StateChanged(
86     sal_uInt16 /*nSID*/,
87     SfxItemState eState,
88 	const SfxPoolItem* /*pState*/ )
89 {
90 	rParent.EnableItem(GetId(), SFX_ITEM_AVAILABLE == eState );
91 }
92 
93 
94 /*
95 	Select-Handler des Menus;
96 	das selektierte Verb mit ausgef"uhrt,
97  */
IMPL_LINK_INLINE_START(SfxThesSubMenuControl,MenuSelect,Menu *,pSelMenu)98 IMPL_LINK_INLINE_START( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
99 {
100 	const sal_uInt16 nSlotId = pSelMenu->GetCurItemId();
101     if( nSlotId )
102         GetBindings().Execute(nSlotId);
103 	return 1;
104 }
IMPL_LINK_INLINE_END(SfxThesSubMenuControl,MenuSelect,Menu *,pSelMenu)105 IMPL_LINK_INLINE_END( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
106 
107 
108 PopupMenu* SfxThesSubMenuControl::GetPopup() const
109 {
110 	return pMenu;
111 }
112 
113 
114 ////////////////////////////////////////////////////////////
115 
GetText(const String & rLookUpString,xub_StrLen nDelimPos)116 OUString SfxThesSubMenuHelper::GetText(
117     const String &rLookUpString,
118     xub_StrLen nDelimPos )
119 {
120     return OUString( rLookUpString.Copy( 0, nDelimPos ) );
121 }
122 
123 
GetLocale(lang::Locale & rLocale,const String & rLookUpString,xub_StrLen nDelimPos)124 void SfxThesSubMenuHelper::GetLocale(
125     lang::Locale /*out */ &rLocale,
126     const String &rLookUpString,
127     xub_StrLen nDelimPos  )
128 {
129     String aIsoLang( rLookUpString.Copy( nDelimPos + 1) );
130     const xub_StrLen nPos = aIsoLang.Search( '-' );
131     if (nPos != STRING_NOTFOUND)
132     {
133         rLocale.Language    = aIsoLang.Copy( 0, nPos );
134         rLocale.Country     = aIsoLang.Copy( nPos + 1 );
135         rLocale.Variant     = String::EmptyString();
136     }
137 }
138 
139 
SfxThesSubMenuHelper()140 SfxThesSubMenuHelper::SfxThesSubMenuHelper()
141 {
142     try
143     {
144         uno::Reference< lang::XMultiServiceFactory >  xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
145         m_xLngMgr = uno::Reference< linguistic2::XLinguServiceManager >( xMSF->createInstance(
146                 OUString( RTL_CONSTASCII_USTRINGPARAM(
147                     "com.sun.star.linguistic2.LinguServiceManager" ))), uno::UNO_QUERY_THROW );
148         m_xThesarus = m_xLngMgr->getThesaurus();
149     }
150     catch (uno::Exception &e)
151     {
152         (void) e;
153         DBG_ASSERT( 0, "failed to get thesaurus" );
154     }
155 }
156 
157 
~SfxThesSubMenuHelper()158 SfxThesSubMenuHelper::~SfxThesSubMenuHelper()
159 {
160 }
161 
162 
IsSupportedLocale(const lang::Locale & rLocale) const163 bool SfxThesSubMenuHelper::IsSupportedLocale( const lang::Locale & rLocale ) const
164 {
165     return m_xThesarus.is() && m_xThesarus->hasLocale( rLocale );
166 }
167 
168 
GetMeanings(std::vector<OUString> & rSynonyms,const OUString & rWord,const lang::Locale & rLocale,sal_Int16 nMaxSynonms)169 bool SfxThesSubMenuHelper::GetMeanings(
170     std::vector< OUString > & rSynonyms,
171     const OUString & rWord,
172     const lang::Locale & rLocale,
173     sal_Int16 nMaxSynonms )
174 {
175     bool bHasMoreSynonyms = false;
176     rSynonyms.clear();
177     if (IsSupportedLocale( rLocale ) && rWord.getLength() && nMaxSynonms > 0)
178     {
179         try
180         {
181             // get all meannings
182             const uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeaningSeq(
183                     m_xThesarus->queryMeanings( rWord, rLocale, uno::Sequence< beans::PropertyValue >() ));
184             const uno::Reference< linguistic2::XMeaning > *pxMeaning = aMeaningSeq.getConstArray();
185             const sal_Int32 nMeanings = aMeaningSeq.getLength();
186 
187             // iterate over all meanings until nMaxSynonms are found or all meanings are processed
188             sal_Int32 nCount = 0;
189             sal_Int32 i = 0;
190             for ( ;  i < nMeanings && nCount < nMaxSynonms;  ++i)
191             {
192                 const uno::Sequence< OUString > aSynonymSeq( pxMeaning[i]->querySynonyms() );
193                 const OUString *pSynonyms = aSynonymSeq.getConstArray();
194                 const sal_Int32 nSynonyms = aSynonymSeq.getLength();
195                 sal_Int32 k = 0;
196                 for ( ;  k < nSynonyms && nCount < nMaxSynonms;  ++k)
197                 {
198                     rSynonyms.push_back( pSynonyms[k] );
199                     ++nCount;
200                 }
201                 bHasMoreSynonyms = k < nSynonyms;    // any synonym from this meaning skipped?
202             }
203 
204             bHasMoreSynonyms |= i < nMeanings;   // any meaning skipped?
205         }
206         catch (uno::Exception &e)
207         {
208             (void) e;
209             DBG_ASSERT( 0, "failed to get synonyms" );
210         }
211     }
212     return bHasMoreSynonyms;
213 }
214 
215 
GetThesImplName(const lang::Locale & rLocale) const216 String SfxThesSubMenuHelper::GetThesImplName( const lang::Locale &rLocale ) const
217 {
218     String aRes;
219     DBG_ASSERT( m_xLngMgr.is(), "LinguServiceManager missing" );
220     if (m_xLngMgr.is())
221     {
222         uno::Sequence< OUString > aServiceNames = m_xLngMgr->getConfiguredServices(
223                 OUString::createFromAscii("com.sun.star.linguistic2.Thesaurus"), rLocale );
224         // there should be at most one thesaurus configured for each language
225         DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
226         if (aServiceNames.getLength() == 1)
227             aRes = aServiceNames[0];
228     }
229     return aRes;
230 }
231 
232 ////////////////////////////////////////////////////////////
233 
234 
235