xref: /trunk/main/sw/source/ui/uno/dlelstnr.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 
29 
30 #include <com/sun/star/linguistic2/DictionaryListEventFlags.hpp>
31 #include <com/sun/star/linguistic2/XDictionaryList.hpp>
32 #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
33 #include <com/sun/star/linguistic2/XLinguServiceEventBroadcaster.hpp>
34 #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
35 #include <com/sun/star/linguistic2/LinguServiceEventFlags.hpp>
36 
37 #include <unotools/lingucfg.hxx>
38 
39 #include <com/sun/star/uno/Reference.h>
40 #include <comphelper/processfactory.hxx>
41 #include <vos/mutex.hxx>
42 #include <vcl/svapp.hxx>
43 #include <tools/shl.hxx>
44 #include "dlelstnr.hxx"
45 #include <swmodule.hxx>
46 #include <wrtsh.hxx>
47 #include <view.hxx>
48 
49 
50 using ::rtl::OUString;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::frame;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::linguistic2;
56 using namespace ::com::sun::star::linguistic2::LinguServiceEventFlags;
57 
58 #define A2OU(x) OUString::createFromAscii(x)
59 
60 /* -----------------------------17.03.00 09:07--------------------------------
61 
62  ---------------------------------------------------------------------------*/
SwLinguServiceEventListener()63 SwLinguServiceEventListener::SwLinguServiceEventListener()
64 {
65     Reference< XMultiServiceFactory > xMgr( comphelper::getProcessServiceFactory() );
66     if (xMgr.is())
67     {
68         try
69         {
70             OUString aSvcName( A2OU( "com.sun.star.frame.Desktop" ) );
71             xDesktop = Reference< frame::XDesktop >(
72                     xMgr->createInstance( aSvcName ), UNO_QUERY );
73             if (xDesktop.is())
74                 xDesktop->addTerminateListener( this );
75 
76             aSvcName = A2OU( "com.sun.star.linguistic2.LinguServiceManager" );
77             xLngSvcMgr = Reference< XLinguServiceManager >( xMgr->createInstance( aSvcName ), UNO_QUERY );
78             if (xLngSvcMgr.is())
79                 xLngSvcMgr->addLinguServiceManagerListener( (XLinguServiceEventListener *) this );
80 
81             if (SvtLinguConfig().HasGrammarChecker())
82             {
83                 aSvcName = A2OU( "com.sun.star.linguistic2.ProofreadingIterator" );
84                 xGCIterator = Reference< XProofreadingIterator >( xMgr->createInstance( aSvcName ), UNO_QUERY );
85                 Reference< XLinguServiceEventBroadcaster > xBC( xGCIterator, UNO_QUERY );
86                 if (xBC.is())
87                     xBC->addLinguServiceEventListener( (XLinguServiceEventListener *) this );
88             }
89         }
90         catch (uno::Exception &)
91         {
92             DBG_ASSERT(0, "exception caught in SwLinguServiceEventListener c-tor" );
93         }
94     }
95 }
96 /* -----------------------------17.03.00 09:07--------------------------------
97 
98  ---------------------------------------------------------------------------*/
~SwLinguServiceEventListener()99 SwLinguServiceEventListener::~SwLinguServiceEventListener()
100 {
101 }
102 
103 /* -----------------------------17.03.00 09:06--------------------------------
104 
105  ---------------------------------------------------------------------------*/
106 
processDictionaryListEvent(const DictionaryListEvent & rDicListEvent)107 void SwLinguServiceEventListener::processDictionaryListEvent(
108             const DictionaryListEvent& rDicListEvent)
109 {
110     vos::OGuard aGuard(Application::GetSolarMutex());
111 
112     sal_Int16 nEvt = rDicListEvent.nCondensedEvent;
113 
114     sal_Int16 nSpellWrongFlags =
115             DictionaryListEventFlags::ADD_POS_ENTRY     |
116             DictionaryListEventFlags::DEL_NEG_ENTRY     |
117             DictionaryListEventFlags::ACTIVATE_POS_DIC  |
118             DictionaryListEventFlags::DEACTIVATE_NEG_DIC;
119     sal_Bool bIsSpellWrong  =  0 != (nEvt & nSpellWrongFlags);
120     sal_Int16 nSpellAllFlags =
121             DictionaryListEventFlags::ADD_NEG_ENTRY     |
122             DictionaryListEventFlags::DEL_POS_ENTRY     |
123             DictionaryListEventFlags::ACTIVATE_NEG_DIC  |
124             DictionaryListEventFlags::DEACTIVATE_POS_DIC;
125     sal_Bool bIsSpellAll    =  0 != (nEvt & nSpellAllFlags);
126 
127     if (bIsSpellWrong || bIsSpellAll)
128         SW_MOD()->CheckSpellChanges( sal_False, bIsSpellWrong, bIsSpellAll, sal_False );
129 }
130 
131 
processLinguServiceEvent(const LinguServiceEvent & rLngSvcEvent)132 void SAL_CALL SwLinguServiceEventListener::processLinguServiceEvent(
133             const LinguServiceEvent& rLngSvcEvent )
134 {
135     vos::OGuard aGuard(Application::GetSolarMutex());
136 
137     sal_Bool bIsSpellWrong = 0 != (rLngSvcEvent.nEvent & SPELL_WRONG_WORDS_AGAIN);
138     sal_Bool bIsSpellAll   = 0 != (rLngSvcEvent.nEvent & SPELL_CORRECT_WORDS_AGAIN);
139     if (0 != (rLngSvcEvent.nEvent & PROOFREAD_AGAIN))
140         bIsSpellWrong = bIsSpellAll = sal_True;     // have all spelling and grammar checked...
141     if (bIsSpellWrong || bIsSpellAll)
142     {
143         SW_MOD()->CheckSpellChanges( sal_False, bIsSpellWrong, bIsSpellAll, sal_False );
144     }
145     if (rLngSvcEvent.nEvent & HYPHENATE_AGAIN)
146     {
147         SwView *pSwView = SW_MOD()->GetFirstView();
148 
149         //!! since this function may be called within the ctor of
150         //!! SwView (during formatting) where the WrtShell is not yet
151         //!! created, we have to check for the WrtShellPtr to see
152         //!! if it is already available
153         while (pSwView && pSwView->GetWrtShellPtr())
154         {
155             pSwView->GetWrtShell().ChgHyphenation();
156             pSwView = SW_MOD()->GetNextView( pSwView );
157         }
158     }
159 }
160 
161 
disposing(const EventObject & rEventObj)162 void SAL_CALL SwLinguServiceEventListener::disposing(
163             const EventObject& rEventObj )
164 {
165     vos::OGuard aGuard(Application::GetSolarMutex());
166 
167     if (xLngSvcMgr.is()  &&  rEventObj.Source == xLngSvcMgr)
168         xLngSvcMgr = 0;
169     if (xLngSvcMgr.is()  &&  rEventObj.Source == xGCIterator)
170         xGCIterator = 0;
171 }
172 
173 
queryTermination(const EventObject &)174 void SAL_CALL SwLinguServiceEventListener::queryTermination(
175             const EventObject& /*rEventObj*/ )
176 {
177     //vos::OGuard aGuard(Application::GetSolarMutex());
178 }
179 
180 
notifyTermination(const EventObject & rEventObj)181 void SAL_CALL SwLinguServiceEventListener::notifyTermination(
182             const EventObject& rEventObj )
183 {
184     vos::OGuard aGuard(Application::GetSolarMutex());
185 
186     if (xDesktop.is()  &&  rEventObj.Source == xDesktop)
187     {
188         if (xLngSvcMgr.is())
189             xLngSvcMgr = 0;
190         if (xGCIterator.is())
191             xGCIterator = 0;
192         xDesktop = NULL;
193     }
194 }
195