00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qimage.h>
00013 #include <qpixmap.h>
00014 #include <qstring.h>
00015 #include <time.h>
00016 #include <qfile.h>
00017 #include <qfileinfo.h>
00018 #include <qtextstream.h>
00019 #include <qdom.h>
00020 #include <qdir.h>
00021 #include <qapplication.h>
00022 #include <qregexp.h>
00023 #include <qdatetime.h>
00024 #include <math.h>
00025
00026
00027 #include "album.h"
00028 #include "subalbum.h"
00029 #include "photo.h"
00030 #include "tools/imageTools.h"
00031 #include "tools/fileTools.h"
00032 #include "tools/md5.h"
00033 #include "tools/xmlTools.h"
00034 #include "../config.h"
00035 #include "../gui/subalbumPreviewWidget.h"
00036 #include "../gui/statusWidget.h"
00037
00038
00040 Album::Album( QString tmpDir, bool createSubalbum )
00041 {
00042
00043 name = "";
00044 description ="";
00045 author = "";
00046 theme = "Slick";
00047 this->tmpDir = tmpDir;
00048
00049
00050 smallRepresentativeImage = NULL;
00051 largeRepresentativeImage = NULL;
00052
00053
00054 firstSubalbum = NULL;
00055 lastSubalbum = NULL;
00056
00057
00058 updateCreationDate();
00059 updateModificationDate();
00060
00061
00062 numSubalbums = 0;
00063 numLoadedSubalbums = 0;
00064
00065
00066 savedToDisk = false;
00067
00068
00069 saveLocation = getTmpDir();
00070
00071 if(createSubalbum)
00072 {
00073 Subalbum* s = new Subalbum( this, 1 );
00074 appendSubalbum( s );
00075 }
00076
00077
00078 modified = false;
00079
00080 nextUniqueID = 0;
00081 }
00082
00083 Album::~Album()
00084 {
00085
00086 delete smallRepresentativeImage;
00087 delete largeRepresentativeImage;
00088
00089
00090 Subalbum* current = firstSubalbum;
00091 Subalbum* temp;
00092 while(current != NULL)
00093 {
00094 temp = current->getNext();
00095 delete current;
00096 current = temp;
00097 }
00098
00099
00100 if(!tmpDir.isNull())
00101 {
00102 QDir oldTmpDir(tmpDir);
00103 QString tmpDirName = oldTmpDir.dirName();
00104 QStringList strLst = oldTmpDir.entryList();
00105 QStringList::iterator it;
00106 for(it = strLst.begin(); it != strLst.end(); it++)
00107 {
00108 oldTmpDir.remove(tmpDir + "/" + *it);
00109 }
00110 oldTmpDir.cdUp();
00111 oldTmpDir.rmdir( tmpDirName );
00112 }
00113 }
00114
00115 int Album::getModificationYear() { return modificationYear; }
00116 int Album::getModificationMonth() { return modificationMonth; }
00117 int Album::getModificationDay() { return modificationDay; }
00118
00119 int Album::getCreationYear() { return creationYear; }
00120 int Album::getCreationMonth() { return creationMonth; }
00121 int Album::getCreationDay() { return creationDay; }
00122
00123 QString Album::getName() { return QString(name); }
00124 QString Album::getDescription() { return QString(description); }
00125 QString Album::getAuthor() { return QString(author); }
00126
00127 QPixmap* Album::getRepresentativeImage(int size)
00128 {
00129 if(size == SMALL) return smallRepresentativeImage;
00130 else if(size == LARGE) return largeRepresentativeImage;
00131 else return NULL;
00132 }
00133
00134 Subalbum* Album::getFirstSubalbum() { return firstSubalbum; }
00135 Subalbum* Album::getLastSubalbum() { return lastSubalbum; }
00136
00137 bool Album::prevSave() { return savedToDisk; }
00138 bool Album::albumModified() { return modified; }
00139
00140 QString Album::getSaveLocation() { return saveLocation; }
00141 QString Album::getTmpDir() { return tmpDir; }
00142 QString Album::getTheme() { return theme; }
00143 int Album::getNumSubalbums() { return numSubalbums; }
00144
00145 int Album::getNumPhotos()
00146 {
00147
00148 int numPhotos = 0;
00149 Subalbum* curr = firstSubalbum;
00150 while(curr != NULL)
00151 {
00152 numPhotos+= curr->getNumPhotos();
00153 curr = curr->getNext();
00154 }
00155 return numPhotos;
00156 }
00157
00158 void Album::setName(QString val)
00159 {
00160 if(name != val)
00161 {
00162 name = val;
00163 modified = true;
00164 }
00165 }
00166
00167 void Album::setDescription(QString val)
00168 {
00169 if(description != val)
00170 {
00171 description = val;
00172 modified = true;
00173 }
00174 }
00175
00176 void Album::setAuthor(QString val)
00177 {
00178 if(author != val)
00179 {
00180 author = val;
00181 modified = true;
00182 }
00183 }
00184
00185 void Album::setRepresentativeImages(QString imageFilename)
00186 {
00187
00188 delete smallRepresentativeImage;
00189 delete largeRepresentativeImage;
00190
00191
00192 if(imageFilename.isNull())
00193 {
00194 smallRepresentativeImage = NULL;
00195 largeRepresentativeImage = NULL;
00196 }
00197 else
00198 {
00199
00200 int imageWidth, imageHeight;
00201 getImageSize( imageFilename, imageWidth, imageHeight );
00202
00203 int smallRepWidth = 0;
00204 int smallRepHeight = 0;
00205 int largeRepWidth = 0;
00206 int largeRepHeight = 0;
00207 calcScaledImageDimensions( imageWidth, imageHeight,
00208 107, REP_IMAGE_HEIGHT,
00209 smallRepWidth, smallRepHeight);
00210 calcScaledImageDimensions( imageWidth, imageHeight,
00211 500, 320,
00212 largeRepWidth, largeRepHeight);
00213
00214
00215
00216
00217 QImage thumbnailSmall;
00218 scaleImage( imageFilename, thumbnailSmall, smallRepWidth, smallRepHeight );
00219 smallRepresentativeImage = new QPixmap( thumbnailSmall.width(), thumbnailSmall.height() );
00220 smallRepresentativeImage->convertFromImage( thumbnailSmall );
00221
00222
00223 QImage thumbnailLarge;
00224 scaleImage( imageFilename, thumbnailLarge, largeRepWidth, largeRepHeight );
00225 largeRepresentativeImage = new QPixmap( thumbnailLarge.width(), thumbnailLarge.height() );
00226 largeRepresentativeImage->convertFromImage( thumbnailLarge );
00227 }
00228
00229
00230 modified = true;
00231 }
00232
00233 void Album::appendSubalbum(Subalbum* val)
00234 {
00235
00236 if( val == NULL) return;
00237
00238
00239 if(firstSubalbum == NULL)
00240 {
00241 firstSubalbum = val;
00242 lastSubalbum = val;
00243 }
00244
00245 else
00246 {
00247 lastSubalbum->setNext( val );
00248 val->setPrev( lastSubalbum );
00249 lastSubalbum = val;
00250 }
00251
00252 numSubalbums++;
00253 modified = true;
00254 }
00255
00256 void Album::removeSubalbum(Subalbum* val)
00257 {
00258
00259 if( val == NULL) return;
00260
00261
00262 if( val == firstSubalbum ) firstSubalbum = val->getNext();
00263 if( val == lastSubalbum ) lastSubalbum = val->getPrev();
00264
00265
00266 if( val->getPrev() != NULL ) val->getPrev()->setNext( val->getNext() );
00267 if( val->getNext() != NULL ) val->getNext()->setPrev( val->getPrev() );
00268
00269
00270 delete val;
00271 val = NULL;
00272 numSubalbums--;
00273 modified = true;
00274 }
00275
00276 void Album::updateCreationDate()
00277 {
00278
00279 QDate date = QDate::currentDate();
00280 creationYear = date.year();
00281 creationMonth = date.month();
00282 creationDay = date.day();
00283 }
00284
00285 void Album::updateModificationDate()
00286 {
00287
00288 QDate date = QDate::currentDate();
00289 modificationYear = date.year();
00290 modificationMonth = date.month();
00291 modificationDay = date.day();
00292 }
00293
00294 int Album::importFromDisk(StatusWidget* status, QString fileName, bool disableCheckPhotoMods)
00295 {
00296
00297 updateXML( QFileInfo(fileName).dirPath(TRUE) );
00298
00299
00300 QFile albumFile( fileName );
00301
00302
00303 if( !albumFile.open( IO_ReadOnly ) )
00304 return ALBUM_READ_ERROR;
00305
00306
00307 QDomDocument albumDom;
00308 if( !albumDom.setContent( &albumFile ) )
00309 return ALBUM_XML_ERROR;
00310
00311
00312 albumFile.close();
00313
00314
00315 QString rootDir = QFileInfo(albumFile).dirPath(TRUE);
00316 saveLocation = rootDir + "/img";
00317
00318
00319 QImage repImage(rootDir + "/img/album.jpg");
00320 if(!repImage.isNull())
00321 {
00322 setRepresentativeImages( rootDir + "/img/album.jpg");
00323 }
00324
00325
00326 int numPhotos = 0;
00327 QDomElement root = albumDom.documentElement();
00328 QDomNode node = root.firstChild();
00329 while( !node.isNull() )
00330 {
00331 if( node.isElement() && node.nodeName() == "subalbum" )
00332 {
00333 QDomNode childNode = node.firstChild();
00334 while( !childNode.isNull() )
00335 {
00336 if( childNode.isElement() && childNode.nodeName() == "photo" )
00337 numPhotos++;
00338 childNode = childNode.nextSibling();
00339 }
00340 }
00341 node = node.nextSibling();
00342 }
00343
00344
00345 status->showProgressBar( StatusWidget::tr("Loading:"), numPhotos );
00346 qApp->processEvents();
00347
00348 int subalbumNum = 0;
00349
00350
00351 root = albumDom.documentElement();
00352 node = root.firstChild();
00353 QDomText val;
00354 while( !node.isNull() )
00355 {
00356
00357
00358 if( node.isElement() && node.nodeName() == "name" )
00359 {
00360 val = node.firstChild().toText();
00361 if(!val.isNull())
00362 name = val.nodeValue();
00363 name.replace("\\"","\"");
00364 }
00365
00366
00367 else if( node.isElement() && node.nodeName() == "description" )
00368 {
00369 val = node.firstChild().toText();
00370 if(!val.isNull())
00371 description = val.nodeValue();
00372 description.replace("\\"","\"");
00373 }
00374
00375
00376 else if( node.isElement() && node.nodeName() == "author" )
00377 {
00378 val = node.firstChild().toText();
00379 if(!val.isNull())
00380 author = val.nodeValue();
00381 author.replace("\\"","\"");
00382 }
00383
00384
00385 else if( node.isElement() && node.nodeName() == "theme" )
00386 {
00387 val = node.firstChild().toText();
00388 if(!val.isNull())
00389 theme = val.nodeValue();
00390 theme.replace("\\"","\"");
00391 }
00392
00393
00394 else if( node.isElement() && node.nodeName() == "created" )
00395 {
00396 val = node.firstChild().toText();
00397
00398
00399 QStringList vals = QStringList::split( QRegExp(" "), val.nodeValue() );
00400 int i=0;
00401 int intVals[3];
00402 QStringList::Iterator it;
00403 for ( it = vals.begin(); it != vals.end(); ++it )
00404 {
00405 intVals[i] = QString(*it).toInt();
00406 i++;
00407
00408
00409 if(i > 2)
00410 break;
00411 }
00412 creationYear = intVals[0];
00413 creationMonth = intVals[1];
00414 creationDay = intVals[2];
00415 }
00416
00417
00418 else if( node.isElement() && node.nodeName() == "subalbum" )
00419 {
00420
00421 subalbumNum++;
00422
00423
00424 Subalbum* salbum = new Subalbum(this, numSubalbums+1);
00425
00426
00427 salbum->importFromDisk( &node, subalbumNum, status, (rootDir + "/"), disableCheckPhotoMods );
00428
00429
00430 appendSubalbum(salbum);
00431 }
00432
00433
00434 node = node.nextSibling();
00435
00436 }
00437
00438
00439 numLoadedSubalbums = numSubalbums;
00440
00441
00442 status->setStatus( "Album loaded." );
00443
00444
00445 saveLocation = rootDir;
00446 savedToDisk = true;
00447
00448 return ALBUM_LOADED;
00449 }
00450
00451 int Album::exportToDisk(StatusWidget* status, QString dirName, QString themeName)
00452 {
00453
00454
00455 bool forceSave = true;
00456
00457 if(saveLocation == dirName)
00458 forceSave = false;
00459
00460
00461 QString oldSaveLocation = saveLocation;
00462 QString oldTheme = theme;
00463
00464
00465 saveLocation = dirName;
00466 theme = themeName;
00467 int result = exportToDisk(status, forceSave);
00468
00469
00470 if(result != ALBUM_EXPORTED)
00471 {
00472 saveLocation = oldSaveLocation;
00473 theme = oldTheme;
00474 }
00475
00476 else
00477 {
00478
00479 QDir oldTmpDir(tmpDir);
00480 QString tmpDirName = oldTmpDir.dirName();
00481 QStringList strLst = oldTmpDir.entryList();
00482 QStringList::iterator it;
00483 for(it = strLst.begin(); it != strLst.end(); it++)
00484 {
00485 oldTmpDir.remove( tmpDir + "/" + *it);
00486 }
00487
00488 oldTmpDir.cdUp();
00489 oldTmpDir.rmdir( tmpDirName );
00490
00491
00492 QDir saveDir( saveLocation );
00493 if(!saveDir.exists( "tmp" ))
00494 saveDir.mkdir( "tmp" );
00495 tmpDir = saveLocation + "/tmp";
00496
00497
00498 nextUniqueID = 0;
00499 }
00500
00501
00502 return result;
00503 }
00504
00505 int Album::exportToDisk(StatusWidget* status, bool forceSave)
00506 {
00507
00508
00509 QDir localDir(saveLocation);
00510
00511 localDir.mkdir("img");
00512
00513 localDir.setPath(saveLocation + "/img");
00514
00515
00516
00517 localDir.mkdir( "0" );
00518
00519
00520 Subalbum* current = firstSubalbum;
00521 int collectionNum = 0;
00522 while(current != NULL)
00523 {
00524 collectionNum++;
00525 QString dirName = QString("%1") .arg( collectionNum );
00526 localDir.mkdir(dirName);
00527 current = current->getNext();
00528 }
00529
00530
00531
00532
00533 int totalPhotos=0;
00534 current = firstSubalbum;
00535 while(current != NULL)
00536 {
00537 totalPhotos+=current->getNumPhotos();
00538 current = current->getNext();
00539 }
00540
00541
00542 status->showProgressBar( StatusWidget::tr("Saving:"), 4*totalPhotos );
00543 qApp->processEvents();
00544
00545
00546 exportThemeResources( theme );
00547
00548
00549 exportTopLevelImages();
00550
00551
00552 exportSubalbumImages(status, forceSave);
00553
00554
00555 removeStagnantOrigFiles(status);
00556
00557
00558 reorderSubalbumImages(status);
00559
00560
00561 current = firstSubalbum;
00562 int n=0;
00563 while(current !=NULL)
00564 {
00565 n++;
00566 current->setSubalbumNumber(n);
00567 current = current->getNext();
00568 }
00569
00570
00571 QDir rootDir(saveLocation + "/img/");
00572 rootDir.rmdir( "0" );
00573
00574
00575 removeStagnantImages();
00576
00577
00578 localDir.setPath(saveLocation);
00579 QStringList list = localDir.entryList( QDir::Files );
00580 QStringList::Iterator file;
00581 for ( file = list.begin(); file != list.end(); ++file )
00582 {
00583 if( (*file).endsWith(".html") || (*file).endsWith(".htm") )
00584 localDir.remove( saveLocation + "/" + *file );
00585 }
00586
00587
00588 int result = exportToXML(status, saveLocation);
00589 if(result != ALBUM_EXPORTED) { return result; }
00590
00591
00592 transformXMLtoHTML( saveLocation, theme, false );
00593
00594
00595
00596 QDir tmpDirHandle( getTmpDir() );
00597 QStringList strLst = tmpDirHandle.entryList();
00598 QStringList::iterator it;
00599 for(it = strLst.begin(); it != strLst.end(); it++)
00600 {
00601 tmpDirHandle.remove( getTmpDir() + "/" + *it);
00602 }
00603
00604 savedToDisk = true;
00605
00606
00607 modified = false;
00608
00609
00610 status->setStatus( "Album saved." );
00611
00612 return ALBUM_EXPORTED;
00613 }
00614
00615 int Album::exportCompressedWebAlbum(StatusWidget* status,
00616 QString exportLocation,
00617 QString exportMessage)
00618 {
00619
00620
00621 QDir localDir(exportLocation);
00622 localDir.mkdir("img");
00623 localDir.setPath(exportLocation + "/img");
00624
00625
00626 if(getRepresentativeImage(LARGE) != NULL)
00627 { getRepresentativeImage(LARGE)->save(exportLocation + "/img/album.jpg", "JPEG", 95); }
00628 else
00629 { localDir.remove(exportLocation + "/img/album.jpg"); }
00630
00631 int numPhotos = getNumPhotos();
00632 int photosLeft = numPhotos;
00633 int updateInverval = numPhotos / 50;
00634 int updateCount = 0;
00635
00636
00637 Subalbum* curCollection = firstSubalbum;
00638 int collectionNum=1;
00639 while(curCollection != NULL)
00640 {
00641 QString collectionDir = QString("%1").arg( collectionNum );
00642 localDir.mkdir( collectionDir );
00643
00644
00645 QString collectionThumbFilename = QString(exportLocation + "/img/%1_thumb.jpg" ).arg(collectionNum);
00646 if(curCollection->getRepresentativeImage(LARGE) != NULL )
00647 { curCollection->getRepresentativeImage(LARGE)->save( collectionThumbFilename, "JPEG", 95); }
00648 else
00649 { localDir.remove( collectionThumbFilename ); }
00650
00651
00652 Photo* curPhoto = curCollection->getFirst();
00653 int photoNum = 1;
00654 while(curPhoto != NULL)
00655 {
00656
00657 status->updateProgress( numPhotos - photosLeft, exportMessage.arg( photosLeft ) );
00658
00659
00660 updateCount++;
00661 if(updateCount > updateInverval)
00662 {
00663 updateCount = 0;
00664 qApp->processEvents();
00665 }
00666
00667
00668 QString newFilePath = QDir::convertSeparators( exportLocation + "/img/" +
00669 collectionDir + "/" +
00670 QString("%1").arg(photoNum) );
00671
00672 copyFile( curPhoto->getSlideshowFilename(), newFilePath + "_slideshow.jpg" );
00673 copyFile( curPhoto->getThumbnailFilename(), newFilePath + "_thumb.jpg" );
00674
00675 curPhoto = curPhoto->getNext();
00676 photoNum++;
00677 photosLeft--;
00678 }
00679
00680 curCollection = curCollection->getNext();
00681 collectionNum++;
00682 }
00683
00684
00685 QStringList fileList;
00686 QStringList::Iterator file;
00687
00688
00689 localDir.setPath(exportLocation);
00690 localDir.mkdir("resources");
00691
00692
00693 localDir.setPath(exportLocation + "/resources");
00694 fileList = localDir.entryList( QDir::Files );
00695 for ( file = fileList.begin(); file != fileList.end(); ++file )
00696 { localDir.remove( exportLocation + "/resources/" + *file ); }
00697
00698
00699 localDir.setPath(THEMES_PATH + theme + "/resources");
00700 fileList = localDir.entryList( QDir::Files );
00701 for ( file = fileList.begin(); file != fileList.end(); ++file )
00702 { copyFile( THEMES_PATH + theme + "/resources/" + *file, exportLocation + "/resources/" + *file); }
00703
00704
00705 exportToXML(status, exportLocation);
00706
00707
00708 localDir.setPath(exportLocation);
00709 fileList = localDir.entryList( QDir::Files );
00710 for ( file = fileList.begin(); file != fileList.end(); ++file )
00711 {
00712 if( (*file).endsWith(".html") || (*file).endsWith(".htm") )
00713 localDir.remove( exportLocation + "/" + *file );
00714 }
00715
00716
00717 transformXMLtoHTML( exportLocation, theme, true );
00718
00719
00720 localDir.remove( exportLocation + "/Album.xml" );
00721
00722 return ALBUM_EXPORTED;
00723 }
00724
00725 int Album::exportLargeImages(StatusWidget* status, QString exportPath, QString exportMessage)
00726 {
00727
00728 uint collectionDigits = (uint) (1 + log( (double) getNumSubalbums() ) / log( 10.0 ) );
00729
00730
00731
00732 int mostPhotos = 0;
00733 Subalbum* curCollection = getFirstSubalbum();
00734 while(curCollection != NULL )
00735 {
00736 mostPhotos = QMAX( mostPhotos, curCollection->getNumPhotos() );
00737 curCollection = curCollection->getNext();
00738 }
00739 uint photoDigits = (uint) ( 1 + log( (double) mostPhotos ) / log( 10.0 ) );
00740
00741
00742 int numPhotos = getNumPhotos();
00743 int photosLeft = numPhotos;
00744
00745 int collectionNum = 1;
00746 curCollection = getFirstSubalbum();
00747
00748 int updateInverval = numPhotos / 50;
00749 int updateCount = 0;
00750
00751 while(curCollection != NULL )
00752 {
00753
00754 QString collectionString = QString("%1").arg(collectionNum);
00755 while(collectionString.length() < collectionDigits)
00756 { collectionString = "0" + collectionString; }
00757
00758
00759 int photoNum = 1;
00760 Photo* curPhoto = curCollection->getFirst();
00761 while(curPhoto != NULL)
00762 {
00763
00764 status->updateProgress( numPhotos - photosLeft, exportMessage.arg( photosLeft ) );
00765
00766
00767 updateCount++;
00768 if(updateCount > updateInverval)
00769 {
00770 updateCount = 0;
00771 qApp->processEvents();
00772 }
00773
00774
00775 QString photoString = QString("%1").arg(photoNum);
00776 while(photoString.length() < photoDigits)
00777 { photoString = "0" + photoString; }
00778
00779
00780 QString newFilePath = QDir::convertSeparators( exportPath + "/" + collectionString +
00781 "_" + photoString + ".jpg" );
00782
00783 copyFile( curPhoto->getImageFilename(), newFilePath );
00784
00785
00786 photosLeft--;
00787 curPhoto = curPhoto->getNext();
00788 photoNum++;
00789
00790 }
00791
00792
00793 curCollection = curCollection->getNext();
00794 collectionNum++;
00795 }
00796
00797 return ALBUM_EXPORTED;
00798 }
00799
00800 int Album::exportToXML(StatusWidget* status, QString exportPath)
00801 {
00802
00803 updateModificationDate();
00804
00805
00806 QFile file( exportPath + "/Album.xml" );
00807 if(file.open(IO_WriteOnly))
00808 {
00809
00810 QTextStream stream;
00811 stream.setDevice( &file );
00812 stream.setEncoding( QTextStream::UnicodeUTF8 );
00813
00814
00815 stream << "<?xml version=\"1.0\"?>\n";
00816 stream << "<album version=\"1.1\">\n";
00817 stream << " <name>" << fixXMLString(name) << "</name>\n";
00818 stream << " <description>" << fixXMLString(description) << "</description>\n";
00819 stream << " <author>" << fixXMLString(author) << "</author>\n";
00820 stream << " <created>" << creationYear << " " << creationMonth << " " << creationDay << "</created>\n";
00821 stream << " <modified>" << modificationYear << " " << modificationMonth << " " << modificationDay << "</modified>\n";
00822 stream << " <theme>" << theme << "</theme>\n";
00823 stream << " <thumbnailDimensions>" << THUMBNAIL_WIDTH << " " << THUMBNAIL_HEIGHT << "</thumbnailDimensions>\n";
00824 stream << " <slideshowDimensions>" << SLIDESHOW_WIDTH << " " << SLIDESHOW_HEIGHT << "</slideshowDimensions>\n";
00825
00826
00827 if(getRepresentativeImage(LARGE) != NULL )
00828 {
00829 stream << " <thumb path=\"img/album.jpg\"/>\n";
00830 }
00831
00832
00833 Subalbum* current = firstSubalbum;
00834 while(current != NULL)
00835 {
00836 current->exportToXML(status, stream);
00837 current = current->getNext();
00838 }
00839
00840
00841 stream << "</album>\n";
00842 file.close();
00843
00844 return ALBUM_EXPORTED;
00845 }
00846 else
00847 {
00848 return ALBUM_ERROR_OPEN_FILE;
00849 }
00850 }
00851
00852 void Album::exportTopLevelImages()
00853 {
00854
00855 if(getRepresentativeImage(LARGE) != NULL)
00856 {
00857 getRepresentativeImage(LARGE)->save(saveLocation + "/img/album.jpg", "JPEG", 95);
00858 }
00859
00860 else
00861 {
00862 QDir rootDir(saveLocation + "/img/");
00863 rootDir.remove(saveLocation + "/img/album.jpg");
00864 }
00865
00866
00867 int n=0;
00868 Subalbum* current = firstSubalbum;
00869 while(current != NULL)
00870 {
00871 n++;
00872
00873 if(current->getRepresentativeImage(LARGE) != NULL )
00874 {
00875 QString fileName = QString(saveLocation + "/img/%1_thumb.jpg" ).arg(n);
00876 current->getRepresentativeImage(LARGE)->save(fileName, "JPEG", 95);
00877 }
00878
00879 else
00880 {
00881 QDir rootDir(saveLocation + "/img/");
00882 rootDir.remove( saveLocation + QString("/img/%1_thumb.jpg").arg(n) );
00883 }
00884 current = current->getNext();
00885 }
00886 }
00887
00888 void Album::exportSubalbumImages(StatusWidget* status, bool forceSave)
00889 {
00890
00891 int subalbumNumber=0;
00892 Subalbum* currentSubalbum = firstSubalbum;
00893 while(currentSubalbum != NULL)
00894 {
00895 subalbumNumber++;
00896
00897
00898 int photoNumber=0;
00899 Photo* currentPhoto = currentSubalbum->getFirst();
00900 while(currentPhoto != NULL)
00901 {
00902 photoNumber++;
00903
00904
00905 if( !forceSave && !currentPhoto->getNeedsSavingVal() )
00906 {
00907 currentPhoto = currentPhoto->getNext();
00908 status->incrementProgress();
00909 qApp->processEvents();
00910 continue;
00911 }
00912
00913
00914 int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
00915 int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
00916
00917
00918 QString oldName = currentPhoto->getThumbnailFilename();
00919 QString newName = QString(saveLocation + "/img/%1/%2_thumb.jpg" )
00920 .arg(initSubalbumNumber).arg(initPhotoNumber);
00921
00922
00923 if( currentPhoto->getNeedsSavingVal() ) { moveFile( oldName, newName ); }
00924
00925
00926 else { copyFile(oldName, newName); }
00927
00928
00929 std::ifstream thumbnailFile( QFile::encodeName(newName) );
00930 if(thumbnailFile.is_open())
00931 {
00932 currentPhoto->setThumbnailChecksum( getMD5(thumbnailFile) );
00933 thumbnailFile.close();
00934 }
00935
00936
00937 oldName = currentPhoto->getSlideshowFilename();
00938 newName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" )
00939 .arg(initSubalbumNumber).arg(initPhotoNumber);
00940
00941
00942 if( currentPhoto->getNeedsSavingVal() ) { moveFile( oldName, newName ); }
00943
00944
00945 else { copyFile(oldName, newName); }
00946
00947
00948 std::ifstream slideshowFile( QFile::encodeName(newName) );
00949 if(slideshowFile.is_open())
00950 {
00951 currentPhoto->setSlideshowChecksum( getMD5(slideshowFile) );
00952 slideshowFile.close();
00953 }
00954
00955
00956 oldName = currentPhoto->getImageFilename();
00957 newName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
00958
00959
00960 if( currentPhoto->getNeedsSavingVal() )
00961 {
00962 QString tempOrigName = getTmpDir() + QString("/%1_%2_orig.jpg")
00963 .arg(initSubalbumNumber).arg(initPhotoNumber);
00964
00965 QString finalOrigName = QString(saveLocation + "/img/%1/%2_orig.jpg" )
00966 .arg(initSubalbumNumber).arg(initPhotoNumber);
00967
00973 QDir tmpDir;
00974 if( !currentPhoto->getRecentlyReverted() &&
00975 tmpDir.exists(newName) &&
00976 !tmpDir.exists(finalOrigName) )
00977 {
00978 moveFile( newName, finalOrigName );
00979 }
00984 else if( !currentPhoto->getRecentlyReverted() &&
00985 !tmpDir.exists(newName) &&
00986 tmpDir.exists(tempOrigName) )
00987 {
00988 moveFile( tempOrigName, finalOrigName );
00989 }
00990
00992 moveFile( oldName, newName );
00993 }
00994
00995
00996
00997 else
00998 {
00999
01000 copyFile( oldName, newName );
01001
01003
01004 QDir tmpDir;
01005
01006 QString tempOrigName = getTmpDir() + QString("/%1_%2_orig.jpg")
01007 .arg(initSubalbumNumber).arg(initPhotoNumber);
01008
01009 QString curOrigName = currentPhoto->getImageFilename();
01010 curOrigName.truncate( curOrigName.length() - 4 );
01011 curOrigName = curOrigName + "_orig.jpg";
01012
01013 QString finalOrigName = QString(saveLocation + "/img/%1/%2_orig.jpg" )
01014 .arg(initSubalbumNumber).arg(initPhotoNumber);
01015
01016
01017 if( !currentPhoto->getRecentlyReverted() )
01018 {
01019
01020
01021 if( !currentPhoto->getEverSaved() &&
01022 tmpDir.exists( tempOrigName ) )
01023 {
01024 copyFile( tempOrigName, finalOrigName );
01025 }
01026
01027
01028 else if( currentPhoto->getEverSaved() &&
01029 tmpDir.exists( curOrigName ) )
01030 {
01031 copyFile( curOrigName, finalOrigName );
01032 }
01033 }
01035 }
01036
01037
01038 std::ifstream imageFile( QFile::encodeName(newName) );
01039 if(imageFile.is_open())
01040 {
01041 currentPhoto->setImageChecksum( getMD5(imageFile) );
01042 imageFile.close();
01043 }
01044
01045
01046 currentPhoto->setImageFilename
01047 ( QString(saveLocation + "/img/%1/%2.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
01048
01049 currentPhoto->setSlideshowFilename
01050 ( QString(saveLocation + "/img/%1/%2_slideshow.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
01051
01052 currentPhoto->setThumbnailFilename
01053 ( QString(saveLocation + "/img/%1/%2_thumb.jpg").arg(initSubalbumNumber).arg(initPhotoNumber) );
01054
01055
01056 currentPhoto->setNeedsSavingVal(false);
01057 currentPhoto->setEverSaved(true);
01058
01059
01060 status->incrementProgress();
01061 qApp->processEvents();
01062
01063
01064 currentPhoto = currentPhoto->getNext();
01065
01066 }
01067
01068
01069 currentSubalbum = currentSubalbum->getNext();
01070 }
01071 }
01072
01073 void Album::removeStagnantOrigFiles(StatusWidget* status)
01074 {
01075 QDir tmpDir;
01076
01077
01078 Subalbum* currentSubalbum = firstSubalbum;
01079 while(currentSubalbum != NULL)
01080 {
01081
01082 Photo* currentPhoto = currentSubalbum->getFirst();
01083 while(currentPhoto != NULL)
01084 {
01085
01086 if(currentPhoto->getRecentlyReverted() )
01087 {
01088 tmpDir.remove( currentPhoto->originalImageFilename() );
01089 currentPhoto->setRecentlyReverted( false );
01090 }
01091
01092
01093 currentPhoto = currentPhoto->getNext();
01094 status->incrementProgress();
01095 qApp->processEvents();
01096 }
01097
01098
01099 currentSubalbum = currentSubalbum->getNext();
01100 }
01101 }
01102
01103 void Album::reorderSubalbumImages(StatusWidget* status)
01104 {
01105
01106
01107
01108
01109
01110
01111
01112 QDir tmpDir;
01113 int subalbumNumber=0;
01114 Subalbum* currentSubalbum = firstSubalbum;
01115 while(currentSubalbum != NULL)
01116 {
01117 subalbumNumber++;
01118
01119
01120 int photoNumber=0;
01121 Photo* currentPhoto = currentSubalbum->getFirst();
01122 while(currentPhoto != NULL)
01123 {
01124 photoNumber++;
01125 int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
01126 int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
01127
01128
01129 if( initPhotoNumber != photoNumber || initSubalbumNumber != subalbumNumber)
01130 {
01131 QString oldName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01132 QString newName = QString(saveLocation + "/img/%1/%2_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01133 moveFile( oldName, newName );
01134
01135 oldName = QString(saveLocation + "/img/%1/%2_orig.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01136 newName = QString(saveLocation + "/img/%1/%2_orig_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01137 if(tmpDir.exists(oldName) ) { moveFile( oldName, newName ); }
01138
01139 oldName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01140 newName = QString(saveLocation + "/img/%1/%2_slideshow_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01141 moveFile( oldName, newName );
01142
01143 oldName = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01144 newName = QString(saveLocation + "/img/%1/%2_thumb_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01145 moveFile( oldName, newName );
01146 }
01147
01148
01149 currentPhoto = currentPhoto->getNext();
01150 status->incrementProgress();
01151 qApp->processEvents();
01152 }
01153
01154
01155 currentSubalbum = currentSubalbum->getNext();
01156 }
01157
01158
01159
01160
01161
01162
01163
01164
01165 subalbumNumber=0;
01166 currentSubalbum = firstSubalbum;
01167 while(currentSubalbum != NULL)
01168 {
01169 subalbumNumber++;
01170
01171
01172 int photoNumber=0;
01173 Photo* currentPhoto = currentSubalbum->getFirst();
01174 while(currentPhoto != NULL)
01175 {
01176 photoNumber++;
01177 int initPhotoNumber = currentPhoto->getInitialPhotoNumber();
01178 int initSubalbumNumber = currentPhoto->getInitialSubalbumNumber();
01179
01180
01181 if( initPhotoNumber != photoNumber || initSubalbumNumber != subalbumNumber)
01182 {
01183 QString oldName = QString(saveLocation + "/img/%1/%2_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01184 QString newName = QString(saveLocation + "/img/%1/%2.jpg" ).arg(subalbumNumber).arg(photoNumber);
01185 moveFile( oldName, newName );
01186
01187 oldName = QString(saveLocation + "/img/%1/%2_orig_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01188 newName = QString(saveLocation + "/img/%1/%2_orig.jpg" ).arg(subalbumNumber).arg(photoNumber);
01189 if(tmpDir.exists(oldName) ) { moveFile( oldName, newName ); }
01190
01191 oldName = QString(saveLocation + "/img/%1/%2_slideshow_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01192 newName = QString(saveLocation + "/img/%1/%2_slideshow.jpg" ).arg(subalbumNumber).arg(photoNumber);
01193 moveFile( oldName, newName );
01194
01195 oldName = QString(saveLocation + "/img/%1/%2_thumb_moved.jpg" ).arg(initSubalbumNumber).arg(initPhotoNumber);
01196 newName = QString(saveLocation + "/img/%1/%2_thumb.jpg" ).arg(subalbumNumber).arg(photoNumber);
01197 moveFile( oldName, newName );
01198
01199
01200 currentPhoto->setInitialPhotoNumber(photoNumber);
01201 currentPhoto->setInitialSubalbumNumber(subalbumNumber);
01202 currentPhoto->setImageFilename( QString(saveLocation + "/img/%1/%2.jpg").
01203 arg(subalbumNumber).arg(photoNumber) );
01204 currentPhoto->setSlideshowFilename( QString(saveLocation + "/img/%1/%2_slideshow.jpg").
01205 arg(subalbumNumber).arg(photoNumber) );
01206 currentPhoto->setThumbnailFilename( QString(saveLocation + "/img/%1/%2_thumb.jpg").
01207 arg(subalbumNumber).arg(photoNumber) );
01208 }
01209
01210
01211 currentPhoto = currentPhoto->getNext();
01212 status->incrementProgress();
01213 qApp->processEvents();
01214 }
01215
01216
01217 currentSubalbum = currentSubalbum->getNext();
01218 }
01219 }
01220
01221 void Album::removeStagnantImages()
01222 {
01223 QDir rootDir(saveLocation + "/img/");
01224
01225
01226 int subalbumNumber=0;
01227 Subalbum* currentSubalbum = firstSubalbum;
01228 while(currentSubalbum != NULL)
01229 {
01230 subalbumNumber++;
01231
01232
01233
01234 int photoNum = currentSubalbum->getNumPhotos()+1;
01235 while(true)
01236 {
01237 QString imageString = QString(saveLocation + "/img/%1/%2.jpg").arg(subalbumNumber).arg(photoNum);
01238 QString origString = QString(saveLocation + "/img/%1/%2_orig.jpg").arg(subalbumNumber).arg(photoNum);
01239 QString slideshowString = QString(saveLocation + "/img/%1/%2_slideshow.jpg").arg(subalbumNumber).arg(photoNum);
01240 QString thumbString = QString(saveLocation + "/img/%1/%2_thumb.jpg").arg(subalbumNumber).arg(photoNum);
01241
01242
01243
01244
01245 if( !rootDir.exists(imageString) && !rootDir.exists(origString) &&
01246 !rootDir.exists(slideshowString) && !rootDir.exists(thumbString) )
01247 break;
01248
01249 else
01250 {
01251 rootDir.remove( imageString );
01252 rootDir.remove( origString );
01253 rootDir.remove( slideshowString );
01254 rootDir.remove( thumbString );
01255 photoNum++;
01256 }
01257 }
01258
01259
01260 currentSubalbum->resetNumLoadedPhotos();
01261
01262
01263 currentSubalbum = currentSubalbum->getNext();
01264 }
01265
01266
01267 subalbumNumber = numSubalbums+1;
01268 while(true)
01269 {
01270
01271 QString imageDirString = QString(saveLocation + "/img/%1/").arg(subalbumNumber);
01272 if( !rootDir.exists(imageDirString) )
01273 break;
01274
01275
01276 QDir imageDir( imageDirString );
01277 QStringList list = imageDir.entryList( QDir::Files );
01278
01279
01280 QStringList::Iterator file;
01281 for ( file = list.begin(); file != list.end(); ++file )
01282 { rootDir.remove( QString(saveLocation + "/img/%1/" + *file).arg(subalbumNumber) ); }
01283
01284
01285 rootDir.rmdir( QString("%1").arg(subalbumNumber) );
01286
01287
01288 rootDir.remove( QString(saveLocation + "/img/%1_thumb.jpg").arg(subalbumNumber) );
01289
01290
01291 subalbumNumber++;
01292 }
01293
01294
01295 numLoadedSubalbums = numSubalbums;
01296
01297 }
01298
01299 void Album::exportThemeResources( QString theme )
01300 {
01301 QStringList fileList;
01302 QStringList::Iterator file;
01303 QDir localDir;
01304
01305
01306 localDir.setPath( saveLocation + "/resources" );
01307 fileList = localDir.entryList();
01308 for(file = fileList.begin(); file != fileList.end(); file++)
01309 {
01310 localDir.remove(saveLocation + "/resources/" + *file);
01311 }
01312 localDir.cdUp();
01313 localDir.rmdir( "resources" );
01314
01315
01316 localDir.setPath(saveLocation);
01317 localDir.mkdir("resources");
01318
01319
01320
01321 localDir.setPath(saveLocation + "/resources");
01322 fileList = localDir.entryList( QDir::Files );
01323 for ( file = fileList.begin(); file != fileList.end(); ++file )
01324 { localDir.remove( saveLocation + "/resources/" + *file ); }
01325
01326
01327
01328
01329
01330
01331
01332
01333 localDir.setPath(THEMES_PATH + theme + "/resources");
01334 fileList = localDir.entryList( QDir::Files );
01335 for ( file = fileList.begin(); file != fileList.end(); ++file )
01336 { copyFile( THEMES_PATH + theme + "/resources/" + *file, saveLocation + "/resources/" + *file); }
01337
01338
01339
01340
01341
01342
01343
01344 }
01345
01346 void Album::syncSubalbumList(SubalbumPreviewWidget* item)
01347 {
01348
01349 bool change = false;
01350 Subalbum* tmp = firstSubalbum;
01351 SubalbumPreviewWidget* tmp2 = item;
01352 while( tmp2 != NULL)
01353 {
01354
01355 if(tmp != tmp2->getSubalbum() )
01356 {
01357 change = true;
01358 break;
01359 }
01360
01361 tmp = tmp->getNext();
01362 tmp2 = (SubalbumPreviewWidget*)tmp2->nextItem();
01363 }
01364
01365
01366 if(!change)
01367 return;
01368
01369
01370 if(item == NULL)
01371 {
01372 firstSubalbum = NULL;
01373 lastSubalbum = NULL;
01374 return;
01375 }
01376
01377
01378 firstSubalbum = item->getSubalbum();
01379 firstSubalbum->setNext(NULL);
01380 firstSubalbum->setPrev(NULL);
01381 lastSubalbum = firstSubalbum;
01382
01383
01384 while(item->nextItem() != NULL)
01385 {
01386 item->getSubalbum()->setNext( ((SubalbumPreviewWidget*)item->nextItem())->getSubalbum() );
01387 item->getSubalbum()->getNext()->setPrev( item->getSubalbum() );
01388 item = (SubalbumPreviewWidget*)item->nextItem();
01389 lastSubalbum = item->getSubalbum();
01390 lastSubalbum->setNext(NULL);
01391 }
01392
01393 }
01394
01395 void Album::setModified(bool val) { modified = val; }
01396
01397 int Album::getNextUniquePhotoID()
01398 {
01399 nextUniqueID++;
01400 return nextUniqueID;
01401 }
01402