1*3b8558fdSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*3b8558fdSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3b8558fdSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3b8558fdSAndrew Rist  * distributed with this work for additional information
6*3b8558fdSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3b8558fdSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3b8558fdSAndrew Rist  * "License"); you may not use this file except in compliance
9*3b8558fdSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*3b8558fdSAndrew Rist  *
11*3b8558fdSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*3b8558fdSAndrew Rist  *
13*3b8558fdSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3b8558fdSAndrew Rist  * software distributed under the License is distributed on an
15*3b8558fdSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3b8558fdSAndrew Rist  * KIND, either express or implied.  See the License for the
17*3b8558fdSAndrew Rist  * specific language governing permissions and limitations
18*3b8558fdSAndrew Rist  * under the License.
19*3b8558fdSAndrew Rist  *
20*3b8558fdSAndrew Rist  *************************************************************/
21*3b8558fdSAndrew Rist 
22*3b8558fdSAndrew Rist 
23cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <sal/config.h>
26cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
27cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
28cdf0e10cSrcweir #include <com/sun/star/linguistic2/XGrammarChecker.hpp>
29cdf0e10cSrcweir #include <com/sun/star/i18n/XBreakIterator.hpp>
30cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
31cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
33cdf0e10cSrcweir #include "linguistic/misc.hxx"
34cdf0e10cSrcweir #include "defs.hxx"
35cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
36cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
40cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp>
41cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
42cdf0e10cSrcweir #include <com/sun/star/linguistic2/XSupportedLocales.hpp>
43cdf0e10cSrcweir #include <com/sun/star/linguistic2/SingleGrammarError.hpp>
44cdf0e10cSrcweir #include <com/sun/star/linguistic2/GrammarCheckingResult.hpp>
45cdf0e10cSrcweir #include "lngopt.hxx"
46cdf0e10cSrcweir #include <comphelper/extract.hxx>
47cdf0e10cSrcweir #include <unotools/processfactory.hxx>
48cdf0e10cSrcweir #include <map>
49cdf0e10cSrcweir #include <com/sun/star/text/TextMarkupType.hpp>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include "grammarchecker.hxx"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir using namespace ::utl;
54cdf0e10cSrcweir using namespace ::rtl;
55cdf0e10cSrcweir using namespace ::com::sun::star;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir ////////////////////////////////////////////////////////////
58cdf0e10cSrcweir 
GrammarChecker()59cdf0e10cSrcweir GrammarChecker::GrammarChecker( /*uno::Reference< uno::XComponentContext > const & context*/ )
60cdf0e10cSrcweir     /*m_xContext(context)*/
61cdf0e10cSrcweir {}
62cdf0e10cSrcweir 
~GrammarChecker()63cdf0e10cSrcweir GrammarChecker::~GrammarChecker()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
isSpellChecker()68cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::isSpellChecker() throw (uno::RuntimeException)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
71cdf0e10cSrcweir 	return sal_False;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 
hasLocale(const lang::Locale & aLocale)75cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::hasLocale( const lang::Locale & aLocale ) throw (uno::RuntimeException)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
78cdf0e10cSrcweir     (void) aLocale;
79cdf0e10cSrcweir 	return sal_False;
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
getLocales()82cdf0e10cSrcweir uno::Sequence< lang::Locale > SAL_CALL GrammarChecker::getLocales(  ) throw (uno::RuntimeException)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
85cdf0e10cSrcweir     return uno::Sequence< lang::Locale >();
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
startDocument(sal_Int32 nDocId)89cdf0e10cSrcweir void SAL_CALL GrammarChecker::startDocument(sal_Int32 nDocId)
90cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
93cdf0e10cSrcweir     (void) nDocId;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
startParagraph(sal_Int32 nDocId)96cdf0e10cSrcweir void SAL_CALL GrammarChecker::startParagraph(sal_Int32 nDocId)
97cdf0e10cSrcweir 	throw (uno::RuntimeException, lang::IllegalArgumentException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
100cdf0e10cSrcweir     (void) nDocId;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
endParagraph(sal_Int32 nDocId)103cdf0e10cSrcweir void SAL_CALL GrammarChecker::endParagraph( sal_Int32 nDocId )
104cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
107cdf0e10cSrcweir     (void) nDocId;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
endDocument(sal_Int32 nDocId)110cdf0e10cSrcweir void SAL_CALL GrammarChecker::endDocument(sal_Int32 nDocId)
111cdf0e10cSrcweir     throw (uno::RuntimeException, lang::IllegalArgumentException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
114cdf0e10cSrcweir     (void) nDocId;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
doGrammarChecking(sal_Int32 nDocId,const rtl::OUString & rText,const lang::Locale & rLocale,sal_Int32 nStartOfSentencePos,sal_Int32 nSuggestedSentenceEndPos,const uno::Sequence<::sal_Int32> & rLanguagePortions,const uno::Sequence<lang::Locale> & rLanguagePortionsLocales)117cdf0e10cSrcweir linguistic2::GrammarCheckingResult SAL_CALL GrammarChecker::doGrammarChecking(
118cdf0e10cSrcweir         sal_Int32 nDocId,
119cdf0e10cSrcweir         const rtl::OUString& rText,
120cdf0e10cSrcweir         const lang::Locale& rLocale,
121cdf0e10cSrcweir         sal_Int32 nStartOfSentencePos,
122cdf0e10cSrcweir         sal_Int32 nSuggestedSentenceEndPos,
123cdf0e10cSrcweir         const uno::Sequence< ::sal_Int32 >& rLanguagePortions,
124cdf0e10cSrcweir         const uno::Sequence< lang::Locale >& rLanguagePortionsLocales )
125cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     (void) rLanguagePortions;
130cdf0e10cSrcweir     (void) rLanguagePortionsLocales;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     linguistic2::GrammarCheckingResult  aRes;
133cdf0e10cSrcweir     aRes.nDocumentId                = nDocId;
134cdf0e10cSrcweir     aRes.aText                      = rText;
135cdf0e10cSrcweir     aRes.aLocale                    = rLocale;
136cdf0e10cSrcweir     aRes.nEndOfSentencePos          = nSuggestedSentenceEndPos;
137cdf0e10cSrcweir     aRes.xGrammarChecker            = this;
138cdf0e10cSrcweir     aRes.aGrammarErrors             = GrammarCheckingInDummy( nDocId, rText, rLocale, nStartOfSentencePos, nSuggestedSentenceEndPos );;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     return aRes;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
GrammarCheckingInDummy(sal_Int32 nDocId,const OUString & rFlatParaText,const lang::Locale & rLocale,sal_Int32 nStartOfSentencePos,sal_Int32 nSuggestedSentenceEndPos)143cdf0e10cSrcweir uno::Sequence< linguistic2::SingleGrammarError > GrammarChecker::GrammarCheckingInDummy(
144cdf0e10cSrcweir 	sal_Int32 nDocId,
145cdf0e10cSrcweir     const OUString & rFlatParaText,
146cdf0e10cSrcweir     const lang::Locale & rLocale,
147cdf0e10cSrcweir     sal_Int32 nStartOfSentencePos,
148cdf0e10cSrcweir     sal_Int32 nSuggestedSentenceEndPos )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 	(void) nDocId;
151cdf0e10cSrcweir     (void) rFlatParaText;
152cdf0e10cSrcweir     (void) rLocale;
153cdf0e10cSrcweir     (void) nStartOfSentencePos;
154cdf0e10cSrcweir     (void) nSuggestedSentenceEndPos;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     typedef std::map< OUString, uno::Sequence<OUString> > Error_t;
158cdf0e10cSrcweir 	Error_t aError;
159cdf0e10cSrcweir     uno::Sequence< OUString > aSuggestion(1);
160cdf0e10cSrcweir     OUString *pSeggestion = aSuggestion.getArray();
161cdf0e10cSrcweir 	pSeggestion[0] = OUString::createFromAscii("Modified");
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     aError[OUString::createFromAscii("GrammarError")]  = aSuggestion;
164cdf0e10cSrcweir     aError[OUString::createFromAscii("Grammar Error")] = aSuggestion;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	typedef std::vector< linguistic2::SingleGrammarError> ErrorVector_t;
167cdf0e10cSrcweir 	ErrorVector_t aErrorVector;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     OUString aText = rFlatParaText.copy( nStartOfSentencePos, nSuggestedSentenceEndPos - nStartOfSentencePos );
170cdf0e10cSrcweir 	sal_Int32 nIndexOf = 0;
171cdf0e10cSrcweir 	for(Error_t::const_iterator it = aError.begin(); it != aError.end(); ++it)
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         while(nIndexOf >= 0)
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir             nIndexOf=aText.indexOf(it->first, nIndexOf);
177cdf0e10cSrcweir 			if(nIndexOf > -1)
178cdf0e10cSrcweir 			{
179cdf0e10cSrcweir 				//error found
180cdf0e10cSrcweir 				linguistic2::SingleGrammarError aErr;
181cdf0e10cSrcweir                 aErr.nErrorStart        = nIndexOf + nStartOfSentencePos;
182cdf0e10cSrcweir 				nIndexOf += it->first.getLength();
183cdf0e10cSrcweir                 aErr.nErrorLength       = it->first.getLength();
184cdf0e10cSrcweir                 aErr.nErrorType         = text::TextMarkupType::GRAMMAR;
185cdf0e10cSrcweir                 aErr.nErrorLevel        = 0;
186cdf0e10cSrcweir                 aErr.aShortComment      = OUString();
187cdf0e10cSrcweir                 aErr.aFullComment       = OUString();
188cdf0e10cSrcweir                 aErr.aNewLocale         = rLocale;
189cdf0e10cSrcweir                 aErr.aSuggestions       = it->second;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir                 aErrorVector.push_back( aErr );
192cdf0e10cSrcweir 			}
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 		nIndexOf = 0;
195cdf0e10cSrcweir 	}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	sal_Int32 nCount = aErrorVector.size();
198cdf0e10cSrcweir 	uno::Sequence< linguistic2::SingleGrammarError > aErrors( nCount );
199cdf0e10cSrcweir 	if( nCount > 0 )
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		linguistic2::SingleGrammarError* pErrors = aErrors.getArray();
202cdf0e10cSrcweir 		for (sal_Int32 i=0; i < nCount; ++i)
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			pErrors[i] = aErrorVector[i];
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 	return aErrors;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
hasOptionsDialog()211cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::hasOptionsDialog(  ) throw (uno::RuntimeException)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
214cdf0e10cSrcweir     return sal_False;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
runOptionsDialog()217cdf0e10cSrcweir void SAL_CALL GrammarChecker::runOptionsDialog()
218cdf0e10cSrcweir     throw (uno::RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
dispose()223cdf0e10cSrcweir void SAL_CALL GrammarChecker::dispose(  ) throw (uno::RuntimeException)
224cdf0e10cSrcweir {
225cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
addEventListener(const uno::Reference<lang::XEventListener> & xListener)228cdf0e10cSrcweir void SAL_CALL GrammarChecker::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
229cdf0e10cSrcweir     throw (uno::RuntimeException)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
232cdf0e10cSrcweir     (void) xListener;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)235cdf0e10cSrcweir void SAL_CALL GrammarChecker::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
236cdf0e10cSrcweir     throw (uno::RuntimeException)
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard(GetMutex());
239cdf0e10cSrcweir     (void) xListener;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
supportsService(const OUString & ServiceName)242cdf0e10cSrcweir sal_Bool SAL_CALL GrammarChecker::supportsService( const OUString& ServiceName ) throw(uno::RuntimeException)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	uno::Sequence< OUString > aSNL = getSupportedServiceNames();
247cdf0e10cSrcweir 	const OUString * pArray = aSNL.getConstArray();
248cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); ++i )
249cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
250cdf0e10cSrcweir 			return sal_True;
251cdf0e10cSrcweir 	return sal_False;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
getSupportedServiceNames_Static()254cdf0e10cSrcweir uno::Sequence< OUString > GrammarChecker::getSupportedServiceNames_Static(  ) throw()
255cdf0e10cSrcweir {
256cdf0e10cSrcweir 	//osl::Guard< osl::Mutex > aGuard(GetMutex());
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	uno::Sequence< OUString > aSNS( 1 );	// auch mehr als 1 Service moeglich
259cdf0e10cSrcweir 	aSNS.getArray()[0] = A2OU( "com.sun.star.linguistic2.GrammarChecker" );//SN_LINGU_SERVCICE_MANAGER
260cdf0e10cSrcweir 	return aSNS;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
getSupportedServiceNames()263cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL GrammarChecker::getSupportedServiceNames(  ) throw(uno::RuntimeException)
264cdf0e10cSrcweir {
265cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
266cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
getImplementationName()269cdf0e10cSrcweir OUString SAL_CALL GrammarChecker::getImplementationName(  ) throw(uno::RuntimeException)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	osl::Guard< osl::Mutex > aGuard(GetMutex());
272cdf0e10cSrcweir 	return getImplementationName_Static();
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
GrammarChecker_CreateInstance(const uno::Reference<lang::XMultiServiceFactory> &)275cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL GrammarChecker_CreateInstance(
276cdf0e10cSrcweir         const uno::Reference< lang::XMultiServiceFactory > & /*rSMgr*/ )
277cdf0e10cSrcweir     throw(uno::Exception)
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     uno::Reference< uno::XInterface > xService = (cppu::OWeakObject*) new GrammarChecker;
280cdf0e10cSrcweir 	return xService;
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
GrammarChecker_getFactory(const sal_Char * pImplName,lang::XMultiServiceFactory * pServiceManager,void *)283cdf0e10cSrcweir void * SAL_CALL GrammarChecker_getFactory( const sal_Char * pImplName, lang::XMultiServiceFactory * pServiceManager, void * /*pRegistryKey*/ )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	void * pRet = 0;
287cdf0e10cSrcweir 	if ( !GrammarChecker::getImplementationName_Static().compareToAscii( pImplName ) )
288cdf0e10cSrcweir 	{
289cdf0e10cSrcweir         uno::Reference< lang::XSingleServiceFactory > xFactory =
290cdf0e10cSrcweir 			cppu::createOneInstanceFactory(
291cdf0e10cSrcweir 				pServiceManager,
292cdf0e10cSrcweir 				GrammarChecker::getImplementationName_Static(),
293cdf0e10cSrcweir 				GrammarChecker_CreateInstance,
294cdf0e10cSrcweir 				GrammarChecker::getSupportedServiceNames_Static());
295cdf0e10cSrcweir 		// acquire, because we return an interface pointer instead of a reference
296cdf0e10cSrcweir 		xFactory->acquire();
297cdf0e10cSrcweir 		pRet = xFactory.get();
298cdf0e10cSrcweir 	}
299cdf0e10cSrcweir 	return pRet;
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302