summaryrefslogtreecommitdiff
path: root/sd/source/ui/framework/configuration/ConfigurationClassifier.cxx
blob: c08e6ff59db5739ab85436eb2d24960c9256a767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "precompiled_sd.hxx"

#include "ConfigurationClassifier.hxx"

#include "framework/FrameworkHelper.hxx"

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::drawing::framework;
using ::rtl::OUString;

#undef VERBOSE
//#define VERBOSE 2


namespace sd { namespace framework {

ConfigurationClassifier::ConfigurationClassifier (
    const Reference<XConfiguration>& rxConfiguration1,
    const Reference<XConfiguration>& rxConfiguration2)
    : mxConfiguration1(rxConfiguration1),
      mxConfiguration2(rxConfiguration2),
      maC1minusC2(),
      maC2minusC1(),
      maC1andC2()
{
}




bool ConfigurationClassifier::Partition (void)
{
    maC1minusC2.clear();
    maC2minusC1.clear();
    maC1andC2.clear();

    PartitionResources(
        mxConfiguration1->getResources(NULL, OUString(), AnchorBindingMode_DIRECT),
        mxConfiguration2->getResources(NULL, OUString(), AnchorBindingMode_DIRECT));

    return !maC1minusC2.empty() || !maC2minusC1.empty();
}




const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1minusC2 (void) const
{
    return maC1minusC2;
}




const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC2minusC1 (void) const
{
    return maC2minusC1;
}



const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1andC2 (void) const
{
    return maC1andC2;
}


void ConfigurationClassifier::PartitionResources (
    const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
    const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2)
{
    ResourceIdVector aC1minusC2;
    ResourceIdVector aC2minusC1;
    ResourceIdVector aC1andC2;

    // Classify the resources in the configurations that are not bound to
    // other resources.
    ClassifyResources(
        rS1,
        rS2,
        aC1minusC2,
        aC2minusC1,
        aC1andC2);

#if defined VERBOSE && VERBOSE >= 2
    OSL_TRACE("copying resource ids to C1-C2\r");
#endif
    CopyResources(aC1minusC2, mxConfiguration1, maC1minusC2);
#if defined VERBOSE && VERBOSE >= 2
    OSL_TRACE("copying resource ids to C2-C1\r");
#endif
    CopyResources(aC2minusC1, mxConfiguration2, maC2minusC1);
    
    // Process the unique resources that belong to both configurations.
    ResourceIdVector::const_iterator iResource;
    for (iResource=aC1andC2.begin(); iResource!=aC1andC2.end(); ++iResource)
    {
        maC1andC2.push_back(*iResource);
        PartitionResources(
            mxConfiguration1->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT),
            mxConfiguration2->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT));
    }
}




void ConfigurationClassifier::ClassifyResources (
    const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
    const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2,
    ResourceIdVector& rS1minusS2,
    ResourceIdVector& rS2minusS1,
    ResourceIdVector& rS1andS2)
{
    // Get arrays from the sequences for faster iteration.
    const Reference<XResourceId>* aA1 = rS1.getConstArray();
    const Reference<XResourceId>* aA2 = rS2.getConstArray();
    sal_Int32 nL1 (rS1.getLength());
    sal_Int32 nL2 (rS2.getLength());

    // Find all elements in rS1 and place them in rS1minusS2 or rS1andS2
    // depending on whether they are in rS2 or not.
    for (sal_Int32 i=0; i<nL1; ++i)
    {
        bool bFound (false);
        for (sal_Int32 j=0; j<nL2 && !bFound; ++j)
            if (aA1[i]->getResourceURL().equals(aA2[j]->getResourceURL()))
                bFound = true;
        
        if (bFound)
            rS1andS2.push_back(aA1[i]);
        else
            rS1minusS2.push_back(aA1[i]);
    }

    // Find all elements in rS2 that are not in rS1.  The elements that are
    // in both rS1 and rS2 have been handled above and are therefore ignored
    // here.
    for (sal_Int32 j=0; j<nL2; ++j)
    {
        bool bFound (false);
        for (sal_Int32 i=0; i<nL1 && !bFound; ++i)
            if (aA2[j]->getResourceURL().equals(aA1[i]->getResourceURL()))
                bFound = true;
        
        if ( ! bFound)
            rS2minusS1.push_back(aA2[j]);
    }
}




void ConfigurationClassifier::CopyResources (
    const ResourceIdVector& rSource,
    const Reference<XConfiguration>& rxConfiguration,
    ResourceIdVector& rTarget)
{
    // Copy all resources bound to the ones in aC1minusC2Unique to rC1minusC2.
    ResourceIdVector::const_iterator iResource (rSource.begin());
    ResourceIdVector::const_iterator iEnd(rSource.end());
    for ( ; iResource!=iEnd; ++iResource)
    {
        const Sequence<Reference<XResourceId> > aBoundResources (
            rxConfiguration->getResources(
                *iResource,
                OUString(),
                AnchorBindingMode_INDIRECT));
        const sal_Int32 nL (aBoundResources.getLength());
        
        rTarget.reserve(rTarget.size() + 1 + nL);
        rTarget.push_back(*iResource);

#if defined VERBOSE && VERBOSE >= 2
        OSL_TRACE("    copying %s\r",
            OUStringToOString(FrameworkHelper::ResourceIdToString(*iResource),
                RTL_TEXTENCODING_UTF8).getStr());
#endif
        
        const Reference<XResourceId>* aA = aBoundResources.getConstArray();
        for (sal_Int32 i=0; i<nL; ++i)
        {
            rTarget.push_back(aA[i]);
#if defined VERBOSE && VERBOSE >= 2
            OSL_TRACE("    copying %s\r",
                OUStringToOString(FrameworkHelper::ResourceIdToString(aA[i]),
                    RTL_TEXTENCODING_UTF8).getStr());
#endif
        }
    }
}


void ConfigurationClassifier::TraceResourceIdVector (
    const sal_Char* pMessage,
    const ResourceIdVector& rResources) const
{

    OSL_TRACE(pMessage);
    ResourceIdVector::const_iterator iResource;
    for (iResource=rResources.begin(); iResource!=rResources.end(); ++iResource)
    {
        OUString sResource (FrameworkHelper::ResourceIdToString(*iResource));
        OSL_TRACE("    %s\r",
            OUStringToOString(sResource, RTL_TEXTENCODING_UTF8).getStr());
    }
}


} } // end of namespace sd::framework