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 <macromgr.hxx>
21 #include <document.hxx>
22
23 #include <basic/basmgr.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <sfx2/objsh.hxx>
26 #include <formulacell.hxx>
27 #include <vector>
28 #include <com/sun/star/container/XContainer.hpp>
29 #include <com/sun/star/script/XLibraryContainer.hpp>
30
31 using namespace ::com::sun::star;
32 using ::com::sun::star::uno::RuntimeException;
33 using ::com::sun::star::uno::Reference;
34 using ::std::vector;
35 using ::std::pair;
36
37 /**
38 * A simple container to keep track of cells that depend on basic modules
39 * changes. We don't check for duplicates at insertion time; instead, we
40 * remove duplicates at query time.
41 */
42 class ScUserMacroDepTracker
43 {
44 public:
addCell(const OUString & rModuleName,ScFormulaCell * pCell)45 void addCell(const OUString& rModuleName, ScFormulaCell* pCell)
46 {
47 ModuleCellMap::iterator itr = maCells.find(rModuleName);
48 if (itr == maCells.end())
49 {
50 pair<ModuleCellMap::iterator, bool> r = maCells.emplace(
51 rModuleName, vector<ScFormulaCell*>());
52
53 if (!r.second)
54 // insertion failed.
55 return;
56
57 itr = r.first;
58 }
59 itr->second.push_back(pCell);
60 }
61
removeCell(const ScFormulaCell * pCell)62 void removeCell(const ScFormulaCell* pCell)
63 {
64 for (auto& rEntry : maCells)
65 {
66 rEntry.second.erase(std::remove(rEntry.second.begin(), rEntry.second.end(), pCell), rEntry.second.end() );
67 }
68 }
69
getCellsByModule(const OUString & rModuleName,vector<ScFormulaCell * > & rCells)70 void getCellsByModule(const OUString& rModuleName, vector<ScFormulaCell*>& rCells)
71 {
72 ModuleCellMap::iterator itr = maCells.find(rModuleName);
73 if (itr == maCells.end())
74 return;
75
76 vector<ScFormulaCell*>& rCellList = itr->second;
77
78 // Remove duplicates.
79 std::sort(rCellList.begin(), rCellList.end());
80 auto last = std::unique(rCellList.begin(), rCellList.end());
81 rCellList.erase(last, rCellList.end());
82
83 // exception safe copy
84 vector<ScFormulaCell*> temp(rCellList);
85 rCells.swap(temp);
86 }
87
88 private:
89 typedef std::unordered_map<OUString, vector<ScFormulaCell*>> ModuleCellMap;
90 ModuleCellMap maCells;
91 };
92
ScMacroManager(ScDocument * pDoc)93 ScMacroManager::ScMacroManager(ScDocument* pDoc) :
94 mpDepTracker(new ScUserMacroDepTracker),
95 mpDoc(pDoc)
96 {
97 }
98
~ScMacroManager()99 ScMacroManager::~ScMacroManager()
100 {
101 }
102
103 typedef ::cppu::WeakImplHelper< css::container::XContainerListener > ContainerListenerHelper;
104
105 class VBAProjectListener : public ContainerListenerHelper
106 {
107 ScMacroManager* mpMacroMgr;
108 public:
VBAProjectListener(ScMacroManager * pMacroMgr)109 explicit VBAProjectListener( ScMacroManager* pMacroMgr ) : mpMacroMgr( pMacroMgr ) {}
110 // XEventListener
disposing(const lang::EventObject &)111 virtual void SAL_CALL disposing( const lang::EventObject& /*Source*/ ) override {}
112
113 // XContainerListener
elementInserted(const container::ContainerEvent &)114 virtual void SAL_CALL elementInserted( const container::ContainerEvent& /*Event*/ ) override {}
elementReplaced(const container::ContainerEvent & Event)115 virtual void SAL_CALL elementReplaced( const container::ContainerEvent& Event ) override
116 {
117 OUString sModuleName;
118 Event.Accessor >>= sModuleName;
119 mpMacroMgr->InitUserFuncData();
120 mpMacroMgr->BroadcastModuleUpdate(sModuleName);
121 }
elementRemoved(const container::ContainerEvent &)122 virtual void SAL_CALL elementRemoved( const container::ContainerEvent& /*Event*/ ) override {}
123
124 };
125
InitUserFuncData()126 void ScMacroManager::InitUserFuncData()
127 {
128 // Clear unordered_map
129 mhFuncToVolatile.clear();
130 OUString sProjectName("Standard");
131
132 Reference< container::XContainer > xModuleContainer;
133 SfxObjectShell* pShell = mpDoc->GetDocumentShell();
134 if (!pShell)
135 return;
136 if (!pShell->GetBasicManager()->GetName().isEmpty())
137 {
138 sProjectName = pShell->GetBasicManager()->GetName();
139 }
140 try
141 {
142 Reference< script::XLibraryContainer > xLibraries( pShell->GetBasicContainer(), uno::UNO_SET_THROW );
143 xModuleContainer.set( xLibraries->getByName( sProjectName ), uno::UNO_QUERY_THROW );
144
145 // remove old listener ( if there was one )
146 if ( mxContainerListener.is() )
147 xModuleContainer->removeContainerListener( mxContainerListener );
148 // Create listener
149 mxContainerListener = new VBAProjectListener( this );
150 xModuleContainer->addContainerListener( mxContainerListener );
151 }
152 catch (const uno::Exception&)
153 {
154 }
155 }
156
SetUserFuncVolatile(const OUString & sName,bool isVolatile)157 void ScMacroManager::SetUserFuncVolatile( const OUString& sName, bool isVolatile )
158 {
159 mhFuncToVolatile[ sName ] = isVolatile;
160 }
161
GetUserFuncVolatile(const OUString & sName)162 bool ScMacroManager::GetUserFuncVolatile( const OUString& sName )
163 {
164 NameBoolMap::iterator it = mhFuncToVolatile.find( sName );
165 if ( it == mhFuncToVolatile.end() )
166 return false;
167 return it->second;
168 }
169
AddDependentCell(const OUString & aModuleName,ScFormulaCell * pCell)170 void ScMacroManager::AddDependentCell(const OUString& aModuleName, ScFormulaCell* pCell)
171 {
172 mpDepTracker->addCell(aModuleName, pCell);
173 }
174
RemoveDependentCell(const ScFormulaCell * pCell)175 void ScMacroManager::RemoveDependentCell(const ScFormulaCell* pCell)
176 {
177 mpDepTracker->removeCell(pCell);
178 }
179
BroadcastModuleUpdate(const OUString & aModuleName)180 void ScMacroManager::BroadcastModuleUpdate(const OUString& aModuleName)
181 {
182 vector<ScFormulaCell*> aCells;
183 mpDepTracker->getCellsByModule(aModuleName, aCells);
184 for (ScFormulaCell* pCell : aCells)
185 {
186 mpDoc->PutInFormulaTree(pCell); // for F9 recalc
187
188 // for recalc on cell value change. If the cell is not volatile, the
189 // cell stops listening right away after it gets re-interpreted.
190 mpDoc->StartListeningArea(BCA_LISTEN_ALWAYS, false, pCell);
191 }
192 }
193
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
195