xref: /AOO42X/main/sfx2/source/sidebar/Context.cxx (revision 055746153e5cd78346b485d23b1c8e3408d513f1)
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 
Context(const::rtl::OUString & rsApplication,const::rtl::OUString & rsContext)44 Context::Context (
45     const ::rtl::OUString& rsApplication,
46     const ::rtl::OUString& rsContext)
47     : msApplication(rsApplication),
48       msContext(rsContext)
49 {
50 }
51 
EvaluateMatch(const Context & rOther) const52 sal_Int32 Context::EvaluateMatch (
53     const Context& rOther) const
54 {
55     const bool bApplicationNameIsAny (rOther.msApplication.equalsAscii(AnyApplicationName));
56     if (rOther.msApplication.equals(msApplication) || bApplicationNameIsAny)
57     {
58         // Application name matches.
59         const bool bContextNameIsAny (rOther.msContext.equalsAscii(AnyContextName));
60         if (rOther.msContext.equals(msContext) || bContextNameIsAny)
61         {
62             // Context name matches.
63             return (bApplicationNameIsAny ? ApplicationWildcardMatch : 0)
64                 + (bContextNameIsAny ? ContextWildcardMatch : 0);
65         }
66     }
67     return NoMatch;
68 }
69 
EvaluateMatch(const::std::vector<Context> & rOthers) const70 sal_Int32 Context::EvaluateMatch (const ::std::vector<Context>& rOthers) const
71 {
72     sal_Int32 nBestMatch (NoMatch);
73 
74     for (::std::vector<Context>::const_iterator
75              iContext(rOthers.begin()),
76              iEnd(rOthers.end());
77          iContext!=iEnd;
78          ++iContext)
79     {
80         const sal_Int32 nMatch (EvaluateMatch(*iContext));
81         if (nMatch < nBestMatch)
82         {
83             if (nMatch == OptimalMatch)
84             {
85                 // We will find no better match so stop searching.
86                 return OptimalMatch;
87             }
88             nBestMatch = nMatch;
89         }
90     }
91     return nBestMatch;
92 }
93 
operator ==(const Context & rOther) const94 bool Context::operator== (const Context& rOther) const
95 {
96     return msApplication.equals(rOther.msApplication)
97         && msContext.equals(rOther.msContext);
98 }
99 
operator !=(const Context & rOther) const100 bool Context::operator!= (const Context& rOther) const
101 {
102     return ( ! msApplication.equals(rOther.msApplication))
103         || ( ! msContext.equals(rOther.msContext));
104 }
105 
106 } } // end of namespace sfx2::sidebar
107 
108 /* vim: set noet sw=4 ts=4: */
109