xref: /aoo42x/main/sfx2/source/sidebar/Context.cxx (revision 7a32b0c8)
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 "Context.hxx"
25 
26 #define AnyApplicationName "any"
27 #define AnyContextName "any"
28 
29 namespace sfx2 { namespace sidebar {
30 
31 const sal_Int32 Context::NoMatch = 4;
32 const sal_Int32 Context::ApplicationWildcardMatch = 1;
33 const sal_Int32 Context::ContextWildcardMatch = 2;
34 const sal_Int32 Context::OptimalMatch = 0;  // Neither application nor context name is "any".
35 
36 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
37 
Context(void)38 Context::Context (void)
39     : msApplication(A2S(AnyApplicationName)),
40       msContext(A2S(AnyContextName))
41 {
42 }
43 
44 
45 
46 
Context(const::rtl::OUString & rsApplication,const::rtl::OUString & rsContext)47 Context::Context (
48     const ::rtl::OUString& rsApplication,
49     const ::rtl::OUString& rsContext)
50     : msApplication(rsApplication),
51       msContext(rsContext)
52 {
53 }
54 
55 
56 
57 
EvaluateMatch(const Context & rOther) const58 sal_Int32 Context::EvaluateMatch (
59     const Context& rOther) const
60 {
61     const bool bApplicationNameIsAny (rOther.msApplication.equalsAscii(AnyApplicationName));
62     if (rOther.msApplication.equals(msApplication) || bApplicationNameIsAny)
63     {
64         // Application name matches.
65         const bool bContextNameIsAny (rOther.msContext.equalsAscii(AnyContextName));
66         if (rOther.msContext.equals(msContext) || bContextNameIsAny)
67         {
68             // Context name matches.
69             return (bApplicationNameIsAny ? ApplicationWildcardMatch : 0)
70                 + (bContextNameIsAny ? ContextWildcardMatch : 0);
71         }
72     }
73     return NoMatch;
74 }
75 
76 
77 
78 
EvaluateMatch(const::std::vector<Context> & rOthers) const79 sal_Int32 Context::EvaluateMatch (const ::std::vector<Context>& rOthers) const
80 {
81     sal_Int32 nBestMatch (NoMatch);
82 
83     for (::std::vector<Context>::const_iterator
84              iContext(rOthers.begin()),
85              iEnd(rOthers.end());
86          iContext!=iEnd;
87          ++iContext)
88     {
89         const sal_Int32 nMatch (EvaluateMatch(*iContext));
90         if (nMatch < nBestMatch)
91         {
92             if (nMatch == OptimalMatch)
93             {
94                 // We will find no better match so stop searching.
95                 return OptimalMatch;
96             }
97             nBestMatch = nMatch;
98         }
99     }
100     return nBestMatch;
101 }
102 
103 
104 
105 
operator ==(const Context & rOther) const106 bool Context::operator== (const Context& rOther) const
107 {
108     return msApplication.equals(rOther.msApplication)
109         && msContext.equals(rOther.msContext);
110 }
111 
112 
113 
114 
operator !=(const Context & rOther) const115 bool Context::operator!= (const Context& rOther) const
116 {
117     return ( ! msApplication.equals(rOther.msApplication))
118         || ( ! msContext.equals(rOther.msContext));
119 }
120 
121 
122 } } // end of namespace sfx2::sidebar
123