Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <vcl/svapp.hxx>
22 : #include <svx/svditer.hxx>
23 : #include <svx/svdoole2.hxx>
24 : #include <svx/svdpage.hxx>
25 :
26 : #include "chartlock.hxx"
27 : #include "document.hxx"
28 : #include "drwlayer.hxx"
29 :
30 : using namespace com::sun::star;
31 : using ::com::sun::star::uno::Reference;
32 : using ::com::sun::star::uno::WeakReference;
33 :
34 : #define SC_CHARTLOCKTIMEOUT 660
35 :
36 : // ====================================================================
37 :
38 : namespace
39 : {
40 :
41 0 : std::vector< WeakReference< frame::XModel > > lcl_getAllLivingCharts( ScDocument* pDoc )
42 : {
43 0 : std::vector< WeakReference< frame::XModel > > aRet;
44 0 : if( !pDoc )
45 0 : return aRet;
46 0 : ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
47 0 : if (!pDrawLayer)
48 0 : return aRet;
49 :
50 0 : for (SCTAB nTab=0; nTab<=pDoc->GetMaxTableNumber(); nTab++)
51 : {
52 0 : if (pDoc->HasTable(nTab))
53 : {
54 0 : SdrPage* pPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nTab));
55 : OSL_ENSURE(pPage,"Page ?");
56 :
57 0 : SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
58 0 : SdrObject* pObject = aIter.Next();
59 0 : while (pObject)
60 : {
61 0 : if( pDoc->IsChart( pObject ) )
62 : {
63 0 : uno::Reference< embed::XEmbeddedObject > xIPObj = ((SdrOle2Obj*)pObject)->GetObjRef();
64 0 : uno::Reference< embed::XComponentSupplier > xCompSupp( xIPObj, uno::UNO_QUERY );
65 0 : if( xCompSupp.is())
66 : {
67 0 : Reference< frame::XModel > xModel( xCompSupp->getComponent(), uno::UNO_QUERY );
68 0 : if( xModel.is() )
69 0 : aRet.push_back( xModel );
70 0 : }
71 : }
72 0 : pObject = aIter.Next();
73 0 : }
74 : }
75 : }
76 0 : return aRet;
77 : }
78 :
79 : }//end anonymous namespace
80 :
81 : // === ScChartLockGuard ======================================
82 :
83 0 : ScChartLockGuard::ScChartLockGuard( ScDocument* pDoc ) :
84 0 : maChartModels( lcl_getAllLivingCharts( pDoc ) )
85 : {
86 0 : std::vector< WeakReference< frame::XModel > >::const_iterator aIter = maChartModels.begin();
87 0 : const std::vector< WeakReference< frame::XModel > >::const_iterator aEnd = maChartModels.end();
88 0 : for( ; aIter != aEnd; ++aIter )
89 : {
90 : try
91 : {
92 0 : Reference< frame::XModel > xModel( *aIter );
93 0 : if( xModel.is())
94 0 : xModel->lockControllers();
95 : }
96 0 : catch ( uno::Exception& )
97 : {
98 : OSL_FAIL("Unexpected exception in ScChartLockGuard");
99 : }
100 : }
101 0 : }
102 :
103 0 : ScChartLockGuard::~ScChartLockGuard()
104 : {
105 0 : std::vector< WeakReference< frame::XModel > >::const_iterator aIter = maChartModels.begin();
106 0 : const std::vector< WeakReference< frame::XModel > >::const_iterator aEnd = maChartModels.end();
107 0 : for( ; aIter != aEnd; ++aIter )
108 : {
109 : try
110 : {
111 0 : Reference< frame::XModel > xModel( *aIter );
112 0 : if( xModel.is())
113 0 : xModel->unlockControllers();
114 : }
115 0 : catch ( uno::Exception& )
116 : {
117 : OSL_FAIL("Unexpected exception in ScChartLockGuard");
118 : }
119 : }
120 0 : }
121 :
122 0 : void ScChartLockGuard::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
123 : {
124 0 : if(!xModel.is())
125 0 : return;
126 :
127 0 : WeakReference< frame::XModel > xWeakModel(xModel);
128 :
129 : std::vector< WeakReference< frame::XModel > >::iterator aFindIter(
130 0 : ::std::find( maChartModels.begin(), maChartModels.end(), xWeakModel ) );
131 :
132 0 : if( aFindIter == maChartModels.end() )
133 : {
134 : try
135 : {
136 0 : xModel->lockControllers();
137 0 : maChartModels.push_back( xModel );
138 : }
139 0 : catch ( uno::Exception& )
140 : {
141 : OSL_FAIL("Unexpected exception in ScChartLockGuard");
142 : }
143 0 : }
144 : }
145 :
146 : // === ScTemporaryChartLock ======================================
147 :
148 197 : ScTemporaryChartLock::ScTemporaryChartLock( ScDocument* pDocP ) :
149 197 : mpDoc( pDocP )
150 : {
151 197 : maTimer.SetTimeout( SC_CHARTLOCKTIMEOUT );
152 197 : maTimer.SetTimeoutHdl( LINK( this, ScTemporaryChartLock, TimeoutHdl ) );
153 197 : }
154 :
155 :
156 333 : ScTemporaryChartLock::~ScTemporaryChartLock()
157 : {
158 111 : mpDoc = 0;
159 111 : StopLocking();
160 222 : }
161 :
162 0 : void ScTemporaryChartLock::StartOrContinueLocking()
163 : {
164 0 : if(!mapScChartLockGuard.get())
165 0 : mapScChartLockGuard = std::auto_ptr< ScChartLockGuard >( new ScChartLockGuard(mpDoc) );
166 0 : maTimer.Start();
167 0 : }
168 :
169 113 : void ScTemporaryChartLock::StopLocking()
170 : {
171 113 : maTimer.Stop();
172 113 : mapScChartLockGuard.reset();
173 113 : }
174 :
175 0 : void ScTemporaryChartLock::AlsoLockThisChart( const Reference< frame::XModel >& xModel )
176 : {
177 0 : if(mapScChartLockGuard.get())
178 0 : mapScChartLockGuard->AlsoLockThisChart( xModel );
179 0 : }
180 :
181 0 : IMPL_LINK_NOARG(ScTemporaryChartLock, TimeoutHdl)
182 : {
183 0 : mapScChartLockGuard.reset();
184 0 : return 0;
185 : }
186 :
187 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|