xref: /AOO41X/main/sfx2/source/sidebar/EnumContext.cxx (revision f120fe4110718eecb5f5933a421104aa4582f505)
1b9e67834SAndre Fischer /**************************************************************
2b9e67834SAndre Fischer  *
3b9e67834SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
4b9e67834SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
5b9e67834SAndre Fischer  * distributed with this work for additional information
6b9e67834SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
7b9e67834SAndre Fischer  * to you under the Apache License, Version 2.0 (the
8b9e67834SAndre Fischer  * "License"); you may not use this file except in compliance
9b9e67834SAndre Fischer  * with the License.  You may obtain a copy of the License at
10b9e67834SAndre Fischer  *
11b9e67834SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
12b9e67834SAndre Fischer  *
13b9e67834SAndre Fischer  * Unless required by applicable law or agreed to in writing,
14b9e67834SAndre Fischer  * software distributed under the License is distributed on an
15b9e67834SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b9e67834SAndre Fischer  * KIND, either express or implied.  See the License for the
17b9e67834SAndre Fischer  * specific language governing permissions and limitations
18b9e67834SAndre Fischer  * under the License.
19b9e67834SAndre Fischer  *
20b9e67834SAndre Fischer  *************************************************************/
21b9e67834SAndre Fischer 
22b9e67834SAndre Fischer #include "precompiled_sfx2.hxx"
23b9e67834SAndre Fischer 
24b9e67834SAndre Fischer #include "sidebar/EnumContext.hxx"
25b9e67834SAndre Fischer 
2695a18594SAndre Fischer #include <map>
2795a18594SAndre Fischer 
28b9e67834SAndre Fischer namespace sfx2 { namespace sidebar {
29b9e67834SAndre Fischer 
30b9e67834SAndre Fischer #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
31b9e67834SAndre Fischer 
32b9e67834SAndre Fischer namespace {
33b9e67834SAndre Fischer 
34b9e67834SAndre Fischer typedef ::std::map<rtl::OUString,EnumContext::Application> ApplicationMap;
35b9e67834SAndre Fischer typedef ::std::vector<rtl::OUString> ApplicationVector;
36b9e67834SAndre Fischer static ApplicationMap maApplicationMap;
37b9e67834SAndre Fischer static ApplicationVector maApplicationVector;
38b9e67834SAndre Fischer 
39b9e67834SAndre Fischer typedef ::std::map<rtl::OUString,EnumContext::Context> ContextMap;
40b9e67834SAndre Fischer typedef ::std::vector<rtl::OUString> ContextVector;
41b9e67834SAndre Fischer static ContextMap maContextMap;
42b9e67834SAndre Fischer static ContextVector maContextVector;
43b9e67834SAndre Fischer 
44b9e67834SAndre Fischer }
45b9e67834SAndre Fischer 
4695a18594SAndre Fischer const sal_Int32 EnumContext::NoMatch = 4;
4795a18594SAndre Fischer const sal_Int32 EnumContext::OptimalMatch = 0;  // Neither application nor context name is "any".
48b9e67834SAndre Fischer 
49b9e67834SAndre Fischer 
50b9e67834SAndre Fischer EnumContext::EnumContext (void)
51*f120fe41SAndre Fischer     : meApplication(Application_None),
5295a18594SAndre Fischer       meContext(Context_Unknown)
53b9e67834SAndre Fischer {
54b9e67834SAndre Fischer }
55b9e67834SAndre Fischer 
56b9e67834SAndre Fischer 
57b9e67834SAndre Fischer 
58b9e67834SAndre Fischer 
59b9e67834SAndre Fischer EnumContext::EnumContext (
60b9e67834SAndre Fischer     const Application eApplication,
61b9e67834SAndre Fischer     const Context eContext)
62b9e67834SAndre Fischer     : meApplication(eApplication),
63b9e67834SAndre Fischer       meContext(eContext)
64b9e67834SAndre Fischer {
65b9e67834SAndre Fischer }
66b9e67834SAndre Fischer 
67b9e67834SAndre Fischer 
68b9e67834SAndre Fischer 
69b9e67834SAndre Fischer 
7095a18594SAndre Fischer EnumContext::EnumContext (
7195a18594SAndre Fischer     const ::rtl::OUString& rsApplicationName,
7295a18594SAndre Fischer     const ::rtl::OUString& rsContextName)
7395a18594SAndre Fischer     : meApplication(GetApplicationEnum(rsApplicationName)),
7495a18594SAndre Fischer       meContext(GetContextEnum(rsContextName))
7595a18594SAndre Fischer {
7695a18594SAndre Fischer }
7795a18594SAndre Fischer 
7895a18594SAndre Fischer 
7995a18594SAndre Fischer 
8095a18594SAndre Fischer 
81b9e67834SAndre Fischer sal_Int32 EnumContext::GetCombinedContext (void) const
82b9e67834SAndre Fischer {
83b9e67834SAndre Fischer     return CombinedEnumContext(meApplication, meContext);
84b9e67834SAndre Fischer }
85b9e67834SAndre Fischer 
86b9e67834SAndre Fischer 
87b9e67834SAndre Fischer 
88b9e67834SAndre Fischer 
89*f120fe41SAndre Fischer sal_Int32 EnumContext::GetCombinedContext_DI (void) const
90*f120fe41SAndre Fischer {
91*f120fe41SAndre Fischer     switch (meApplication)
92*f120fe41SAndre Fischer     {
93*f120fe41SAndre Fischer         case Application_Draw:
94*f120fe41SAndre Fischer         case Application_Impress:
95*f120fe41SAndre Fischer             return CombinedEnumContext(Application_DrawImpress, meContext);
96*f120fe41SAndre Fischer 
97*f120fe41SAndre Fischer         default:
98*f120fe41SAndre Fischer             return CombinedEnumContext(meApplication, meContext);
99*f120fe41SAndre Fischer     }
100*f120fe41SAndre Fischer }
101*f120fe41SAndre Fischer 
102*f120fe41SAndre Fischer 
103*f120fe41SAndre Fischer 
104*f120fe41SAndre Fischer 
10595a18594SAndre Fischer const ::rtl::OUString& EnumContext::GetApplicationName (void) const
10695a18594SAndre Fischer {
10795a18594SAndre Fischer     return EnumContext::GetApplicationName(meApplication);
10895a18594SAndre Fischer }
10995a18594SAndre Fischer 
11095a18594SAndre Fischer 
11195a18594SAndre Fischer 
11295a18594SAndre Fischer 
11395a18594SAndre Fischer const ::rtl::OUString& EnumContext::GetContextName (void) const
11495a18594SAndre Fischer {
11595a18594SAndre Fischer     return EnumContext::GetContextName(meContext);
11695a18594SAndre Fischer }
11795a18594SAndre Fischer 
11895a18594SAndre Fischer 
11995a18594SAndre Fischer 
12095a18594SAndre Fischer 
121b9e67834SAndre Fischer bool EnumContext::operator== (const EnumContext aOther)
122b9e67834SAndre Fischer {
123b9e67834SAndre Fischer     return meApplication==aOther.meApplication
124b9e67834SAndre Fischer         && meContext==aOther.meContext;
125b9e67834SAndre Fischer }
126b9e67834SAndre Fischer 
127b9e67834SAndre Fischer 
128b9e67834SAndre Fischer 
129b9e67834SAndre Fischer 
13095a18594SAndre Fischer bool EnumContext::operator!= (const EnumContext aOther)
13195a18594SAndre Fischer {
13295a18594SAndre Fischer     return meApplication!=aOther.meApplication
13395a18594SAndre Fischer         || meContext!=aOther.meContext;
13495a18594SAndre Fischer }
13595a18594SAndre Fischer 
13695a18594SAndre Fischer 
13795a18594SAndre Fischer 
13895a18594SAndre Fischer 
139b9e67834SAndre Fischer void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
140b9e67834SAndre Fischer {
141b9e67834SAndre Fischer     maApplicationMap[rsName] = eApplication;
142b9e67834SAndre Fischer     OSL_ASSERT(eApplication<=__LastApplicationEnum);
14395a18594SAndre Fischer     if (maApplicationVector.size() <= size_t(eApplication))
144b9e67834SAndre Fischer         maApplicationVector.resize(eApplication+1);
145b9e67834SAndre Fischer     maApplicationVector[eApplication]=rsName;
146b9e67834SAndre Fischer }
147b9e67834SAndre Fischer 
148b9e67834SAndre Fischer 
149b9e67834SAndre Fischer 
150b9e67834SAndre Fischer 
151b9e67834SAndre Fischer void EnumContext::ProvideApplicationContainers (void)
152b9e67834SAndre Fischer {
153b9e67834SAndre Fischer     if (maApplicationMap.empty())
154b9e67834SAndre Fischer     {
155b9e67834SAndre Fischer         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
156b9e67834SAndre Fischer         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
15795a18594SAndre Fischer         AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
15895a18594SAndre Fischer         AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
159b9e67834SAndre Fischer         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
160*f120fe41SAndre Fischer 
16195a18594SAndre Fischer         AddEntry(A2S("any"), EnumContext::Application_Any);
162*f120fe41SAndre Fischer         AddEntry(A2S("none"), EnumContext::Application_None);
163b9e67834SAndre Fischer     }
164b9e67834SAndre Fischer }
165b9e67834SAndre Fischer 
166b9e67834SAndre Fischer 
167b9e67834SAndre Fischer 
168b9e67834SAndre Fischer 
169b9e67834SAndre Fischer EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
170b9e67834SAndre Fischer {
171b9e67834SAndre Fischer     ProvideApplicationContainers();
172b9e67834SAndre Fischer 
173b9e67834SAndre Fischer     ApplicationMap::const_iterator iApplication(
174b9e67834SAndre Fischer         maApplicationMap.find(rsApplicationName));
175b9e67834SAndre Fischer     if (iApplication != maApplicationMap.end())
176b9e67834SAndre Fischer         return iApplication->second;
177b9e67834SAndre Fischer     else
178*f120fe41SAndre Fischer         return EnumContext::Application_None;
179b9e67834SAndre Fischer }
180b9e67834SAndre Fischer 
181b9e67834SAndre Fischer 
182b9e67834SAndre Fischer 
183b9e67834SAndre Fischer 
184b9e67834SAndre Fischer const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
185b9e67834SAndre Fischer {
186b9e67834SAndre Fischer     ProvideApplicationContainers();
187b9e67834SAndre Fischer 
188b9e67834SAndre Fischer     const sal_Int32 nIndex (eApplication);
189b9e67834SAndre Fischer     if (nIndex<0 || nIndex>= __LastApplicationEnum)
190*f120fe41SAndre Fischer         return maApplicationVector[Application_None];
191b9e67834SAndre Fischer     else
192b9e67834SAndre Fischer         return maApplicationVector[nIndex];
193b9e67834SAndre Fischer }
194b9e67834SAndre Fischer 
195b9e67834SAndre Fischer 
196b9e67834SAndre Fischer 
197b9e67834SAndre Fischer 
198b9e67834SAndre Fischer void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
199b9e67834SAndre Fischer {
200b9e67834SAndre Fischer     maContextMap[rsName] = eApplication;
201b9e67834SAndre Fischer     OSL_ASSERT(eApplication<=__LastContextEnum);
20295a18594SAndre Fischer     if (maContextVector.size() <= size_t(eApplication))
203b9e67834SAndre Fischer         maContextVector.resize(eApplication+1);
204b9e67834SAndre Fischer     maContextVector[eApplication] = rsName;
205b9e67834SAndre Fischer }
206b9e67834SAndre Fischer 
207b9e67834SAndre Fischer 
208b9e67834SAndre Fischer 
209b9e67834SAndre Fischer 
210b9e67834SAndre Fischer void EnumContext::ProvideContextContainers (void)
211b9e67834SAndre Fischer {
212b9e67834SAndre Fischer     if (maContextMap.empty())
213b9e67834SAndre Fischer     {
214b9e67834SAndre Fischer         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
21595a18594SAndre Fischer         AddEntry(A2S("any"), Context_Any);
216b9e67834SAndre Fischer         AddEntry(A2S("default"), Context_Default);
217*f120fe41SAndre Fischer #define AddContext(context) AddEntry(A2S(#context), Context_##context);
218*f120fe41SAndre Fischer         AddContext(3DObject);
219*f120fe41SAndre Fischer         AddContext(Annotation);
220*f120fe41SAndre Fischer         AddContext(Auditing);
221*f120fe41SAndre Fischer         AddContext(Cell);
222*f120fe41SAndre Fischer         AddContext(Chart);
223*f120fe41SAndre Fischer         AddContext(Chart);
224*f120fe41SAndre Fischer         AddContext(Draw);
225*f120fe41SAndre Fischer         AddContext(DrawPage);
226*f120fe41SAndre Fischer         AddContext(DrawText);
227*f120fe41SAndre Fischer         AddContext(EditCell);
228*f120fe41SAndre Fischer         AddContext(Form);
229*f120fe41SAndre Fischer         AddContext(Frame);
230*f120fe41SAndre Fischer         AddContext(Graphic);
231*f120fe41SAndre Fischer         AddContext(HandoutPage);
232*f120fe41SAndre Fischer         AddContext(MasterPage);
233*f120fe41SAndre Fischer         AddContext(Media);
234*f120fe41SAndre Fischer         AddContext(MultiObject);
235*f120fe41SAndre Fischer         AddContext(NotesPage);
236*f120fe41SAndre Fischer         AddContext(OLE);
237*f120fe41SAndre Fischer         AddContext(OutlineText);
238*f120fe41SAndre Fischer         AddContext(Pivot);
239*f120fe41SAndre Fischer         AddContext(SlidesorterPage);
240*f120fe41SAndre Fischer         AddContext(Table);
241*f120fe41SAndre Fischer         AddContext(Text);
242*f120fe41SAndre Fischer         AddContext(TextObject);
243*f120fe41SAndre Fischer #undef AddContext
244b9e67834SAndre Fischer     }
245b9e67834SAndre Fischer }
246b9e67834SAndre Fischer 
247b9e67834SAndre Fischer 
248b9e67834SAndre Fischer 
249b9e67834SAndre Fischer 
250b9e67834SAndre Fischer EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
251b9e67834SAndre Fischer {
252b9e67834SAndre Fischer     ProvideContextContainers();
253b9e67834SAndre Fischer 
254b9e67834SAndre Fischer     ContextMap::const_iterator iContext(
255b9e67834SAndre Fischer         maContextMap.find(rsContextName));
256b9e67834SAndre Fischer     if (iContext != maContextMap.end())
257b9e67834SAndre Fischer         return iContext->second;
258b9e67834SAndre Fischer     else
25995a18594SAndre Fischer         return EnumContext::Context_Unknown;
260b9e67834SAndre Fischer }
261b9e67834SAndre Fischer 
262b9e67834SAndre Fischer 
263b9e67834SAndre Fischer 
264b9e67834SAndre Fischer 
265b9e67834SAndre Fischer const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
266b9e67834SAndre Fischer {
267b9e67834SAndre Fischer     ProvideContextContainers();
268b9e67834SAndre Fischer 
269b9e67834SAndre Fischer     const sal_Int32 nIndex (eContext);
270b9e67834SAndre Fischer     if (nIndex<0 || nIndex>= __LastContextEnum)
27195a18594SAndre Fischer         return maContextVector[Context_Unknown];
272b9e67834SAndre Fischer     else
273b9e67834SAndre Fischer         return maContextVector[nIndex];
274b9e67834SAndre Fischer }
275b9e67834SAndre Fischer 
276b9e67834SAndre Fischer 
277b9e67834SAndre Fischer 
278b9e67834SAndre Fischer 
27995a18594SAndre Fischer sal_Int32 EnumContext::EvaluateMatch (
28095a18594SAndre Fischer     const EnumContext& rOther) const
28195a18594SAndre Fischer {
28295a18594SAndre Fischer     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
28395a18594SAndre Fischer     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
28495a18594SAndre Fischer     {
28595a18594SAndre Fischer         // Application name matches.
28695a18594SAndre Fischer         const bool bContextNameIsAny (rOther.meContext == Context_Any);
28795a18594SAndre Fischer         if (rOther.meContext==meContext || bContextNameIsAny)
28895a18594SAndre Fischer         {
28995a18594SAndre Fischer             // Context name matches.
29095a18594SAndre Fischer             return (bApplicationNameIsAny ? 1 : 0)
29195a18594SAndre Fischer                 + (bContextNameIsAny ? 2 : 0);
29295a18594SAndre Fischer         }
29395a18594SAndre Fischer     }
29495a18594SAndre Fischer     return NoMatch;
29595a18594SAndre Fischer }
29695a18594SAndre Fischer 
29795a18594SAndre Fischer 
29895a18594SAndre Fischer 
29995a18594SAndre Fischer 
30095a18594SAndre Fischer sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
30195a18594SAndre Fischer {
30295a18594SAndre Fischer     sal_Int32 nBestMatch (NoMatch);
30395a18594SAndre Fischer 
30495a18594SAndre Fischer     for (::std::vector<EnumContext>::const_iterator
30595a18594SAndre Fischer              iContext(rOthers.begin()),
30695a18594SAndre Fischer              iEnd(rOthers.end());
30795a18594SAndre Fischer          iContext!=iEnd;
30895a18594SAndre Fischer          ++iContext)
30995a18594SAndre Fischer     {
31095a18594SAndre Fischer         const sal_Int32 nMatch (EvaluateMatch(*iContext));
31195a18594SAndre Fischer         if (nMatch < nBestMatch)
31295a18594SAndre Fischer         {
31395a18594SAndre Fischer             if (nMatch == OptimalMatch)
31495a18594SAndre Fischer             {
31595a18594SAndre Fischer                 // We will find no better match so stop searching.
31695a18594SAndre Fischer                 return OptimalMatch;
31795a18594SAndre Fischer             }
31895a18594SAndre Fischer             nBestMatch = nMatch;
31995a18594SAndre Fischer         }
32095a18594SAndre Fischer     }
32195a18594SAndre Fischer     return nBestMatch;
32295a18594SAndre Fischer }
32395a18594SAndre Fischer 
32495a18594SAndre Fischer 
32595a18594SAndre Fischer 
326b9e67834SAndre Fischer } } // end of namespace sfx2::sidebar
327