00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <Q3ValueList>
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 kDebug(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 kDebug(30003) << "KoPictureCollection::insertPicture " << key.toString() << endl;
00056 #endif
00057 KoPicture c = findPicture(key);
00058 if (c.isNull())
00059 {
00060 #ifdef DEBUG_PICTURES
00061 kDebug(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 kDebug(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 kDebug(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 kWarning(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 kDebug(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 kDebug(30003) << "Trying to load picture from file " << fileName << endl;
00116 #endif
00117 if (c.loadFromFile(fileName))
00118 insertPicture(key, c);
00119 else
00120 kWarning(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::getOasisFileName(const KoPicture& picture) const
00140 {
00141 QString storeURL( "Pictures/");
00142 if ( !picture.uniquePictureId().isEmpty() )
00143 storeURL+=picture.uniquePictureId();
00144 else
00145 storeURL+=picture.getKey().toString();
00146 storeURL+='.';
00147 storeURL+=picture.getExtension();
00148 return storeURL;
00149 }
00150
00151 bool KoPictureCollection::saveToStore(const Type pictureType, KoStore *store, const Q3ValueList<KoPictureKey>& keys)
00152 {
00153 int counter=0;
00154 Q3ValueList<KoPictureKey>::const_iterator it = keys.begin();
00155 for ( ; it != keys.end(); ++it )
00156 {
00157 KoPicture c = findPicture( *it );
00158 if (c.isNull())
00159 kWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00160 else
00161 {
00162 QString storeURL=getFileName(pictureType, c, counter);
00163
00164 if (store->open(storeURL))
00165 {
00166 KoStoreDevice dev(store);
00167 if ( ! c.save(&dev) )
00168 return false;
00169 if ( !store->close() )
00170 return false;
00171 }
00172 }
00173 }
00174 return true;
00175 }
00176
00177 bool KoPictureCollection::saveOasisToStore( KoStore *store, Q3ValueList<KoPictureKey> keys, KoXmlWriter* manifestWriter )
00178 {
00179 Q3ValueList<KoPictureKey>::Iterator it = keys.begin();
00180 for ( ; it != keys.end(); ++it )
00181 {
00182 KoPicture c = findPicture( *it );
00183 if (c.isNull())
00184 kWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00185 else
00186 {
00187 QString storeURL( getOasisFileName(c) );
00188 if (store->open(storeURL))
00189 {
00190 KoStoreDevice dev(store);
00191 if ( ! c.save(&dev) )
00192 return false;
00193 if ( !store->close() )
00194 return false;
00195 manifestWriter->addManifestEntry( storeURL, c.getMimeType() );
00196 }
00197 }
00198 }
00199 return true;
00200 }
00201
00202 QDomElement KoPictureCollection::saveXML(const Type pictureType, QDomDocument &doc, Q3ValueList<KoPictureKey> keys)
00203 {
00204 QString strElementName("PICTURES");
00205 if (pictureType==CollectionImage)
00206 strElementName="PIXMAPS";
00207 else if (pictureType==CollectionClipart)
00208 strElementName="CLIPARTS";
00209 QDomElement cliparts = doc.createElement( strElementName );
00210 int counter=0;
00211 Q3ValueList<KoPictureKey>::Iterator it = keys.begin();
00212 for ( ; it != keys.end(); ++it )
00213 {
00214 KoPicture picture = findPicture( *it );
00215 if ( picture.isNull() )
00216 kWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl;
00217 else
00218 {
00219 QString pictureName=getFileName(pictureType, picture, counter);
00220 QDomElement keyElem = doc.createElement( "KEY" );
00221 cliparts.appendChild(keyElem);
00222 (*it).saveAttributes(keyElem);
00223 keyElem.setAttribute("name", pictureName);
00224 }
00225 }
00226 return cliparts;
00227 }
00228
00229 void KoPictureCollection::readXML( QDomElement& pixmapsElem, QMap <KoPictureKey, QString>& map )
00230 {
00231 for( QDomNode n = pixmapsElem.firstChild();
00232 !n.isNull();
00233 n = n.nextSibling() )
00234 {
00235 QDomElement keyElement = n.toElement();
00236 if (keyElement.isNull()) continue;
00237 if (keyElement.tagName()=="KEY")
00238 {
00239 KoPictureKey key;
00240 key.loadAttributes(keyElement);
00241 map.insert(key, keyElement.attribute("name"));
00242 }
00243 }
00244 }
00245
00246
00247 KoPictureCollection::StoreMap KoPictureCollection::readXML( QDomElement& pixmapsElem )
00248 {
00249 StoreMap map;
00250 readXML(pixmapsElem, map);
00251 return map;
00252 }
00253
00254 void KoPictureCollection::readFromStore( KoStore * store, const StoreMap & storeMap )
00255 {
00256 #ifdef DEBUG_PICTURES
00257 kDebug(30003) << "KoPictureCollection::readFromStore " << store << endl;
00258 #endif
00259 StoreMap::ConstIterator it = storeMap.begin();
00260 for ( ; it != storeMap.end(); ++it )
00261 {
00262 KoPicture c = findPicture(it.key());
00263 if(!c.isNull())
00264 {
00265
00266
00267 continue;
00268 }
00269 QString u( it.value() );
00270 if( u.isEmpty() )
00271 {
00272
00273 u=it.key().toString();
00274 }
00275
00276 KoPicture picture;
00277
00278 if( !store->open( u ))
00279 {
00280 u.prepend( "file:" );
00281 if (!store->open( u ))
00282 {
00283 kWarning(30003) << "Could load neither from store nor from file: " << it.value() << endl;
00284 return;
00285 }
00286 }
00287
00288 const int pos = u.lastIndexOf( '.' );
00289 if (pos==-1)
00290 {
00291 kError(30003) << "File with no extension! Not supported!" << endl;
00292 return;
00293 }
00294 const QString extension(u.mid(pos+1));
00295
00296 KoStoreDevice dev(store);
00297 picture.load(&dev, extension);
00298 store->close();
00299
00300 if (!picture.isNull())
00301 insertPicture(it.key(), picture);
00302 }
00303 }
00304
00305 KoPicture KoPictureCollection::findOrLoad(const QString& fileName, const QDateTime& dateTime)
00306 {
00307
00308 ConstIterator it = find( KoPictureKey ( fileName, dateTime ) );
00309 if ( it == end() )
00310 {
00311 return loadPicture( fileName );
00312 }
00313 return *it;
00314 }
00315
00316 void KoPictureCollection::assignUniqueIds()
00317 {
00318 uint idx = 0;
00319 Iterator it;
00320 for ( it = begin(); it != end(); ++it, ++idx )
00321 {
00322 it.value().assignPictureId(idx);
00323 }
00324 }