xref: /aoo42x/main/sfx2/source/menu/thessubmenu.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2008 by Sun Microsystems, Inc.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * $RCSfile: thessubmenu.cxx,v $
10  * $Revision: 1.0 $
11  *
12  * This file is part of OpenOffice.org.
13  *
14  * OpenOffice.org is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License version 3
16  * only, as published by the Free Software Foundation.
17  *
18  * OpenOffice.org is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Lesser General Public License version 3 for more details
22  * (a copy is included in the LICENSE file that accompanied this code).
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * version 3 along with OpenOffice.org.  If not, see
26  * <http://www.openoffice.org/license.html>
27  * for a copy of the LGPLv3 License.
28  *
29  ************************************************************************/
30 
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/linguistic2/XThesaurus.hpp>
36 #include <com/sun/star/linguistic2/XMeaning.hpp>
37 #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
38 
39 #include <comphelper/processfactory.hxx>
40 #include <svl/stritem.hxx>
41 #include <tools/debug.hxx>
42 #include <vcl/graph.hxx>
43 #include <svtools/filter.hxx>
44 
45 
46 #include <vector>
47 
48 #include <sfx2/bindings.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include <sfx2/viewsh.hxx>
51 #include "thessubmenu.hxx"
52 
53 
54 using namespace ::com::sun::star;
55 using ::rtl::OUString;
56 
57 
58 // STATIC DATA -----------------------------------------------------------
59 
60 SFX_IMPL_MENU_CONTROL(SfxThesSubMenuControl, SfxStringItem);
61 
62 ////////////////////////////////////////////////////////////
63 
64 
65 /*
66 	Ctor; setzt Select-Handler am Menu und traegt Menu
67 	in seinen Parent ein.
68  */
69 SfxThesSubMenuControl::SfxThesSubMenuControl( sal_uInt16 nSlotId, Menu &rMenu, SfxBindings &rBindings )
70     : SfxMenuControl( nSlotId, rBindings ),
71     pMenu(new PopupMenu),
72     rParent(rMenu)
73 {
74 	rMenu.SetPopupMenu(nSlotId, pMenu);
75 	pMenu->SetSelectHdl(LINK(this, SfxThesSubMenuControl, MenuSelect));
76 	pMenu->Clear();
77 	rParent.EnableItem( GetId(), sal_False );
78 }
79 
80 
81 SfxThesSubMenuControl::~SfxThesSubMenuControl()
82 {
83 	delete pMenu;
84 }
85 
86 
87 /*
88 	Statusbenachrichtigung;
89 	Ist die Funktionalit"at disabled, wird der entsprechende
90 	Menueeintrag im Parentmenu disabled, andernfalls wird er enabled.
91  */
92 void SfxThesSubMenuControl::StateChanged(
93     sal_uInt16 /*nSID*/,
94     SfxItemState eState,
95 	const SfxPoolItem* /*pState*/ )
96 {
97 	rParent.EnableItem(GetId(), SFX_ITEM_AVAILABLE == eState );
98 }
99 
100 
101 /*
102 	Select-Handler des Menus;
103 	das selektierte Verb mit ausgef"uhrt,
104  */
105 IMPL_LINK_INLINE_START( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
106 {
107 	const sal_uInt16 nSlotId = pSelMenu->GetCurItemId();
108     if( nSlotId )
109         GetBindings().Execute(nSlotId);
110 	return 1;
111 }
112 IMPL_LINK_INLINE_END( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
113 
114 
115 PopupMenu* SfxThesSubMenuControl::GetPopup() const
116 {
117 	return pMenu;
118 }
119 
120 
121 ////////////////////////////////////////////////////////////
122 
123 OUString SfxThesSubMenuHelper::GetText(
124     const String &rLookUpString,
125     xub_StrLen nDelimPos )
126 {
127     return OUString( rLookUpString.Copy( 0, nDelimPos ) );
128 }
129 
130 
131 void SfxThesSubMenuHelper::GetLocale(
132     lang::Locale /*out */ &rLocale,
133     const String &rLookUpString,
134     xub_StrLen nDelimPos  )
135 {
136     String aIsoLang( rLookUpString.Copy( nDelimPos + 1) );
137     const xub_StrLen nPos = aIsoLang.Search( '-' );
138     if (nPos != STRING_NOTFOUND)
139     {
140         rLocale.Language    = aIsoLang.Copy( 0, nPos );
141         rLocale.Country     = aIsoLang.Copy( nPos + 1 );
142         rLocale.Variant     = String::EmptyString();
143     }
144 }
145 
146 
147 SfxThesSubMenuHelper::SfxThesSubMenuHelper()
148 {
149     try
150     {
151         uno::Reference< lang::XMultiServiceFactory >  xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
152         m_xLngMgr = uno::Reference< linguistic2::XLinguServiceManager >( xMSF->createInstance(
153                 OUString( RTL_CONSTASCII_USTRINGPARAM(
154                     "com.sun.star.linguistic2.LinguServiceManager" ))), uno::UNO_QUERY_THROW );
155         m_xThesarus = m_xLngMgr->getThesaurus();
156     }
157     catch (uno::Exception &e)
158     {
159         (void) e;
160         DBG_ASSERT( 0, "failed to get thesaurus" );
161     }
162 }
163 
164 
165 SfxThesSubMenuHelper::~SfxThesSubMenuHelper()
166 {
167 }
168 
169 
170 bool SfxThesSubMenuHelper::IsSupportedLocale( const lang::Locale & rLocale ) const
171 {
172     return m_xThesarus.is() && m_xThesarus->hasLocale( rLocale );
173 }
174 
175 
176 bool SfxThesSubMenuHelper::GetMeanings(
177     std::vector< OUString > & rSynonyms,
178     const OUString & rWord,
179     const lang::Locale & rLocale,
180     sal_Int16 nMaxSynonms )
181 {
182     bool bHasMoreSynonyms = false;
183     rSynonyms.clear();
184     if (IsSupportedLocale( rLocale ) && rWord.getLength() && nMaxSynonms > 0)
185     {
186         try
187         {
188             // get all meannings
189             const uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeaningSeq(
190                     m_xThesarus->queryMeanings( rWord, rLocale, uno::Sequence< beans::PropertyValue >() ));
191             const uno::Reference< linguistic2::XMeaning > *pxMeaning = aMeaningSeq.getConstArray();
192             const sal_Int32 nMeanings = aMeaningSeq.getLength();
193 
194             // iterate over all meanings until nMaxSynonms are found or all meanings are processed
195             sal_Int32 nCount = 0;
196             sal_Int32 i = 0;
197             for ( ;  i < nMeanings && nCount < nMaxSynonms;  ++i)
198             {
199                 const uno::Sequence< OUString > aSynonymSeq( pxMeaning[i]->querySynonyms() );
200                 const OUString *pSynonyms = aSynonymSeq.getConstArray();
201                 const sal_Int32 nSynonyms = aSynonymSeq.getLength();
202                 sal_Int32 k = 0;
203                 for ( ;  k < nSynonyms && nCount < nMaxSynonms;  ++k)
204                 {
205                     rSynonyms.push_back( pSynonyms[k] );
206                     ++nCount;
207                 }
208                 bHasMoreSynonyms = k < nSynonyms;    // any synonym from this meaning skipped?
209             }
210 
211             bHasMoreSynonyms |= i < nMeanings;   // any meaning skipped?
212         }
213         catch (uno::Exception &e)
214         {
215             (void) e;
216             DBG_ASSERT( 0, "failed to get synonyms" );
217         }
218     }
219     return bHasMoreSynonyms;
220 }
221 
222 
223 String SfxThesSubMenuHelper::GetThesImplName( const lang::Locale &rLocale ) const
224 {
225     String aRes;
226     DBG_ASSERT( m_xLngMgr.is(), "LinguServiceManager missing" );
227     if (m_xLngMgr.is())
228     {
229         uno::Sequence< OUString > aServiceNames = m_xLngMgr->getConfiguredServices(
230                 OUString::createFromAscii("com.sun.star.linguistic2.Thesaurus"), rLocale );
231         // there should be at most one thesaurus configured for each language
232         DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
233         if (aServiceNames.getLength() == 1)
234             aRes = aServiceNames[0];
235     }
236     return aRes;
237 }
238 
239 ////////////////////////////////////////////////////////////
240 
241 
242