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