xref: /trunk/main/sfx2/source/sidebar/EnumContext.cxx (revision 10405e3b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sfx2.hxx"
23 
24 #include "sidebar/EnumContext.hxx"
25 
26 #include <map>
27 
28 namespace sfx2 { namespace sidebar {
29 
30 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
31 
32 namespace {
33 
34 typedef ::std::map<rtl::OUString,EnumContext::Application> ApplicationMap;
35 typedef ::std::vector<rtl::OUString> ApplicationVector;
36 static ApplicationMap maApplicationMap;
37 static ApplicationVector maApplicationVector;
38 
39 typedef ::std::map<rtl::OUString,EnumContext::Context> ContextMap;
40 typedef ::std::vector<rtl::OUString> ContextVector;
41 static ContextMap maContextMap;
42 static ContextVector maContextVector;
43 
44 }
45 
46 const sal_Int32 EnumContext::NoMatch = 4;
47 const sal_Int32 EnumContext::OptimalMatch = 0;  // Neither application nor context name is "any".
48 
49 
EnumContext(void)50 EnumContext::EnumContext (void)
51     : meApplication(Application_None),
52       meContext(Context_Unknown)
53 {
54 }
55 
56 
57 
58 
EnumContext(const Application eApplication,const Context eContext)59 EnumContext::EnumContext (
60     const Application eApplication,
61     const Context eContext)
62     : meApplication(eApplication),
63       meContext(eContext)
64 {
65 }
66 
67 
68 
69 
EnumContext(const::rtl::OUString & rsApplicationName,const::rtl::OUString & rsContextName)70 EnumContext::EnumContext (
71     const ::rtl::OUString& rsApplicationName,
72     const ::rtl::OUString& rsContextName)
73     : meApplication(GetApplicationEnum(rsApplicationName)),
74       meContext(GetContextEnum(rsContextName))
75 {
76 }
77 
78 
79 
80 
GetCombinedContext(void) const81 sal_Int32 EnumContext::GetCombinedContext (void) const
82 {
83     return CombinedEnumContext(meApplication, meContext);
84 }
85 
86 
87 
88 
GetCombinedContext_DI(void) const89 sal_Int32 EnumContext::GetCombinedContext_DI (void) const
90 {
91     return CombinedEnumContext(GetApplication_DI(), meContext);
92 }
93 
94 
95 
96 
GetApplication_DI(void) const97 EnumContext::Application EnumContext::GetApplication_DI (void) const
98 {
99      switch (meApplication)
100      {
101          case Application_Draw:
102          case Application_Impress:
103             return Application_DrawImpress;
104 
105          case Application_Writer:
106          case Application_WriterGlobal:
107          case Application_WriterWeb:
108          case Application_WriterXML:
109          case Application_WriterForm:
110          case Application_WriterReport:
111              return Application_WriterVariants;
112 
113          default:
114              return meApplication;
115      }
116 }
117 
118 
119 
120 
GetApplication(void) const121 EnumContext::Application EnumContext::GetApplication (void) const
122 {
123     return meApplication;
124 }
125 
126 
127 
128 
GetApplicationName(void) const129 const ::rtl::OUString& EnumContext::GetApplicationName (void) const
130 {
131     return EnumContext::GetApplicationName(meApplication);
132 }
133 
134 
135 
136 
GetContextName(void) const137 const ::rtl::OUString& EnumContext::GetContextName (void) const
138 {
139     return EnumContext::GetContextName(meContext);
140 }
141 
142 
143 
144 
GetContext(void) const145 EnumContext::Context EnumContext::GetContext (void) const
146 {
147     return meContext;
148 }
149 
150 
151 
152 
operator ==(const EnumContext aOther)153 bool EnumContext::operator== (const EnumContext aOther)
154 {
155     return meApplication==aOther.meApplication
156         && meContext==aOther.meContext;
157 }
158 
159 
160 
161 
operator !=(const EnumContext aOther)162 bool EnumContext::operator!= (const EnumContext aOther)
163 {
164     return meApplication!=aOther.meApplication
165         || meContext!=aOther.meContext;
166 }
167 
168 
169 
170 
AddEntry(const::rtl::OUString & rsName,const Application eApplication)171 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
172 {
173     maApplicationMap[rsName] = eApplication;
174     OSL_ASSERT(eApplication<=__LastApplicationEnum);
175     if (maApplicationVector.size() <= size_t(eApplication))
176         maApplicationVector.resize(eApplication+1);
177     maApplicationVector[eApplication]=rsName;
178 }
179 
180 
181 
182 
ProvideApplicationContainers(void)183 void EnumContext::ProvideApplicationContainers (void)
184 {
185     if (maApplicationMap.empty())
186     {
187         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
188         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
189         AddEntry(A2S("com.sun.star.text.GlobalDocument"), EnumContext::Application_WriterGlobal);
190         AddEntry(A2S("com.sun.star.text.WebDocument"), EnumContext::Application_WriterWeb);
191         AddEntry(A2S("com.sun.star.xforms.XMLFormDocument"), EnumContext::Application_WriterXML);
192         AddEntry(A2S("com.sun.star.sdb.FormDesign"), EnumContext::Application_WriterForm);
193         AddEntry(A2S("com.sun.star.sdb.TextReportDesign"), EnumContext::Application_WriterReport);
194         AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
195         AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
196         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
197 
198         AddEntry(A2S("any"), EnumContext::Application_Any);
199         AddEntry(A2S("none"), EnumContext::Application_None);
200     }
201 }
202 
203 
204 
205 
GetApplicationEnum(const::rtl::OUString & rsApplicationName)206 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
207 {
208     ProvideApplicationContainers();
209 
210     ApplicationMap::const_iterator iApplication(
211         maApplicationMap.find(rsApplicationName));
212     if (iApplication != maApplicationMap.end())
213         return iApplication->second;
214     else
215         return EnumContext::Application_None;
216 }
217 
218 
219 
220 
GetApplicationName(const Application eApplication)221 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
222 {
223     ProvideApplicationContainers();
224 
225     const sal_Int32 nIndex (eApplication);
226     if (nIndex<0 || nIndex>= __LastApplicationEnum)
227         return maApplicationVector[Application_None];
228     else
229         return maApplicationVector[nIndex];
230 }
231 
232 
233 
234 
AddEntry(const::rtl::OUString & rsName,const Context eApplication)235 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
236 {
237     maContextMap[rsName] = eApplication;
238     OSL_ASSERT(eApplication<=__LastContextEnum);
239     if (maContextVector.size() <= size_t(eApplication))
240         maContextVector.resize(eApplication+1);
241     maContextVector[eApplication] = rsName;
242 }
243 
244 
245 
246 
ProvideContextContainers(void)247 void EnumContext::ProvideContextContainers (void)
248 {
249     if (maContextMap.empty())
250     {
251         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
252         AddEntry(A2S("any"), Context_Any);
253         AddEntry(A2S("default"), Context_Default);
254         AddEntry(A2S("empty"), Context_Empty);
255 #define AddContext(context) AddEntry(A2S(#context), Context_##context);
256         AddContext(3DObject);
257         AddContext(Annotation);
258         AddContext(Auditing);
259         AddContext(Cell);
260         AddContext(Chart);
261         AddContext(Chart);
262         AddContext(Draw);
263         AddContext(DrawPage);
264         AddContext(DrawText);
265         AddContext(EditCell);
266         AddContext(Form);
267         AddContext(Frame);
268         AddContext(Graphic);
269         AddContext(HandoutPage);
270         AddContext(MasterPage);
271         AddContext(Media);
272         AddContext(MultiObject);
273         AddContext(NotesPage);
274         AddContext(OLE);
275         AddContext(OutlineText);
276         AddContext(Pivot);
277         AddContext(SlidesorterPage);
278         AddContext(Table);
279         AddContext(Text);
280         AddContext(TextObject);
281 #undef AddContext
282     }
283 }
284 
285 
286 
287 
GetContextEnum(const::rtl::OUString & rsContextName)288 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
289 {
290     ProvideContextContainers();
291 
292     ContextMap::const_iterator iContext(
293         maContextMap.find(rsContextName));
294     if (iContext != maContextMap.end())
295         return iContext->second;
296     else
297         return EnumContext::Context_Unknown;
298 }
299 
300 
301 
302 
GetContextName(const Context eContext)303 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
304 {
305     ProvideContextContainers();
306 
307     const sal_Int32 nIndex (eContext);
308     if (nIndex<0 || nIndex>= __LastContextEnum)
309         return maContextVector[Context_Unknown];
310     else
311         return maContextVector[nIndex];
312 }
313 
314 
315 
316 
EvaluateMatch(const EnumContext & rOther) const317 sal_Int32 EnumContext::EvaluateMatch (
318     const EnumContext& rOther) const
319 {
320     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
321     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
322     {
323         // Application name matches.
324         const bool bContextNameIsAny (rOther.meContext == Context_Any);
325         if (rOther.meContext==meContext || bContextNameIsAny)
326         {
327             // Context name matches.
328             return (bApplicationNameIsAny ? 1 : 0)
329                 + (bContextNameIsAny ? 2 : 0);
330         }
331     }
332     return NoMatch;
333 }
334 
335 
336 
337 
EvaluateMatch(const::std::vector<EnumContext> & rOthers) const338 sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
339 {
340     sal_Int32 nBestMatch (NoMatch);
341 
342     for (::std::vector<EnumContext>::const_iterator
343              iContext(rOthers.begin()),
344              iEnd(rOthers.end());
345          iContext!=iEnd;
346          ++iContext)
347     {
348         const sal_Int32 nMatch (EvaluateMatch(*iContext));
349         if (nMatch < nBestMatch)
350         {
351             if (nMatch == OptimalMatch)
352             {
353                 // We will find no better match so stop searching.
354                 return OptimalMatch;
355             }
356             nBestMatch = nMatch;
357         }
358     }
359     return nBestMatch;
360 }
361 
362 
363 
364 } } // end of namespace sfx2::sidebar
365