1*7a32b0c8SAndre Fischer /**************************************************************
2*7a32b0c8SAndre Fischer *
3*7a32b0c8SAndre Fischer * Licensed to the Apache Software Foundation (ASF) under one
4*7a32b0c8SAndre Fischer * or more contributor license agreements. See the NOTICE file
5*7a32b0c8SAndre Fischer * distributed with this work for additional information
6*7a32b0c8SAndre Fischer * regarding copyright ownership. The ASF licenses this file
7*7a32b0c8SAndre Fischer * to you under the Apache License, Version 2.0 (the
8*7a32b0c8SAndre Fischer * "License"); you may not use this file except in compliance
9*7a32b0c8SAndre Fischer * with the License. You may obtain a copy of the License at
10*7a32b0c8SAndre Fischer *
11*7a32b0c8SAndre Fischer * http://www.apache.org/licenses/LICENSE-2.0
12*7a32b0c8SAndre Fischer *
13*7a32b0c8SAndre Fischer * Unless required by applicable law or agreed to in writing,
14*7a32b0c8SAndre Fischer * software distributed under the License is distributed on an
15*7a32b0c8SAndre Fischer * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7a32b0c8SAndre Fischer * KIND, either express or implied. See the License for the
17*7a32b0c8SAndre Fischer * specific language governing permissions and limitations
18*7a32b0c8SAndre Fischer * under the License.
19*7a32b0c8SAndre Fischer *
20*7a32b0c8SAndre Fischer *************************************************************/
21*7a32b0c8SAndre Fischer
22*7a32b0c8SAndre Fischer #include "precompiled_sfx2.hxx"
23*7a32b0c8SAndre Fischer
24*7a32b0c8SAndre Fischer #include "ContextMatcher.hxx"
25*7a32b0c8SAndre Fischer #include "Context.hxx"
26*7a32b0c8SAndre Fischer
27*7a32b0c8SAndre Fischer using ::rtl::OUString;
28*7a32b0c8SAndre Fischer
29*7a32b0c8SAndre Fischer namespace sfx2 { namespace sidebar {
30*7a32b0c8SAndre Fischer
31*7a32b0c8SAndre Fischer namespace {
32*7a32b0c8SAndre Fischer static const sal_Char* gsAny = "any";
33*7a32b0c8SAndre Fischer }
34*7a32b0c8SAndre Fischer
35*7a32b0c8SAndre Fischer
36*7a32b0c8SAndre Fischer
ContextMatcher(void)37*7a32b0c8SAndre Fischer ContextMatcher::ContextMatcher (void)
38*7a32b0c8SAndre Fischer : maEntries()
39*7a32b0c8SAndre Fischer {
40*7a32b0c8SAndre Fischer }
41*7a32b0c8SAndre Fischer
42*7a32b0c8SAndre Fischer
43*7a32b0c8SAndre Fischer
44*7a32b0c8SAndre Fischer
~ContextMatcher(void)45*7a32b0c8SAndre Fischer ContextMatcher::~ContextMatcher (void)
46*7a32b0c8SAndre Fischer {
47*7a32b0c8SAndre Fischer }
48*7a32b0c8SAndre Fischer
49*7a32b0c8SAndre Fischer
50*7a32b0c8SAndre Fischer
51*7a32b0c8SAndre Fischer
EvaluateMatch(const Context & rContext) const52*7a32b0c8SAndre Fischer sal_Int32 ContextMatcher::EvaluateMatch (
53*7a32b0c8SAndre Fischer const Context& rContext) const
54*7a32b0c8SAndre Fischer {
55*7a32b0c8SAndre Fischer sal_Int32 nBestMatch (Context::NoMatch);
56*7a32b0c8SAndre Fischer
57*7a32b0c8SAndre Fischer for (::std::vector<Entry>::const_iterator
58*7a32b0c8SAndre Fischer iEntry(maEntries.begin()),
59*7a32b0c8SAndre Fischer iEnd(maEntries.end());
60*7a32b0c8SAndre Fischer iEntry!=iEnd;
61*7a32b0c8SAndre Fischer ++iEntry)
62*7a32b0c8SAndre Fischer {
63*7a32b0c8SAndre Fischer const sal_Int32 nMatch (EvaluateMatch(rContext, *iEntry));
64*7a32b0c8SAndre Fischer if (nMatch < nBestMatch)
65*7a32b0c8SAndre Fischer nBestMatch = nMatch;
66*7a32b0c8SAndre Fischer if (nBestMatch == Context::OptimalMatch)
67*7a32b0c8SAndre Fischer break;
68*7a32b0c8SAndre Fischer }
69*7a32b0c8SAndre Fischer
70*7a32b0c8SAndre Fischer return nBestMatch;
71*7a32b0c8SAndre Fischer }
72*7a32b0c8SAndre Fischer
73*7a32b0c8SAndre Fischer
74*7a32b0c8SAndre Fischer
75*7a32b0c8SAndre Fischer
EvaluateMatch(const Context & rContext,const Entry & rEntry) const76*7a32b0c8SAndre Fischer sal_Int32 ContextMatcher::EvaluateMatch (
77*7a32b0c8SAndre Fischer const Context& rContext,
78*7a32b0c8SAndre Fischer const Entry& rEntry) const
79*7a32b0c8SAndre Fischer {
80*7a32b0c8SAndre Fischer sal_Int32 nApplicationMatch (Context::NoMatch);
81*7a32b0c8SAndre Fischer if (rContext.msApplication.equals(rEntry.msApplicationName))
82*7a32b0c8SAndre Fischer nApplicationMatch = 0;
83*7a32b0c8SAndre Fischer else if (rEntry.msApplicationName.equalsAscii(gsAny))
84*7a32b0c8SAndre Fischer nApplicationMatch = Context::ApplicationWildcardMatch;
85*7a32b0c8SAndre Fischer else
86*7a32b0c8SAndre Fischer return Context::NoMatch;
87*7a32b0c8SAndre Fischer
88*7a32b0c8SAndre Fischer sal_Int32 nBestContextMatch (Context::NoMatch);
89*7a32b0c8SAndre Fischer for (::std::vector<OUString>::const_iterator
90*7a32b0c8SAndre Fischer iContext(rEntry.maContextNames.begin()),
91*7a32b0c8SAndre Fischer iEnd(rEntry.maContextNames.end());
92*7a32b0c8SAndre Fischer iContext!=iEnd;
93*7a32b0c8SAndre Fischer ++iContext)
94*7a32b0c8SAndre Fischer {
95*7a32b0c8SAndre Fischer sal_Int32 nContextMatch (Context::NoMatch);
96*7a32b0c8SAndre Fischer if (rContext.msContext.equals(*iContext))
97*7a32b0c8SAndre Fischer nContextMatch = 0;
98*7a32b0c8SAndre Fischer else if (iContext->equalsAscii(gsAny))
99*7a32b0c8SAndre Fischer nContextMatch = Context::ContextWildcardMatch;
100*7a32b0c8SAndre Fischer else
101*7a32b0c8SAndre Fischer continue;
102*7a32b0c8SAndre Fischer if (nContextMatch < nBestContextMatch)
103*7a32b0c8SAndre Fischer nBestContextMatch = nContextMatch;
104*7a32b0c8SAndre Fischer }
105*7a32b0c8SAndre Fischer
106*7a32b0c8SAndre Fischer if (rEntry.mbIsContextListNegated)
107*7a32b0c8SAndre Fischer nBestContextMatch = Context::NoMatch - nBestContextMatch;
108*7a32b0c8SAndre Fischer
109*7a32b0c8SAndre Fischer return nApplicationMatch + nBestContextMatch;
110*7a32b0c8SAndre Fischer }
111*7a32b0c8SAndre Fischer
112*7a32b0c8SAndre Fischer
113*7a32b0c8SAndre Fischer
114*7a32b0c8SAndre Fischer
AddMatcher(const::rtl::OUString & rsApplicationName,const::std::vector<rtl::OUString> & rContextNames,const bool bIsContextListNegated)115*7a32b0c8SAndre Fischer void ContextMatcher::AddMatcher (
116*7a32b0c8SAndre Fischer const ::rtl::OUString& rsApplicationName,
117*7a32b0c8SAndre Fischer const ::std::vector<rtl::OUString>& rContextNames,
118*7a32b0c8SAndre Fischer const bool bIsContextListNegated)
119*7a32b0c8SAndre Fischer {
120*7a32b0c8SAndre Fischer maEntries.push_back(Entry());
121*7a32b0c8SAndre Fischer maEntries.back().msApplicationName = rsApplicationName;
122*7a32b0c8SAndre Fischer maEntries.back().maContextNames = rContextNames;
123*7a32b0c8SAndre Fischer maEntries.back().mbIsContextListNegated = bIsContextListNegated;
124*7a32b0c8SAndre Fischer }
125*7a32b0c8SAndre Fischer
126*7a32b0c8SAndre Fischer
127*7a32b0c8SAndre Fischer
128*7a32b0c8SAndre Fischer
AddMatcher(const::rtl::OUString & rsApplicationName,const::rtl::OUString & rsContextName)129*7a32b0c8SAndre Fischer void ContextMatcher::AddMatcher (
130*7a32b0c8SAndre Fischer const ::rtl::OUString& rsApplicationName,
131*7a32b0c8SAndre Fischer const ::rtl::OUString& rsContextName)
132*7a32b0c8SAndre Fischer {
133*7a32b0c8SAndre Fischer maEntries.push_back(Entry());
134*7a32b0c8SAndre Fischer maEntries.back().msApplicationName = rsApplicationName;
135*7a32b0c8SAndre Fischer maEntries.back().maContextNames.push_back(rsContextName);
136*7a32b0c8SAndre Fischer maEntries.back().mbIsContextListNegated = false;
137*7a32b0c8SAndre Fischer }
138*7a32b0c8SAndre Fischer
139*7a32b0c8SAndre Fischer
140*7a32b0c8SAndre Fischer } } // end of namespace sfx2::sidebar
141