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 "ContextMatcher.hxx"
25 #include "Context.hxx"
26 
27 using ::rtl::OUString;
28 
29 namespace sfx2 { namespace sidebar {
30 
31 namespace {
32     static const sal_Char* gsAny = "any";
33 }
34 
35 
36 
ContextMatcher(void)37 ContextMatcher::ContextMatcher (void)
38     : maEntries()
39 {
40 }
41 
42 
43 
44 
~ContextMatcher(void)45 ContextMatcher::~ContextMatcher (void)
46 {
47 }
48 
49 
50 
51 
EvaluateMatch(const Context & rContext) const52 sal_Int32 ContextMatcher::EvaluateMatch (
53     const Context& rContext) const
54 {
55     sal_Int32 nBestMatch (Context::NoMatch);
56 
57     for (::std::vector<Entry>::const_iterator
58              iEntry(maEntries.begin()),
59              iEnd(maEntries.end());
60          iEntry!=iEnd;
61          ++iEntry)
62     {
63         const sal_Int32 nMatch (EvaluateMatch(rContext, *iEntry));
64         if (nMatch < nBestMatch)
65             nBestMatch = nMatch;
66             if (nBestMatch == Context::OptimalMatch)
67             break;
68     }
69 
70     return nBestMatch;
71 }
72 
73 
74 
75 
EvaluateMatch(const Context & rContext,const Entry & rEntry) const76 sal_Int32 ContextMatcher::EvaluateMatch (
77     const Context& rContext,
78     const Entry& rEntry) const
79 {
80     sal_Int32 nApplicationMatch (Context::NoMatch);
81     if (rContext.msApplication.equals(rEntry.msApplicationName))
82         nApplicationMatch = 0;
83     else if (rEntry.msApplicationName.equalsAscii(gsAny))
84         nApplicationMatch = Context::ApplicationWildcardMatch;
85     else
86         return Context::NoMatch;
87 
88     sal_Int32 nBestContextMatch (Context::NoMatch);
89     for (::std::vector<OUString>::const_iterator
90              iContext(rEntry.maContextNames.begin()),
91              iEnd(rEntry.maContextNames.end());
92          iContext!=iEnd;
93          ++iContext)
94     {
95         sal_Int32 nContextMatch (Context::NoMatch);
96         if (rContext.msContext.equals(*iContext))
97             nContextMatch = 0;
98         else if (iContext->equalsAscii(gsAny))
99             nContextMatch = Context::ContextWildcardMatch;
100         else
101             continue;
102         if (nContextMatch < nBestContextMatch)
103             nBestContextMatch = nContextMatch;
104     }
105 
106     if (rEntry.mbIsContextListNegated)
107         nBestContextMatch = Context::NoMatch - nBestContextMatch;
108 
109     return nApplicationMatch + nBestContextMatch;
110 }
111 
112 
113 
114 
AddMatcher(const::rtl::OUString & rsApplicationName,const::std::vector<rtl::OUString> & rContextNames,const bool bIsContextListNegated)115 void ContextMatcher::AddMatcher (
116     const ::rtl::OUString& rsApplicationName,
117     const ::std::vector<rtl::OUString>& rContextNames,
118     const bool bIsContextListNegated)
119 {
120     maEntries.push_back(Entry());
121     maEntries.back().msApplicationName = rsApplicationName;
122     maEntries.back().maContextNames = rContextNames;
123     maEntries.back().mbIsContextListNegated = bIsContextListNegated;
124 }
125 
126 
127 
128 
AddMatcher(const::rtl::OUString & rsApplicationName,const::rtl::OUString & rsContextName)129 void ContextMatcher::AddMatcher (
130     const ::rtl::OUString& rsApplicationName,
131     const ::rtl::OUString& rsContextName)
132 {
133     maEntries.push_back(Entry());
134     maEntries.back().msApplicationName = rsApplicationName;
135     maEntries.back().maContextNames.push_back(rsContextName);
136     maEntries.back().mbIsContextListNegated = false;
137 }
138 
139 
140 } } // end of namespace sfx2::sidebar
141