lib Library API Documentation

koPictureCollection.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org> 00003 Copyright (c) 2001 David Faure <faure@kde.org> 00004 Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <qdom.h> 00023 00024 #include <kdebug.h> 00025 #include <kurl.h> 00026 00027 #include <koStoreDevice.h> 00028 00029 #include "koPicture.h" 00030 #include "koPictureCollection.h" 00031 00032 //#define DEBUG_PICTURES 00033 00034 KoPicture KoPictureCollection::findPicture(const KoPictureKey& key) const 00035 { 00036 #ifdef DEBUG_PICTURES 00037 kdDebug(30003) << "KoPictureCollection::findPicture " << key.toString() << endl; 00038 #endif 00039 ConstIterator it = find( key ); 00040 if ( it == end() ) 00041 { 00042 KoPicture picture; 00043 picture.setKey(key); 00044 return picture; 00045 } 00046 00047 return *it; 00048 } 00049 00050 KoPicture KoPictureCollection::insertPicture(const KoPictureKey& key, const KoPicture& picture) 00051 { 00052 #ifdef DEBUG_PICTURES 00053 kdDebug(30003) << "KoPictureCollection::insertPicture " << key.toString() << endl; 00054 #endif 00055 KoPicture c = findPicture(key); 00056 if (c.isNull()) 00057 { 00058 #ifdef DEBUG_PICTURES 00059 kdDebug(30003) << "KoPictureCollection::insertPicture not found -> inserting" << endl; 00060 #endif 00061 c=picture; 00062 c.setKey(key); // Be sure that the key is correctly set in the KoPicture! 00063 insert(key, c); 00064 } 00065 return c; 00066 } 00067 00068 KoPicture KoPictureCollection::insertPicture(const KoPicture& picture) 00069 { 00070 return insertPicture(picture.getKey(), picture); 00071 } 00072 00073 KoPicture KoPictureCollection::downloadPicture(const KURL& url) 00074 { 00075 #ifdef DEBUG_PICTURES 00076 kdDebug(30003) << "KoPictureCollection::downloadPicture " << url.prettyURL() << endl; 00077 #endif 00078 00079 // If it is a local file, we can check the last modification date, so we should better use loadPicture 00080 if (url.isLocalFile()) 00081 return loadPicture(url.path()); 00082 00083 00084 // We have really a remote file, so we cannot check the last modification date 00085 // Therefore we have to always download the file 00086 00087 KoPicture pic; 00088 #ifdef DEBUG_PICTURES 00089 kdDebug(30003) << "Trying to download picture from file " << url.prettyURL() << endl; 00090 #endif 00091 00092 if (pic.setKeyAndDownloadPicture(url)) 00093 insertPicture(pic.getKey(), pic); 00094 else 00095 kdWarning(30003) << "Could not download KoPicture from " << url.prettyURL() << endl; 00096 00097 return pic; 00098 } 00099 00100 KoPicture KoPictureCollection::loadPicture(const QString& fileName) 00101 { 00102 #ifdef DEBUG_PICTURES 00103 kdDebug(30003) << "KoPictureCollection::loadPicture " << fileName << endl; 00104 #endif 00105 // Check the last modified date of the file, as it is now. 00106 KoPictureKey key; 00107 key.setKeyFromFile(fileName); 00108 00109 KoPicture c = findPicture(key); 00110 if (c.isNull()) 00111 { 00112 #ifdef DEBUG_PICTURES 00113 kdDebug(30003) << "Trying to load picture from file " << fileName << endl; 00114 #endif 00115 if (c.loadFromFile(fileName)) 00116 insertPicture(key, c); 00117 else 00118 kdWarning(30003) << "Could not load KoPicture from " << fileName << endl; 00119 } 00120 return c; 00121 } 00122 00123 QString KoPictureCollection::getFileName(const Type pictureType, KoPicture& picture, int& counter) 00124 { 00125 QString storeURL; 00126 // ### TODO: remove "cliparts" when KPresenter is ready for it 00127 if (pictureType==CollectionClipart) 00128 storeURL="cliparts/clipart"; 00129 else 00130 storeURL="pictures/picture"; 00131 storeURL+=QString::number(++counter); 00132 storeURL+='.'; 00133 storeURL+=picture.getExtension(); 00134 return storeURL; 00135 } 00136 00137 QString KoPictureCollection::getFileNameAsKOffice1Dot1(const Type pictureType, KoPicture& picture, int& counter) 00138 { 00139 QString storeURL; 00140 if (pictureType==CollectionClipart) 00141 storeURL="cliparts/clipart"; 00142 else 00143 storeURL="pictures/picture"; 00144 storeURL+=QString::number(++counter); 00145 storeURL+='.'; 00146 storeURL+=picture.getExtensionAsKOffice1Dot1(); 00147 return storeURL; 00148 } 00149 00150 00151 bool KoPictureCollection::saveToStoreInternal(const Type pictureType, KoStore *store, QValueList<KoPictureKey>& keys, const bool koffice11) 00152 { 00153 int counter=0; 00154 QValueList<KoPictureKey>::Iterator it = keys.begin(); 00155 for ( ; it != keys.end(); ++it ) 00156 { 00157 KoPicture c = findPicture( *it ); 00158 if (c.isNull()) 00159 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl; 00160 else 00161 { 00162 QString storeURL; 00163 if (koffice11) 00164 storeURL=getFileNameAsKOffice1Dot1(pictureType, c, counter); 00165 else 00166 storeURL=getFileName(pictureType, c, counter); 00167 00168 if (store->open(storeURL)) 00169 { 00170 KoStoreDevice dev(store); 00171 if (koffice11) 00172 { 00173 if ( !c.saveAsKOffice1Dot1(&dev) ) 00174 return false; 00175 } 00176 else 00177 { 00178 if ( ! c.save(&dev) ) 00179 return false; // e.g. bad image? 00180 } 00181 if ( !store->close() ) 00182 return false; // e.g. disk full 00183 } 00184 } 00185 } 00186 return true; 00187 } 00188 00189 bool KoPictureCollection::saveToStore(const Type pictureType, KoStore *store, QValueList<KoPictureKey> keys) 00190 { 00191 return saveToStoreInternal(pictureType,store, keys, false); 00192 } 00193 00194 bool KoPictureCollection::saveToStoreAsKOffice1Dot1(const Type pictureType, KoStore *store, QValueList<KoPictureKey> keys) 00195 { 00196 return saveToStoreInternal(pictureType,store, keys, true); 00197 } 00198 00199 QDomElement KoPictureCollection::saveXML(const Type pictureType, QDomDocument &doc, QValueList<KoPictureKey> keys) 00200 { 00201 QString strElementName("PICTURES"); 00202 if (pictureType==CollectionImage) 00203 strElementName="PIXMAPS"; 00204 else if (pictureType==CollectionClipart) 00205 strElementName="CLIPARTS"; 00206 QDomElement cliparts = doc.createElement( strElementName ); 00207 int counter=0; 00208 QValueList<KoPictureKey>::Iterator it = keys.begin(); 00209 for ( ; it != keys.end(); ++it ) 00210 { 00211 KoPicture picture = findPicture( *it ); 00212 if ( picture.isNull() ) 00213 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl; 00214 else 00215 { 00216 QString pictureName=getFileName(pictureType, picture, counter); 00217 QDomElement keyElem = doc.createElement( "KEY" ); 00218 cliparts.appendChild(keyElem); 00219 (*it).saveAttributes(keyElem); 00220 keyElem.setAttribute("name", pictureName); 00221 } 00222 } 00223 return cliparts; 00224 } 00225 00226 void KoPictureCollection::saveXMLAsKOffice1Dot1(QDomDocument &doc, QDomElement& parent, QValueList<KoPictureKey> keys) 00227 { 00228 QDomElement pixmaps = doc.createElement( "PIXMAPS" ); 00229 QDomElement cliparts = doc.createElement( "CLIPARTS" ); 00230 parent.appendChild(pixmaps); 00231 parent.appendChild(cliparts); 00232 int counter=0; 00233 QValueList<KoPictureKey>::Iterator it = keys.begin(); 00234 for ( ; it != keys.end(); ++it ) 00235 { 00236 KoPicture picture = findPicture( *it ); 00237 if ( picture.isNull() ) 00238 kdWarning(30003) << "Picture " << (*it).toString() << " not found in collection !" << endl; 00239 else 00240 { 00241 QString pictureName("error"); 00242 QDomElement keyElem = doc.createElement( "KEY" ); 00243 00244 if (picture.isClipartAsKOffice1Dot1()) 00245 { 00246 pictureName=getFileNameAsKOffice1Dot1(CollectionClipart, picture, counter); 00247 cliparts.appendChild(keyElem); 00248 } 00249 else 00250 { 00251 pictureName=getFileNameAsKOffice1Dot1(CollectionImage, picture, counter); 00252 pixmaps.appendChild(keyElem); 00253 } 00254 00255 (*it).saveAttributes(keyElem); 00256 keyElem.setAttribute("name", pictureName); 00257 } 00258 } 00259 return; 00260 } 00261 00262 void KoPictureCollection::readXML( QDomElement& pixmapsElem, QMap <KoPictureKey, QString>& map ) 00263 { 00264 for( 00265 QDomElement keyElement = pixmapsElem.firstChild().toElement(); 00266 !keyElement.isNull(); 00267 keyElement = keyElement.nextSibling().toElement() 00268 ) 00269 { 00270 if (keyElement.tagName()=="KEY") 00271 { 00272 KoPictureKey key; 00273 key.loadAttributes(keyElement); 00274 map.insert(key, keyElement.attribute("name")); 00275 } 00276 } 00277 } 00278 00279 00280 KoPictureCollection::StoreMap KoPictureCollection::readXML( QDomElement& pixmapsElem ) 00281 { 00282 StoreMap map; 00283 readXML(pixmapsElem, map); 00284 return map; 00285 } 00286 00287 void KoPictureCollection::readFromStore( KoStore * store, const StoreMap & storeMap ) 00288 { 00289 #ifdef DEBUG_PICTURES 00290 kdDebug(30003) << "KoPictureCollection::readFromStore " << store << endl; 00291 #endif 00292 StoreMap::ConstIterator it = storeMap.begin(); 00293 for ( ; it != storeMap.end(); ++it ) 00294 { 00295 KoPicture c = findPicture(it.key()); 00296 if (!c.isNull()) 00297 { 00298 // Do not load a file that we already have! 00299 //kdDebug(30003) << store << endl; 00300 continue; 00301 } 00302 QString u(it.data()); 00303 if (u.isEmpty()) 00304 { 00305 // old naming I suppose ? 00306 u=it.key().toString(); 00307 } 00308 00309 KoPicture picture; 00310 00311 if (!store->open( u )) 00312 { 00313 u.prepend( "file:" ); 00314 if (!store->open( u )) 00315 { 00316 kdWarning(30003) << "Could load neither from store nor from file: " << it.data() << endl; 00317 return; 00318 } 00319 } 00320 00321 const int pos=u.findRev('.'); 00322 if (pos==-1) 00323 { 00324 kdError(30003) << "File with no extension! Not supported!" << endl; 00325 return; 00326 } 00327 const QString extension(u.mid(pos+1)); 00328 00329 KoStoreDevice dev(store); 00330 picture.load(&dev, extension); 00331 store->close(); 00332 00333 if (!picture.isNull()) 00334 insertPicture(it.key(), picture); 00335 } 00336 } 00337 00338 KoPicture KoPictureCollection::findOrLoad(const QString& fileName, const QDateTime& dateTime) 00339 { 00340 // As now all KoPictureKey objects have a valid QDateTime, we must do it without a date/time check. 00341 ConstIterator it = find( KoPictureKey ( fileName, dateTime ) ); 00342 if ( it == end() ) 00343 { 00344 return loadPicture( fileName ); 00345 } 00346 return *it; 00347 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 06:54:17 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003