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