xref: /AOO41X/main/sfx2/source/sidebar/EnumContext.cxx (revision d3e0dd8eb215533c15e891ee35bd141abe9397ee)
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 
EnumContext(void)50b9e67834SAndre Fischer EnumContext::EnumContext (void)
51f120fe41SAndre Fischer     : meApplication(Application_None),
5295a18594SAndre Fischer       meContext(Context_Unknown)
53b9e67834SAndre Fischer {
54b9e67834SAndre Fischer }
55b9e67834SAndre Fischer 
56b9e67834SAndre Fischer 
57b9e67834SAndre Fischer 
58b9e67834SAndre Fischer 
EnumContext(const Application eApplication,const Context eContext)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 
EnumContext(const::rtl::OUString & rsApplicationName,const::rtl::OUString & rsContextName)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 
GetCombinedContext(void) const81b9e67834SAndre 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 
GetCombinedContext_DI(void) const89f120fe41SAndre Fischer sal_Int32 EnumContext::GetCombinedContext_DI (void) const
90f120fe41SAndre Fischer {
91ae13266dSAndre Fischer     return CombinedEnumContext(GetApplication_DI(), meContext);
92ae13266dSAndre Fischer }
93ae13266dSAndre Fischer 
94ae13266dSAndre Fischer 
95ae13266dSAndre Fischer 
96ae13266dSAndre Fischer 
GetApplication_DI(void) const97ae13266dSAndre Fischer EnumContext::Application EnumContext::GetApplication_DI (void) const
98ae13266dSAndre Fischer {
99f120fe41SAndre Fischer      switch (meApplication)
100f120fe41SAndre Fischer      {
101f120fe41SAndre Fischer          case Application_Draw:
102f120fe41SAndre Fischer          case Application_Impress:
103ae13266dSAndre Fischer             return Application_DrawImpress;
104f120fe41SAndre Fischer 
105dfbb0ba8SAndre Fischer          case Application_Writer:
10685f1aca2SAndre Fischer          case Application_WriterGlobal:
107dfbb0ba8SAndre Fischer          case Application_WriterWeb:
10885f1aca2SAndre Fischer          case Application_WriterXML:
109a7f63c05SOliver-Rainer Wittmann          case Application_WriterForm:
110a7f63c05SOliver-Rainer Wittmann          case Application_WriterReport:
111ae13266dSAndre Fischer              return Application_WriterVariants;
112dfbb0ba8SAndre Fischer 
113f120fe41SAndre Fischer          default:
114ae13266dSAndre Fischer              return meApplication;
115f120fe41SAndre Fischer      }
116f120fe41SAndre Fischer }
117f120fe41SAndre Fischer 
118f120fe41SAndre Fischer 
119f120fe41SAndre Fischer 
120f120fe41SAndre Fischer 
GetApplication(void) const121ae13266dSAndre Fischer EnumContext::Application EnumContext::GetApplication (void) const
122ae13266dSAndre Fischer {
123ae13266dSAndre Fischer     return meApplication;
124ae13266dSAndre Fischer }
125ae13266dSAndre Fischer 
126ae13266dSAndre Fischer 
127ae13266dSAndre Fischer 
128ae13266dSAndre Fischer 
GetApplicationName(void) const12995a18594SAndre Fischer const ::rtl::OUString& EnumContext::GetApplicationName (void) const
13095a18594SAndre Fischer {
13195a18594SAndre Fischer     return EnumContext::GetApplicationName(meApplication);
13295a18594SAndre Fischer }
13395a18594SAndre Fischer 
13495a18594SAndre Fischer 
13595a18594SAndre Fischer 
13695a18594SAndre Fischer 
GetContextName(void) const13795a18594SAndre Fischer const ::rtl::OUString& EnumContext::GetContextName (void) const
13895a18594SAndre Fischer {
13995a18594SAndre Fischer     return EnumContext::GetContextName(meContext);
14095a18594SAndre Fischer }
14195a18594SAndre Fischer 
14295a18594SAndre Fischer 
14395a18594SAndre Fischer 
14495a18594SAndre Fischer 
GetContext(void) const145*10405e3bSAndre Fischer EnumContext::Context EnumContext::GetContext (void) const
146*10405e3bSAndre Fischer {
147*10405e3bSAndre Fischer     return meContext;
148*10405e3bSAndre Fischer }
149*10405e3bSAndre Fischer 
150*10405e3bSAndre Fischer 
151*10405e3bSAndre Fischer 
152*10405e3bSAndre Fischer 
operator ==(const EnumContext aOther)153b9e67834SAndre Fischer bool EnumContext::operator== (const EnumContext aOther)
154b9e67834SAndre Fischer {
155b9e67834SAndre Fischer     return meApplication==aOther.meApplication
156b9e67834SAndre Fischer         && meContext==aOther.meContext;
157b9e67834SAndre Fischer }
158b9e67834SAndre Fischer 
159b9e67834SAndre Fischer 
160b9e67834SAndre Fischer 
161b9e67834SAndre Fischer 
operator !=(const EnumContext aOther)16295a18594SAndre Fischer bool EnumContext::operator!= (const EnumContext aOther)
16395a18594SAndre Fischer {
16495a18594SAndre Fischer     return meApplication!=aOther.meApplication
16595a18594SAndre Fischer         || meContext!=aOther.meContext;
16695a18594SAndre Fischer }
16795a18594SAndre Fischer 
16895a18594SAndre Fischer 
16995a18594SAndre Fischer 
17095a18594SAndre Fischer 
AddEntry(const::rtl::OUString & rsName,const Application eApplication)171b9e67834SAndre Fischer void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
172b9e67834SAndre Fischer {
173b9e67834SAndre Fischer     maApplicationMap[rsName] = eApplication;
174b9e67834SAndre Fischer     OSL_ASSERT(eApplication<=__LastApplicationEnum);
17595a18594SAndre Fischer     if (maApplicationVector.size() <= size_t(eApplication))
176b9e67834SAndre Fischer         maApplicationVector.resize(eApplication+1);
177b9e67834SAndre Fischer     maApplicationVector[eApplication]=rsName;
178b9e67834SAndre Fischer }
179b9e67834SAndre Fischer 
180b9e67834SAndre Fischer 
181b9e67834SAndre Fischer 
182b9e67834SAndre Fischer 
ProvideApplicationContainers(void)183b9e67834SAndre Fischer void EnumContext::ProvideApplicationContainers (void)
184b9e67834SAndre Fischer {
185b9e67834SAndre Fischer     if (maApplicationMap.empty())
186b9e67834SAndre Fischer     {
187b9e67834SAndre Fischer         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
188b9e67834SAndre Fischer         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
18985f1aca2SAndre Fischer         AddEntry(A2S("com.sun.star.text.GlobalDocument"), EnumContext::Application_WriterGlobal);
190dfbb0ba8SAndre Fischer         AddEntry(A2S("com.sun.star.text.WebDocument"), EnumContext::Application_WriterWeb);
19185f1aca2SAndre Fischer         AddEntry(A2S("com.sun.star.xforms.XMLFormDocument"), EnumContext::Application_WriterXML);
19265d25098SAndre Fischer         AddEntry(A2S("com.sun.star.sdb.FormDesign"), EnumContext::Application_WriterForm);
193a7f63c05SOliver-Rainer Wittmann         AddEntry(A2S("com.sun.star.sdb.TextReportDesign"), EnumContext::Application_WriterReport);
19495a18594SAndre Fischer         AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
19595a18594SAndre Fischer         AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
196b9e67834SAndre Fischer         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
197f120fe41SAndre Fischer 
19895a18594SAndre Fischer         AddEntry(A2S("any"), EnumContext::Application_Any);
199f120fe41SAndre Fischer         AddEntry(A2S("none"), EnumContext::Application_None);
200b9e67834SAndre Fischer     }
201b9e67834SAndre Fischer }
202b9e67834SAndre Fischer 
203b9e67834SAndre Fischer 
204b9e67834SAndre Fischer 
205b9e67834SAndre Fischer 
GetApplicationEnum(const::rtl::OUString & rsApplicationName)206b9e67834SAndre Fischer EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
207b9e67834SAndre Fischer {
208b9e67834SAndre Fischer     ProvideApplicationContainers();
209b9e67834SAndre Fischer 
210b9e67834SAndre Fischer     ApplicationMap::const_iterator iApplication(
211b9e67834SAndre Fischer         maApplicationMap.find(rsApplicationName));
212b9e67834SAndre Fischer     if (iApplication != maApplicationMap.end())
213b9e67834SAndre Fischer         return iApplication->second;
214b9e67834SAndre Fischer     else
215f120fe41SAndre Fischer         return EnumContext::Application_None;
216b9e67834SAndre Fischer }
217b9e67834SAndre Fischer 
218b9e67834SAndre Fischer 
219b9e67834SAndre Fischer 
220b9e67834SAndre Fischer 
GetApplicationName(const Application eApplication)221b9e67834SAndre Fischer const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
222b9e67834SAndre Fischer {
223b9e67834SAndre Fischer     ProvideApplicationContainers();
224b9e67834SAndre Fischer 
225b9e67834SAndre Fischer     const sal_Int32 nIndex (eApplication);
226b9e67834SAndre Fischer     if (nIndex<0 || nIndex>= __LastApplicationEnum)
227f120fe41SAndre Fischer         return maApplicationVector[Application_None];
228b9e67834SAndre Fischer     else
229b9e67834SAndre Fischer         return maApplicationVector[nIndex];
230b9e67834SAndre Fischer }
231b9e67834SAndre Fischer 
232b9e67834SAndre Fischer 
233b9e67834SAndre Fischer 
234b9e67834SAndre Fischer 
AddEntry(const::rtl::OUString & rsName,const Context eApplication)235b9e67834SAndre Fischer void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
236b9e67834SAndre Fischer {
237b9e67834SAndre Fischer     maContextMap[rsName] = eApplication;
238b9e67834SAndre Fischer     OSL_ASSERT(eApplication<=__LastContextEnum);
23995a18594SAndre Fischer     if (maContextVector.size() <= size_t(eApplication))
240b9e67834SAndre Fischer         maContextVector.resize(eApplication+1);
241b9e67834SAndre Fischer     maContextVector[eApplication] = rsName;
242b9e67834SAndre Fischer }
243b9e67834SAndre Fischer 
244b9e67834SAndre Fischer 
245b9e67834SAndre Fischer 
246b9e67834SAndre Fischer 
ProvideContextContainers(void)247b9e67834SAndre Fischer void EnumContext::ProvideContextContainers (void)
248b9e67834SAndre Fischer {
249b9e67834SAndre Fischer     if (maContextMap.empty())
250b9e67834SAndre Fischer     {
251b9e67834SAndre Fischer         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
25295a18594SAndre Fischer         AddEntry(A2S("any"), Context_Any);
253b9e67834SAndre Fischer         AddEntry(A2S("default"), Context_Default);
25454eaaa32SAndre Fischer         AddEntry(A2S("empty"), Context_Empty);
255f120fe41SAndre Fischer #define AddContext(context) AddEntry(A2S(#context), Context_##context);
256f120fe41SAndre Fischer         AddContext(3DObject);
257f120fe41SAndre Fischer         AddContext(Annotation);
258f120fe41SAndre Fischer         AddContext(Auditing);
259f120fe41SAndre Fischer         AddContext(Cell);
260f120fe41SAndre Fischer         AddContext(Chart);
261f120fe41SAndre Fischer         AddContext(Chart);
262f120fe41SAndre Fischer         AddContext(Draw);
263f120fe41SAndre Fischer         AddContext(DrawPage);
264f120fe41SAndre Fischer         AddContext(DrawText);
265f120fe41SAndre Fischer         AddContext(EditCell);
266f120fe41SAndre Fischer         AddContext(Form);
267f120fe41SAndre Fischer         AddContext(Frame);
268f120fe41SAndre Fischer         AddContext(Graphic);
269f120fe41SAndre Fischer         AddContext(HandoutPage);
270f120fe41SAndre Fischer         AddContext(MasterPage);
271f120fe41SAndre Fischer         AddContext(Media);
272f120fe41SAndre Fischer         AddContext(MultiObject);
273f120fe41SAndre Fischer         AddContext(NotesPage);
274f120fe41SAndre Fischer         AddContext(OLE);
275f120fe41SAndre Fischer         AddContext(OutlineText);
276f120fe41SAndre Fischer         AddContext(Pivot);
277f120fe41SAndre Fischer         AddContext(SlidesorterPage);
278f120fe41SAndre Fischer         AddContext(Table);
279f120fe41SAndre Fischer         AddContext(Text);
280f120fe41SAndre Fischer         AddContext(TextObject);
281f120fe41SAndre Fischer #undef AddContext
282b9e67834SAndre Fischer     }
283b9e67834SAndre Fischer }
284b9e67834SAndre Fischer 
285b9e67834SAndre Fischer 
286b9e67834SAndre Fischer 
287b9e67834SAndre Fischer 
GetContextEnum(const::rtl::OUString & rsContextName)288b9e67834SAndre Fischer EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
289b9e67834SAndre Fischer {
290b9e67834SAndre Fischer     ProvideContextContainers();
291b9e67834SAndre Fischer 
292b9e67834SAndre Fischer     ContextMap::const_iterator iContext(
293b9e67834SAndre Fischer         maContextMap.find(rsContextName));
294b9e67834SAndre Fischer     if (iContext != maContextMap.end())
295b9e67834SAndre Fischer         return iContext->second;
296b9e67834SAndre Fischer     else
29795a18594SAndre Fischer         return EnumContext::Context_Unknown;
298b9e67834SAndre Fischer }
299b9e67834SAndre Fischer 
300b9e67834SAndre Fischer 
301b9e67834SAndre Fischer 
302b9e67834SAndre Fischer 
GetContextName(const Context eContext)303b9e67834SAndre Fischer const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
304b9e67834SAndre Fischer {
305b9e67834SAndre Fischer     ProvideContextContainers();
306b9e67834SAndre Fischer 
307b9e67834SAndre Fischer     const sal_Int32 nIndex (eContext);
308b9e67834SAndre Fischer     if (nIndex<0 || nIndex>= __LastContextEnum)
30995a18594SAndre Fischer         return maContextVector[Context_Unknown];
310b9e67834SAndre Fischer     else
311b9e67834SAndre Fischer         return maContextVector[nIndex];
312b9e67834SAndre Fischer }
313b9e67834SAndre Fischer 
314b9e67834SAndre Fischer 
315b9e67834SAndre Fischer 
316b9e67834SAndre Fischer 
EvaluateMatch(const EnumContext & rOther) const31795a18594SAndre Fischer sal_Int32 EnumContext::EvaluateMatch (
31895a18594SAndre Fischer     const EnumContext& rOther) const
31995a18594SAndre Fischer {
32095a18594SAndre Fischer     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
32195a18594SAndre Fischer     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
32295a18594SAndre Fischer     {
32395a18594SAndre Fischer         // Application name matches.
32495a18594SAndre Fischer         const bool bContextNameIsAny (rOther.meContext == Context_Any);
32595a18594SAndre Fischer         if (rOther.meContext==meContext || bContextNameIsAny)
32695a18594SAndre Fischer         {
32795a18594SAndre Fischer             // Context name matches.
32895a18594SAndre Fischer             return (bApplicationNameIsAny ? 1 : 0)
32995a18594SAndre Fischer                 + (bContextNameIsAny ? 2 : 0);
33095a18594SAndre Fischer         }
33195a18594SAndre Fischer     }
33295a18594SAndre Fischer     return NoMatch;
33395a18594SAndre Fischer }
33495a18594SAndre Fischer 
33595a18594SAndre Fischer 
33695a18594SAndre Fischer 
33795a18594SAndre Fischer 
EvaluateMatch(const::std::vector<EnumContext> & rOthers) const33895a18594SAndre Fischer sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
33995a18594SAndre Fischer {
34095a18594SAndre Fischer     sal_Int32 nBestMatch (NoMatch);
34195a18594SAndre Fischer 
34295a18594SAndre Fischer     for (::std::vector<EnumContext>::const_iterator
34395a18594SAndre Fischer              iContext(rOthers.begin()),
34495a18594SAndre Fischer              iEnd(rOthers.end());
34595a18594SAndre Fischer          iContext!=iEnd;
34695a18594SAndre Fischer          ++iContext)
34795a18594SAndre Fischer     {
34895a18594SAndre Fischer         const sal_Int32 nMatch (EvaluateMatch(*iContext));
34995a18594SAndre Fischer         if (nMatch < nBestMatch)
35095a18594SAndre Fischer         {
35195a18594SAndre Fischer             if (nMatch == OptimalMatch)
35295a18594SAndre Fischer             {
35395a18594SAndre Fischer                 // We will find no better match so stop searching.
35495a18594SAndre Fischer                 return OptimalMatch;
35595a18594SAndre Fischer             }
35695a18594SAndre Fischer             nBestMatch = nMatch;
35795a18594SAndre Fischer         }
35895a18594SAndre Fischer     }
35995a18594SAndre Fischer     return nBestMatch;
36095a18594SAndre Fischer }
36195a18594SAndre Fischer 
36295a18594SAndre Fischer 
36395a18594SAndre Fischer 
364b9e67834SAndre Fischer } } // end of namespace sfx2::sidebar
365