xref: /trunk/main/sw/source/ui/uno/dlelstnr.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 
32 
33 
34 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
35 #include <com/sun/star/linguistic2/XDictionaryList.hpp>
36 #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
37 #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp>
38 #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
39 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
40 
41 #include <unotools/lingucfg.hxx>
42 
43 #include <com/sun/star/uno/Reference.h>
44 #include <comphelper/processfactory.hxx>
45 #include <vos/mutex.hxx>
46 #include <vcl/svapp.hxx>
47 #include <tools/shl.hxx>
48 #include "dlelstnr.hxx"
49 #include <swmodule.hxx>
50 #include <wrtsh.hxx>
51 #include <view.hxx>
52 
53 
54 using ::rtl::OUString;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::frame;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::linguistic2;
60 using namespace ::com::sun::star::linguistic2::LinguServiceEventFlags;
61 
62 #define A2OU(x) OUString::createFromAscii(x)
63 
64 /* -----------------------------17.03.00 09:07--------------------------------
65 
66  ---------------------------------------------------------------------------*/
67 SwLinguServiceEventListener::SwLinguServiceEventListener()
68 {
69     Reference< XMultiServiceFactory > xMgr( comphelper::getProcessServiceFactory() );
70     if (xMgr.is())
71     {
72         try
73         {
74             OUString aSvcName( A2OU( "com.sun.star.frame.Desktop" ) );
75             xDesktop = Reference< frame::XDesktop >(
76                     xMgr->createInstance( aSvcName ), UNO_QUERY );
77             if (xDesktop.is())
78                 xDesktop->addTerminateListener( this );
79 
80             aSvcName = A2OU( "com.sun.star.linguistic2.LinguServiceManager" );
81             xLngSvcMgr = Reference< XLinguServiceManager >( xMgr->createInstance( aSvcName ), UNO_QUERY );
82             if (xLngSvcMgr.is())
83                 xLngSvcMgr->addLinguServiceManagerListener( (XLinguServiceEventListener *) this );
84 
85             if (SvtLinguConfig().HasGrammarChecker())
86             {
87                 aSvcName = A2OU( "com.sun.star.linguistic2.ProofreadingIterator" );
88                 xGCIterator = Reference< XProofreadingIterator >( xMgr->createInstance( aSvcName ), UNO_QUERY );
89                 Reference< XLinguServiceEventBroadcaster > xBC( xGCIterator, UNO_QUERY );
90                 if (xBC.is())
91                     xBC->addLinguServiceEventListener( (XLinguServiceEventListener *) this );
92             }
93         }
94         catch (uno::Exception &)
95         {
96             DBG_ASSERT(0, "exception caught in SwLinguServiceEventListener c-tor" );
97         }
98     }
99 }
100 /* -----------------------------17.03.00 09:07--------------------------------
101 
102  ---------------------------------------------------------------------------*/
103 SwLinguServiceEventListener::~SwLinguServiceEventListener()
104 {
105 }
106 
107 /* -----------------------------17.03.00 09:06--------------------------------
108 
109  ---------------------------------------------------------------------------*/
110 
111 void SwLinguServiceEventListener::processDictionaryListEvent(
112             const DictionaryListEvent& rDicListEvent)
113         throw( RuntimeException )
114 {
115     vos::OGuard aGuard(Application::GetSolarMutex());
116 
117     sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
118 
119     sal_Int16 nSpellWrongFlags =
120             DictionaryListEventFlags::ADD_POS_ENTRY     |
121             DictionaryListEventFlags::DEL_NEG_ENTRY     |
122             DictionaryListEventFlags::ACTIVATE_POS_DIC  |
123             DictionaryListEventFlags::DEACTIVATE_NEG_DIC;
124     sal_Bool bIsSpellWrong  =  0 != (nEvt & nSpellWrongFlags);
125     sal_Int16 nSpellAllFlags =
126             DictionaryListEventFlags::ADD_NEG_ENTRY     |
127             DictionaryListEventFlags::DEL_POS_ENTRY     |
128             DictionaryListEventFlags::ACTIVATE_NEG_DIC  |
129             DictionaryListEventFlags::DEACTIVATE_POS_DIC;
130     sal_Bool bIsSpellAll    =  0 != (nEvt & nSpellAllFlags);
131 
132     if (bIsSpellWrong || bIsSpellAll)
133         SW_MOD()->CheckSpellChanges( sal_False, bIsSpellWrong, bIsSpellAll, sal_False );
134 }
135 
136 
137 void SAL_CALL SwLinguServiceEventListener::processLinguServiceEvent(
138             const LinguServiceEvent& rLngSvcEvent )
139         throw(RuntimeException)
140 {
141     vos::OGuard aGuard(Application::GetSolarMutex());
142 
143     sal_Bool bIsSpellWrong = 0 != (rLngSvcEvent.nEvent & SPELL_WRONG_WORDS_AGAIN);
144     sal_Bool bIsSpellAll   = 0 != (rLngSvcEvent.nEvent & SPELL_CORRECT_WORDS_AGAIN);
145     if (0 != (rLngSvcEvent.nEvent & PROOFREAD_AGAIN))
146         bIsSpellWrong = bIsSpellAll = sal_True;     // have all spelling and grammar checked...
147     if (bIsSpellWrong || bIsSpellAll)
148     {
149         SW_MOD()->CheckSpellChanges( sal_False, bIsSpellWrong, bIsSpellAll, sal_False );
150     }
151     if (rLngSvcEvent.nEvent & HYPHENATE_AGAIN)
152     {
153         SwView *pSwView = SW_MOD()->GetFirstView();
154 
155         //!! since this function may be called within the ctor of
156         //!! SwView (during formatting) where the WrtShell is not yet
157         //!! created, we have to check for the WrtShellPtr to see
158         //!! if it is already availbale
159         while (pSwView && pSwView->GetWrtShellPtr())
160         {
161             pSwView->GetWrtShell().ChgHyphenation();
162             pSwView = SW_MOD()->GetNextView( pSwView );
163         }
164     }
165 }
166 
167 
168 void SAL_CALL SwLinguServiceEventListener::disposing(
169             const EventObject& rEventObj )
170         throw(RuntimeException)
171 {
172     vos::OGuard aGuard(Application::GetSolarMutex());
173 
174     if (xLngSvcMgr.is()  &&  rEventObj.Source == xLngSvcMgr)
175         xLngSvcMgr = 0;
176     if (xLngSvcMgr.is()  &&  rEventObj.Source == xGCIterator)
177         xGCIterator = 0;
178 }
179 
180 
181 void SAL_CALL SwLinguServiceEventListener::queryTermination(
182             const EventObject& /*rEventObj*/ )
183         throw(TerminationVetoException, RuntimeException)
184 {
185     //vos::OGuard aGuard(Application::GetSolarMutex());
186 }
187 
188 
189 void SAL_CALL SwLinguServiceEventListener::notifyTermination(
190             const EventObject& rEventObj )
191         throw(RuntimeException)
192 {
193     vos::OGuard aGuard(Application::GetSolarMutex());
194 
195     if (xDesktop.is()  &&  rEventObj.Source == xDesktop)
196     {
197         if (xLngSvcMgr.is())
198             xLngSvcMgr = 0;
199         if (xGCIterator.is())
200             xGCIterator = 0;
201         xDesktop = NULL;
202     }
203 }
204 
205