bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / ContextMatcher.cxx
blob9de3567d8da1465f6f9164d3058f4bba27a3df00
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 #include "precompiled_sfx2.hxx"
20 #include "ContextMatcher.hxx"
21 #include "Context.hxx"
23 using ::rtl::OUString;
25 namespace sfx2 { namespace sidebar {
27 namespace {
28 static const sal_Char* gsAny = "any";
33 ContextMatcher::ContextMatcher (void)
34 : maEntries()
41 ContextMatcher::~ContextMatcher (void)
48 sal_Int32 ContextMatcher::EvaluateMatch (
49 const Context& rContext) const
51 sal_Int32 nBestMatch (Context::NoMatch);
53 for (::std::vector<Entry>::const_iterator
54 iEntry(maEntries.begin()),
55 iEnd(maEntries.end());
56 iEntry!=iEnd;
57 ++iEntry)
59 const sal_Int32 nMatch (EvaluateMatch(rContext, *iEntry));
60 if (nMatch < nBestMatch)
61 nBestMatch = nMatch;
62 if (nBestMatch == Context::OptimalMatch)
63 break;
66 return nBestMatch;
72 sal_Int32 ContextMatcher::EvaluateMatch (
73 const Context& rContext,
74 const Entry& rEntry) const
76 sal_Int32 nApplicationMatch (Context::NoMatch);
77 if (rContext.msApplication.equals(rEntry.msApplicationName))
78 nApplicationMatch = 0;
79 else if (rEntry.msApplicationName.equalsAscii(gsAny))
80 nApplicationMatch = Context::ApplicationWildcardMatch;
81 else
82 return Context::NoMatch;
84 sal_Int32 nBestContextMatch (Context::NoMatch);
85 for (::std::vector<OUString>::const_iterator
86 iContext(rEntry.maContextNames.begin()),
87 iEnd(rEntry.maContextNames.end());
88 iContext!=iEnd;
89 ++iContext)
91 sal_Int32 nContextMatch (Context::NoMatch);
92 if (rContext.msContext.equals(*iContext))
93 nContextMatch = 0;
94 else if (iContext->equalsAscii(gsAny))
95 nContextMatch = Context::ContextWildcardMatch;
96 else
97 continue;
98 if (nContextMatch < nBestContextMatch)
99 nBestContextMatch = nContextMatch;
102 if (rEntry.mbIsContextListNegated)
103 nBestContextMatch = Context::NoMatch - nBestContextMatch;
105 return nApplicationMatch + nBestContextMatch;
111 void ContextMatcher::AddMatcher (
112 const ::rtl::OUString& rsApplicationName,
113 const ::std::vector<rtl::OUString>& rContextNames,
114 const bool bIsContextListNegated)
116 maEntries.push_back(Entry());
117 maEntries.back().msApplicationName = rsApplicationName;
118 maEntries.back().maContextNames = rContextNames;
119 maEntries.back().mbIsContextListNegated = bIsContextListNegated;
125 void ContextMatcher::AddMatcher (
126 const ::rtl::OUString& rsApplicationName,
127 const ::rtl::OUString& rsContextName)
129 maEntries.push_back(Entry());
130 maEntries.back().msApplicationName = rsApplicationName;
131 maEntries.back().maContextNames.push_back(rsContextName);
132 maEntries.back().mbIsContextListNegated = false;
136 } } // end of namespace sfx2::sidebar