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