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