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