bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / Context.cxx
blob99edf6d82a144f211c99436d9ce2cb212b030f3c
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 "Context.hxx"
20 #define AnyApplicationName "any"
21 #define AnyContextName "any"
23 namespace sfx2 { namespace sidebar {
25 const sal_Int32 Context::NoMatch = 4;
26 const sal_Int32 Context::ApplicationWildcardMatch = 1;
27 const sal_Int32 Context::ContextWildcardMatch = 2;
28 const sal_Int32 Context::OptimalMatch = 0; // Neither application nor context name is "any".
30 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
32 Context::Context (void)
33 : msApplication(A2S(AnyApplicationName)),
34 msContext(A2S(AnyContextName))
41 Context::Context (
42 const ::rtl::OUString& rsApplication,
43 const ::rtl::OUString& rsContext)
44 : msApplication(rsApplication),
45 msContext(rsContext)
52 sal_Int32 Context::EvaluateMatch (
53 const Context& rOther) const
55 const bool bApplicationNameIsAny (rOther.msApplication.equalsAscii(AnyApplicationName));
56 if (rOther.msApplication.equals(msApplication) || bApplicationNameIsAny)
58 // Application name matches.
59 const bool bContextNameIsAny (rOther.msContext.equalsAscii(AnyContextName));
60 if (rOther.msContext.equals(msContext) || bContextNameIsAny)
62 // Context name matches.
63 return (bApplicationNameIsAny ? ApplicationWildcardMatch : 0)
64 + (bContextNameIsAny ? ContextWildcardMatch : 0);
67 return NoMatch;
73 sal_Int32 Context::EvaluateMatch (const ::std::vector<Context>& rOthers) const
75 sal_Int32 nBestMatch (NoMatch);
77 for (::std::vector<Context>::const_iterator
78 iContext(rOthers.begin()),
79 iEnd(rOthers.end());
80 iContext!=iEnd;
81 ++iContext)
83 const sal_Int32 nMatch (EvaluateMatch(*iContext));
84 if (nMatch < nBestMatch)
86 if (nMatch == OptimalMatch)
88 // We will find no better match so stop searching.
89 return OptimalMatch;
91 nBestMatch = nMatch;
94 return nBestMatch;
100 bool Context::operator== (const Context& rOther) const
102 return msApplication.equals(rOther.msApplication)
103 && msContext.equals(rOther.msContext);
109 bool Context::operator!= (const Context& rOther) const
111 return ( ! msApplication.equals(rOther.msApplication))
112 || ( ! msContext.equals(rOther.msContext));
116 } } // end of namespace sfx2::sidebar