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 #define AddContext(context) AddEntry(A2S(#context), Context_##context);
218         AddContext(3DObject);
219         AddContext(Annotation);
220         AddContext(Auditing);
221         AddContext(Cell);
222         AddContext(Chart);
223         AddContext(Chart);
224         AddContext(Draw);
225         AddContext(DrawPage);
226         AddContext(DrawText);
227         AddContext(EditCell);
228         AddContext(Form);
229         AddContext(Frame);
230         AddContext(Graphic);
231         AddContext(HandoutPage);
232         AddContext(MasterPage);
233         AddContext(Media);
234         AddContext(MultiObject);
235         AddContext(NotesPage);
236         AddContext(OLE);
237         AddContext(OutlineText);
238         AddContext(Pivot);
239         AddContext(SlidesorterPage);
240         AddContext(Table);
241         AddContext(Text);
242         AddContext(TextObject);
243 #undef AddContext
244     }
245 }
246 
247 
248 
249 
250 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
251 {
252     ProvideContextContainers();
253 
254     ContextMap::const_iterator iContext(
255         maContextMap.find(rsContextName));
256     if (iContext != maContextMap.end())
257         return iContext->second;
258     else
259         return EnumContext::Context_Unknown;
260 }
261 
262 
263 
264 
265 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
266 {
267     ProvideContextContainers();
268 
269     const sal_Int32 nIndex (eContext);
270     if (nIndex<0 || nIndex>= __LastContextEnum)
271         return maContextVector[Context_Unknown];
272     else
273         return maContextVector[nIndex];
274 }
275 
276 
277 
278 
279 sal_Int32 EnumContext::EvaluateMatch (
280     const EnumContext& rOther) const
281 {
282     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
283     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
284     {
285         // Application name matches.
286         const bool bContextNameIsAny (rOther.meContext == Context_Any);
287         if (rOther.meContext==meContext || bContextNameIsAny)
288         {
289             // Context name matches.
290             return (bApplicationNameIsAny ? 1 : 0)
291                 + (bContextNameIsAny ? 2 : 0);
292         }
293     }
294     return NoMatch;
295 }
296 
297 
298 
299 
300 sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
301 {
302     sal_Int32 nBestMatch (NoMatch);
303 
304     for (::std::vector<EnumContext>::const_iterator
305              iContext(rOthers.begin()),
306              iEnd(rOthers.end());
307          iContext!=iEnd;
308          ++iContext)
309     {
310         const sal_Int32 nMatch (EvaluateMatch(*iContext));
311         if (nMatch < nBestMatch)
312         {
313             if (nMatch == OptimalMatch)
314             {
315                 // We will find no better match so stop searching.
316                 return OptimalMatch;
317             }
318             nBestMatch = nMatch;
319         }
320     }
321     return nBestMatch;
322 }
323 
324 
325 
326 } } // end of namespace sfx2::sidebar
327