1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include "debugtrace.hxx"
23 #include <com/sun/star/uno/Reference.hxx>
24 
25 #include <vector>
26 
27 namespace com::sun::star::drawing::framework
28 {
29 class XConfiguration;
30 }
31 namespace com::sun::star::drawing::framework
32 {
33 class XResourceId;
34 }
35 
36 namespace sd::framework
37 {
38 /** A ConfigurationClassifier object compares two configurations of
39     resources and gives access to the differences.  It is used mainly when
40     changes to the current configuration have been requested and the various
41     resource controllers have to be supplied with the set of resources that
42     are to be activated or deactivated.
43 */
44 class ConfigurationClassifier
45 {
46 public:
47     /** Create a new ConfigurationClassifier object that will compare the
48         two given configurations.
49     */
50     ConfigurationClassifier(
51         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration1,
52         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration2);
53 
54     /** Calculate three lists of resource ids.  These contain the resources
55         that belong to one configuration but not the other, or that belong
56         to both configurations.
57         @return
58             When the two configurations differ then return <TRUE/>.  When
59             they are equivalent then return <FALSE/>.
60     */
61     bool Partition();
62 
63     typedef ::std::vector<css::uno::Reference<css::drawing::framework::XResourceId>>
64         ResourceIdVector;
65 
66     /** Return the resources that belong to the configuration given as
67         rxConfiguration1 to the constructor but that do not belong to
68         rxConfiguration2.
69         @return
70             A reference to the, possibly empty, list of resources is
71             returned.  This reference remains valid as long as the called
72             ConfigurationClassifier object stays alive.
73     */
GetC1minusC2() const74     const ResourceIdVector& GetC1minusC2() const { return maC1minusC2; }
75 
76     /** Return the resources that belong to the configuration given as
77         rxConfiguration2 to the constructor but that do not belong to
78         rxConfiguration1.
79         @return
80             A reference to the, possibly empty, list of resources is
81             returned.  This reference remains valid as long as the called
82             ConfigurationClassifier object stays alive.
83     */
GetC2minusC1() const84     const ResourceIdVector& GetC2minusC1() const { return maC2minusC1; }
85 
86 #if DEBUG_SD_CONFIGURATION_TRACE
87 
88     /** Return the resources that belong to both the configurations that
89         where given to the constructor.
90         @return
91             A reference to the, possibly empty, list of resources is
92             returned.  This reference remains valid as long as the called
93             ConfigurationClassifier object stays alive.
94     */
GetC1andC2() const95     const ResourceIdVector& GetC1andC2() const { return maC1andC2; }
96 
97     static void TraceResourceIdVector(const char* pMessage, const ResourceIdVector& rResources);
98 
99 #endif
100 
101 private:
102     css::uno::Reference<css::drawing::framework::XConfiguration> mxConfiguration1;
103     css::uno::Reference<css::drawing::framework::XConfiguration> mxConfiguration2;
104 
105     /** After the call to Classify() this vector holds all elements from
106         mxConfiguration1 that are not in mxConfiguration2.
107     */
108     ResourceIdVector maC1minusC2;
109 
110     /** After the call to Classify() this vector holds all elements from
111         mxConfiguration2 that are not in mxConfiguration1.
112     */
113     ResourceIdVector maC2minusC1;
114 
115     /** Put all the elements in the two given sequences of resource ids and
116         copy them into one of the resource id result vectors maC1minusC2,
117         maC2minusC1, and maC1andC2.  This is done by using only the resource
118         URLs for classification.  Therefore this method calls itself
119         recursively.
120         @param rS1
121             One sequence of XResourceId objects.
122         @param rS2
123             Another sequence of XResourceId objects.
124     */
125     void PartitionResources(
126         const css::uno::Sequence<css::uno::Reference<css::drawing::framework::XResourceId>>& rS1,
127         const css::uno::Sequence<css::uno::Reference<css::drawing::framework::XResourceId>>& rS2);
128 
129     /** Compare the given sequences of resource ids and put their elements
130         in one of three vectors depending on whether an element belongs to
131         both sequences or to one but not the other.  Note that only the
132         resource URLs of the XResourceId objects are used for the
133         classification.
134         @param rS1
135             One sequence of XResourceId objects.
136         @param rS2
137             Another sequence of XResourceId objects.
138     */
139     static void ClassifyResources(
140         const css::uno::Sequence<css::uno::Reference<css::drawing::framework::XResourceId>>& rS1,
141         const css::uno::Sequence<css::uno::Reference<css::drawing::framework::XResourceId>>& rS2,
142         ResourceIdVector& rS1minusS2, ResourceIdVector& rS2minusS1, ResourceIdVector& rS1andS2);
143 
144     /** Copy the resources given in rSource to the list of resources
145         specified by rTarget.  Resources bound to the ones in rSource,
146         either directly or indirectly, are copied as well.
147         @param rSource
148             All resources and the ones bound to them, either directly or
149             indirectly, are copied.
150         @param rxConfiguration
151             This configuration is used to determine the resources bound to
152             the ones in rSource.
153         @param rTarget
154             This list is filled with resources from rSource and the ones
155             bound to them.
156     */
157     static void CopyResources(
158         const ResourceIdVector& rSource,
159         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration,
160         ResourceIdVector& rTarget);
161 };
162 
163 } // end of namespace sd::framework
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
166