xref: /aoo41x/main/sc/source/core/tool/chartlock.cxx (revision cdf0e10c)
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_sc.hxx"
30 
31 #include <vcl/svapp.hxx>
32 #include <svx/svditer.hxx>
33 #include <svx/svdoole2.hxx>
34 #include <svx/svdpage.hxx>
35 
36 #include "chartlock.hxx"
37 #include "document.hxx"
38 #include "drwlayer.hxx"
39 
40 using namespace com::sun::star;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::WeakReference;
43 
44 #define SC_CHARTLOCKTIMEOUT 660
45 
46 // ====================================================================
47 
48 namespace
49 {
50 
51 std::vector< WeakReference< frame::XModel > > lcl_getAllLivingCharts( ScDocument* pDoc )
52 {
53     std::vector< WeakReference< frame::XModel > > aRet;
54     if( !pDoc )
55         return aRet;
56     ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
57 	if (!pDrawLayer)
58 		return aRet;
59 
60 	for (SCTAB nTab=0; nTab<=pDoc->GetMaxTableNumber(); nTab++)
61 	{
62 		if (pDoc->HasTable(nTab))
63 		{
64 			SdrPage* pPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nTab));
65 			DBG_ASSERT(pPage,"Page ?");
66 
67 			SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
68 			SdrObject* pObject = aIter.Next();
69 			while (pObject)
70 			{
71                 if( pDoc->IsChart( pObject ) )
72                 {
73                     uno::Reference< embed::XEmbeddedObject > xIPObj = ((SdrOle2Obj*)pObject)->GetObjRef();
74                     uno::Reference< embed::XComponentSupplier > xCompSupp( xIPObj, uno::UNO_QUERY );
75                     if( xCompSupp.is())
76                     {
77                         Reference< frame::XModel > xModel( xCompSupp->getComponent(), uno::UNO_QUERY );
78                         if( xModel.is() )
79                             aRet.push_back( xModel );
80                     }
81                 }
82 				pObject = aIter.Next();
83 			}
84 		}
85 	}
86     return aRet;
87 }
88 
89 }//end anonymous namespace
90 
91 // === ScChartLockGuard ======================================
92 
93 ScChartLockGuard::ScChartLockGuard( ScDocument* pDoc ) :
94     maChartModels( lcl_getAllLivingCharts( pDoc ) )
95 {
96     std::vector< WeakReference< frame::XModel > >::const_iterator aIter = maChartModels.begin();
97     const std::vector< WeakReference< frame::XModel > >::const_iterator aEnd = maChartModels.end();
98     for( ; aIter != aEnd; ++aIter )
99     {
100         try
101         {
102             Reference< frame::XModel > xModel( *aIter );
103             if( xModel.is())
104                 xModel->lockControllers();
105         }
106         catch ( uno::Exception& )
107         {
108             DBG_ERROR("Unexpected exception in ScChartLockGuard");
109         }
110     }
111 }
112 
113 ScChartLockGuard::~ScChartLockGuard()
114 {
115     std::vector< WeakReference< frame::XModel > >::const_iterator aIter = maChartModels.begin();
116     const std::vector< WeakReference< frame::XModel > >::const_iterator aEnd = maChartModels.end();
117     for( ; aIter != aEnd; ++aIter )
118     {
119         try
120         {
121             Reference< frame::XModel > xModel( *aIter );
122             if( xModel.is())
123                 xModel->unlockControllers();
124         }
125         catch ( uno::Exception& )
126         {
127             DBG_ERROR("Unexpected exception in ScChartLockGuard");
128         }
129     }
130 }
131 
132 void ScChartLockGuard::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
133 {
134     if(!xModel.is())
135         return;
136 
137     WeakReference< frame::XModel > xWeakModel(xModel);
138 
139     std::vector< WeakReference< frame::XModel > >::iterator aFindIter(
140             ::std::find( maChartModels.begin(), maChartModels.end(), xWeakModel ) );
141 
142     if( aFindIter == maChartModels.end() )
143     {
144         try
145         {
146             xModel->lockControllers();
147             maChartModels.push_back( xModel );
148         }
149         catch ( uno::Exception& )
150         {
151             DBG_ERROR("Unexpected exception in ScChartLockGuard");
152         }
153     }
154 }
155 
156 // === ScTemporaryChartLock ======================================
157 
158 ScTemporaryChartLock::ScTemporaryChartLock( ScDocument* pDocP ) :
159 	mpDoc( pDocP )
160 {
161     maTimer.SetTimeout( SC_CHARTLOCKTIMEOUT );
162 	maTimer.SetTimeoutHdl( LINK( this, ScTemporaryChartLock, TimeoutHdl ) );
163 }
164 
165 
166 ScTemporaryChartLock::~ScTemporaryChartLock()
167 {
168     mpDoc = 0;
169     StopLocking();
170 }
171 
172 void ScTemporaryChartLock::StartOrContinueLocking()
173 {
174     if(!mapScChartLockGuard.get())
175         mapScChartLockGuard = std::auto_ptr< ScChartLockGuard >( new ScChartLockGuard(mpDoc) );
176 	maTimer.Start();
177 }
178 
179 void ScTemporaryChartLock::StopLocking()
180 {
181     maTimer.Stop();
182     mapScChartLockGuard.reset();
183 }
184 
185 void ScTemporaryChartLock::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
186 {
187     if(mapScChartLockGuard.get())
188         mapScChartLockGuard->AlsoLockThisChart( xModel );
189 }
190 
191 IMPL_LINK( ScTemporaryChartLock, TimeoutHdl, Timer*, EMPTYARG )
192 {
193 	mapScChartLockGuard.reset();
194 	return 0;
195 }
196