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