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.sdb.FormDesign"), EnumContext::Application_WriterForm); 167 AddEntry(A2S("com.sun.star.sheet.SpreadsheetDocument"), EnumContext::Application_Calc); 168 AddEntry(A2S("com.sun.star.drawing.DrawingDocument"), EnumContext::Application_Draw); 169 AddEntry(A2S("com.sun.star.presentation.PresentationDocument"), EnumContext::Application_Impress); 170 171 AddEntry(A2S("any"), EnumContext::Application_Any); 172 AddEntry(A2S("none"), EnumContext::Application_None); 173 } 174 } 175 176 177 178 179 EnumContext::Application EnumContext::GetApplicationEnum (const ::rtl::OUString& rsApplicationName) 180 { 181 ProvideApplicationContainers(); 182 183 ApplicationMap::const_iterator iApplication( 184 maApplicationMap.find(rsApplicationName)); 185 if (iApplication != maApplicationMap.end()) 186 return iApplication->second; 187 else 188 return EnumContext::Application_None; 189 } 190 191 192 193 194 const ::rtl::OUString& EnumContext::GetApplicationName (const Application eApplication) 195 { 196 ProvideApplicationContainers(); 197 198 const sal_Int32 nIndex (eApplication); 199 if (nIndex<0 || nIndex>= __LastApplicationEnum) 200 return maApplicationVector[Application_None]; 201 else 202 return maApplicationVector[nIndex]; 203 } 204 205 206 207 208 void EnumContext::AddEntry (const ::rtl::OUString& rsName, const Context eApplication) 209 { 210 maContextMap[rsName] = eApplication; 211 OSL_ASSERT(eApplication<=__LastContextEnum); 212 if (maContextVector.size() <= size_t(eApplication)) 213 maContextVector.resize(eApplication+1); 214 maContextVector[eApplication] = rsName; 215 } 216 217 218 219 220 void EnumContext::ProvideContextContainers (void) 221 { 222 if (maContextMap.empty()) 223 { 224 maContextVector.resize(static_cast<size_t>(__LastContextEnum)+1); 225 AddEntry(A2S("any"), Context_Any); 226 AddEntry(A2S("default"), Context_Default); 227 AddEntry(A2S("empty"), Context_Empty); 228 #define AddContext(context) AddEntry(A2S(#context), Context_##context); 229 AddContext(3DObject); 230 AddContext(Annotation); 231 AddContext(Auditing); 232 AddContext(Cell); 233 AddContext(Chart); 234 AddContext(Chart); 235 AddContext(Draw); 236 AddContext(DrawPage); 237 AddContext(DrawText); 238 AddContext(EditCell); 239 AddContext(Form); 240 AddContext(Frame); 241 AddContext(Graphic); 242 AddContext(HandoutPage); 243 AddContext(MasterPage); 244 AddContext(Media); 245 AddContext(MultiObject); 246 AddContext(NotesPage); 247 AddContext(OLE); 248 AddContext(OutlineText); 249 AddContext(Pivot); 250 AddContext(SlidesorterPage); 251 AddContext(Table); 252 AddContext(Text); 253 AddContext(TextObject); 254 #undef AddContext 255 } 256 } 257 258 259 260 261 EnumContext::Context EnumContext::GetContextEnum (const ::rtl::OUString& rsContextName) 262 { 263 ProvideContextContainers(); 264 265 ContextMap::const_iterator iContext( 266 maContextMap.find(rsContextName)); 267 if (iContext != maContextMap.end()) 268 return iContext->second; 269 else 270 return EnumContext::Context_Unknown; 271 } 272 273 274 275 276 const ::rtl::OUString& EnumContext::GetContextName (const Context eContext) 277 { 278 ProvideContextContainers(); 279 280 const sal_Int32 nIndex (eContext); 281 if (nIndex<0 || nIndex>= __LastContextEnum) 282 return maContextVector[Context_Unknown]; 283 else 284 return maContextVector[nIndex]; 285 } 286 287 288 289 290 sal_Int32 EnumContext::EvaluateMatch ( 291 const EnumContext& rOther) const 292 { 293 const bool bApplicationNameIsAny (rOther.meApplication == Application_Any); 294 if (rOther.meApplication==meApplication || bApplicationNameIsAny) 295 { 296 // Application name matches. 297 const bool bContextNameIsAny (rOther.meContext == Context_Any); 298 if (rOther.meContext==meContext || bContextNameIsAny) 299 { 300 // Context name matches. 301 return (bApplicationNameIsAny ? 1 : 0) 302 + (bContextNameIsAny ? 2 : 0); 303 } 304 } 305 return NoMatch; 306 } 307 308 309 310 311 sal_Int32 EnumContext::EvaluateMatch (const ::std::vector<EnumContext>& rOthers) const 312 { 313 sal_Int32 nBestMatch (NoMatch); 314 315 for (::std::vector<EnumContext>::const_iterator 316 iContext(rOthers.begin()), 317 iEnd(rOthers.end()); 318 iContext!=iEnd; 319 ++iContext) 320 { 321 const sal_Int32 nMatch (EvaluateMatch(*iContext)); 322 if (nMatch < nBestMatch) 323 { 324 if (nMatch == OptimalMatch) 325 { 326 // We will find no better match so stop searching. 327 return OptimalMatch; 328 } 329 nBestMatch = nMatch; 330 } 331 } 332 return nBestMatch; 333 } 334 335 336 337 } } // end of namespace sfx2::sidebar 338