xref: /aoo42x/main/sfx2/source/sidebar/Context.cxx (revision 7a32b0c8)
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 
29*7a32b0c8SAndre Fischer namespace sfx2 { namespace sidebar {
3022de8995SAndre Fischer 
3122de8995SAndre Fischer const sal_Int32 Context::NoMatch = 4;
32*7a32b0c8SAndre Fischer const sal_Int32 Context::ApplicationWildcardMatch = 1;
33*7a32b0c8SAndre 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 
4422de8995SAndre Fischer 
4522de8995SAndre Fischer 
4622de8995SAndre Fischer 
Context(const::rtl::OUString & rsApplication,const::rtl::OUString & rsContext)4722de8995SAndre Fischer Context::Context (
4822de8995SAndre Fischer     const ::rtl::OUString& rsApplication,
4922de8995SAndre Fischer     const ::rtl::OUString& rsContext)
5022de8995SAndre Fischer     : msApplication(rsApplication),
5122de8995SAndre Fischer       msContext(rsContext)
5222de8995SAndre Fischer {
5322de8995SAndre Fischer }
5422de8995SAndre Fischer 
5522de8995SAndre Fischer 
5622de8995SAndre Fischer 
5722de8995SAndre Fischer 
EvaluateMatch(const Context & rOther) const5822de8995SAndre Fischer sal_Int32 Context::EvaluateMatch (
5922de8995SAndre Fischer     const Context& rOther) const
6022de8995SAndre Fischer {
6122de8995SAndre Fischer     const bool bApplicationNameIsAny (rOther.msApplication.equalsAscii(AnyApplicationName));
6222de8995SAndre Fischer     if (rOther.msApplication.equals(msApplication) || bApplicationNameIsAny)
6322de8995SAndre Fischer     {
6422de8995SAndre Fischer         // Application name matches.
6522de8995SAndre Fischer         const bool bContextNameIsAny (rOther.msContext.equalsAscii(AnyContextName));
6622de8995SAndre Fischer         if (rOther.msContext.equals(msContext) || bContextNameIsAny)
6722de8995SAndre Fischer         {
6822de8995SAndre Fischer             // Context name matches.
69*7a32b0c8SAndre Fischer             return (bApplicationNameIsAny ? ApplicationWildcardMatch : 0)
70*7a32b0c8SAndre Fischer                 + (bContextNameIsAny ? ContextWildcardMatch : 0);
7122de8995SAndre Fischer         }
7222de8995SAndre Fischer     }
7322de8995SAndre Fischer     return NoMatch;
7422de8995SAndre Fischer }
7522de8995SAndre Fischer 
7622de8995SAndre Fischer 
7722de8995SAndre Fischer 
7822de8995SAndre Fischer 
EvaluateMatch(const::std::vector<Context> & rOthers) const7922de8995SAndre Fischer sal_Int32 Context::EvaluateMatch (const ::std::vector<Context>& rOthers) const
8022de8995SAndre Fischer {
8122de8995SAndre Fischer     sal_Int32 nBestMatch (NoMatch);
8222de8995SAndre Fischer 
8322de8995SAndre Fischer     for (::std::vector<Context>::const_iterator
8422de8995SAndre Fischer              iContext(rOthers.begin()),
8522de8995SAndre Fischer              iEnd(rOthers.end());
8622de8995SAndre Fischer          iContext!=iEnd;
8722de8995SAndre Fischer          ++iContext)
8822de8995SAndre Fischer     {
8922de8995SAndre Fischer         const sal_Int32 nMatch (EvaluateMatch(*iContext));
9022de8995SAndre Fischer         if (nMatch < nBestMatch)
9122de8995SAndre Fischer         {
9222de8995SAndre Fischer             if (nMatch == OptimalMatch)
9322de8995SAndre Fischer             {
9422de8995SAndre Fischer                 // We will find no better match so stop searching.
9522de8995SAndre Fischer                 return OptimalMatch;
9622de8995SAndre Fischer             }
9722de8995SAndre Fischer             nBestMatch = nMatch;
9822de8995SAndre Fischer         }
9922de8995SAndre Fischer     }
10022de8995SAndre Fischer     return nBestMatch;
10122de8995SAndre Fischer }
10222de8995SAndre Fischer 
103*7a32b0c8SAndre Fischer 
104*7a32b0c8SAndre Fischer 
105*7a32b0c8SAndre Fischer 
operator ==(const Context & rOther) const106*7a32b0c8SAndre Fischer bool Context::operator== (const Context& rOther) const
107*7a32b0c8SAndre Fischer {
108*7a32b0c8SAndre Fischer     return msApplication.equals(rOther.msApplication)
109*7a32b0c8SAndre Fischer         && msContext.equals(rOther.msContext);
110*7a32b0c8SAndre Fischer }
111*7a32b0c8SAndre Fischer 
112*7a32b0c8SAndre Fischer 
113*7a32b0c8SAndre Fischer 
114*7a32b0c8SAndre Fischer 
operator !=(const Context & rOther) const115*7a32b0c8SAndre Fischer bool Context::operator!= (const Context& rOther) const
116*7a32b0c8SAndre Fischer {
117*7a32b0c8SAndre Fischer     return ( ! msApplication.equals(rOther.msApplication))
118*7a32b0c8SAndre Fischer         || ( ! msContext.equals(rOther.msContext));
119*7a32b0c8SAndre Fischer }
120*7a32b0c8SAndre Fischer 
121*7a32b0c8SAndre Fischer 
122*7a32b0c8SAndre Fischer } } // end of namespace sfx2::sidebar
123