xref: /trunk/main/sfx2/source/sidebar/EnumContext.cxx (revision ae13266d)
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 
50 EnumContext::EnumContext (void)
51     : meApplication(Application_None),
52       meContext(Context_Unknown)
53 {
54 }
55 
56 
57 
58 
59 EnumContext::EnumContext (
60     const Application eApplication,
61     const Context eContext)
62     : meApplication(eApplication),
63       meContext(eContext)
64 {
65 }
66 
67 
68 
69 
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 
81 sal_Int32 EnumContext::GetCombinedContext (void) const
82 {
83     return CombinedEnumContext(meApplication, meContext);
84 }
85 
86 
87 
88 
89 sal_Int32 EnumContext::GetCombinedContext_DI (void) const
90 {
91     return CombinedEnumContext(GetApplication_DI(), meContext);
92 }
93 
94 
95 
96 
97 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 
121 EnumContext::Application EnumContext::GetApplication (void) const
122 {
123     return meApplication;
124 }
125 
126 
127 
128 
129 const ::rtl::OUString& EnumContext::GetApplicationName (void) const
130 {
131     return EnumContext::GetApplicationName(meApplication);
132 }
133 
134 
135 
136 
137 const ::rtl::OUString& EnumContext::GetContextName (void) const
138 {
139     return EnumContext::GetContextName(meContext);
140 }
141 
142 
143 
144 
145 bool EnumContext::operator== (const EnumContext aOther)
146 {
147     return meApplication==aOther.meApplication
148         && meContext==aOther.meContext;
149 }
150 
151 
152 
153 
154 bool EnumContext::operator!= (const EnumContext aOther)
155 {
156     return meApplication!=aOther.meApplication
157         || meContext!=aOther.meContext;
158 }
159 
160 
161 
162 
163 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
164 {
165     maApplicationMap[rsName] = eApplication;
166     OSL_ASSERT(eApplication<=__LastApplicationEnum);
167     if (maApplicationVector.size() <= size_t(eApplication))
168         maApplicationVector.resize(eApplication+1);
169     maApplicationVector[eApplication]=rsName;
170 }
171 
172 
173 
174 
175 void EnumContext::ProvideApplicationContainers (void)
176 {
177     if (maApplicationMap.empty())
178     {
179         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
180         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
181         AddEntry(A2S("com.sun.star.text.GlobalDocument"), EnumContext::Application_WriterGlobal);
182         AddEntry(A2S("com.sun.star.text.WebDocument"), EnumContext::Application_WriterWeb);
183         AddEntry(A2S("com.sun.star.xforms.XMLFormDocument"), EnumContext::Application_WriterXML);
184         AddEntry(A2S("com.sun.star.sdb.FormDesign"), EnumContext::Application_WriterForm);
185         AddEntry(A2S("com.sun.star.sdb.TextReportDesign"), EnumContext::Application_WriterReport);
186         AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
187         AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
188         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
189 
190         AddEntry(A2S("any"), EnumContext::Application_Any);
191         AddEntry(A2S("none"), EnumContext::Application_None);
192     }
193 }
194 
195 
196 
197 
198 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
199 {
200     ProvideApplicationContainers();
201 
202     ApplicationMap::const_iterator iApplication(
203         maApplicationMap.find(rsApplicationName));
204     if (iApplication != maApplicationMap.end())
205         return iApplication->second;
206     else
207         return EnumContext::Application_None;
208 }
209 
210 
211 
212 
213 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
214 {
215     ProvideApplicationContainers();
216 
217     const sal_Int32 nIndex (eApplication);
218     if (nIndex<0 || nIndex>= __LastApplicationEnum)
219         return maApplicationVector[Application_None];
220     else
221         return maApplicationVector[nIndex];
222 }
223 
224 
225 
226 
227 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
228 {
229     maContextMap[rsName] = eApplication;
230     OSL_ASSERT(eApplication<=__LastContextEnum);
231     if (maContextVector.size() <= size_t(eApplication))
232         maContextVector.resize(eApplication+1);
233     maContextVector[eApplication] = rsName;
234 }
235 
236 
237 
238 
239 void EnumContext::ProvideContextContainers (void)
240 {
241     if (maContextMap.empty())
242     {
243         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
244         AddEntry(A2S("any"), Context_Any);
245         AddEntry(A2S("default"), Context_Default);
246         AddEntry(A2S("empty"), Context_Empty);
247 #define AddContext(context) AddEntry(A2S(#context), Context_##context);
248         AddContext(3DObject);
249         AddContext(Annotation);
250         AddContext(Auditing);
251         AddContext(Cell);
252         AddContext(Chart);
253         AddContext(Chart);
254         AddContext(Draw);
255         AddContext(DrawPage);
256         AddContext(DrawText);
257         AddContext(EditCell);
258         AddContext(Form);
259         AddContext(Frame);
260         AddContext(Graphic);
261         AddContext(HandoutPage);
262         AddContext(MasterPage);
263         AddContext(Media);
264         AddContext(MultiObject);
265         AddContext(NotesPage);
266         AddContext(OLE);
267         AddContext(OutlineText);
268         AddContext(Pivot);
269         AddContext(SlidesorterPage);
270         AddContext(Table);
271         AddContext(Text);
272         AddContext(TextObject);
273 #undef AddContext
274     }
275 }
276 
277 
278 
279 
280 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
281 {
282     ProvideContextContainers();
283 
284     ContextMap::const_iterator iContext(
285         maContextMap.find(rsContextName));
286     if (iContext != maContextMap.end())
287         return iContext->second;
288     else
289         return EnumContext::Context_Unknown;
290 }
291 
292 
293 
294 
295 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
296 {
297     ProvideContextContainers();
298 
299     const sal_Int32 nIndex (eContext);
300     if (nIndex<0 || nIndex>= __LastContextEnum)
301         return maContextVector[Context_Unknown];
302     else
303         return maContextVector[nIndex];
304 }
305 
306 
307 
308 
309 sal_Int32 EnumContext::EvaluateMatch (
310     const EnumContext& rOther) const
311 {
312     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
313     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
314     {
315         // Application name matches.
316         const bool bContextNameIsAny (rOther.meContext == Context_Any);
317         if (rOther.meContext==meContext || bContextNameIsAny)
318         {
319             // Context name matches.
320             return (bApplicationNameIsAny ? 1 : 0)
321                 + (bContextNameIsAny ? 2 : 0);
322         }
323     }
324     return NoMatch;
325 }
326 
327 
328 
329 
330 sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
331 {
332     sal_Int32 nBestMatch (NoMatch);
333 
334     for (::std::vector<EnumContext>::const_iterator
335              iContext(rOthers.begin()),
336              iEnd(rOthers.end());
337          iContext!=iEnd;
338          ++iContext)
339     {
340         const sal_Int32 nMatch (EvaluateMatch(*iContext));
341         if (nMatch < nBestMatch)
342         {
343             if (nMatch == OptimalMatch)
344             {
345                 // We will find no better match so stop searching.
346                 return OptimalMatch;
347             }
348             nBestMatch = nMatch;
349         }
350     }
351     return nBestMatch;
352 }
353 
354 
355 
356 } } // end of namespace sfx2::sidebar
357