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_Unknown),
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 const ::rtl::OUString& EnumContext::GetApplicationName (void) const
90 {
91     return EnumContext::GetApplicationName(meApplication);
92 }
93 
94 
95 
96 
97 const ::rtl::OUString& EnumContext::GetContextName (void) const
98 {
99     return EnumContext::GetContextName(meContext);
100 }
101 
102 
103 
104 
105 bool EnumContext::operator== (const EnumContext aOther)
106 {
107     return meApplication==aOther.meApplication
108         && meContext==aOther.meContext;
109 }
110 
111 
112 
113 
114 bool EnumContext::operator!= (const EnumContext aOther)
115 {
116     return meApplication!=aOther.meApplication
117         || meContext!=aOther.meContext;
118 }
119 
120 
121 
122 
123 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Application eApplication)
124 {
125     maApplicationMap[rsName] = eApplication;
126     OSL_ASSERT(eApplication<=__LastApplicationEnum);
127     if (maApplicationVector.size() <= size_t(eApplication))
128         maApplicationVector.resize(eApplication+1);
129     maApplicationVector[eApplication]=rsName;
130 }
131 
132 
133 
134 
135 void EnumContext::ProvideApplicationContainers (void)
136 {
137     if (maApplicationMap.empty())
138     {
139         maApplicationVector.resize(static_cast<size_t>(EnumContext::__LastApplicationEnum)+1);
140         AddEntry(A2S("com.sun.star.text.TextDocument"), EnumContext::Application_Writer);
141         AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc);
142         AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw);
143         AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress);
144         AddEntry(A2S("any"), EnumContext::Application_Any);
145         AddEntry(A2S("unknown"), EnumContext::Application_Unknown);
146     }
147 }
148 
149 
150 
151 
152 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName)
153 {
154     ProvideApplicationContainers();
155 
156     ApplicationMap::const_iterator iApplication(
157         maApplicationMap.find(rsApplicationName));
158     if (iApplication != maApplicationMap.end())
159         return iApplication->second;
160     else
161         return EnumContext::Application_Unknown;
162 }
163 
164 
165 
166 
167 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication)
168 {
169     ProvideApplicationContainers();
170 
171     const sal_Int32 nIndex (eApplication);
172     if (nIndex<0 || nIndex>= __LastApplicationEnum)
173         return maApplicationVector[Application_Unknown];
174     else
175         return maApplicationVector[nIndex];
176 }
177 
178 
179 
180 
181 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication)
182 {
183     maContextMap[rsName] = eApplication;
184     OSL_ASSERT(eApplication<=__LastContextEnum);
185     if (maContextVector.size() <= size_t(eApplication))
186         maContextVector.resize(eApplication+1);
187     maContextVector[eApplication]=rsName;
188 }
189 
190 
191 
192 
193 void EnumContext::ProvideContextContainers (void)
194 {
195     if (maContextMap.empty())
196     {
197         maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1);
198         AddEntry(A2S("text"), Context_Text);
199         AddEntry(A2S("any"), Context_Any);
200         AddEntry(A2S("default"), Context_Default);
201     }
202 }
203 
204 
205 
206 
207 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName)
208 {
209     ProvideContextContainers();
210 
211     ContextMap::const_iterator iContext(
212         maContextMap.find(rsContextName));
213     if (iContext != maContextMap.end())
214         return iContext->second;
215     else
216         return EnumContext::Context_Unknown;
217 }
218 
219 
220 
221 
222 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext)
223 {
224     ProvideContextContainers();
225 
226     const sal_Int32 nIndex (eContext);
227     if (nIndex<0 || nIndex>= __LastContextEnum)
228         return maContextVector[Context_Unknown];
229     else
230         return maContextVector[nIndex];
231 }
232 
233 
234 
235 
236 sal_Int32 EnumContext::EvaluateMatch (
237     const EnumContext& rOther) const
238 {
239     const bool bApplicationNameIsAny (rOther.meApplication == Application_Any);
240     if (rOther.meApplication==meApplication || bApplicationNameIsAny)
241     {
242         // Application name matches.
243         const bool bContextNameIsAny (rOther.meContext == Context_Any);
244         if (rOther.meContext==meContext || bContextNameIsAny)
245         {
246             // Context name matches.
247             return (bApplicationNameIsAny ? 1 : 0)
248                 + (bContextNameIsAny ? 2 : 0);
249         }
250     }
251     return NoMatch;
252 }
253 
254 
255 
256 
257 sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const
258 {
259     sal_Int32 nBestMatch (NoMatch);
260 
261     for (::std::vector<EnumContext>::const_iterator
262              iContext(rOthers.begin()),
263              iEnd(rOthers.end());
264          iContext!=iEnd;
265          ++iContext)
266     {
267         const sal_Int32 nMatch (EvaluateMatch(*iContext));
268         if (nMatch < nBestMatch)
269         {
270             if (nMatch == OptimalMatch)
271             {
272                 // We will find no better match so stop searching.
273                 return OptimalMatch;
274             }
275             nBestMatch = nMatch;
276         }
277     }
278     return nBestMatch;
279 }
280 
281 
282 
283 } } // end of namespace sfx2::sidebar
284