KoPictureCollection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (c) 2001, 2004 David Faure <faure@kde.org>
4 Copyright (C) 2002, 2004 Nicolas GOUTTE <goutte@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include <tqdom.h>
23
24#include <kdebug.h>
25#include <kurl.h>
26
27#include <KoStoreDevice.h>
28#include <KoXmlWriter.h>
29
30#include "KoPicture.h"
31#include "KoPictureCollection.h"
32
33//#define DEBUG_PICTURES
34
36{
37#ifdef DEBUG_PICTURES
38 kdDebug(30003) << "KoPictureCollection::findPicture " << key.toString() << endl;
39#endif
40 ConstIterator it = find( key );
41 if ( it == end() )
42 {
43 KoPicture picture;
44 picture.setKey(key);
45 return picture;
46 }
47
48 return *it;
49}
50
51
53{
54#ifdef DEBUG_PICTURES
55 kdDebug(30003) << "KoPictureCollection::insertPicture " << key.toString() << endl;
56#endif
57 KoPicture c = findPicture(key);
58 if (c.isNull())
59 {
60#ifdef DEBUG_PICTURES
61 kdDebug(30003) << "KoPictureCollection::insertPicture not found -> inserting" << endl;
62#endif
63 c=picture;
64 c.setKey(key); // Be sure that the key is correctly set in the KoPicture!
65 insert(key, c);
66 }
67 return c;
68}
69
71{
72 return insertPicture(picture.getKey(), picture);
73}
74
75KoPicture KoPictureCollection::downloadPicture(const KURL& url, TQWidget *window)
76{
77#ifdef DEBUG_PICTURES
78 kdDebug(30003) << "KoPictureCollection::downloadPicture " << url.prettyURL() << endl;
79#endif
80
81 // If it is a local file, we can check the last modification date, so we should better use loadPicture
82 if (url.isLocalFile())
83 return loadPicture(url.path());
84
85
86 // We have really a remote file, so we cannot check the last modification date
87 // Therefore we have to always download the file
88
89 KoPicture pic;
90#ifdef DEBUG_PICTURES
91 kdDebug(30003) << "Trying to download picture from file " << url.prettyURL() << endl;
92#endif
93
94 if (pic.setKeyAndDownloadPicture(url, window))
95 insertPicture(pic.getKey(), pic);
96 else
97 kdWarning(30003) << "Could not download KoPicture from " << url.prettyURL() << endl;
98
99 return pic;
100}
101
103{
104#ifdef DEBUG_PICTURES
105 kdDebug(30003) << "KoPictureCollection::loadPicture " << fileName << endl;
106#endif
107 // Check the last modified date of the file, as it is now.
108 KoPictureKey key;
109 key.setKeyFromFile(fileName);
110
111 KoPicture c = findPicture(key);
112 if (c.isNull())
113 {
114#ifdef DEBUG_PICTURES
115 kdDebug(30003) << "Trying to load picture from file " << fileName << endl;
116#endif
117 if (c.loadFromFile(fileName))
118 insertPicture(key, c);
119 else
120 kdWarning(30003) << "Could not load KoPicture from " << fileName << endl;
121 }
122 return c;
123}
124
125TQString KoPictureCollection::getFileName(const Type pictureType, KoPicture& picture, int& counter)
126{
127 TQString storeURL;
128 // ### TODO: remove "cliparts" when KPresenter is ready for it
129 if (pictureType==CollectionClipart)
130 storeURL="cliparts/clipart";
131 else
132 storeURL="pictures/picture";
133 storeURL+=TQString::number(++counter);
134 storeURL+='.';
135 storeURL+=picture.getExtension();
136 return storeURL;
137}
138
140{
141 TQString storeURL( "Pictures/");
142 if ( !picture.uniquePictureId().isEmpty() )
143 storeURL+=picture.uniquePictureId();
144 else
145 storeURL+=picture.getKey().toString();
146 storeURL+='.';
147 storeURL+=picture.getExtension();
148 return storeURL;
149}
150
151bool KoPictureCollection::saveToStore(const Type pictureType, KoStore *store, const TQValueList<KoPictureKey>& keys)
152{
153 int counter=0;
154 TQValueList<KoPictureKey>::const_iterator it = keys.begin();
155 for ( ; it != keys.end(); ++it )
156 {
157 KoPicture c = findPicture( *it );
158 if (c.isNull())
159 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
160 else
161 {
162 TQString storeURL=getFileName(pictureType, c, counter);
163
164 if (store->open(storeURL))
165 {
166 KoStoreDevice dev(store);
167 if ( ! c.save(&dev) )
168 return false; // e.g. bad image?
169 if ( !store->close() )
170 return false; // e.g. disk full
171 }
172 }
173 }
174 return true;
175}
176
177bool KoPictureCollection::saveOasisToStore( KoStore *store, TQValueList<KoPictureKey> keys, KoXmlWriter* manifestWriter )
178{
179 TQValueList<KoPictureKey>::Iterator it = keys.begin();
180 for ( ; it != keys.end(); ++it )
181 {
182 KoPicture c = findPicture( *it );
183 if (c.isNull())
184 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
185 else
186 {
187 TQString storeURL( getOasisFileName(c) );
188 if (store->open(storeURL))
189 {
190 KoStoreDevice dev(store);
191 if ( ! c.save(&dev) )
192 return false; // e.g. bad image?
193 if ( !store->close() )
194 return false; // e.g. disk full
195 manifestWriter->addManifestEntry( storeURL, c.getMimeType() );
196 }
197 }
198 }
199 return true;
200}
201
202TQDomElement KoPictureCollection::saveXML(const Type pictureType, TQDomDocument &doc, TQValueList<KoPictureKey> keys)
203{
204 TQString strElementName("PICTURES");
205 if (pictureType==CollectionImage)
206 strElementName="PIXMAPS";
207 else if (pictureType==CollectionClipart)
208 strElementName="CLIPARTS";
209 TQDomElement cliparts = doc.createElement( strElementName );
210 int counter=0;
211 TQValueList<KoPictureKey>::Iterator it = keys.begin();
212 for ( ; it != keys.end(); ++it )
213 {
214 KoPicture picture = findPicture( *it );
215 if ( picture.isNull() )
216 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
217 else
218 {
219 TQString pictureName=getFileName(pictureType, picture, counter);
220 TQDomElement keyElem = doc.createElement( "KEY" );
221 cliparts.appendChild(keyElem);
222 (*it).saveAttributes(keyElem);
223 keyElem.setAttribute("name", pictureName);
224 }
225 }
226 return cliparts;
227}
228
229void KoPictureCollection::readXML( TQDomElement& pixmapsElem, TQMap <KoPictureKey, TQString>& map )
230{
231 for( TQDomNode n = pixmapsElem.firstChild();
232 !n.isNull();
233 n = n.nextSibling() )
234 {
235 TQDomElement keyElement = n.toElement();
236 if (keyElement.isNull()) continue;
237 if (keyElement.tagName()=="KEY")
238 {
239 KoPictureKey key;
240 key.loadAttributes(keyElement);
241 map.insert(key, keyElement.attribute("name"));
242 }
243 }
244}
245
246
247KoPictureCollection::StoreMap KoPictureCollection::readXML( TQDomElement& pixmapsElem )
248{
249 StoreMap map;
250 readXML(pixmapsElem, map);
251 return map;
252}
253
254void KoPictureCollection::readFromStore( KoStore * store, const StoreMap & storeMap )
255{
256#ifdef DEBUG_PICTURES
257 kdDebug(30003) << "KoPictureCollection::readFromStore " << store << endl;
258#endif
259 StoreMap::ConstIterator it = storeMap.begin();
260 for ( ; it != storeMap.end(); ++it )
261 {
262 KoPicture c = findPicture(it.key());
263 if (!c.isNull())
264 {
265 // Do not load a file that we already have!
266 //kdDebug(30003) << store << endl;
267 continue;
268 }
269 TQString u(it.data());
270 if (u.isEmpty())
271 {
272 // old naming I suppose ?
273 u=it.key().toString();
274 }
275
276 KoPicture picture;
277
278 if (!store->open( u ))
279 {
280 u.prepend( "file:" );
281 if (!store->open( u ))
282 {
283 kdWarning(30003) << "Could load neither from store nor from file: " << it.data() << endl;
284 return;
285 }
286 }
287
288 const int pos=u.findRev('.');
289 if (pos==-1)
290 {
291 kdError(30003) << "File with no extension! Not supported!" << endl;
292 return;
293 }
294 const TQString extension(u.mid(pos+1));
295
296 KoStoreDevice dev(store);
297 picture.load(&dev, extension);
298 store->close();
299
300 if (!picture.isNull())
301 insertPicture(it.key(), picture);
302 }
303}
304
305KoPicture KoPictureCollection::findOrLoad(const TQString& fileName, const TQDateTime& dateTime)
306{
307 // As now all KoPictureKey objects have a valid TQDateTime, we must do it without a date/time check.
308 ConstIterator it = find( KoPictureKey ( fileName, dateTime ) );
309 if ( it == end() )
310 {
311 return loadPicture( fileName );
312 }
313 return *it;
314}
315
317{
318 uint idx = 0;
319 Iterator it;
320 for ( it = begin(); it != end(); ++it, ++idx )
321 {
322 it.data().assignPictureId(idx);
323 }
324}
TQDomElement saveXML(const Type pictureType, TQDomDocument &doc, TQValueList< KoPictureKey > keys)
Generate the <PICTURES>, <PIXMAPS> or <CLIPARTS> tag, that saves the key and the related relative pat...
KoPicture findOrLoad(const TQString &fileName, const TQDateTime &dateTime)
KoPicture downloadPicture(const KURL &url, TQWidget *window)
Download a possibly remote file.
KoPicture loadPicture(const TQString &fileName)
Load a clipart from a file (and insert into the collection).
KoPicture findPicture(const KoPictureKey &key) const
Looks for a clipart in the collection, returns a new KoPicture with that key if not found.
void readFromStore(KoStore *store, const StoreMap &storeMap)
Read all pictures from the store, into this collection The map comes from readXML,...
StoreMap readXML(TQDomElement &pixmapsElem)
Read the <PICTURES>, <PIXMAPS> or <CLIPARTS> tag, and save the result (key<->store-filename associati...
TQString getOasisFileName(const KoPicture &picture) const
Return filename as url for picture.
void assignUniqueIds()
Call it before to save Oasis file.
@ CollectionImage
collection with images only
@ CollectionClipart
collection with cliparts only
KoPicture insertPicture(const KoPictureKey &key, const KoPicture &picture)
Inserts a picture into the collection, if not already there.
bool saveToStore(const Type pictureType, KoStore *store, const TQValueList< KoPictureKey > &keys)
Save the used picturess from the collection into the store Usually called from completeSaving().
KoPictureKey is the structure describing a picture in a unique way.
TQString toString() const
Convert this key into a string representation of it.
void loadAttributes(const TQDomElement &elem)
Load this key from XML (as KOffice 1.3)
void setKeyFromFile(const TQString &filename)
Sets the key according to filename, including modification time.
KoPicture is a container class for various types of pictures supported by KOffice.
Definition KoPicture.h:44
TQString getExtension(void) const
bool setKeyAndDownloadPicture(const KURL &url, TQWidget *window)
Download and set the key for a possibly remote file.
TQString getMimeType(void) const
void setKey(const KoPictureKey &key)
Set the key structure describing the picture in a unique way.
bool loadFromFile(const TQString &fileName)
Load the picture from a file named fileName.
bool isNull(void) const
Returns true if the picture is null.
bool save(TQIODevice *io) const
Save picture into a TQIODevice.
KoPictureKey getKey(void) const
Retrieve the key structure describing the picture in a unique way.
This class implements a TQIODevice around KoStore, so that it can be used to create a TQDomDocument f...
Saves and loads KOffice documents using various backends.
Definition KoStore.h:40
bool close()
Close the file inside the store.
Definition KoStore.cpp:242
bool open(const TQString &name)
Open a new file inside the store.
Definition KoStore.cpp:188
A class for writing out XML (to any TQIODevice), with a special attention on performance.
Definition KoXmlWriter.h:37
void addManifestEntry(const TQString &fullPath, const TQString &mediaType)
Special helper for writing "manifest" files This is equivalent to startElement/2*addAttribute/endElem...