xref: /trunk/main/xmloff/source/core/xmlmultiimagehelper.cxx (revision ddde725d65c83fe3ba1186d46f6e3e08f12ba47e)
1*ddde725dSArmin Le Grand /**************************************************************
2*ddde725dSArmin Le Grand  *
3*ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4*ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5*ddde725dSArmin Le Grand  * distributed with this work for additional information
6*ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7*ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8*ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9*ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10*ddde725dSArmin Le Grand  *
11*ddde725dSArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12*ddde725dSArmin Le Grand  *
13*ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14*ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15*ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17*ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18*ddde725dSArmin Le Grand  * under the License.
19*ddde725dSArmin Le Grand  *
20*ddde725dSArmin Le Grand  *************************************************************/
21*ddde725dSArmin Le Grand 
22*ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23*ddde725dSArmin Le Grand #include "precompiled_xmloff.hxx"
24*ddde725dSArmin Le Grand 
25*ddde725dSArmin Le Grand #include <xmloff/xmlmultiimagehelper.hxx>
26*ddde725dSArmin Le Grand #include <rtl/ustring.hxx>
27*ddde725dSArmin Le Grand 
28*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
29*ddde725dSArmin Le Grand 
30*ddde725dSArmin Le Grand using namespace ::com::sun::star;
31*ddde725dSArmin Le Grand 
32*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
33*ddde725dSArmin Le Grand 
34*ddde725dSArmin Le Grand namespace
35*ddde725dSArmin Le Grand {
36*ddde725dSArmin Le Grand     sal_uInt32 getQualityIndex(const rtl::OUString& rString)
37*ddde725dSArmin Le Grand     {
38*ddde725dSArmin Le Grand         sal_uInt32 nRetval(0);
39*ddde725dSArmin Le Grand 
40*ddde725dSArmin Le Grand         // pixel formats first
41*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".bmp")))
42*ddde725dSArmin Le Grand         {
43*ddde725dSArmin Le Grand             return 10;
44*ddde725dSArmin Le Grand         }
45*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".gif")))
46*ddde725dSArmin Le Grand         {
47*ddde725dSArmin Le Grand             return 20;
48*ddde725dSArmin Le Grand         }
49*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".jpg")))
50*ddde725dSArmin Le Grand         {
51*ddde725dSArmin Le Grand             return 30;
52*ddde725dSArmin Le Grand         }
53*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".png")))
54*ddde725dSArmin Le Grand         {
55*ddde725dSArmin Le Grand             return 40;
56*ddde725dSArmin Le Grand         }
57*ddde725dSArmin Le Grand 
58*ddde725dSArmin Le Grand         // vector formats, prefer always
59*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".svm")))
60*ddde725dSArmin Le Grand         {
61*ddde725dSArmin Le Grand             return 1000;
62*ddde725dSArmin Le Grand         }
63*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".wmf")))
64*ddde725dSArmin Le Grand         {
65*ddde725dSArmin Le Grand             return 1010;
66*ddde725dSArmin Le Grand         }
67*ddde725dSArmin Le Grand         if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".emf")))
68*ddde725dSArmin Le Grand         {
69*ddde725dSArmin Le Grand             return 1020;
70*ddde725dSArmin Le Grand         }
71*ddde725dSArmin Le Grand         else if(rString.endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(".svg")))
72*ddde725dSArmin Le Grand         {
73*ddde725dSArmin Le Grand             return 1030;
74*ddde725dSArmin Le Grand         }
75*ddde725dSArmin Le Grand 
76*ddde725dSArmin Le Grand         return nRetval;
77*ddde725dSArmin Le Grand     }
78*ddde725dSArmin Le Grand }
79*ddde725dSArmin Le Grand 
80*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
81*ddde725dSArmin Le Grand 
82*ddde725dSArmin Le Grand multiImageImportHelper::multiImageImportHelper()
83*ddde725dSArmin Le Grand :   maImplContextVector(),
84*ddde725dSArmin Le Grand     mbSupportsMultipleContents(false)
85*ddde725dSArmin Le Grand {
86*ddde725dSArmin Le Grand }
87*ddde725dSArmin Le Grand 
88*ddde725dSArmin Le Grand multiImageImportHelper::~multiImageImportHelper()
89*ddde725dSArmin Le Grand {
90*ddde725dSArmin Le Grand     while(!maImplContextVector.empty())
91*ddde725dSArmin Le Grand     {
92*ddde725dSArmin Le Grand         delete *(maImplContextVector.end() - 1);
93*ddde725dSArmin Le Grand         maImplContextVector.pop_back();
94*ddde725dSArmin Le Grand     }
95*ddde725dSArmin Le Grand }
96*ddde725dSArmin Le Grand 
97*ddde725dSArmin Le Grand void multiImageImportHelper::solveMultipleImages()
98*ddde725dSArmin Le Grand {
99*ddde725dSArmin Le Grand     if(maImplContextVector.size() > 1)
100*ddde725dSArmin Le Grand     {
101*ddde725dSArmin Le Grand         // multiple child contexts were imported, decide which is the most valuable one
102*ddde725dSArmin Le Grand         // and remove the rest
103*ddde725dSArmin Le Grand         sal_uInt32 nIndexOfPreferred(maImplContextVector.size());
104*ddde725dSArmin Le Grand         sal_uInt32 nBestQuality(0), a(0);
105*ddde725dSArmin Le Grand 
106*ddde725dSArmin Le Grand         for(a = 0; a < maImplContextVector.size(); a++)
107*ddde725dSArmin Le Grand         {
108*ddde725dSArmin Le Grand             const rtl::OUString aStreamURL(getGraphicURLFromImportContext(**maImplContextVector[a]));
109*ddde725dSArmin Le Grand             const sal_uInt32 nNewQuality(getQualityIndex(aStreamURL));
110*ddde725dSArmin Le Grand 
111*ddde725dSArmin Le Grand             if(nNewQuality > nBestQuality)
112*ddde725dSArmin Le Grand             {
113*ddde725dSArmin Le Grand                 nBestQuality = nNewQuality;
114*ddde725dSArmin Le Grand                 nIndexOfPreferred = a;
115*ddde725dSArmin Le Grand             }
116*ddde725dSArmin Le Grand         }
117*ddde725dSArmin Le Grand 
118*ddde725dSArmin Le Grand         // correct if needed, default is to use the last entry
119*ddde725dSArmin Le Grand         if(nIndexOfPreferred >= maImplContextVector.size())
120*ddde725dSArmin Le Grand         {
121*ddde725dSArmin Le Grand             nIndexOfPreferred = maImplContextVector.size() - 1;
122*ddde725dSArmin Le Grand         }
123*ddde725dSArmin Le Grand 
124*ddde725dSArmin Le Grand         // Take out the most valuable one
125*ddde725dSArmin Le Grand         const std::vector< SvXMLImportContextRef* >::iterator aRemove(maImplContextVector.begin() + nIndexOfPreferred);
126*ddde725dSArmin Le Grand         delete *aRemove;
127*ddde725dSArmin Le Grand         maImplContextVector.erase(aRemove);
128*ddde725dSArmin Le Grand 
129*ddde725dSArmin Le Grand         // remove the rest from parent
130*ddde725dSArmin Le Grand         for(a = 0; a < maImplContextVector.size(); a++)
131*ddde725dSArmin Le Grand         {
132*ddde725dSArmin Le Grand             removeGraphicFromImportContext(**maImplContextVector[a]);
133*ddde725dSArmin Le Grand         }
134*ddde725dSArmin Le Grand     }
135*ddde725dSArmin Le Grand }
136*ddde725dSArmin Le Grand 
137*ddde725dSArmin Le Grand void multiImageImportHelper::addContent(const SvXMLImportContext& rSvXMLImportContext)
138*ddde725dSArmin Le Grand {
139*ddde725dSArmin Le Grand     if(dynamic_cast< const SvXMLImportContext* >(&rSvXMLImportContext))
140*ddde725dSArmin Le Grand     {
141*ddde725dSArmin Le Grand         maImplContextVector.push_back(new SvXMLImportContextRef(const_cast< SvXMLImportContext* >(&rSvXMLImportContext)));
142*ddde725dSArmin Le Grand     }
143*ddde725dSArmin Le Grand }
144*ddde725dSArmin Le Grand 
145*ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
146*ddde725dSArmin Le Grand //eof
147