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 :
21 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : #include <com/sun/star/linguistic2/XThesaurus.hpp>
23 : #include <com/sun/star/linguistic2/XMeaning.hpp>
24 : #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
25 :
26 : #include <comphelper/processfactory.hxx>
27 : #include <svl/stritem.hxx>
28 : #include <tools/debug.hxx>
29 : #include <vcl/graph.hxx>
30 : #include <vcl/graphicfilter.hxx>
31 :
32 :
33 : #include <vector>
34 :
35 : #include <sfx2/bindings.hxx>
36 : #include <sfx2/dispatch.hxx>
37 : #include <sfx2/viewsh.hxx>
38 : #include "thessubmenu.hxx"
39 :
40 : using namespace ::com::sun::star;
41 :
42 0 : OUString SfxThesSubMenuHelper::GetText(
43 : const String &rLookUpString,
44 : xub_StrLen nDelimPos )
45 : {
46 0 : return OUString( rLookUpString.Copy( 0, nDelimPos ) );
47 : }
48 :
49 :
50 0 : void SfxThesSubMenuHelper::GetLocale(
51 : lang::Locale /*out */ &rLocale,
52 : const String &rLookUpString,
53 : xub_StrLen nDelimPos )
54 : {
55 0 : String aIsoLang( rLookUpString.Copy( nDelimPos + 1) );
56 0 : const xub_StrLen nPos = aIsoLang.Search( '-' );
57 0 : if (nPos != STRING_NOTFOUND)
58 : {
59 0 : rLocale.Language = aIsoLang.Copy( 0, nPos );
60 0 : rLocale.Country = aIsoLang.Copy( nPos + 1 );
61 0 : rLocale.Variant = String::EmptyString();
62 0 : }
63 0 : }
64 :
65 :
66 0 : SfxThesSubMenuHelper::SfxThesSubMenuHelper():
67 : m_xLngMgr(
68 : linguistic2::LinguServiceManager::create(
69 : comphelper::getProcessComponentContext())),
70 0 : m_xThesarus(m_xLngMgr->getThesaurus())
71 : {
72 0 : }
73 :
74 :
75 0 : SfxThesSubMenuHelper::~SfxThesSubMenuHelper()
76 : {
77 0 : }
78 :
79 :
80 0 : bool SfxThesSubMenuHelper::IsSupportedLocale( const lang::Locale & rLocale ) const
81 : {
82 0 : return m_xThesarus.is() && m_xThesarus->hasLocale( rLocale );
83 : }
84 :
85 :
86 0 : bool SfxThesSubMenuHelper::GetMeanings(
87 : std::vector< OUString > & rSynonyms,
88 : const OUString & rWord,
89 : const lang::Locale & rLocale,
90 : sal_Int16 nMaxSynonms )
91 : {
92 0 : bool bHasMoreSynonyms = false;
93 0 : rSynonyms.clear();
94 0 : if (IsSupportedLocale( rLocale ) && !rWord.isEmpty() && nMaxSynonms > 0)
95 : {
96 : try
97 : {
98 : // get all meannings
99 : const uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeaningSeq(
100 0 : m_xThesarus->queryMeanings( rWord, rLocale, uno::Sequence< beans::PropertyValue >() ));
101 0 : const uno::Reference< linguistic2::XMeaning > *pxMeaning = aMeaningSeq.getConstArray();
102 0 : const sal_Int32 nMeanings = aMeaningSeq.getLength();
103 :
104 : // iterate over all meanings until nMaxSynonms are found or all meanings are processed
105 0 : sal_Int32 nCount = 0;
106 0 : sal_Int32 i = 0;
107 0 : for ( ; i < nMeanings && nCount < nMaxSynonms; ++i)
108 : {
109 0 : const uno::Sequence< OUString > aSynonymSeq( pxMeaning[i]->querySynonyms() );
110 0 : const OUString *pSynonyms = aSynonymSeq.getConstArray();
111 0 : const sal_Int32 nSynonyms = aSynonymSeq.getLength();
112 0 : sal_Int32 k = 0;
113 0 : for ( ; k < nSynonyms && nCount < nMaxSynonms; ++k)
114 : {
115 0 : rSynonyms.push_back( pSynonyms[k] );
116 0 : ++nCount;
117 : }
118 0 : bHasMoreSynonyms = k < nSynonyms; // any synonym from this meaning skipped?
119 0 : }
120 :
121 0 : bHasMoreSynonyms |= i < nMeanings; // any meaning skipped?
122 : }
123 0 : catch (const uno::Exception &)
124 : {
125 : DBG_ASSERT( 0, "failed to get synonyms" );
126 : }
127 : }
128 0 : return bHasMoreSynonyms;
129 : }
130 :
131 :
132 0 : String SfxThesSubMenuHelper::GetThesImplName( const lang::Locale &rLocale ) const
133 : {
134 0 : String aRes;
135 0 : uno::Sequence< OUString > aServiceNames = m_xLngMgr->getConfiguredServices(
136 0 : OUString("com.sun.star.linguistic2.Thesaurus"), rLocale );
137 : // there should be at most one thesaurus configured for each language
138 : DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
139 0 : if (aServiceNames.getLength() == 1)
140 0 : aRes = aServiceNames[0];
141 0 : return aRes;
142 408 : }
143 :
144 :
145 :
146 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|