xref: /trunk/main/sfx2/source/sidebar/Context.cxx (revision 549760eabef7046a021eb562805aa3542b2e9b62)
122de8995SAndre Fischer /**************************************************************
222de8995SAndre Fischer  *
322de8995SAndre Fischer  * Licensed to the Apache Software Foundation (ASF) under one
422de8995SAndre Fischer  * or more contributor license agreements.  See the NOTICE file
522de8995SAndre Fischer  * distributed with this work for additional information
622de8995SAndre Fischer  * regarding copyright ownership.  The ASF licenses this file
722de8995SAndre Fischer  * to you under the Apache License, Version 2.0 (the
822de8995SAndre Fischer  * "License"); you may not use this file except in compliance
922de8995SAndre Fischer  * with the License.  You may obtain a copy of the License at
1022de8995SAndre Fischer  *
1122de8995SAndre Fischer  *   http://www.apache.org/licenses/LICENSE-2.0
1222de8995SAndre Fischer  *
1322de8995SAndre Fischer  * Unless required by applicable law or agreed to in writing,
1422de8995SAndre Fischer  * software distributed under the License is distributed on an
1522de8995SAndre Fischer  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1622de8995SAndre Fischer  * KIND, either express or implied.  See the License for the
1722de8995SAndre Fischer  * specific language governing permissions and limitations
1822de8995SAndre Fischer  * under the License.
1922de8995SAndre Fischer  *
2022de8995SAndre Fischer  *************************************************************/
2122de8995SAndre Fischer 
2222de8995SAndre Fischer #include "precompiled_sfx2.hxx"
2322de8995SAndre Fischer 
2422de8995SAndre Fischer #include "Context.hxx"
2522de8995SAndre Fischer 
2622de8995SAndre Fischer #define AnyApplicationName "any"
2722de8995SAndre Fischer #define AnyContextName "any"
2822de8995SAndre Fischer 
297a32b0c8SAndre Fischer namespace sfx2 { namespace sidebar {
3022de8995SAndre Fischer 
3122de8995SAndre Fischer const sal_Int32 Context::NoMatch = 4;
327a32b0c8SAndre Fischer const sal_Int32 Context::ApplicationWildcardMatch = 1;
337a32b0c8SAndre Fischer const sal_Int32 Context::ContextWildcardMatch = 2;
3422de8995SAndre Fischer const sal_Int32 Context::OptimalMatch = 0; // Neither application nor context name is "any".
3522de8995SAndre Fischer 
3622de8995SAndre Fischer #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
3722de8995SAndre Fischer 
Context(void)3822de8995SAndre Fischer Context::Context (void)
3922de8995SAndre Fischer     : msApplication(A2S(AnyApplicationName)),
4022de8995SAndre Fischer       msContext(A2S(AnyContextName))
4122de8995SAndre Fischer {
4222de8995SAndre Fischer }
4322de8995SAndre Fischer 
Context(const::rtl::OUString & rsApplication,const::rtl::OUString & rsContext)4422de8995SAndre Fischer Context::Context (
4522de8995SAndre Fischer     const ::rtl::OUString& rsApplication,
4622de8995SAndre Fischer     const ::rtl::OUString& rsContext)
4722de8995SAndre Fischer     : msApplication(rsApplication),
4822de8995SAndre Fischer       msContext(rsContext)
4922de8995SAndre Fischer {
5022de8995SAndre Fischer }
5122de8995SAndre Fischer 
EvaluateMatch(const Context & rOther) const5222de8995SAndre Fischer sal_Int32 Context::EvaluateMatch (
5322de8995SAndre Fischer     const Context& rOther) const
5422de8995SAndre Fischer {
5522de8995SAndre Fischer     const bool bApplicationNameIsAny (rOther.msApplication.equalsAscii(AnyApplicationName));
5622de8995SAndre Fischer     if (rOther.msApplication.equals(msApplication) || bApplicationNameIsAny)
5722de8995SAndre Fischer     {
5822de8995SAndre Fischer         // Application name matches.
5922de8995SAndre Fischer         const bool bContextNameIsAny (rOther.msContext.equalsAscii(AnyContextName));
6022de8995SAndre Fischer         if (rOther.msContext.equals(msContext) || bContextNameIsAny)
6122de8995SAndre Fischer         {
6222de8995SAndre Fischer             // Context name matches.
637a32b0c8SAndre Fischer             return (bApplicationNameIsAny ? ApplicationWildcardMatch : 0)
647a32b0c8SAndre Fischer                 + (bContextNameIsAny ? ContextWildcardMatch : 0);
6522de8995SAndre Fischer         }
6622de8995SAndre Fischer     }
6722de8995SAndre Fischer     return NoMatch;
6822de8995SAndre Fischer }
6922de8995SAndre Fischer 
EvaluateMatch(const::std::vector<Context> & rOthers) const7022de8995SAndre Fischer sal_Int32 Context::EvaluateMatch (const ::std::vector<Context>& rOthers) const
7122de8995SAndre Fischer {
7222de8995SAndre Fischer     sal_Int32 nBestMatch (NoMatch);
7322de8995SAndre Fischer 
7422de8995SAndre Fischer     for (::std::vector<Context>::const_iterator
7522de8995SAndre Fischer              iContext(rOthers.begin()),
7622de8995SAndre Fischer              iEnd(rOthers.end());
7722de8995SAndre Fischer          iContext!=iEnd;
7822de8995SAndre Fischer          ++iContext)
7922de8995SAndre Fischer     {
8022de8995SAndre Fischer         const sal_Int32 nMatch (EvaluateMatch(*iContext));
8122de8995SAndre Fischer         if (nMatch < nBestMatch)
8222de8995SAndre Fischer         {
8322de8995SAndre Fischer             if (nMatch == OptimalMatch)
8422de8995SAndre Fischer             {
8522de8995SAndre Fischer                 // We will find no better match so stop searching.
8622de8995SAndre Fischer                 return OptimalMatch;
8722de8995SAndre Fischer             }
8822de8995SAndre Fischer             nBestMatch = nMatch;
8922de8995SAndre Fischer         }
9022de8995SAndre Fischer     }
9122de8995SAndre Fischer     return nBestMatch;
9222de8995SAndre Fischer }
9322de8995SAndre Fischer 
operator ==(const Context & rOther) const947a32b0c8SAndre Fischer bool Context::operator== (const Context& rOther) const
957a32b0c8SAndre Fischer {
967a32b0c8SAndre Fischer     return msApplication.equals(rOther.msApplication)
977a32b0c8SAndre Fischer         && msContext.equals(rOther.msContext);
987a32b0c8SAndre Fischer }
997a32b0c8SAndre Fischer 
operator !=(const Context & rOther) const1007a32b0c8SAndre Fischer bool Context::operator!= (const Context& rOther) const
1017a32b0c8SAndre Fischer {
1027a32b0c8SAndre Fischer     return ( ! msApplication.equals(rOther.msApplication))
1037a32b0c8SAndre Fischer         || ( ! msContext.equals(rOther.msContext));
1047a32b0c8SAndre Fischer }
1057a32b0c8SAndre Fischer 
1067a32b0c8SAndre Fischer } } // end of namespace sfx2::sidebar
107*549760eaSmseidel 
108*549760eaSmseidel /* vim: set noet sw=4 ts=4: */
109