1ff12d537SAndre Fischer /**************************************************************
2ff12d537SAndre Fischer  *
3ff12d537SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
4ff12d537SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
5ff12d537SAndre Fischer  * distributed with this work for additional information
6ff12d537SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
7ff12d537SAndre Fischer  * to you under the Apache License, Version 2.0 (the
8ff12d537SAndre Fischer  * "License"); you may not use this file except in compliance
9ff12d537SAndre Fischer  * with the License.  You may obtain a copy of the License at
10ff12d537SAndre Fischer  *
11ff12d537SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
12ff12d537SAndre Fischer  *
13ff12d537SAndre Fischer  * Unless required by applicable law or agreed to in writing,
14ff12d537SAndre Fischer  * software distributed under the License is distributed on an
15ff12d537SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ff12d537SAndre Fischer  * KIND, either express or implied.  See the License for the
17ff12d537SAndre Fischer  * specific language governing permissions and limitations
18ff12d537SAndre Fischer  * under the License.
19ff12d537SAndre Fischer  *
20ff12d537SAndre Fischer  *************************************************************/
21ff12d537SAndre Fischer 
22ff12d537SAndre Fischer #include "precompiled_sfx2.hxx"
23ff12d537SAndre Fischer 
24ff12d537SAndre Fischer #include "ResourceManager.hxx"
25ff12d537SAndre Fischer #include <unotools/confignode.hxx>
26ff12d537SAndre Fischer #include <comphelper/componentcontext.hxx>
27*95a18594SAndre Fischer #include <comphelper/processfactory.hxx>
28ff12d537SAndre Fischer #include <comphelper/namedvaluecollection.hxx>
29*95a18594SAndre Fischer #include <comphelper/types.hxx>
30*95a18594SAndre Fischer #include <rtl/ustrbuf.hxx>
31ff12d537SAndre Fischer #include <tools/diagnose_ex.h>
32ff12d537SAndre Fischer 
33*95a18594SAndre Fischer #include <com/sun/star/frame/XModuleManager.hpp>
34*95a18594SAndre Fischer 
35*95a18594SAndre Fischer #include <map>
36*95a18594SAndre Fischer 
37ff12d537SAndre Fischer 
38ff12d537SAndre Fischer #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
39ff12d537SAndre Fischer 
40ff12d537SAndre Fischer using ::rtl::OUString;
41ff12d537SAndre Fischer using namespace css;
42ff12d537SAndre Fischer using namespace cssu;
43ff12d537SAndre Fischer 
44ff12d537SAndre Fischer namespace sfx2 { namespace sidebar {
45ff12d537SAndre Fischer 
46ff12d537SAndre Fischer #define gsPrivateResourceToolpanelPrefix "private:resource/toolpanel/"
47ff12d537SAndre Fischer 
48ff12d537SAndre Fischer 
49ff12d537SAndre Fischer 
50ff12d537SAndre Fischer class ResourceManager::Deleter
51ff12d537SAndre Fischer {
52ff12d537SAndre Fischer public:
53ff12d537SAndre Fischer     void operator() (ResourceManager* pObject)
54ff12d537SAndre Fischer     {
55ff12d537SAndre Fischer         delete pObject;
56ff12d537SAndre Fischer     }
57ff12d537SAndre Fischer };
58ff12d537SAndre Fischer 
59ff12d537SAndre Fischer 
60ff12d537SAndre Fischer ResourceManager& ResourceManager::Instance (void)
61ff12d537SAndre Fischer {
62ff12d537SAndre Fischer     static ResourceManager maInstance;
63ff12d537SAndre Fischer     return maInstance;
64ff12d537SAndre Fischer }
65ff12d537SAndre Fischer 
66ff12d537SAndre Fischer 
67ff12d537SAndre Fischer 
68ff12d537SAndre Fischer 
69ff12d537SAndre Fischer ResourceManager::ResourceManager (void)
70ff12d537SAndre Fischer     : maDecks(),
71ff12d537SAndre Fischer       maPanels(),
72ff12d537SAndre Fischer       maProcessedApplications()
73ff12d537SAndre Fischer {
74ff12d537SAndre Fischer     ReadDeckList();
75ff12d537SAndre Fischer     ReadPanelList();
76ff12d537SAndre Fischer }
77ff12d537SAndre Fischer 
78ff12d537SAndre Fischer 
79ff12d537SAndre Fischer 
80ff12d537SAndre Fischer 
81ff12d537SAndre Fischer ResourceManager::~ResourceManager (void)
82ff12d537SAndre Fischer {
83ff12d537SAndre Fischer     maPanels.clear();
84ff12d537SAndre Fischer     maDecks.clear();
85ff12d537SAndre Fischer }
86ff12d537SAndre Fischer 
87ff12d537SAndre Fischer 
88ff12d537SAndre Fischer 
89ff12d537SAndre Fischer 
90ff12d537SAndre Fischer const DeckDescriptor* ResourceManager::GetBestMatchingDeck (
91*95a18594SAndre Fischer     const EnumContext& rContext,
92ff12d537SAndre Fischer     const Reference<frame::XFrame>& rxFrame)
93ff12d537SAndre Fischer {
94ff12d537SAndre Fischer     ReadLegacyAddons(rxFrame);
95ff12d537SAndre Fischer 
96*95a18594SAndre Fischer     sal_Int32 nBestMatch (EnumContext::NoMatch);
97ff12d537SAndre Fischer     const DeckContainer::const_iterator iEnd (maDecks.end());
98ff12d537SAndre Fischer     DeckContainer::const_iterator iBestDeck (iEnd);
99ff12d537SAndre Fischer 
100ff12d537SAndre Fischer     for (DeckContainer::const_iterator iDeck(maDecks.begin());
101ff12d537SAndre Fischer          iDeck!=iEnd;
102ff12d537SAndre Fischer          ++iDeck)
103ff12d537SAndre Fischer     {
104ff12d537SAndre Fischer         const sal_Int32 nMatch (rContext.EvaluateMatch(iDeck->maContexts));
105ff12d537SAndre Fischer         if (nMatch < nBestMatch)
106ff12d537SAndre Fischer         {
107*95a18594SAndre Fischer             // Found a better matching deck.
108ff12d537SAndre Fischer             nBestMatch = nMatch;
109ff12d537SAndre Fischer             iBestDeck = iDeck;
110*95a18594SAndre Fischer             if (nBestMatch == EnumContext::OptimalMatch)
111ff12d537SAndre Fischer             {
112ff12d537SAndre Fischer                 // We will not find a better match.
113ff12d537SAndre Fischer                 break;
114ff12d537SAndre Fischer             }
115ff12d537SAndre Fischer         }
116ff12d537SAndre Fischer     }
117ff12d537SAndre Fischer     if (iBestDeck != iEnd)
118ff12d537SAndre Fischer         return &*iBestDeck;
119ff12d537SAndre Fischer     else
120ff12d537SAndre Fischer         return NULL;
121ff12d537SAndre Fischer }
122ff12d537SAndre Fischer 
123ff12d537SAndre Fischer 
124ff12d537SAndre Fischer 
125ff12d537SAndre Fischer 
126*95a18594SAndre Fischer const DeckDescriptor* ResourceManager::GetDeckDescriptor (
127*95a18594SAndre Fischer     const ::rtl::OUString& rsDeckId) const
128*95a18594SAndre Fischer {
129*95a18594SAndre Fischer     for (DeckContainer::const_iterator
130*95a18594SAndre Fischer              iDeck(maDecks.begin()),
131*95a18594SAndre Fischer              iEnd(maDecks.end());
132*95a18594SAndre Fischer          iDeck!=iEnd;
133*95a18594SAndre Fischer          ++iDeck)
134*95a18594SAndre Fischer     {
135*95a18594SAndre Fischer         if (iDeck->msId.equals(rsDeckId))
136*95a18594SAndre Fischer             return &*iDeck;
137*95a18594SAndre Fischer     }
138*95a18594SAndre Fischer     return NULL;
139*95a18594SAndre Fischer }
140*95a18594SAndre Fischer 
141*95a18594SAndre Fischer 
142*95a18594SAndre Fischer 
143*95a18594SAndre Fischer 
144*95a18594SAndre Fischer const PanelDescriptor* ResourceManager::GetPanelDescriptor (
145*95a18594SAndre Fischer     const ::rtl::OUString& rsPanelId) const
146*95a18594SAndre Fischer {
147*95a18594SAndre Fischer     for (PanelContainer::const_iterator
148*95a18594SAndre Fischer              iPanel(maPanels.begin()),
149*95a18594SAndre Fischer              iEnd(maPanels.end());
150*95a18594SAndre Fischer          iPanel!=iEnd;
151*95a18594SAndre Fischer          ++iPanel)
152*95a18594SAndre Fischer     {
153*95a18594SAndre Fischer         if (iPanel->msId.equals(rsPanelId))
154*95a18594SAndre Fischer             return &*iPanel;
155*95a18594SAndre Fischer     }
156*95a18594SAndre Fischer     return NULL;
157*95a18594SAndre Fischer }
158*95a18594SAndre Fischer 
159*95a18594SAndre Fischer 
160*95a18594SAndre Fischer 
161*95a18594SAndre Fischer 
162*95a18594SAndre Fischer void ResourceManager::SetIsDeckEnabled (
163*95a18594SAndre Fischer     const ::rtl::OUString& rsDeckId,
164*95a18594SAndre Fischer     const bool bIsEnabled)
165*95a18594SAndre Fischer {
166*95a18594SAndre Fischer     for (DeckContainer::iterator
167*95a18594SAndre Fischer              iDeck(maDecks.begin()),
168*95a18594SAndre Fischer              iEnd(maDecks.end());
169*95a18594SAndre Fischer          iDeck!=iEnd;
170*95a18594SAndre Fischer          ++iDeck)
171*95a18594SAndre Fischer     {
172*95a18594SAndre Fischer         if (iDeck->msId.equals(rsDeckId))
173*95a18594SAndre Fischer         {
174*95a18594SAndre Fischer             iDeck->mbIsEnabled = bIsEnabled;
175*95a18594SAndre Fischer             return;
176*95a18594SAndre Fischer         }
177*95a18594SAndre Fischer     }
178*95a18594SAndre Fischer }
179*95a18594SAndre Fischer 
180*95a18594SAndre Fischer 
181*95a18594SAndre Fischer 
182*95a18594SAndre Fischer 
183*95a18594SAndre Fischer const ResourceManager::IdContainer& ResourceManager::GetMatchingDecks (
184*95a18594SAndre Fischer     IdContainer& rDeckIds,
185*95a18594SAndre Fischer     const EnumContext& rContext,
186ff12d537SAndre Fischer     const Reference<frame::XFrame>& rxFrame)
187ff12d537SAndre Fischer {
188ff12d537SAndre Fischer     ReadLegacyAddons(rxFrame);
189ff12d537SAndre Fischer 
190ff12d537SAndre Fischer     for (DeckContainer::const_iterator
191ff12d537SAndre Fischer              iDeck(maDecks.begin()),
192ff12d537SAndre Fischer              iEnd (maDecks.end());
193ff12d537SAndre Fischer          iDeck!=iEnd;
194ff12d537SAndre Fischer          ++iDeck)
195ff12d537SAndre Fischer     {
196*95a18594SAndre Fischer         if (rContext.EvaluateMatch(iDeck->maContexts) != EnumContext::NoMatch)
197*95a18594SAndre Fischer             rDeckIds.push_back(iDeck->msId);
198ff12d537SAndre Fischer     }
199ff12d537SAndre Fischer 
200*95a18594SAndre Fischer     return rDeckIds;
201ff12d537SAndre Fischer }
202ff12d537SAndre Fischer 
203ff12d537SAndre Fischer 
204ff12d537SAndre Fischer 
205ff12d537SAndre Fischer 
206*95a18594SAndre Fischer const ResourceManager::IdContainer& ResourceManager::GetMatchingPanels (
207*95a18594SAndre Fischer     IdContainer& rPanelIds,
208*95a18594SAndre Fischer     const EnumContext& rContext,
209ff12d537SAndre Fischer     const ::rtl::OUString& rsDeckId,
210ff12d537SAndre Fischer     const Reference<frame::XFrame>& rxFrame)
211ff12d537SAndre Fischer {
212ff12d537SAndre Fischer     ReadLegacyAddons(rxFrame);
213ff12d537SAndre Fischer 
214*95a18594SAndre Fischer     ::std::multimap<sal_Int32,OUString> aOrderedIds;
215ff12d537SAndre Fischer     for (PanelContainer::const_iterator
216ff12d537SAndre Fischer              iPanel(maPanels.begin()),
217ff12d537SAndre Fischer              iEnd(maPanels.end());
218ff12d537SAndre Fischer          iPanel!=iEnd;
219ff12d537SAndre Fischer          ++iPanel)
220ff12d537SAndre Fischer     {
221ff12d537SAndre Fischer         const PanelDescriptor& rPanelDescriptor (*iPanel);
222ff12d537SAndre Fischer         if (rPanelDescriptor.msDeckId.equals(rsDeckId))
223*95a18594SAndre Fischer             if (rContext.EvaluateMatch(rPanelDescriptor.maContexts) != EnumContext::NoMatch)
224*95a18594SAndre Fischer                 aOrderedIds.insert(::std::multimap<sal_Int32,OUString>::value_type(
225*95a18594SAndre Fischer                         iPanel->mnOrderIndex,
226*95a18594SAndre Fischer                         iPanel->msId));
227ff12d537SAndre Fischer     }
228ff12d537SAndre Fischer 
229*95a18594SAndre Fischer     for (::std::multimap<sal_Int32,OUString>::const_iterator
230*95a18594SAndre Fischer              iId(aOrderedIds.begin()),
231*95a18594SAndre Fischer              iEnd(aOrderedIds.end());
232*95a18594SAndre Fischer          iId!=iEnd;
233*95a18594SAndre Fischer          ++iId)
234*95a18594SAndre Fischer     {
235*95a18594SAndre Fischer         rPanelIds.push_back(iId->second);
236*95a18594SAndre Fischer     }
237*95a18594SAndre Fischer 
238*95a18594SAndre Fischer     return rPanelIds;
239ff12d537SAndre Fischer }
240ff12d537SAndre Fischer 
241ff12d537SAndre Fischer 
242ff12d537SAndre Fischer 
243ff12d537SAndre Fischer 
244ff12d537SAndre Fischer void ResourceManager::ReadDeckList (void)
245ff12d537SAndre Fischer {
246ff12d537SAndre Fischer     const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
247ff12d537SAndre Fischer     const ::utl::OConfigurationTreeRoot aDeckRootNode (
248ff12d537SAndre Fischer         aContext,
249ff12d537SAndre Fischer         A2S("org.openoffice.Office.UI.Sidebar/Content/DeckList"),
250ff12d537SAndre Fischer         false);
251ff12d537SAndre Fischer     if ( ! aDeckRootNode.isValid() )
252ff12d537SAndre Fischer         return;
253ff12d537SAndre Fischer 
254ff12d537SAndre Fischer     const Sequence<OUString> aDeckNodeNames (aDeckRootNode.getNodeNames());
255ff12d537SAndre Fischer     const sal_Int32 nCount (aDeckNodeNames.getLength());
256ff12d537SAndre Fischer     maDecks.resize(nCount);
257ff12d537SAndre Fischer     sal_Int32 nWriteIndex(0);
258ff12d537SAndre Fischer     for (sal_Int32 nReadIndex(0); nReadIndex<nCount; ++nReadIndex)
259ff12d537SAndre Fischer     {
260ff12d537SAndre Fischer         const ::utl::OConfigurationNode aDeckNode (aDeckRootNode.openNode(aDeckNodeNames[nReadIndex]));
261ff12d537SAndre Fischer         if ( ! aDeckNode.isValid())
262ff12d537SAndre Fischer             continue;
263ff12d537SAndre Fischer 
264ff12d537SAndre Fischer         DeckDescriptor& rDeckDescriptor (maDecks[nWriteIndex++]);
265ff12d537SAndre Fischer 
266ff12d537SAndre Fischer         rDeckDescriptor.msTitle = ::comphelper::getString(aDeckNode.getNodeValue("Title"));
267ff12d537SAndre Fischer         rDeckDescriptor.msId = ::comphelper::getString(aDeckNode.getNodeValue("Id"));
268ff12d537SAndre Fischer         rDeckDescriptor.msIconURL = ::comphelper::getString(aDeckNode.getNodeValue("IconURL"));
269ff12d537SAndre Fischer         rDeckDescriptor.msHighContrastIconURL = ::comphelper::getString(aDeckNode.getNodeValue("HighContrastIconURL"));
270ff12d537SAndre Fischer         rDeckDescriptor.msHelpURL = ::comphelper::getString(aDeckNode.getNodeValue("HelpURL"));
271ff12d537SAndre Fischer         rDeckDescriptor.msHelpText = rDeckDescriptor.msTitle;
272*95a18594SAndre Fischer         rDeckDescriptor.mbIsEnabled = true;
273ff12d537SAndre Fischer         ReadContextList(aDeckNode.openNode("ContextList"), rDeckDescriptor.maContexts);
274ff12d537SAndre Fischer     }
275ff12d537SAndre Fischer 
276ff12d537SAndre Fischer     // When there where invalid nodes then we have to adapt the size
277ff12d537SAndre Fischer     // of the deck vector.
278ff12d537SAndre Fischer     if (nWriteIndex<nCount)
279ff12d537SAndre Fischer         maDecks.resize(nWriteIndex);
280ff12d537SAndre Fischer }
281ff12d537SAndre Fischer 
282ff12d537SAndre Fischer 
283ff12d537SAndre Fischer 
284ff12d537SAndre Fischer 
285ff12d537SAndre Fischer void ResourceManager::ReadPanelList (void)
286ff12d537SAndre Fischer {
287ff12d537SAndre Fischer     const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
288ff12d537SAndre Fischer     const ::utl::OConfigurationTreeRoot aPanelRootNode (
289ff12d537SAndre Fischer         aContext,
290ff12d537SAndre Fischer         A2S("org.openoffice.Office.UI.Sidebar/Content/PanelList"),
291ff12d537SAndre Fischer         false);
292ff12d537SAndre Fischer     if ( ! aPanelRootNode.isValid() )
293ff12d537SAndre Fischer         return;
294ff12d537SAndre Fischer 
295ff12d537SAndre Fischer     const Sequence<OUString> aPanelNodeNames (aPanelRootNode.getNodeNames());
296ff12d537SAndre Fischer     const sal_Int32 nCount (aPanelNodeNames.getLength());
297ff12d537SAndre Fischer     maPanels.resize(nCount);
298ff12d537SAndre Fischer     sal_Int32 nWriteIndex (0);
299ff12d537SAndre Fischer     for (sal_Int32 nReadIndex(0); nReadIndex<nCount; ++nReadIndex)
300ff12d537SAndre Fischer     {
301ff12d537SAndre Fischer         const ::utl::OConfigurationNode aPanelNode (aPanelRootNode.openNode(aPanelNodeNames[nReadIndex]));
302ff12d537SAndre Fischer         if ( ! aPanelNode.isValid())
303ff12d537SAndre Fischer             continue;
304ff12d537SAndre Fischer 
305ff12d537SAndre Fischer         PanelDescriptor& rPanelDescriptor (maPanels[nWriteIndex++]);
306ff12d537SAndre Fischer 
307ff12d537SAndre Fischer         rPanelDescriptor.msTitle = ::comphelper::getString(
308ff12d537SAndre Fischer             aPanelNode.getNodeValue("Title"));
309ff12d537SAndre Fischer         rPanelDescriptor.mbIsTitleBarOptional = ::comphelper::getBOOL(
310ff12d537SAndre Fischer             aPanelNode.getNodeValue("TitleBarIsOptional"));
311ff12d537SAndre Fischer         rPanelDescriptor.msId = ::comphelper::getString(
312ff12d537SAndre Fischer             aPanelNode.getNodeValue("Id"));
313ff12d537SAndre Fischer         rPanelDescriptor.msDeckId = ::comphelper::getString(
314ff12d537SAndre Fischer             aPanelNode.getNodeValue("DeckId"));
315ff12d537SAndre Fischer         rPanelDescriptor.msHelpURL = ::comphelper::getString(
316ff12d537SAndre Fischer             aPanelNode.getNodeValue("HelpURL"));
317ff12d537SAndre Fischer         rPanelDescriptor.msLayout = ::comphelper::getString(
318ff12d537SAndre Fischer             aPanelNode.getNodeValue("Layout"));
319ff12d537SAndre Fischer         rPanelDescriptor.msImplementationURL = ::comphelper::getString(
320ff12d537SAndre Fischer             aPanelNode.getNodeValue("ImplementationURL"));
321*95a18594SAndre Fischer         rPanelDescriptor.mnOrderIndex = ::comphelper::getINT32(
322*95a18594SAndre Fischer             aPanelNode.getNodeValue("OrderIndex"));
323ff12d537SAndre Fischer         ReadContextList(aPanelNode.openNode("ContextList"), rPanelDescriptor.maContexts);
324ff12d537SAndre Fischer     }
325ff12d537SAndre Fischer 
326ff12d537SAndre Fischer     // When there where invalid nodes then we have to adapt the size
327ff12d537SAndre Fischer     // of the deck vector.
328ff12d537SAndre Fischer     if (nWriteIndex<nCount)
329ff12d537SAndre Fischer         maPanels.resize(nWriteIndex);
330ff12d537SAndre Fischer }
331ff12d537SAndre Fischer 
332ff12d537SAndre Fischer 
333ff12d537SAndre Fischer 
334ff12d537SAndre Fischer 
335ff12d537SAndre Fischer void ResourceManager::ReadContextList (
336ff12d537SAndre Fischer     const ::utl::OConfigurationNode& rNode,
337*95a18594SAndre Fischer     ::std::vector<EnumContext>& rContextContainer) const
338ff12d537SAndre Fischer {
339ff12d537SAndre Fischer     const Sequence<OUString> aChildNodeNames (rNode.getNodeNames());
340ff12d537SAndre Fischer     const sal_Int32 nCount (aChildNodeNames.getLength());
341ff12d537SAndre Fischer     rContextContainer.resize(nCount);
342ff12d537SAndre Fischer     for (sal_Int32 nIndex(0); nIndex<nCount; ++nIndex)
343ff12d537SAndre Fischer     {
344ff12d537SAndre Fischer         const ::utl::OConfigurationNode aChildNode (rNode.openNode(aChildNodeNames[nIndex]));
345*95a18594SAndre Fischer         rContextContainer[nIndex] = EnumContext(
346*95a18594SAndre Fischer             ::comphelper::getString(aChildNode.getNodeValue("Application")),
347*95a18594SAndre Fischer             ::comphelper::getString(aChildNode.getNodeValue("ApplicationContext")));
348ff12d537SAndre Fischer     }
349ff12d537SAndre Fischer }
350ff12d537SAndre Fischer 
351ff12d537SAndre Fischer 
352ff12d537SAndre Fischer 
353ff12d537SAndre Fischer 
354ff12d537SAndre Fischer void ResourceManager::ReadLegacyAddons (const Reference<frame::XFrame>& rxFrame)
355ff12d537SAndre Fischer {
356ff12d537SAndre Fischer     // Get module name for given frame.
357ff12d537SAndre Fischer     ::rtl::OUString sModuleName (GetModuleName(rxFrame));
358ff12d537SAndre Fischer     if (sModuleName.getLength() == 0)
359ff12d537SAndre Fischer         return;
360ff12d537SAndre Fischer     if (maProcessedApplications.find(sModuleName) != maProcessedApplications.end())
361ff12d537SAndre Fischer     {
362ff12d537SAndre Fischer         // Addons for this application have already been read.
363ff12d537SAndre Fischer         // There is nothing more to do.
364ff12d537SAndre Fischer         return;
365ff12d537SAndre Fischer     }
366ff12d537SAndre Fischer 
367ff12d537SAndre Fischer     // Mark module as processed.  Even when there is an error that
368ff12d537SAndre Fischer     // prevents the configuration data from being read, this error
369ff12d537SAndre Fischer     // will not be triggered a second time.
370ff12d537SAndre Fischer     maProcessedApplications.insert(sModuleName);
371ff12d537SAndre Fischer 
372ff12d537SAndre Fischer     // Get access to the configuration root node for the application.
373ff12d537SAndre Fischer     ::utl::OConfigurationTreeRoot aLegacyRootNode (GetLegacyAddonRootNode(sModuleName));
374ff12d537SAndre Fischer     if ( ! aLegacyRootNode.isValid())
375ff12d537SAndre Fischer         return;
376ff12d537SAndre Fischer 
377ff12d537SAndre Fischer     // Process child nodes.
378ff12d537SAndre Fischer     ::std::vector<OUString> aMatchingNodeNames;
379ff12d537SAndre Fischer     GetToolPanelNodeNames(aMatchingNodeNames, aLegacyRootNode);
380ff12d537SAndre Fischer     const sal_Int32 nCount (aMatchingNodeNames.size());
381*95a18594SAndre Fischer     size_t nDeckWriteIndex (maDecks.size());
382*95a18594SAndre Fischer     size_t nPanelWriteIndex (maPanels.size());
383ff12d537SAndre Fischer     maDecks.resize(maDecks.size() + nCount);
384ff12d537SAndre Fischer     maPanels.resize(maPanels.size() + nCount);
385ff12d537SAndre Fischer     for (sal_Int32 nReadIndex(0); nReadIndex<nCount; ++nReadIndex)
386ff12d537SAndre Fischer     {
387ff12d537SAndre Fischer         const OUString& rsNodeName (aMatchingNodeNames[nReadIndex]);
388ff12d537SAndre Fischer         const ::utl::OConfigurationNode aChildNode (aLegacyRootNode.openNode(rsNodeName));
389ff12d537SAndre Fischer         if ( ! aChildNode.isValid())
390ff12d537SAndre Fischer             continue;
391ff12d537SAndre Fischer 
392ff12d537SAndre Fischer         DeckDescriptor& rDeckDescriptor (maDecks[nDeckWriteIndex++]);
393ff12d537SAndre Fischer         rDeckDescriptor.msTitle = ::comphelper::getString(aChildNode.getNodeValue("UIName"));
394ff12d537SAndre Fischer         rDeckDescriptor.msId = rsNodeName;
395ff12d537SAndre Fischer         rDeckDescriptor.msIconURL = ::comphelper::getString(aChildNode.getNodeValue("ImageURL"));
396ff12d537SAndre Fischer         rDeckDescriptor.msHighContrastIconURL = rDeckDescriptor.msIconURL;
397ff12d537SAndre Fischer         rDeckDescriptor.msHelpURL = ::comphelper::getString(aChildNode.getNodeValue("HelpURL"));
398ff12d537SAndre Fischer         rDeckDescriptor.msHelpText = rDeckDescriptor.msTitle;
399ff12d537SAndre Fischer         rDeckDescriptor.maContexts.resize(1);
400*95a18594SAndre Fischer         rDeckDescriptor.maContexts.front() = EnumContext(sModuleName, A2S("any"));
401*95a18594SAndre Fischer         rDeckDescriptor.mbIsEnabled = true;
402ff12d537SAndre Fischer 
403ff12d537SAndre Fischer         PanelDescriptor& rPanelDescriptor (maPanels[nPanelWriteIndex++]);
404ff12d537SAndre Fischer         rPanelDescriptor.msTitle = ::comphelper::getString(aChildNode.getNodeValue("UIName"));
405ff12d537SAndre Fischer         rPanelDescriptor.mbIsTitleBarOptional = false;
406ff12d537SAndre Fischer         rPanelDescriptor.msId = rsNodeName;
407ff12d537SAndre Fischer         rPanelDescriptor.msDeckId = rsNodeName;
408ff12d537SAndre Fischer         rPanelDescriptor.msHelpURL = ::comphelper::getString(aChildNode.getNodeValue("HelpURL"));
409ff12d537SAndre Fischer         rPanelDescriptor.maContexts.resize(1);
410*95a18594SAndre Fischer         rPanelDescriptor.maContexts.front() = EnumContext(sModuleName, A2S("any"));
411ff12d537SAndre Fischer         rPanelDescriptor.msLayout = A2S("full");
412ff12d537SAndre Fischer         rPanelDescriptor.msImplementationURL = rsNodeName;
413ff12d537SAndre Fischer     }
414ff12d537SAndre Fischer 
415ff12d537SAndre Fischer     // When there where invalid nodes then we have to adapt the size
416ff12d537SAndre Fischer     // of the deck and panel vectors.
417ff12d537SAndre Fischer     if (nDeckWriteIndex < maDecks.size())
418ff12d537SAndre Fischer         maDecks.resize(nDeckWriteIndex);
419ff12d537SAndre Fischer     if (nPanelWriteIndex < maPanels.size())
420ff12d537SAndre Fischer         maPanels.resize(nPanelWriteIndex);
421ff12d537SAndre Fischer }
422ff12d537SAndre Fischer 
423ff12d537SAndre Fischer 
424ff12d537SAndre Fischer 
425ff12d537SAndre Fischer 
426ff12d537SAndre Fischer ::rtl::OUString ResourceManager::GetModuleName (
427*95a18594SAndre Fischer     const cssu::Reference<css::frame::XFrame>& rxFrame)
428ff12d537SAndre Fischer {
429ff12d537SAndre Fischer     try
430ff12d537SAndre Fischer     {
431ff12d537SAndre Fischer         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
432ff12d537SAndre Fischer         const Reference<frame::XModuleManager> xModuleManager (
433ff12d537SAndre Fischer             aContext.createComponent("com.sun.star.frame.ModuleManager" ),
434ff12d537SAndre Fischer             UNO_QUERY_THROW );
435ff12d537SAndre Fischer         return xModuleManager->identify(rxFrame);
436ff12d537SAndre Fischer     }
437ff12d537SAndre Fischer     catch (const Exception&)
438ff12d537SAndre Fischer     {
439ff12d537SAndre Fischer         DBG_UNHANDLED_EXCEPTION();
440ff12d537SAndre Fischer     }
441ff12d537SAndre Fischer     return OUString();
442ff12d537SAndre Fischer }
443ff12d537SAndre Fischer 
444ff12d537SAndre Fischer 
445ff12d537SAndre Fischer 
446ff12d537SAndre Fischer 
447ff12d537SAndre Fischer ::utl::OConfigurationTreeRoot ResourceManager::GetLegacyAddonRootNode (
448ff12d537SAndre Fischer     const ::rtl::OUString& rsModuleName) const
449ff12d537SAndre Fischer {
450ff12d537SAndre Fischer     try
451ff12d537SAndre Fischer     {
452ff12d537SAndre Fischer         const ::comphelper::ComponentContext aContext (::comphelper::getProcessServiceFactory());
453ff12d537SAndre Fischer         const Reference<container::XNameAccess> xModuleAccess (
454ff12d537SAndre Fischer             aContext.createComponent("com.sun.star.frame.ModuleManager"),
455ff12d537SAndre Fischer             UNO_QUERY_THROW);
456ff12d537SAndre Fischer         const ::comphelper::NamedValueCollection aModuleProperties (xModuleAccess->getByName(rsModuleName));
457ff12d537SAndre Fischer         const ::rtl::OUString sWindowStateRef (aModuleProperties.getOrDefault(
458ff12d537SAndre Fischer                 "ooSetupFactoryWindowStateConfigRef",
459ff12d537SAndre Fischer                 ::rtl::OUString()));
460ff12d537SAndre Fischer 
461ff12d537SAndre Fischer         ::rtl::OUStringBuffer aPathComposer;
462ff12d537SAndre Fischer         aPathComposer.appendAscii("org.openoffice.Office.UI.");
463ff12d537SAndre Fischer         aPathComposer.append(sWindowStateRef);
464ff12d537SAndre Fischer         aPathComposer.appendAscii("/UIElements/States");
465ff12d537SAndre Fischer 
466ff12d537SAndre Fischer         return ::utl::OConfigurationTreeRoot(aContext, aPathComposer.makeStringAndClear(), false);
467ff12d537SAndre Fischer     }
468ff12d537SAndre Fischer     catch( const Exception& )
469ff12d537SAndre Fischer     {
470ff12d537SAndre Fischer         DBG_UNHANDLED_EXCEPTION();
471ff12d537SAndre Fischer     }
472ff12d537SAndre Fischer 
473ff12d537SAndre Fischer     return ::utl::OConfigurationTreeRoot();
474ff12d537SAndre Fischer }
475ff12d537SAndre Fischer 
476ff12d537SAndre Fischer 
477ff12d537SAndre Fischer 
478ff12d537SAndre Fischer 
479ff12d537SAndre Fischer void ResourceManager::GetToolPanelNodeNames (
480ff12d537SAndre Fischer     ::std::vector<OUString>& rMatchingNames,
481ff12d537SAndre Fischer     const ::utl::OConfigurationTreeRoot aRoot) const
482ff12d537SAndre Fischer {
483ff12d537SAndre Fischer     Sequence<OUString> aChildNodeNames (aRoot.getNodeNames());
484ff12d537SAndre Fischer     const sal_Int32 nCount (aChildNodeNames.getLength());
485ff12d537SAndre Fischer     for (sal_Int32 nIndex(0); nIndex<nCount; ++nIndex)
486ff12d537SAndre Fischer     {
487ff12d537SAndre Fischer         if (aChildNodeNames[nIndex].matchAsciiL(
488ff12d537SAndre Fischer                 RTL_CONSTASCII_STRINGPARAM( "private:resource/toolpanel/")))
489ff12d537SAndre Fischer             rMatchingNames.push_back(aChildNodeNames[nIndex]);
490ff12d537SAndre Fischer     }
491ff12d537SAndre Fischer }
492ff12d537SAndre Fischer 
493ff12d537SAndre Fischer 
494ff12d537SAndre Fischer 
495ff12d537SAndre Fischer } } // end of namespace sfx2::sidebar
496