Files
libreoffice/linguistic/source/thesdsp.cxx

250 lines
7.8 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-11-17 11:37:46 +00:00
#include <i18nlangtag/lang.h>
2000-11-17 11:37:46 +00:00
#include <tools/debug.hxx>
#include <svl/lngmisc.hxx>
2000-11-17 11:37:46 +00:00
#include <cppuhelper/factory.hxx>
2000-11-17 11:37:46 +00:00
#include <com/sun/star/registry/XRegistryKey.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <comphelper/processfactory.hxx>
2000-11-17 11:37:46 +00:00
#include <osl/mutex.hxx>
#include "thesdsp.hxx"
#include <linguistic/lngprops.hxx>
2000-11-17 11:37:46 +00:00
using namespace osl;
using namespace com::sun::star;
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::linguistic2;
using namespace linguistic;
2011-02-26 15:54:59 +01:00
static bool SvcListHasLanguage(
const Sequence< Reference< XThesaurus > > &rRefs,
const Locale &rLocale )
{
bool bHasLanguage = false;
const Reference< XThesaurus > *pRef = rRefs.getConstArray();
sal_Int32 nLen = rRefs.getLength();
for (sal_Int32 k = 0; k < nLen && !bHasLanguage; ++k)
{
if (pRef[k].is())
bHasLanguage = pRef[k]->hasLocale( rLocale );
}
return bHasLanguage;
}
2000-11-17 11:37:46 +00:00
ThesaurusDispatcher::ThesaurusDispatcher()
{
}
ThesaurusDispatcher::~ThesaurusDispatcher()
{
ClearSvcList();
}
void ThesaurusDispatcher::ClearSvcList()
{
// release memory for each table entry
2008-12-15 12:01:46 +00:00
ThesSvcByLangMap_t aTmp;
aSvcMap.swap( aTmp );
2000-11-17 11:37:46 +00:00
}
Sequence< Locale > SAL_CALL
ThesaurusDispatcher::getLocales()
{
MutexGuard aGuard( GetLinguMutex() );
2008-12-15 12:01:46 +00:00
Sequence< Locale > aLocales( static_cast< sal_Int32 >(aSvcMap.size()) );
Locale *pLocales = aLocales.getArray();
ThesSvcByLangMap_t::const_iterator aIt;
for (aIt = aSvcMap.begin(); aIt != aSvcMap.end(); ++aIt)
2000-11-17 11:37:46 +00:00
{
*pLocales++ = LanguageTag::convertToLocale( aIt->first );
2000-11-17 11:37:46 +00:00
}
return aLocales;
}
sal_Bool SAL_CALL
ThesaurusDispatcher::hasLocale( const Locale& rLocale )
{
MutexGuard aGuard( GetLinguMutex() );
ThesSvcByLangMap_t::const_iterator aIt( aSvcMap.find( LinguLocaleToLanguage( rLocale ) ) );
2008-12-15 12:01:46 +00:00
return aIt != aSvcMap.end();
2000-11-17 11:37:46 +00:00
}
Sequence< Reference< XMeaning > > SAL_CALL
ThesaurusDispatcher::queryMeanings(
const OUString& rTerm, const Locale& rLocale,
const PropertyValues& rProperties )
{
MutexGuard aGuard( GetLinguMutex() );
Sequence< Reference< XMeaning > > aMeanings;
LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
if (LinguIsUnspecified( nLanguage) || rTerm.isEmpty())
2000-11-17 11:37:46 +00:00
return aMeanings;
// search for entry with that language
CWS-TOOLING: integrate CWS fwk125 2009-11-19 08:11:20 +0100 jsk r277554 : fwk125: #i105760# - removed warnlog, re-enabled autotest e_help.bas 2009-11-16 16:21:49 +0100 tl r277521 : #i106571# fixed locale list for dictionaries to get rid of duplicate entries 2009-11-12 08:47:31 +0100 hde r277469 : rework of hyphenation 2009-11-11 15:25:16 +0100 hde r277462 : rework of hyphenation 2009-11-11 15:24:09 +0100 hde r277461 : rework of hyphenation 2009-11-11 13:52:02 +0100 hde r277453 : new include for hyphenation tools 2009-11-10 12:28:51 +0100 tl r277427 : #i102304# fix for hyphenation dialog 2009-11-10 12:00:10 +0100 mav r277425 : #i10000# A workaround for pch and windows headers conflict 2009-11-10 11:58:32 +0100 mav r277424 : #i10000# A workaround for pch and windows headers conflict 2009-11-09 15:47:59 +0100 kso r277414 : #i16732# fixed url matching. 2009-11-09 11:51:40 +0100 mav r277406 : #i10000# use precompiled headers 2009-11-08 23:53:03 +0100 mav r277399 : #i10000# fix warning 2009-11-08 23:50:23 +0100 mav r277398 : #i10000# adapt for unix 2009-11-08 23:49:52 +0100 mav r277397 : #i10000# adapt for unix 2009-11-08 21:26:06 +0100 mav r277396 : CWS-TOOLING: rebase CWS fwk125 to branches/OOO320@277373 (milestone: OOO320:m4) 2009-11-06 16:30:27 +0100 ab r277394 : #i106100# Hold references to packages to speed up further calls to getDeployedPackages() 2009-11-06 15:58:48 +0100 mav r277392 : #i102464# check the Windows ACL 2009-11-06 15:58:23 +0100 mav r277391 : #i102464# check the Windows ACL 2009-11-04 11:54:06 +0100 tl r277342 : #i106497# incorrectly found 'supported languages' fixed 2009-11-04 10:40:51 +0100 tl r277335 : #i106571# dictionary updates for Polish and Swedish. 2009-10-28 09:54:41 +0100 tl r277236 : #i106100# context menu performance 2009-10-27 15:47:05 +0100 tl r277213 : #i106100# context menu performance 2009-10-23 17:31:56 +0200 mav r277170 : #i106075# use correct path to inprocserv.dll 2009-10-23 16:07:27 +0200 mav r277168 : #i105760# seek the temporary file to beginning also for URL case 2009-10-23 11:03:58 +0200 mav r277134 : CWS-TOOLING: rebase CWS fwk125 to branches/OOO320@276942 (milestone: OOO320:m2)
2009-11-23 16:33:39 +00:00
ThesSvcByLangMap_t::iterator aIt( aSvcMap.find( nLanguage ) );
LangSvcEntries_Thes *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : nullptr;
2000-11-17 11:37:46 +00:00
if (pEntry)
2000-11-17 11:37:46 +00:00
{
OUString aChkWord( rTerm );
aChkWord = aChkWord.replace( SVT_HARD_SPACE, ' ' );
RemoveHyphens( aChkWord );
if (IsIgnoreControlChars( rProperties, GetPropSet() ))
RemoveControlChars( aChkWord );
sal_Int32 nLen = pEntry->aSvcRefs.getLength();
2001-02-27 15:11:15 +00:00
DBG_ASSERT( nLen == pEntry->aSvcImplNames.getLength(),
2000-11-17 11:37:46 +00:00
"lng : sequence length mismatch");
2008-12-15 12:01:46 +00:00
DBG_ASSERT( pEntry->nLastTriedSvcIndex < nLen,
2000-11-17 11:37:46 +00:00
"lng : index out of range");
sal_Int32 i = 0;
2000-11-17 11:37:46 +00:00
// try already instantiated services first
{
const Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getConstArray();
2008-12-15 12:01:46 +00:00
while (i <= pEntry->nLastTriedSvcIndex
2000-11-17 11:37:46 +00:00
&& aMeanings.getLength() == 0)
{
if (pRef[i].is() && pRef[i]->hasLocale( rLocale ))
aMeanings = pRef[i]->queryMeanings( aChkWord, rLocale, rProperties );
2000-11-17 11:37:46 +00:00
++i;
}
}
// if still no result instantiate new services and try those
if (aMeanings.getLength() == 0
2008-12-15 12:01:46 +00:00
&& pEntry->nLastTriedSvcIndex < nLen - 1)
2000-11-17 11:37:46 +00:00
{
const OUString *pImplNames = pEntry->aSvcImplNames.getConstArray();
Reference< XThesaurus > *pRef = pEntry->aSvcRefs.getArray();
Reference< XComponentContext > xContext(
comphelper::getProcessComponentContext() );
// build service initialization argument
Sequence< Any > aArgs(1);
aArgs.getArray()[0] <<= GetPropSet();
2000-11-17 11:37:46 +00:00
while (i < nLen && aMeanings.getLength() == 0)
{
// create specific service via it's implementation name
Reference< XThesaurus > xThes;
try
2000-11-17 11:37:46 +00:00
{
xThes.set( xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
pImplNames[i], aArgs, xContext ),
UNO_QUERY );
2000-11-17 11:37:46 +00:00
}
catch (uno::Exception &)
{
SAL_WARN( "linguistic", "createInstanceWithArguments failed" );
}
pRef[i] = xThes;
if (xThes.is() && xThes->hasLocale( rLocale ))
aMeanings = xThes->queryMeanings( aChkWord, rLocale, rProperties );
pEntry->nLastTriedSvcIndex = (sal_Int16) i;
++i;
}
// if language is not supported by any of the services
// remove it from the list.
if (i == nLen && aMeanings.getLength() == 0)
{
if (!SvcListHasLanguage( pEntry->aSvcRefs, rLocale ))
aSvcMap.erase( nLanguage );
2000-11-17 11:37:46 +00:00
}
}
}
return aMeanings;
}
void ThesaurusDispatcher::SetServiceList( const Locale &rLocale,
const Sequence< OUString > &rSvcImplNames )
{
MutexGuard aGuard( GetLinguMutex() );
LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
2000-11-17 11:37:46 +00:00
sal_Int32 nLen = rSvcImplNames.getLength();
2008-12-15 12:01:46 +00:00
if (0 == nLen)
// remove entry
2008-12-15 12:01:46 +00:00
aSvcMap.erase( nLanguage );
2000-11-17 11:37:46 +00:00
else
{
// modify/add entry
2008-12-15 12:01:46 +00:00
LangSvcEntries_Thes *pEntry = aSvcMap[ nLanguage ].get();
if (pEntry)
{
2008-12-15 12:01:46 +00:00
pEntry->Clear();
pEntry->aSvcImplNames = rSvcImplNames;
2008-12-15 12:01:46 +00:00
pEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
}
else
{
std::shared_ptr< LangSvcEntries_Thes > pTmpEntry( new LangSvcEntries_Thes( rSvcImplNames ) );
2008-12-15 12:01:46 +00:00
pTmpEntry->aSvcRefs = Sequence< Reference < XThesaurus > >( nLen );
aSvcMap[ nLanguage ] = pTmpEntry;
}
2000-11-17 11:37:46 +00:00
}
}
Sequence< OUString >
ThesaurusDispatcher::GetServiceList( const Locale &rLocale ) const
2000-11-17 11:37:46 +00:00
{
MutexGuard aGuard( GetLinguMutex() );
Sequence< OUString > aRes;
// search for entry with that language and use data from that
LanguageType nLanguage = LinguLocaleToLanguage( rLocale );
const ThesSvcByLangMap_t::const_iterator aIt( aSvcMap.find( nLanguage ) );
const LangSvcEntries_Thes *pEntry = aIt != aSvcMap.end() ? aIt->second.get() : nullptr;
2000-11-17 11:37:46 +00:00
if (pEntry)
aRes = pEntry->aSvcImplNames;
return aRes;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */