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