LibreOffice Module sc (master)  1
chartlock.cxx
Go to the documentation of this file.
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 #include <svx/svditer.hxx>
21 #include <svx/svdoole2.hxx>
22 #include <svx/svdpage.hxx>
23 #include <tools/diagnose_ex.h>
24 
25 #include <chartlock.hxx>
26 #include <document.hxx>
27 #include <drwlayer.hxx>
28 
29 #include <com/sun/star/embed/XEmbeddedObject.hpp>
30 #include <com/sun/star/embed/XComponentSupplier.hpp>
31 #include <com/sun/star/frame/XModel.hpp>
32 
33 using namespace com::sun::star;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::WeakReference;
36 
37 #define SC_CHARTLOCKTIMEOUT 660
38 
39 namespace
40 {
41 
42 std::vector< WeakReference< frame::XModel > > lcl_getAllLivingCharts( ScDocument* pDoc )
43 {
44  std::vector< WeakReference< frame::XModel > > aRet;
45  if( !pDoc )
46  return aRet;
47  ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
48  if (!pDrawLayer)
49  return aRet;
50 
51  for (SCTAB nTab=0; nTab<=pDoc->GetMaxTableNumber(); nTab++)
52  {
53  if (pDoc->HasTable(nTab))
54  {
55  SdrPage* pPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nTab));
56  OSL_ENSURE(pPage,"Page ?");
57 
58  SdrObjListIter aIter( pPage, SdrIterMode::DeepNoGroups );
59  SdrObject* pObject = aIter.Next();
60  while (pObject)
61  {
62  if( ScDocument::IsChart( pObject ) )
63  {
64  uno::Reference< embed::XEmbeddedObject > xIPObj = static_cast<SdrOle2Obj*>(pObject)->GetObjRef();
65  uno::Reference< embed::XComponentSupplier > xCompSupp = xIPObj;
66  if( xCompSupp.is())
67  {
68  Reference< frame::XModel > xModel( xCompSupp->getComponent(), uno::UNO_QUERY );
69  if( xModel.is() )
70  aRet.emplace_back(xModel );
71  }
72  }
73  pObject = aIter.Next();
74  }
75  }
76  }
77  return aRet;
78 }
79 
80 }//end anonymous namespace
81 
82 // ScChartLockGuard
84  maChartModels( lcl_getAllLivingCharts( pDoc ) )
85 {
86  for( const auto& rxChartModel : maChartModels )
87  {
88  try
89  {
90  Reference< frame::XModel > xModel( rxChartModel );
91  if( xModel.is())
92  xModel->lockControllers();
93  }
94  catch ( uno::Exception& )
95  {
96  TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
97  }
98  }
99 }
100 
102 {
103  for( const auto& rxChartModel : maChartModels )
104  {
105  try
106  {
107  Reference< frame::XModel > xModel( rxChartModel );
108  if( xModel.is())
109  xModel->unlockControllers();
110  }
111  catch ( uno::Exception& )
112  {
113  TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
114  }
115  }
116 }
117 
119 {
120  if(!xModel.is())
121  return;
122 
123  WeakReference< frame::XModel > xWeakModel(xModel);
124 
125  std::vector< WeakReference< frame::XModel > >::iterator aFindIter(
126  ::std::find( maChartModels.begin(), maChartModels.end(), xWeakModel ) );
127 
128  if( aFindIter == maChartModels.end() )
129  {
130  try
131  {
132  xModel->lockControllers();
133  maChartModels.emplace_back(xModel );
134  }
135  catch ( uno::Exception& )
136  {
137  TOOLS_WARN_EXCEPTION( "sc", "Unexpected exception in ScChartLockGuard");
138  }
139  }
140 }
141 
142 // ScTemporaryChartLock
144  mpDoc( pDocP )
145 {
147  maTimer.SetInvokeHandler( LINK( this, ScTemporaryChartLock, TimeoutHdl ) );
148 }
149 
151 {
152  mpDoc = nullptr;
153  StopLocking();
154 }
155 
157 {
158  if (!mapScChartLockGuard)
160  maTimer.Start();
161 }
162 
164 {
165  maTimer.Stop();
166  mapScChartLockGuard.reset();
167 }
168 
170 {
172  mapScChartLockGuard->AlsoLockThisChart( xModel );
173 }
174 
176 {
177  mapScChartLockGuard.reset();
178 }
179 
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SC_CHARTLOCKTIMEOUT
Definition: chartlock.cxx:37
void AlsoLockThisChart(const css::uno::Reference< css::frame::XModel > &xModel)
Definition: chartlock.cxx:118
Use this to lock all charts in the calc for a little time.
Definition: chartlock.hxx:53
std::vector< css::uno::WeakReference< css::frame::XModel > > maChartModels
Definition: chartlock.hxx:45
EmbeddedObjectRef * pObject
void StartOrContinueLocking()
Definition: chartlock.cxx:156
void AlsoLockThisChart(const css::uno::Reference< css::frame::XModel > &xModel)
Definition: chartlock.cxx:169
IMPL_LINK_NOARG(ScTemporaryChartLock, TimeoutHdl, Timer *, void)
Definition: chartlock.cxx:175
SC_DLLPUBLIC bool HasTable(SCTAB nTab) const
Definition: document.cxx:189
SCTAB GetMaxTableNumber() const
Definition: document.hxx:803
All current charts in the calc will be locked in constructor and unlocked in destructor.
Definition: chartlock.hxx:36
SC_DLLPUBLIC ScDrawLayer * GetDrawLayer()
Definition: document.hxx:1056
#define TOOLS_WARN_EXCEPTION(area, stream)
const SdrPage * GetPage(sal_uInt16 nPgNum) const
ScChartLockGuard(ScDocument *pDoc)
Definition: chartlock.cxx:83
virtual void Start() override
void SetTimeout(sal_uInt64 nTimeoutMs)
ScTemporaryChartLock(ScDocument *pDoc)
Definition: chartlock.cxx:143
SdrObject * Next()
void Stop()
std::unique_ptr< ScChartLockGuard > mapScChartLockGuard
Definition: chartlock.hxx:66
void SetInvokeHandler(const Link< Timer *, void > &rLink)
static bool IsChart(const SdrObject *pObject)
Definition: documen9.cxx:201
Reference< XModel > xModel
sal_Int16 SCTAB
Definition: types.hxx:23
ScDocument * mpDoc
Definition: chartlock.hxx:64