00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qdom.h>
00023
00024 #include <kdebug.h>
00025 #include <kurl.h>
00026
00027 #include <koStoreDevice.h>
00028 #include <koxmlwriter.h>
00029
00030 #include "koPicture.h"
00031 #include "koPictureCollection.h"
00032
00033
00034
00035 KoPicture KoPictureCollection::findPicture(const KoPictureKey& key) const
00036 {
00037 #ifdef DEBUG_PICTURES
00038 kdDebug(30003) << "KoPictureCollection::findPicture " << key.toString() << endl;
00039 #endif
00040 ConstIterator it = find( key );
00041 if ( it == end() )
00042 {
00043 KoPicture picture;
00044 picture.setKey(key);
00045 return picture;
00046 }
00047
00048 return *it;
00049 }
00050
00051
00052 KoPicture KoPictureCollection::insertPicture(const KoPictureKey& key, const KoPicture& picture)
00053 {
00054 #ifdef DEBUG_PICTURES
00055 kdDebug(30003) << "KoPictureCollection::insertPicture " << key.toString() << endl;
00056 #endif
00057 KoPicture c = findPicture(key);
00058 if (c.isNull())
00059 {
00060 #ifdef DEBUG_PICTURES
00061 kdDebug(30003) << "KoPictureCollection::insertPicture not found -> inserting" << endl;
00062 #endif
00063 c=picture;
00064 c.setKey(key);
00065 insert(key, c);
00066 }
00067 return c;
00068 }
00069
00070 KoPicture KoPictureCollection::insertPicture(const KoPicture& picture)
00071 {
00072 return insertPicture(picture.getKey(), picture);
00073 }
00074
00075 KoPicture KoPictureCollection::downloadPicture(const KURL& url, QWidget *window)
00076 {
00077 #ifdef DEBUG_PICTURES
00078 kdDebug(30003) << "KoPictureCollection::downloadPicture " << url.prettyURL() << endl;
00079 #endif
00080
00081
00082 if (url.isLocalFile())
00083 return loadPicture(url.path());
00084
00085
00086
00087
00088
00089 KoPicture pic;
00090 #ifdef DEBUG_PICTURES
00091 kdDebug(30003) << "Trying to download picture from file " << url.prettyURL() << endl;
00092 #endif
00093
00094 if (pic.setKeyAndDownloadPicture(url, window))
00095 insertPicture(pic.getKey(), pic);
00096 else
00097 kdWarning(30003) << "Could not download KoPicture from " << url.prettyURL() << endl;
00098
00099 return pic;
00100 }
00101
00102 KoPicture KoPictureCollection::loadPicture(const QString& fileName)
00103 {
00104 #ifdef DEBUG_PICTURES
00105 kdDebug(30003) << "KoPictureCollection::loadPicture " << fileName << endl;
00106 #endif
00107
00108 KoPictureKey key;
00109 key.setKeyFromFile(fileName);
00110
00111 KoPicture c = findPicture(key);
00112 if (c.isNull())
00113 {
00114 #ifdef DEBUG_PICTURES
00115 kdDebug(30003) << "Trying to load picture from file " << fileName << endl;
00116 #endif
00117 if (c.loadFromFile(fileName))
00118 insertPicture(key, c);
00119 else
00120 kdWarning(30003) << "Could not load KoPicture from " << fileName << endl;
00121 }
00122 return c;
00123 }
00124
00125 QString KoPictureCollection::getFileName(const Type pictureType, KoPicture& picture, int& counter)
00126 {
00127 QString storeURL;
00128
00129 if (pictureType==CollectionClipart)
00130 storeURL="cliparts/clipart";
00131 else
00132 storeURL="pictures/picture";
00133 storeURL+=QString::number(++counter);
00134 storeURL+='.';
00135 storeURL+=picture.getExtension();
00136 return storeURL;
00137 }
00138
00139 QString KoPictureCollection::getFileNameAsKOffice1Dot1(const Type pictureType, KoPicture& picture, int& counter)
00140 {
00141 QString storeURL;
00142 if (pictureType==CollectionClipart)
00143 storeURL="cliparts/clipart";
00144 else
00145 storeURL="pictures/picture";
00146 storeURL+=QString::number(++counter);
00147 storeURL+='.';
00148 storeURL+=picture.getExtensionAsKOffice1Dot1();
00149 return storeURL;
00150 }
00151
00152 QString KoPictureCollection::getOasisFileName(const KoPicture& picture) const
00153 {
00154 QString storeURL( "Pictures/");
00155 if ( !picture.uniquePictureId().isEmpty() )
00156 storeURL+=picture.uniquePictureId();
00157 else
00158 storeURL+=picture.getKey().toString();
00159 storeURL+='.';
00160 storeURL+=picture.getExtensionAsKOffice1Dot1();
00161 return storeURL;
00162 }
00163
00164 bool KoPictureCollection::saveToStoreInternal(const Type pictureType, KoStore *store, QValueList<KoPictureKey>& keys, const bool koffice11)
00165 {
00166 int counter=0;
00167 QValueList<KoPictureKey>::Iterator it = keys.begin();
00168 for ( ; it != keys.end(); ++it )
00169 {
00170 KoPicture c = findPicture( *it );
00171 if (c.isNull())
00172 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00173 else
00174 {
00175 QString storeURL;
00176 if (koffice11)
00177 storeURL=getFileNameAsKOffice1Dot1(pictureType, c, counter);
00178 else
00179 storeURL=getFileName(pictureType, c, counter);
00180
00181 if (store->open(storeURL))
00182 {
00183 KoStoreDevice dev(store);
00184 if (koffice11)
00185 {
00186 if ( !c.saveAsKOffice1Dot1(&dev) )
00187 return false;
00188 }
00189 else
00190 {
00191 if ( ! c.save(&dev) )
00192 return false;
00193 }
00194 if ( !store->close() )
00195 return false;
00196 }
00197 }
00198 }
00199 return true;
00200 }
00201
00202 bool KoPictureCollection::saveOasisToStore( KoStore *store, QValueList<KoPictureKey> keys, KoXmlWriter* manifestWriter )
00203 {
00204 QValueList<KoPictureKey>::Iterator it = keys.begin();
00205 for ( ; it != keys.end(); ++it )
00206 {
00207 KoPicture c = findPicture( *it );
00208 if (c.isNull())
00209 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00210 else
00211 {
00212 QString storeURL( getOasisFileName(c) );
00213 if (store->open(storeURL))
00214 {
00215 KoStoreDevice dev(store);
00216 if ( ! c.save(&dev) )
00217 return false;
00218 if ( !store->close() )
00219 return false;
00220 manifestWriter->addManifestEntry( storeURL, c.getMimeType() );
00221 }
00222 }
00223 }
00224 return true;
00225 }
00226
00227 bool KoPictureCollection::saveToStore(const Type pictureType, KoStore *store, QValueList<KoPictureKey> keys)
00228 {
00229 return saveToStoreInternal(pictureType,store, keys, false);
00230 }
00231
00232 bool KoPictureCollection::saveToStoreAsKOffice1Dot1(const Type pictureType, KoStore *store, QValueList<KoPictureKey> keys)
00233 {
00234 return saveToStoreInternal(pictureType,store, keys, true);
00235 }
00236
00237 QDomElement KoPictureCollection::saveXML(const Type pictureType, QDomDocument &doc, QValueList<KoPictureKey> keys)
00238 {
00239 QString strElementName("PICTURES");
00240 if (pictureType==CollectionImage)
00241 strElementName="PIXMAPS";
00242 else if (pictureType==CollectionClipart)
00243 strElementName="CLIPARTS";
00244 QDomElement cliparts = doc.createElement( strElementName );
00245 int counter=0;
00246 QValueList<KoPictureKey>::Iterator it = keys.begin();
00247 for ( ; it != keys.end(); ++it )
00248 {
00249 KoPicture picture = findPicture( *it );
00250 if ( picture.isNull() )
00251 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00252 else
00253 {
00254 QString pictureName=getFileName(pictureType, picture, counter);
00255 QDomElement keyElem = doc.createElement( "KEY" );
00256 cliparts.appendChild(keyElem);
00257 (*it).saveAttributes(keyElem);
00258 keyElem.setAttribute("name", pictureName);
00259 }
00260 }
00261 return cliparts;
00262 }
00263
00264 void KoPictureCollection::saveXMLAsKOffice1Dot1(QDomDocument &doc, QDomElement& parent, QValueList<KoPictureKey> keys)
00265 {
00266 QDomElement pixmaps = doc.createElement( "PIXMAPS" );
00267 QDomElement cliparts = doc.createElement( "CLIPARTS" );
00268 parent.appendChild(pixmaps);
00269 parent.appendChild(cliparts);
00270 int counter=0;
00271 QValueList<KoPictureKey>::Iterator it = keys.begin();
00272 for ( ; it != keys.end(); ++it )
00273 {
00274 KoPicture picture = findPicture( *it );
00275 if ( picture.isNull() )
00276 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00277 else
00278 {
00279 QString pictureName("error");
00280 QDomElement keyElem = doc.createElement( "KEY" );
00281
00282 if (picture.isClipartAsKOffice1Dot1())
00283 {
00284 pictureName=getFileNameAsKOffice1Dot1(CollectionClipart, picture, counter);
00285 cliparts.appendChild(keyElem);
00286 }
00287 else
00288 {
00289 pictureName=getFileNameAsKOffice1Dot1(CollectionImage, picture, counter);
00290 pixmaps.appendChild(keyElem);
00291 }
00292
00293 (*it).saveAttributes(keyElem);
00294 keyElem.setAttribute("name", pictureName);
00295 }
00296 }
00297 return;
00298 }
00299
00300 void KoPictureCollection::readXML( QDomElement& pixmapsElem, QMap <KoPictureKey, QString>& map )
00301 {
00302 for( QDomNode n = pixmapsElem.firstChild();
00303 !n.isNull();
00304 n = n.nextSibling() )
00305 {
00306 QDomElement keyElement = n.toElement();
00307 if (keyElement.isNull()) continue;
00308 if (keyElement.tagName()=="KEY")
00309 {
00310 KoPictureKey key;
00311 key.loadAttributes(keyElement);
00312 map.insert(key, keyElement.attribute("name"));
00313 }
00314 }
00315 }
00316
00317
00318 KoPictureCollection::StoreMap KoPictureCollection::readXML( QDomElement& pixmapsElem )
00319 {
00320 StoreMap map;
00321 readXML(pixmapsElem, map);
00322 return map;
00323 }
00324
00325 void KoPictureCollection::readFromStore( KoStore * store, const StoreMap & storeMap )
00326 {
00327 #ifdef DEBUG_PICTURES
00328 kdDebug(30003) << "KoPictureCollection::readFromStore " << store << endl;
00329 #endif
00330 StoreMap::ConstIterator it = storeMap.begin();
00331 for ( ; it != storeMap.end(); ++it )
00332 {
00333 KoPicture c = findPicture(it.key());
00334 if (!c.isNull())
00335 {
00336
00337
00338 continue;
00339 }
00340 QString u(it.data());
00341 if (u.isEmpty())
00342 {
00343
00344 u=it.key().toString();
00345 }
00346
00347 KoPicture picture;
00348
00349 if (!store->open( u ))
00350 {
00351 u.prepend( "file:" );
00352 if (!store->open( u ))
00353 {
00354 kdWarning(30003) << "Could load neither from store nor from file: " << it.data() << endl;
00355 return;
00356 }
00357 }
00358
00359 const int pos=u.findRev('.');
00360 if (pos==-1)
00361 {
00362 kdError(30003) << "File with no extension! Not supported!" << endl;
00363 return;
00364 }
00365 const QString extension(u.mid(pos+1));
00366
00367 KoStoreDevice dev(store);
00368 picture.load(&dev, extension);
00369 store->close();
00370
00371 if (!picture.isNull())
00372 insertPicture(it.key(), picture);
00373 }
00374 }
00375
00376 KoPicture KoPictureCollection::findOrLoad(const QString& fileName, const QDateTime& dateTime)
00377 {
00378
00379 ConstIterator it = find( KoPictureKey ( fileName, dateTime ) );
00380 if ( it == end() )
00381 {
00382 return loadPicture( fileName );
00383 }
00384 return *it;
00385 }
00386
00387 void KoPictureCollection::assignUniqueIds()
00388 {
00389 uint idx = 0;
00390 Iterator it;
00391 for ( it = begin(); it != end(); ++it, ++idx )
00392 {
00393 it.data().assignPictureId(idx);
00394 }
00395 }