Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

album.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it 
00005 //  and/or modify it under the terms of the GNU General 
00006 //  Public License as published by the Free Software 
00007 //  Foundation; either version 2 of the License, or  
00008 //  (at your option) any later version.         
00009 //
00010 //  As a special exception, Will Stokes gives permission to 
00011 //  link this program with Qt non-commercial edition, and 
00012 //  distribute the resulting executable, without including the 
00013 //  source code for the Qt non-commercial edition in the 
00014 //  source distribution. 
00015 //==============================================
00016 
00017 //Systemwide includes
00018 #include <qimage.h>
00019 #include <qpixmap.h>
00020 #include <qstring.h>
00021 #include <time.h>
00022 #include <qfile.h>
00023 #include <qfileinfo.h>
00024 #include <qtextstream.h>
00025 #include <qdom.h>
00026 #include <qdir.h>
00027 #include <qapplication.h>
00028 #include <qregexp.h>
00029 #include <qdatetime.h>
00030 
00031 //Projectwide includes
00032 #include "album.h"
00033 #include "subalbum.h"
00034 #include "photo.h"
00035 #include "imageTools.h"
00036 #include "xmlTools.h"
00037 #include "md5.h"
00038 #include "../gui/subalbumPreviewWidget.h"
00039 #include "../gui/statusWidget.h"
00040 #include "../config.h"
00041 
00042 //==============================================
00044 Album::Album()
00045 {
00046   //set strings to default values
00047   name = "";
00048   description ="";
00049   author = "";
00050     
00051   theme = "Classic";
00052 
00053   //by default no representative image
00054   smallRepresentativeImage = new QPixmap( QString(IMAGE_PATH)+"notSpecified.png" );
00055   largeRepresentativeImage = NULL;
00056   
00057   //no Subalbums by default
00058   firstSubalbum = NULL;
00059   lastSubalbum = NULL;
00060 
00061   //set the creation date to today
00062   updateCreationDate();
00063   
00064   //no subalbums
00065   numSubalbums = 0;
00066   numLoadedSubalbums = 0;
00067   
00068   //not previously saved by default
00069   savedToDisk = false;
00070   
00071   //no interesting modifications yet
00072   modified = false;
00073   
00074   //construct temp directory to save images to
00075   QDir rootDir( QDir::homeDirPath() );
00076   if(!rootDir.exists( ".albumShaper" ))
00077   {
00078     rootDir.mkdir( ".albumShaper" );
00079   }
00080   rootDir.cd( ".albumShaper" );
00081   if(!rootDir.exists( "tmp" ))
00082   {
00083     rootDir.mkdir( "tmp" );
00084   }
00085   saveLocation = QDir::homeDirPath() + "/.albumShaper/tmp";  
00086 }
00087 //==============================================
00089 Album::~Album()    
00090 {
00091   //delete representative image
00092   delete smallRepresentativeImage;
00093   delete largeRepresentativeImage;
00094   
00095   //delete subalbums  
00096   Subalbum* current = firstSubalbum;
00097   Subalbum* temp;
00098   while(current != NULL)
00099   {
00100     temp = current->getNext();
00101     delete current;
00102     current = temp;
00103   }
00104 } 
00105 //==============================================
00107 void Album::setName(QString val)
00108 {
00109   if(name != val)
00110   {
00111     name = val;
00112     modified = true;
00113   }
00114 }
00115 //==============================================
00117 QString Album::getName()
00118 {
00119   return QString(name);
00120 }
00121 //==============================================
00123 void Album::setDescription(QString val)
00124 {
00125   if(description != val)
00126   {
00127     description = val;
00128     modified = true;
00129   }
00130 }
00131 //==============================================
00133 QString Album::getDescription()
00134 {
00135   return QString(description);
00136 }
00137 //==============================================
00139 void Album::setAuthor(QString val)
00140 {
00141   if(author != val)
00142   {
00143     author = val;
00144     modified = true;
00145   }
00146 }
00147 //==============================================
00149 QString Album::getAuthor()
00150 {
00151   return QString(author);
00152 }
00153 //==============================================
00155 void Album::setRepresentativeImages(QImage* rawImage)
00156 {
00157   //---------------------------------------------------------    
00158   //delete representative images
00159   delete smallRepresentativeImage;
00160   delete largeRepresentativeImage;
00161   //---------------------------------------------------------    
00162   //compute representative image sizes
00163   int smallRepWidth = 0;
00164   int smallRepHeight = 0;
00165   int largeRepWidth = 0;
00166   int largeRepHeight = 0;
00167   resizeImage( rawImage->width(), rawImage->height(),
00168                107, 80,
00169                smallRepWidth, smallRepHeight);
00170   resizeImage( rawImage->width(), rawImage->height(),
00171                500, 320,
00172                largeRepWidth, largeRepHeight);
00173   //---------------------------------------------------------    
00174   //create various representative images
00175   //---------------------------------------------------------    
00176   //copy and scale small version
00177   QImage thumbnailSmall = rawImage->smoothScale( smallRepWidth, smallRepHeight );
00178   thumbnailSmall.setAlphaBuffer(true);
00179   smallRepresentativeImage = new QPixmap( smallRepWidth, smallRepHeight );
00180   smallRepresentativeImage->convertFromImage( thumbnailSmall );  
00181 
00182   //copy and scale large version
00183   QImage thumbnailLarge = rawImage->smoothScale( largeRepWidth, largeRepHeight );
00184   thumbnailLarge.setAlphaBuffer(true);
00185   largeRepresentativeImage = new QPixmap( largeRepWidth, largeRepHeight );
00186   largeRepresentativeImage->convertFromImage( thumbnailLarge );  
00187 
00188   //set modified
00189   modified = true;
00190 }
00191 //==============================================
00193 QPixmap* Album::getRepresentativeImage(int size)
00194 {
00195   if(size == SMALL)
00196     return smallRepresentativeImage;
00197   else if(size == LARGE)
00198     return largeRepresentativeImage;
00199   else
00200     return NULL;
00201 }
00202 //==============================================
00204 Subalbum* Album::getFirstSubalbum()
00205 {
00206   return firstSubalbum;
00207 }
00208 //==============================================
00210 Subalbum* Album::getLastSubalbum()
00211 {
00212   return lastSubalbum;
00213 }
00214 //==============================================
00215 void Album::appendSubalbum(Subalbum* val)
00216 {
00217   numSubalbums++;
00218   
00219   //first subalbum
00220   if(firstSubalbum == NULL)
00221   {
00222     firstSubalbum = val;
00223     lastSubalbum = val;
00224   }
00225   //append to end of list
00226   else
00227   {
00228     lastSubalbum->setNext( val );
00229     lastSubalbum = val;
00230   }
00231 
00232   //set modified
00233   modified = true;
00234 }
00235 //==============================================
00236 void Album::removeSubalbum(Subalbum* val)
00237 {
00238   //search through linked list and remove item
00239   Subalbum* prev = NULL;
00240   Subalbum* current = firstSubalbum;
00241   while(current != NULL)
00242   {
00243     //found it! remove it
00244     if(current == val)
00245     { 
00246       //update first pointer
00247       if(firstSubalbum == val)
00248         firstSubalbum = val->getNext();
00249       
00250       //update last pointer
00251       if(lastSubalbum == val) 
00252         lastSubalbum = prev;
00253 
00254       //update next pointer
00255       if(prev != NULL)
00256         prev->setNext( current->getNext() );
00257             
00258       //delete object
00259       delete val;
00260       val = NULL;
00261       numSubalbums--;
00262       return;
00263     }
00264     
00265     prev = current;
00266     current = current->getNext();
00267   }
00268 
00269   //set modified
00270   modified = true;
00271 }
00272 //==============================================
00274 int Album::getModificationYear()
00275 {
00276   return modificationYear;
00277 }
00278 //==============================================
00280 int Album::getModificationMonth()
00281 {
00282   return modificationMonth;
00283 }
00284 //==============================================
00286 int Album::getModificationDay()
00287 {
00288   return modificationDay;
00289 }
00290 //==============================================
00292 void Album::updateCreationDate()
00293 {
00294   //set creation date to today
00295   QDate date = QDate::currentDate();
00296   creationYear = date.year();
00297   creationMonth = date.month();
00298   creationDay = date.day();
00299 }
00300 //==============================================
00302 void Album::updateModificationDate()
00303 {
00304   //set last modification date to today
00305   QDate date = QDate::currentDate();
00306   modificationYear = date.year();
00307   modificationMonth = date.month();
00308   modificationDay = date.day();
00309 }
00310 //==============================================
00311 int Album::importFromDisk(StatusWidget* status, QString fileName)
00312 {
00313   //update file
00314   updateXML( QFileInfo(fileName).dirPath(TRUE) );
00315   
00316   //open file
00317   QFile albumFile( fileName );
00318   
00319   //unable ot open xml file? alert user
00320   if( !albumFile.open( IO_ReadOnly ) )
00321     return ALBUM_READ_ERROR;
00322     
00323   //parse dom
00324   QDomDocument albumDom;
00325   if( !albumDom.setContent( &albumFile ) )
00326     return ALBUM_XML_ERROR;
00327 
00328   //close file
00329   albumFile.close();
00330 
00331   //get main directory all other files and subdirectories are in  
00332   QString rootDir = QFileInfo(albumFile).dirPath(TRUE);
00333   saveLocation = rootDir + "/img";
00334   
00335   
00336   //if representative image exists load
00337   QImage repImage(rootDir + "/img/album.jpg");
00338   if(!repImage.isNull())
00339   {
00340     setRepresentativeImages(&repImage);  
00341   }  
00342 
00343   //count number of photos in album, needed for showing loading progress
00344   int numPhotos = 0;
00345   QDomElement root = albumDom.documentElement();
00346   QDomNode node = root.firstChild();
00347   while( !node.isNull() )
00348   {
00349     if( node.isElement() && node.nodeName() == "subalbum" )
00350     {
00351       QDomNode childNode = node.firstChild();
00352       while( !childNode.isNull() )
00353       {
00354         if( childNode.isElement() && childNode.nodeName() == "photo" )
00355           numPhotos++;
00356         childNode = childNode.nextSibling();      
00357       }
00358     }
00359     node = node.nextSibling();
00360   }
00361     
00362   //setup progress bar
00363   status->showProgressBar( "Loading:", numPhotos );
00364   qApp->processEvents();
00365   
00366   int subalbumNum = 0;
00367   
00368   //get root node and start parsing DOM
00369   root = albumDom.documentElement();
00370   node = root.firstChild();
00371   QDomText val;  
00372   while( !node.isNull() )
00373   {
00374     //------------------------------------------------------------
00375     //album name
00376     if( node.isElement() && node.nodeName() == "name" )
00377     { 
00378       val = node.firstChild().toText();
00379       if(!val.isNull())
00380         name = val.nodeValue();
00381     }
00382     //------------------------------------------------------------
00383     //album description
00384     else if( node.isElement() && node.nodeName() == "description" )
00385     { 
00386       val = node.firstChild().toText();
00387       if(!val.isNull())
00388         description = val.nodeValue();
00389     }
00390     //------------------------------------------------------------
00391     //album author
00392     else if( node.isElement() && node.nodeName() == "author" )
00393     { 
00394       val = node.firstChild().toText();
00395       if(!val.isNull())
00396         author = val.nodeValue();
00397     }
00398     //------------------------------------------------------------
00399     //album theme
00400     else if( node.isElement() && node.nodeName() == "theme" )
00401     { 
00402       val = node.firstChild().toText();
00403       if(!val.isNull())
00404         theme = val.nodeValue();
00405     }
00406     //------------------------------------------------------------
00407     //album creation date
00408     else if( node.isElement() && node.nodeName() == "created" )
00409     {     
00410       val = node.firstChild().toText();
00411           
00412       //split value based on spaces, should be 7 fields
00413       QStringList vals = QStringList::split( QRegExp(" "), val.nodeValue() );
00414       int i=0;
00415       int intVals[3];
00416       QStringList::Iterator it;
00417       for ( it = vals.begin(); it != vals.end(); ++it ) 
00418       {
00419         intVals[i] = QString(*it).toInt();
00420         i++;
00421         //only read first 3 entires, year/month/day, don't overwrite 
00422         //buffer on addition entries if xml messed up
00423         if(i > 2)
00424           break;
00425       }    
00426       creationYear = intVals[0];
00427       creationMonth = intVals[1];
00428       creationDay = intVals[2];
00429     }
00430     //------------------------------------------------------------
00431     //subalbum
00432     else if( node.isElement() && node.nodeName() == "subalbum" )
00433     {
00434       //increase counter    
00435       subalbumNum++;
00436 
00437       //create new subalbum
00438       Subalbum* salbum = new Subalbum(this, numSubalbums+1);
00439       
00440       //populate it
00441       salbum->importFromDisk( &node, subalbumNum, status, (rootDir + "/") );
00442       
00443       //append it to list of subalbums
00444       appendSubalbum(salbum);
00445     }
00446     //------------------------------------------------------------
00447     //advance to next node   
00448     node = node.nextSibling();
00449     //------------------------------------------------------------
00450   }
00451 
00452   //reset number of loaded subalbums  
00453   numLoadedSubalbums = numSubalbums;
00454 
00455   //hide progress bar
00456   status->setStatus( "Album loaded." );
00457   
00458   //save load directory name and loaded/saved bit
00459   saveLocation = rootDir;
00460   savedToDisk = true;
00461   
00462   return ALBUM_LOADED;
00463 }
00464 //==============================================
00465 int Album::exportToDisk(StatusWidget* status, 
00466                                     bool smallSave,
00467                                     QString dirName,
00468                                     QString themeName)
00469 {
00470   //check to see if save location has actually changed, if not don't force save images
00471   //this occurs when user blindly selects the same old spot, or is just changing the theme used by default
00472   bool forceSave = true;
00473   
00474   if(saveLocation == dirName)
00475    forceSave = false;
00476    
00477   saveLocation = dirName;
00478   theme = themeName;
00479   return exportToDisk(status, smallSave, forceSave);
00480 }
00481 //==============================================
00482 int Album::exportToDisk(StatusWidget* status,
00483                                     bool smallSave,
00484                                     bool forceSave)
00485 {
00486   //------------------------------------------
00487   //create subdirs
00488   QDir localDir(saveLocation);
00489   //img dirs
00490   localDir.mkdir("img");
00491   //subalbum dirs
00492   localDir.setPath(saveLocation + "/img");
00493   int i;
00494   for(i=1; i <= numSubalbums; i++)
00495   {
00496     QString dirName = QString("%1") .arg(i);
00497     localDir.mkdir(dirName);
00498   }
00499   //------------------------------------------    
00500   //checks worked, go ahead with export
00501   
00502   //count number of photos
00503   int totalPhotos=0;
00504   Subalbum* current = firstSubalbum;
00505   while(current != NULL)
00506   {
00507     totalPhotos+=current->getNumPhotos();
00508     current = current->getNext();  
00509   }  
00510 
00511   //setup progress bar
00512   status->showProgressBar( "Saving:", 3*totalPhotos );
00513   qApp->processEvents();
00514 
00515   //copy over theme resources
00516   exportThemeResources( theme );
00517     
00518   //export album cover image, subalbum thumbnails
00519   exportTopLevelImages();
00520   
00521   //export subalbum images (thumbnail, slideshow, and full versions of all images)
00522   exportSubalbumImages(status, smallSave, forceSave);
00523   
00524   //apply reordering to all images
00525   reorderSubalbumImages(status);
00526   
00527   //remove old images that nolonger belong
00528   removeStagnantImages();
00529   
00530   //remove previous html files
00531   localDir.setPath(saveLocation);
00532   QStringList list = localDir.entryList( QDir::Files );
00533   QStringList::Iterator file;
00534   for ( file = list.begin(); file != list.end(); ++file ) 
00535   {
00536     if((*file).endsWith(".html"))
00537     localDir.remove( saveLocation + "/" + *file );
00538   }
00539   
00540   //export xml structure of album
00541   if(!smallSave)
00542   {
00543     int result = exportToXML();
00544     if(result != ALBUM_EXPORTED)
00545       return result;
00546   }
00547   
00548   //export various html pages using selected theme
00549   transformXMLtoHTML( saveLocation, theme );
00550   
00551   //------------------------------------------
00552   //remove files from temp folder
00553   QDir tmpDir(QDir::homeDirPath() + "/.albumShaper/tmp" );
00554   QStringList strLst = tmpDir.entryList();
00555   QStringList::iterator it;
00556   for(it = strLst.begin(); it != strLst.end(); it++)
00557   {
00558       tmpDir.remove(*it);
00559   }
00560   //------------------------------------------
00561   savedToDisk = true;
00562   
00563   //just saved so no modifications since last save
00564   modified = false;
00565   
00566   //hide progress bar
00567   status->setStatus( "Album saved." );
00568   //------------------------------------------
00569   return ALBUM_EXPORTED;
00570 }
00571 //==============================================
00572 int Album::exportToXML()
00573 {
00574   //update modification date
00575   updateModificationDate();
00576   
00577   //create/open xml file
00578   QFile* xml = new QFile(saveLocation + "/Album.xml");
00579   if(xml->open(IO_WriteOnly))
00580   {
00581     QTextStream stream( xml );
00582    
00583     //write album information
00584     stream << "<?xml version=\"1.0\"?>\n";
00585     stream << "<album version=\"1.1\">\n";
00586     stream << "  <name>" << fixXMLString(name) << "</name>\n";
00587     stream << "  <description>" << fixXMLString(description) << "</description>\n";
00588     stream << "  <author>" << fixXMLString(author) << "</author>\n";  
00589     stream << "  <created>" << creationYear << " " << creationMonth << " " << creationDay << "</created>\n";
00590     stream << "  <modified>" << modificationYear << " " << modificationMonth << " " << modificationDay << "</modified>\n";
00591     stream << "  <theme>" << theme << "</theme>\n";
00592     stream << "  <thumbnailDimensions>" << THUMBNAIL_WIDTH << " " << THUMBNAIL_HEIGHT << "</thumbnailDimensions>\n";
00593     stream << "  <slideshowDimensions>" << SLIDESHOW_WIDTH << " " << SLIDESHOW_HEIGHT << "</slideshowDimensions>\n";
00594     
00595     //if album has a represenatative image save it's path
00596     if(getRepresentativeImage(LARGE) != NULL )
00597     {
00598       stream << "  <thumb path=\"img/album.jpg\"/>\n"; 
00599     }  
00600     
00601     //write subalbums
00602     Subalbum* current = firstSubalbum;
00603     while(current != NULL)
00604     {
00605       current->exportToXML(stream);
00606       current = current->getNext();
00607     }
00608   
00609     //end album
00610     stream << "</album>\n";    
00611     xml->close(); 
00612     
00613     return ALBUM_EXPORTED;
00614   }
00615   else
00616   {
00617     return ALBUM_ERROR_OPEN_FILE;
00618   }
00619 }
00620 //==============================================
00621 void Album::exportTopLevelImages()
00622 {
00623   //if image set export it
00624   if(getRepresentativeImage(LARGE) != NULL)
00625   {
00626     getRepresentativeImage(LARGE)->save(saveLocation + "/img/album.jpg", "JPEG", 100);
00627   }
00628   //else make sure any previously set images are removed
00629   else
00630   {
00631     QDir rootDir(saveLocation + "/img/");
00632     rootDir.remove("album.jpg");
00633   }
00634   
00635   //export subalbum thumbs
00636   int n=0;
00637   Subalbum* current = firstSubalbum;
00638   while(current != NULL)
00639   {
00640     n++;
00641     //if subalbum has representative image export it
00642     if(current->getRepresentativeImage(LARGE) != NULL )
00643     {
00644       QString fileName = QString(saveLocation + "/img/%1_thumb.jpg" ).arg(n);
00645       current->getRepresentativeImage(LARGE)->save(fileName, "JPEG", 100);
00646     }
00647     //otherwise make sure anyprevious set images are removed
00648     else
00649     {
00650       QDir rootDir(saveLocation + "/img/");
00651       rootDir.remove( QString("%1_thumb.jpg").arg(n) );
00652     }
00653     current = current->getNext();
00654   }  
00655 }
00656 //==============================================
00657 void Album::exportSubalbumImages(StatusWidget* status, bool smallSave, bool forceSave)
00658 {  
00659   //iterate over all subalbums
00660   int subalbumNumber=0;
00661   Subalbum* currentSubalbum = firstSubalbum;
00662   while(currentSubalbum != NULL)
00663   {
00664     subalbumNumber++;    
00665     
00666     //iterate over all photos in this subalbum
00667     int photoNumber=0;    
00668     Photo* currentPhoto = currentSubalbum->getFirst();
00669     while(currentPhoto != NULL)
00670     {
00671       photoNumber++;
00672       //---------------------------------------
00673       //if the current photo does not need to be saved then move on
00674       if( !forceSave && !currentPhoto->getNeedsSavingVal() )
00675       {
00676         currentPhoto = currentPhoto->getNext();
00677         status->incrementProgress();
00678         qApp->processEvents();
00679         continue;
00680       }
00681       //---------------------------------------
00682       //construct a QDir object for the tmp folder
00683       QDir rootDir;
00684       //---------------------------------------
00685       //get initial photo # and subalbum #, used for saving      
00686       int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
00687       int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
00688       //---------------------------------------
00689       //export thumbnail image
00690       QString fileName = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00691       currentPhoto->getImage(THUMBNAIL)->save(fileName, "JPEG", 100);
00692 
00693       //compute and store md5 for thumbnail     
00694       std::ifstream thumbnailFile(fileName.ascii());
00695       if(thumbnailFile.is_open())
00696       {
00697         currentPhoto->setThumbnailChecksum( getMD5(thumbnailFile) ); 
00698         thumbnailFile.close();
00699       }
00700       //---------------------------------------
00701       //export slideshow image
00702       QString oldName = currentPhoto->getSlideshowFilename();
00703       QString newName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00704 
00705       //if file has been modified move from current location to final location
00706       if( currentPhoto->getNeedsSavingVal() )
00707       {
00708         //attempt to rename file, if fails do explicit copy, then delete prev file
00709          if(!rootDir.rename( oldName, newName))
00710          {
00711            copyFile(oldName, newName);
00712            rootDir.remove(oldName);
00713          }
00714       }
00715       //If file has not been modified we must be doing a save-as and saving has been forced. In this case
00716       //COPY file from current location to final location, DON'T delete previous copy!!!
00717        else
00718        {
00719          copyFile(oldName, newName);
00720        }
00721              
00722       //compute and store md5 for slideshow image
00723       std::ifstream file(newName.ascii());
00724       if(file.is_open())
00725       {
00726         currentPhoto->setSlideshowChecksum( getMD5(file) ); 
00727         file.close();
00728       }
00729       //---------------------------------------
00730       //export full size image, skip is doing small save
00731       if(!smallSave) 
00732       {
00733         oldName = currentPhoto->getImageFilename();   
00734         newName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00735       
00736         //if file has been modified move from current location to final location
00737         if( currentPhoto->getNeedsSavingVal() )
00738         {
00739           //attempt to rename file, if fails do explicit copy, then delete prev file
00740            if(!rootDir.rename( oldName, newName))
00741            {
00742              copyFile(oldName, newName);
00743              rootDir.remove(oldName);
00744            }
00745         }
00746         //If file has not been modified we must be doing a save-as and saving has been forced. In this case
00747         //COPY file from current location to final location, DON'T delete previous copy!!!
00748         else
00749         {
00750           copyFile(oldName, newName);
00751         }
00752             
00753         //compute and store md5 for image
00754         std::ifstream imageFile(newName.ascii());
00755         if(imageFile.is_open())
00756         {
00757           currentPhoto->setImageChecksum( getMD5(imageFile) ); 
00758           imageFile.close();
00759         }
00760       }
00761       //---------------------------------------
00762       //set new storage locations of files (skip if doing small save since large photo not exported)
00763       if(!smallSave)
00764       {
00765         currentPhoto->setImageFilename( QString(saveLocation + "/img/%1/%2.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
00766         currentPhoto->setSlideshowFilename( QString(saveLocation + "/img/%1/%2_slideshow.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
00767         currentPhoto->setThumbnailFilename( QString(saveLocation + "/img/%1/%2_thumb.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
00768       }
00769       //---------------------------------------
00770       //set image as not needing saving (skip if doing small save since large photo not exported)
00771       if(!smallSave)
00772         currentPhoto->setNeedsSavingVal(false);
00773       //---------------------------------------
00774       //update progress bar
00775       status->incrementProgress();
00776       qApp->processEvents();
00777       //---------------------------------------   
00778       //move on to next photo in subalbum
00779       currentPhoto = currentPhoto->getNext();
00780       //---------------------------------------
00781     }
00782     //---------------------------------------
00783     //move on to next subalbum
00784     currentSubalbum = currentSubalbum->getNext();  
00785   }
00786 }
00787 //==============================================
00788 void Album::reorderSubalbumImages(StatusWidget* status)
00789 {
00790   //--------------------------------------------------------
00791   //--------------------------------------------------------
00792   //first pass over all photos, those whose initial and current numbers don't match up
00793   //rename slightly so we don't overwrte them the second time around
00794   //--------------------------------------------------------
00795   //--------------------------------------------------------
00796   //iterate over all subalbums
00797   int subalbumNumber=0;
00798   Subalbum* currentSubalbum = firstSubalbum;
00799   while(currentSubalbum != NULL)
00800   {
00801     subalbumNumber++;    
00802     
00803     //iterate over all photos in this subalbum
00804     int photoNumber=0;    
00805     Photo* currentPhoto = currentSubalbum->getFirst();
00806     while(currentPhoto != NULL)
00807     {
00808       photoNumber++;
00809       //---------------------------------------
00810       //get initial photo # and subalbum #, used for checks and moving
00811       int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
00812       int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
00813       //---------------------------------------
00814       //if the current photo has not moved then move on
00815       if( initPhotoNumber == photoNumber &&
00816           initSubalbumNumber == subalbumNumber)
00817       {
00818         currentPhoto = currentPhoto->getNext();
00819         status->incrementProgress();
00820         qApp->processEvents();
00821         continue;
00822       }
00823       //---------------------------------------
00824       //rename full image, slideshow image, and thumbnail image     
00825       QDir rootDir;
00826       QString oldName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00827       QString newName = QString(saveLocation + "/img/%1/%2_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);     
00828       if(!rootDir.rename( oldName, newName))
00829       {
00830         copyFile(oldName, newName);      
00831         rootDir.remove( oldName );
00832       }
00833       //-----
00834       oldName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00835       newName = QString(saveLocation + "/img/%1/%2_slideshow_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);      
00836       if(!rootDir.rename( oldName, newName))
00837       {
00838         copyFile(oldName, newName);      
00839         rootDir.remove( oldName );
00840       }
00841       //-----
00842       oldName = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00843       newName = QString(saveLocation + "/img/%1/%2_thumb_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);      
00844       if(!rootDir.rename( oldName, newName))
00845       {
00846         copyFile(oldName, newName);      
00847         rootDir.remove( oldName );
00848       }
00849       //---------------------------------------
00850       //update progress bar
00851       status->incrementProgress();
00852       qApp->processEvents();
00853       //---------------------------------------   
00854       //move on to next photo in subalbum
00855       currentPhoto = currentPhoto->getNext();
00856       //---------------------------------------
00857     }
00858     //---------------------------------------
00859     //move on to next subalbum
00860     currentSubalbum = currentSubalbum->getNext();  
00861   }
00862 
00863   //--------------------------------------------------------
00864   //--------------------------------------------------------
00865   //second pass over all photos, those whose initial and current numbers don't match up
00866   //rename to their final names and reset initial photo and subalbum numbers
00867   //--------------------------------------------------------
00868   //--------------------------------------------------------
00869   //iterate over all subalbums
00870   subalbumNumber=0;
00871   currentSubalbum = firstSubalbum;
00872   while(currentSubalbum != NULL)
00873   {
00874     subalbumNumber++;    
00875     
00876     //iterate over all photos in this subalbum
00877     int photoNumber=0;    
00878     Photo* currentPhoto = currentSubalbum->getFirst();
00879     while(currentPhoto != NULL)
00880     {
00881       photoNumber++;
00882       //---------------------------------------
00883       //get initial photo # and subalbum #, used for checks and moving
00884       int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
00885       int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
00886       //---------------------------------------
00887       //if the current photo has not moved then move on
00888       if( initPhotoNumber == photoNumber &&
00889           initSubalbumNumber == subalbumNumber)
00890       {
00891         currentPhoto = currentPhoto->getNext();
00892         status->incrementProgress();
00893         qApp->processEvents();
00894         continue;
00895       }
00896       //---------------------------------------
00897       //rename full image, slideshow image, and thumbnail image to their final names
00898       QDir rootDir;
00899       QString oldName = QString(saveLocation + "/img/%1/%2_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00900       QString newName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(subalbumNumber).arg(photoNumber);     
00901       if(!rootDir.rename( oldName, newName))
00902       {
00903         copyFile(oldName, newName);      
00904         rootDir.remove( oldName );
00905       }
00906       //-----
00907       oldName = QString(saveLocation + "/img/%1/%2_slideshow_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00908       newName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(subalbumNumber).arg(photoNumber);      
00909       if(!rootDir.rename( oldName, newName))
00910       {
00911         copyFile(oldName, newName);      
00912         rootDir.remove( oldName );
00913       }
00914       //-----
00915       oldName = QString(saveLocation + "/img/%1/%2_thumb_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00916       newName = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(subalbumNumber).arg(photoNumber);      
00917       if(!rootDir.rename( oldName, newName))
00918       {
00919         copyFile(oldName, newName);      
00920         rootDir.remove( oldName );
00921       }
00922       //---------------------------------------
00923       //reset initial photo and subalbum numbers, and filenames
00924       currentPhoto->setInitialPhotoNumber(photoNumber);
00925       currentPhoto->setInitialSubalbumNumber(subalbumNumber);      
00926       currentPhoto->setImageFilename( QString(saveLocation + "/img/%1/%2.jpg").arg(subalbumNumber).arg(photoNumber) );
00927       currentPhoto->setSlideshowFilename( QString(saveLocation + "/img/%1/%2_slideshow.jpg").arg(subalbumNumber).arg(photoNumber) );
00928       currentPhoto->setThumbnailFilename( QString(saveLocation + "/img/%1/%2_thumb.jpg").arg(subalbumNumber).arg(photoNumber) );
00929       //---------------------------------------
00930       //update progress bar
00931       status->incrementProgress();
00932       qApp->processEvents();
00933       //---------------------------------------   
00934       //move on to next photo in subalbum
00935       currentPhoto = currentPhoto->getNext();
00936       //---------------------------------------
00937     }
00938     //---------------------------------------
00939     //move on to next subalbum
00940     currentSubalbum = currentSubalbum->getNext();  
00941   }
00942 }
00943 //==============================================
00944 void Album::removeStagnantImages()
00945 {
00946   QDir rootDir(saveLocation + "/img/");
00947   //---------------------------------
00948   //remove stagnant photos in existing subalbums
00949   int subalbumNumber=0;
00950   Subalbum* currentSubalbum = firstSubalbum;
00951   while(currentSubalbum != NULL)
00952   {
00953     subalbumNumber++;    
00954 
00955     int i;
00956     for(i=currentSubalbum->getNumPhotos() + 1;
00957         i <= currentSubalbum->getNumLoadedPhotos();
00958         i++)
00959     {
00960       QString thumbString = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(subalbumNumber).arg(i);
00961       QString slideshowString = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(subalbumNumber).arg(i);
00962       QString imageString = QString(saveLocation + "/img/%1/%2.jpg" ).arg(subalbumNumber).arg(i);
00963       rootDir.remove(thumbString);
00964       rootDir.remove(slideshowString);
00965       rootDir.remove(imageString);
00966     }
00967     
00968     //reset number of loaded photos since old photos removed now
00969     currentSubalbum->resetNumLoadedPhotos();
00970     
00971     //move on to next subalbum
00972     currentSubalbum = currentSubalbum->getNext();  
00973   }
00974   //---------------------------------
00975   //remove stagnant subalbums and all their contents
00976   int i;
00977   for(i=numSubalbums+1; i<=numLoadedSubalbums; i++)
00978   {
00979     //get filelist for directory
00980     QDir imageDir(  QString(saveLocation + "/img/%1/").arg(i) );
00981     QStringList list = imageDir.entryList( QDir::Files );
00982     
00983     //remove each file in directory
00984     QStringList::Iterator file;
00985     for ( file = list.begin(); file != list.end(); ++file ) 
00986     {
00987       rootDir.remove( QString(saveLocation + "/img/%1/" + *file).arg(i) );
00988     }
00989     
00990     //remove directory
00991     rootDir.rmdir( QString("%1").arg(i) );
00992     
00993     //remove thumbnail image
00994     rootDir.remove( QString(saveLocation + "/img/%1_thumb.jpg").arg(i) );
00995     
00996     //remove slideshow and thumbnail html pages
00997     rootDir.remove( QString(saveLocation + "/subalbum_%1_thumbs.html").arg(i) );    
00998     rootDir.remove( QString(saveLocation + "/subalbum_%1_slideshow.html").arg(i) );        
00999   }
01000   
01001   //reset number of loaded subalbums since stagnant directories removed now
01002   numLoadedSubalbums = numSubalbums;
01003   //---------------------------------
01004 }  
01005 //==============================================
01006 void Album::exportThemeResources( QString theme )
01007 {
01008   //create resources directory if it doesn't exist
01009   QDir localDir(saveLocation);
01010   localDir.mkdir("resources");
01011   localDir.setPath(saveLocation + "/resources");
01012     
01013   //remove all files in this directory from previous saves with other themes
01014   QStringList list = localDir.entryList( QDir::Files );
01015   QStringList::Iterator file;
01016   for ( file = list.begin(); file != list.end(); ++file ) 
01017   {
01018     localDir.remove( saveLocation + "/resources/" + *file );
01019   }
01020     
01021   //copy resource files over from theme's resources directory
01022   localDir.setPath(THEMES_PATH + theme + "/resources");
01023   list = localDir.entryList( QDir::Files );
01024   for ( file = list.begin(); file != list.end(); ++file ) 
01025   {
01026     copyFile( THEMES_PATH + theme + "/resources/" + *file,
01027                   saveLocation + "/resources/" + *file);
01028   } 
01029 }
01030 //==============================================
01031 bool Album::prevSave()
01032 {
01033   return savedToDisk;
01034 }
01035 //==============================================
01036 bool Album::albumModified()
01037 {
01038   return modified;
01039 }
01040 //==============================================
01041 void Album::syncSubalbumList(SubalbumPreviewWidget* item)
01042 {
01043   //base case, no items
01044   if(item == NULL)
01045   {
01046     firstSubalbum = NULL;
01047     lastSubalbum = NULL;
01048     return;
01049   }
01050   
01051   //set first and last pointers
01052   firstSubalbum = item->getSubalbum();
01053   firstSubalbum->setNext(NULL);
01054   lastSubalbum = firstSubalbum;
01055     
01056   //set all next pointers
01057   while(item->nextItem() != NULL)
01058   {
01059     item->getSubalbum()->setNext( ((SubalbumPreviewWidget*)item->nextItem())->getSubalbum() );
01060     item = (SubalbumPreviewWidget*)item->nextItem();
01061     lastSubalbum = item->getSubalbum();
01062     lastSubalbum->setNext(NULL);
01063   }
01064 }
01065 //==============================================
01066 QString Album::getSaveLocation()
01067 {
01068   return saveLocation;
01069 }
01070 //==============================================
01071 int Album::getNumSubalbums()
01072 {
01073   return numSubalbums;
01074 }
01075 //==============================================
01076 QString Album::getTheme()
01077 {
01078   return QString(theme);
01079 }
01080 //==============================================
01081 void Album::setModified(bool val)
01082 {
01083   modified = val;
01084 }
01085 //==============================================

Generated on Thu Nov 13 00:10:53 2003 for AlbumShaper by doxygen 1.3.4