00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "cWebshots.h"
00027
00028 #include <cstdio>
00029 #include <sstream>
00030
00031 using namespace std;
00032
00033 namespace cWebshots
00034 {
00035
00036
00037
00038
00039
00040
00044 cWebshotsCollection::cWebshotsCollection()
00045 {
00046 CreateDate = 0;
00047 EditDate = 0;
00048 In_Use = false;
00049 }
00050
00056 cWebshotsCollection::cWebshotsCollection(const cWebshots::cWebshotsCollection &source)
00057 {
00058 *this = source;
00059 }
00060
00061
00062
00063
00064
00073 void cWebshotsCollection::LoadFromFile(string File)
00074 {
00075 ifstream in_file;
00076
00077
00078 Clear();
00079
00080
00081 in_file.open(File.c_str(), ios_base::in | ios_base::binary);
00082
00083 if (in_file.is_open())
00084 {
00085
00086 switch (IdentifyFile(in_file))
00087 {
00088 case WBZ:
00089 LoadWBZFile(in_file);
00090 break;
00091 case WBC:
00092 LoadWBCFile(in_file);
00093 break;
00094 case WB1:
00095 case JPG:
00096 AddPicture().LoadFromFile(File);
00097 break;
00098 case Unknown:
00099 {
00100 Exceptions::UnknownFormat Err("The file \"" + File + "\" has an unknown format.");
00101 throw Err;
00102 }
00103 }
00104
00105
00106 in_file.close();
00107 }
00108 else
00109 {
00110 Exceptions::FileSystemError Err("Unable to open the file \"" + File + "\" for reading.");
00111 throw Err;
00112 }
00113 }
00114
00122 void cWebshotsCollection::LoadWBCFile(ifstream &in_file)
00123 {
00124 long Count;
00125 int i;
00126
00127 sWBC_Header Header;
00128 sWBC_Index* Index;
00129 sWBC_Unit_Header Unit_Header;
00130
00131 char* Buffer;
00132
00133 cWebshotsPicture* Pic;
00134
00135
00136 in_file.seekg(0);
00137 in_file.read((char *)&Header, sizeof Header);
00138
00139
00140 if (Header.File_Marker != WBC_File_Marker)
00141 {
00142 Exceptions::UnknownFormat Err("The file has an unknown format.");
00143 throw Err;
00144 }
00145
00146 Title = Header.File_Title;
00147
00148
00149 in_file.seekg(2196);
00150 in_file.read((char *)&Count, sizeof Count);
00151
00152 Index = new sWBC_Index[Count];
00153 in_file.read((char *)Index, Count * sizeof *Index);
00154
00155
00156 for (i = 0; i < Count; i++)
00157 {
00158
00159 in_file.seekg(Index[i].Unit_Position);
00160 in_file.read((char *)&Unit_Header, sizeof Unit_Header);
00161
00162
00163 Buffer = new char[Unit_Header.JPG_File_Size];
00164 in_file.read(Buffer, Unit_Header.JPG_File_Size);
00165
00166
00167 try
00168 {
00169 Pic = new cWebshotsPicture;
00170
00171 Pic->LoadFromMemory(Buffer, Unit_Header.JPG_File_Size);
00172 delete[] Buffer;
00173
00174 Pic->AdditionDate = Unit_Header.Addition_Timestamp;
00175 Pic->Caption = Unit_Header.Description;
00176 Pic->Category = Unit_Header.Category;
00177 Pic->Credits = Unit_Header.Credits;
00178 Pic->DailyDate = Unit_Header.Daily_Date;
00179 if (strlen(Unit_Header.ID) > 1)
00180 Pic->ID = atol(&Unit_Header.ID[1]);
00181 if (strlen(Unit_Header.ID) > 0)
00182 Pic->Source = Unit_Header.ID[0];
00183 Pic->Original_Filename = Unit_Header.Original_Filename;
00184 Pic->Album = Unit_Header.Album;
00185 Pic->Title = Unit_Header.Title;
00186
00187 Pictures.push_back(Pic);
00188 }
00189
00190 catch (Exceptions::UnknownFormat)
00191 {
00192 delete Pic;
00193 }
00194 }
00195
00196 delete[] Index;
00197 }
00198
00206 void cWebshotsCollection::LoadWBZFile(ifstream &in_file)
00207 {
00208 sWBZ_Header File_Header;
00209 sWBZ_Element_Header Element_Header;
00210 int i;
00211 char *ElementContent;
00212 string Metadata;
00213
00214
00215 in_file.seekg(0);
00216 in_file.read((char *)&File_Header,sizeof File_Header);
00217
00218
00219 if (File_Header.File_Marker != WBZ_File_Marker)
00220 {
00221 Exceptions::UnknownFormat Err("The file has an unknown format.");
00222 throw Err;
00223 }
00224
00225
00226 for (i = 0; i < File_Header.Element_Count; i++)
00227 {
00228
00229 in_file.read((char *)&Element_Header,sizeof Element_Header);
00230
00231
00232 if (Element_Header.Element_Marker != WBZ_Element_Marker)
00233 {
00234 stringstream Description;
00235 Description << "Corrupted data : The header of the element n°" << (i + 1) << " is incorrect.";
00236 Exceptions::CorruptedData Err(Description.str());
00237 throw Err;
00238 }
00239
00240
00241 ElementContent = new char[Element_Header.File_Size];
00242
00243 try
00244 {
00245 in_file.read(ElementContent, Element_Header.File_Size);
00246
00247
00248 if ((strstr(Element_Header.File_Name,"wb1") != NULL) || (strstr(Element_Header.File_Name,"wbd") != NULL) || (strstr(Element_Header.File_Name,"jpg") != NULL))
00249 {
00250 AddPicture().LoadFromMemory(ElementContent,Element_Header.File_Size);
00251 }
00252
00253 else if (strstr(Element_Header.File_Name,"wbx") != NULL)
00254 {
00255 Metadata.assign(ElementContent);
00256 }
00257 }
00258
00259 catch (...)
00260 {
00261 delete[] ElementContent;
00262 throw;
00263 }
00264
00265 delete[] ElementContent;
00266 }
00267
00268
00269 for (i = 0; i < PictureCount(); i++)
00270 {
00271 try
00272 {
00273 Pictures[i]->Source = GetINI(Metadata, IntToStr(i), "source");
00274 Pictures[i]->Original_Filename = GetINI(Metadata, IntToStr(i), "filename");
00275 Pictures[i]->ID = StrToLong(GetINI(Metadata, IntToStr(i), "id"));
00276 Pictures[i]->Title = GetINI(Metadata, IntToStr(i), "title");
00277 Pictures[i]->Credits = GetINI(Metadata, IntToStr(i), "credit");
00278 Pictures[i]->DailyDate = StrToLong(GetINI(Metadata, IntToStr(i), "dailyDateYYYYMMDD"));
00279 Pictures[i]->Category = GetINI(Metadata, IntToStr(i), "catTitle");
00280 Pictures[i]->Album = GetINI(Metadata, IntToStr(i), "albumTitle");
00281 Pictures[i]->AlbumID = GetINI(Metadata, IntToStr(i), "albumID");
00282 }
00283
00284 catch (Exceptions::CorruptedData)
00285 {
00286
00287 }
00288 }
00289 }
00290
00298 void cWebshotsCollection::LoadFromFolder(const string Path)
00299 {
00300 cWebshotsTXT album;
00301 cWebshotsPicture pic;
00302 int i;
00303
00304
00305 Clear();
00306
00307
00308 album.Load(Path);
00309
00310 CreateDate = album.Album.create_date;
00311 EditDate = album.Album.edit_date;
00312 Description = album.Album.description;
00313 ID = album.Album.id;
00314 In_Use = album.Album.in_use;
00315 Title = album.Album.title;
00316 Topic = album.Album.topic_name;
00317
00318
00319 for (i = 0; i < album.PictureCount(); i++)
00320 {
00321 try
00322 {
00323 pic.Clear();
00324
00325
00326 pic.LoadFromFile(Path + album.Picture(i).url_photo);
00327
00328
00329 pic.ID = album.Picture(i).id;
00330 pic.Original_Filename = album.Picture(i).url_photo;
00331 pic.Title = album.Picture(i).title;
00332 pic.Caption = album.Picture(i).caption;
00333 pic.Credits = album.Picture(i).credit;
00334 pic.Source = album.Picture(i).source;
00335 pic.Album = Title;
00336 pic.AlbumID = ID;
00337 pic.Category = Topic;
00338
00339
00340 AddPicture(pic);
00341 }
00342
00343 catch (Exceptions::Exception)
00344 {
00345
00346 }
00347 }
00348 }
00349
00350
00359 void cWebshotsCollection::SaveToFile(string File, Webshots_Formats Format) const
00360 {
00361
00362 ofstream out_file(File.c_str(), ios_base::out | ios_base::binary | ios::trunc);
00363
00364
00365 if (out_file.is_open())
00366 {
00367
00368 if (Format == WBC)
00369 SaveWBCFile(out_file);
00370 else if (Format == WBZ)
00371 SaveWBZFile(out_file);
00372
00373 out_file.close();
00374 }
00375 else
00376 {
00377 Exceptions::FileSystemError Err("Unable to open the file \"" + File + "\" for writing.");
00378 throw Err;
00379 }
00380 }
00381
00387 void cWebshotsCollection::SaveWBCFile(ofstream &out_file) const
00388 {
00389
00390 sWBC_Header file_header;
00391
00392 file_header.File_Marker = WBC_File_Marker;
00393 strcpy(file_header.File_Title,Title.c_str());
00394 file_header.Header_Size = 8608;
00395 file_header.unknown = 0;
00396
00397 out_file.seekp(0);
00398 out_file.write((char *)&file_header, sizeof file_header);
00399
00400 int i;
00401
00402
00403 out_file.seekp(8608);
00404
00405 sWBC_Unit_Header unit_header;
00406
00407 for (i = 0; i < PictureCount(); i++)
00408 {
00409 unit_header.Unit_Marker = WBC_Unit_Marker;
00410 unit_header.Header_Size = sizeof unit_header;
00411 unit_header.Unit_Size = unit_header.Header_Size + Pictures[i]->PictureSize() + Pictures[i]->ThumbnailSize();
00412
00413 strcpy(unit_header.Original_Filename, Pictures[i]->Original_Filename.c_str());
00414 strcpy(unit_header.Title, Pictures[i]->Title.c_str());
00415 strcpy(unit_header.Description, Pictures[i]->Caption.c_str());
00416 strcpy(unit_header.Credits, Pictures[i]->Credits.c_str());
00417 unit_header.Extension[0] = 0;
00418 unit_header.JPG_File_Size = Pictures[i]->PictureSize();
00419 unit_header.BMP_Thumbnail_Size = Pictures[i]->ThumbnailSize();
00420 unit_header.Unknown[0] = 0;
00421 unit_header.Daily_Date = Pictures[i]->DailyDate;
00422 unit_header.Addition_Timestamp = Pictures[i]->AdditionDate;
00423 unit_header.Fit_To_Screen = 0;
00424
00425 strcpy(unit_header.ID,Pictures[i]->Source.c_str());
00426 ltoa(Pictures[i]->ID, unit_header.ID + 1, 10);
00427
00428 strcpy(unit_header.Album,Pictures[i]->Album.c_str());
00429 strcpy(unit_header.Category,Pictures[i]->Category.c_str());
00430 unit_header.Unknown2[0] = 0;
00431
00432 out_file.write((char *)&unit_header, sizeof unit_header);
00433
00434 if (Pictures[i]->PictureSize() > 0)
00435 out_file.write(Pictures[i]->PicturePtr(), Pictures[i]->PictureSize());
00436 if (Pictures[i]->ThumbnailSize() > 0)
00437 out_file.write(Pictures[i]->ThumbnailPtr(), Pictures[i]->ThumbnailSize());
00438 }
00439
00440
00441 out_file.seekp(2196);
00442
00443 sWBC_Index index;
00444 long offset;
00445 long count;
00446
00447 count = PictureCount();
00448
00449 out_file.write((char *)&count, sizeof count);
00450
00451 offset = 8608;
00452
00453 for (i = 0; i < PictureCount(); i++)
00454 {
00455 index.Addition_TimeStamp = Pictures[i]->AdditionDate;
00456 index.Unit_Position = offset;
00457 index.Unit_Size = sizeof unit_header + Pictures[i]->PictureSize() + Pictures[i]->ThumbnailSize();
00458 index.unknown = 0;
00459 index.unknown2[0] = 0;
00460
00461 out_file.write((char *)&index, sizeof index);
00462
00463 offset += index.Unit_Size;
00464 }
00465 }
00466
00467
00473 void cWebshotsCollection::SaveWBZFile(ofstream &out_file) const
00474 {
00475
00476 sWBZ_Header header;
00477
00478 header.Element_Count = PictureCount() + 1;
00479 header.File_Marker = WBZ_File_Marker;
00480 header.Header_Size = sizeof header;
00481 header.unknown1 = 0;
00482 header.Header_Version = 1;
00483 header.unknown3 = 0;
00484 header.unknown4[0] = 0;
00485
00486 out_file.write((char *)&header,sizeof header);
00487
00488
00489 int i;
00490
00491 for (i = 0; i < PictureCount(); i++)
00492 {
00493 sWBZ_Element_Header pic_header;
00494
00495 pic_header.Element_Marker = WBZ_Element_Marker;
00496 strcpy(pic_header.File_Name,Picture(i).Original_Filename.c_str());
00497 pic_header.File_Size = Picture(i).PictureSize();
00498 pic_header.File_Size2 = Picture(i).PictureSize();
00499 pic_header.Header_Size = sizeof pic_header;
00500 pic_header.Header_Version = 1;
00501 pic_header.unknown2 = 0;
00502 pic_header.unknown4[0] = 0;
00503
00504 out_file.write((char *)&pic_header,sizeof pic_header);
00505 out_file.write(Picture(i).PicturePtr(),Picture(i).PictureSize());
00506 }
00507
00508
00509 stringstream meta_stream;
00510 string meta;
00511
00512 meta_stream << "[Header]" << "\r\n";
00513 meta_stream << "Count=" << PictureCount() << "\r\n";
00514
00515
00516 for (i = 0; i < PictureCount(); i++)
00517 meta_stream << i << "=100" << "\r\n";
00518
00519 for (i = 0; i < PictureCount(); i++)
00520 {
00521 meta_stream << "[" << i << "]" << "\r\n";
00522 meta_stream << "BOO_MakeWallpaper=1\r\n";
00523 meta_stream << "FIL_ImageFileName=" << Picture(i).Original_Filename << "\r\n";
00524 meta_stream << "STR_ImageTitle=" << Picture(i).Title << "\r\n";
00525 meta_stream << "STR_ImageCredit=" << Picture(i).Credits << "\r\n";
00526 meta_stream << "STR_ImagePID=" << Picture(i).Source << Picture(i).ID << "\r\n";
00527 meta_stream << "STR_CollectionTitle=" << Picture(i).Category << " - " << Picture(i).Album << "\r\n";
00528 meta_stream << "source=" << Picture(i).Source << "\r\n";
00529 meta_stream << "filename=" << Picture(i).Original_Filename << "\r\n";
00530 meta_stream << "id=" << Picture(i).ID << "\r\n";
00531 meta_stream << "title=" << Picture(i).Title << "\r\n";
00532 meta_stream << "credit=" << Picture(i).Credits << "\r\n";
00533 meta_stream << "position=\r\n";
00534 meta_stream << "catTitle=" << Picture(i).Category << "\r\n";
00535 meta_stream << "albumID=" << Picture(i).AlbumID << "\r\n";
00536 meta_stream << "albumTitle=" << Picture(i).Album << "\r\n";
00537 meta_stream << "dailyDateYYYYMMDD=" << Picture(i).DailyDate << "\r\n";
00538 }
00539
00540 meta = meta_stream.str();
00541
00542 sWBZ_Element_Header meta_header;
00543
00544 meta_header.Element_Marker = WBZ_Element_Marker;
00545 strcpy(meta_header.File_Name,"info.wbx");
00546 meta_header.File_Size = (long)meta.length();
00547 meta_header.File_Size2 = (long)meta.length();
00548 meta_header.Header_Size = sizeof meta_header;
00549 meta_header.Header_Version = 1;
00550 meta_header.unknown2 = 0;
00551 meta_header.unknown4[0] = 0;
00552
00553 out_file.write((char *)&meta_header,sizeof meta_header);
00554 out_file.write(meta.c_str(),meta.length());
00555 }
00556
00557
00567 void cWebshotsCollection::SaveToFolder(const string Path) const
00568 {
00569 ofstream out_file;
00570 string filename;
00571 stringstream filename_stream;
00572 int i;
00573
00574
00575 filename = Path + "album.txt";
00576 out_file.open(filename.c_str(), ios_base::out | ios::trunc);
00577
00578 if (out_file.is_open())
00579 {
00580 out_file << "count||1" << endl
00581 << "ID_0||" << ID << endl
00582 << "TITLE_0||" << Title << endl
00583 << "PHOTO_COUNT_0||" << PictureCount() << endl
00584 << "IN_USE_0||" << (In_Use ? 1 : 0) << endl
00585 << "DESCRIPTION_0||" << Description << endl
00586 << "TOPIC_NAME_0||" << Topic << endl
00587 << "CREATE_DATE_0||" << CreateDate << endl
00588 << "EDIT_DATE_0||" << EditDate << endl;
00589
00590 out_file.close();
00591 }
00592 else
00593 {
00594 Exceptions::FileSystemError Err("Unable to open the file \"" + filename + "\" for writing.");
00595 throw Err;
00596 }
00597
00598
00599 filename = Path + "photos.txt";
00600 out_file.open(filename.c_str(), ios_base::out | ios::trunc);
00601
00602 if (out_file.is_open())
00603 {
00604 out_file << "count||" << PictureCount() << endl;
00605
00606 for (i = 0; i < PictureCount(); i++)
00607 {
00608
00609 filename_stream << Path << "ph" << Picture(i).ID << ".wb1";
00610 Picture(i).SaveToFile(filename_stream.str());
00611 filename_stream.str("");
00612
00613 out_file << "ID_" << i << "||" << Picture(i).ID << endl
00614 << "POSITION_" << i << "||" << Picture(0).ID + i << endl
00615 << "TITLE_" << i << "||" << Picture(i).Title << endl
00616 << "CAPTION_" << i << "||" << Picture(i).Caption << endl
00617 << "CREDIT_" << i << "||" << Picture(i).Credits << endl
00618 << "URL_PHOTO_" << i << "||" << "ph" << Picture(i).ID << ".wb1" << endl
00619 << "URL_THUMB_" << i << "||" << "ph" << Picture(i).ID << ".wb1" << endl
00620 << "SOURCE_" << i << "||" << (Picture(i).Source.length() > 0 ? Picture(i).Source : "u") << endl;
00621 }
00622
00623 out_file.close();
00624 }
00625 else
00626 {
00627 Exceptions::FileSystemError Err("Unable to open the file \"" + filename + "\" for writing.");
00628 throw Err;
00629 }
00630 }
00631
00632
00633
00634
00635
00643 cWebshotsPicture& cWebshotsCollection::Picture(int Index)
00644 {
00645 if ((Index < (int)Pictures.size()) && (Index >= 0))
00646 return *Pictures[Index];
00647 else
00648 {
00649 Exceptions::OutOfRange Err("Index out of range.");
00650 throw Err;
00651 }
00652 }
00653
00661 const cWebshotsPicture& cWebshotsCollection::Picture(int Index) const
00662 {
00663 if ((Index < (int)Pictures.size()) && (Index >= 0))
00664 return *Pictures[Index];
00665 else
00666 {
00667 Exceptions::OutOfRange Err("Index out of range.");
00668 throw Err;
00669 }
00670 }
00671
00679 void cWebshotsCollection::RemovePicture(int Index)
00680 {
00681 if ((Index < (int)Pictures.size()) && (Index >= 0))
00682 {
00683 delete Pictures[Index];
00684 Pictures.erase(Pictures.begin() + Index);
00685 }
00686 else
00687 {
00688 Exceptions::OutOfRange Err("Index out of range.");
00689 throw Err;
00690 }
00691 }
00692
00698 cWebshotsPicture& cWebshotsCollection::AddPicture()
00699 {
00700 cWebshotsPicture* Pic = new cWebshotsPicture;
00701 Pictures.push_back(Pic);
00702
00703 return *Pic;
00704 }
00705
00713 cWebshotsPicture& cWebshotsCollection::AddPicture(const cWebshotsPicture& Picture)
00714 {
00715 return AddPicture() = Picture;
00716 }
00717
00723 int cWebshotsCollection::PictureCount() const
00724 {
00725 return (int)Pictures.size();
00726 }
00727
00731 void cWebshotsCollection::Clear()
00732 {
00733 unsigned long i;
00734
00735
00736 for (i = 0; i < Pictures.size(); i++)
00737 delete Pictures[i];
00738
00739
00740 Pictures.clear();
00741
00742
00743 ID.clear();
00744 Title.clear();
00745 Description.clear();
00746 Topic.clear();
00747 In_Use = false;
00748 CreateDate = 0;
00749 EditDate = 0;
00750 }
00751
00752
00753
00754
00755
00760 cWebshotsCollection& cWebshotsCollection::operator=(const cWebshotsCollection& source)
00761 {
00762 if (&source != this)
00763 {
00764 int i;
00765
00766
00767 Clear();
00768
00769
00770 for (i = 0; i < source.PictureCount(); i++)
00771 AddPicture(source.Picture(i));
00772
00773
00774 ID = source.ID;
00775 Title = source.Title;
00776 Description = source.Description;
00777 Topic =source.Topic;
00778 In_Use = source.In_Use;
00779 CreateDate = source.CreateDate;
00780 EditDate = source.EditDate;
00781 }
00782
00783 return *this;
00784 }
00785
00790 cWebshotsCollection& cWebshotsCollection::operator+=(const cWebshotsCollection& source)
00791 {
00792 int i;
00793
00794 for (i = 0; i < source.PictureCount(); i++)
00795 AddPicture(source.Picture(i));
00796
00797 return *this;
00798 }
00799
00804 cWebshotsCollection& cWebshotsCollection::operator=(const cWebshotsPicture& source)
00805 {
00806 Clear();
00807 AddPicture(source);
00808
00809 return *this;
00810 }
00811
00816 cWebshotsCollection& cWebshotsCollection::operator+=(const cWebshotsPicture& source)
00817 {
00818 AddPicture(source);
00819
00820 return *this;
00821 }
00822
00831 cWebshotsPicture& cWebshotsCollection::operator[](const int Index)
00832 {
00833 if ((Index < (int)Pictures.size()) && (Index >= 0))
00834 return *Pictures[Index];
00835 else
00836 {
00837 Exceptions::OutOfRange Err("Index out of range.");
00838 throw Err;
00839 }
00840 }
00841
00850 const cWebshotsPicture& cWebshotsCollection::operator[](const int Index) const
00851 {
00852 if ((Index < (int)Pictures.size()) && (Index >= 0))
00853 return *Pictures[Index];
00854 else
00855 {
00856 Exceptions::OutOfRange Err("Index out of range.");
00857 throw Err;
00858 }
00859 }
00860
00861
00868 cWebshotsCollection operator+(const cWebshotsCollection& col1, const cWebshotsCollection& col2)
00869 {
00870 cWebshotsCollection Collection;
00871
00872 Collection = col1;
00873 Collection += col2;
00874
00875 return Collection;
00876 }
00877
00883 cWebshotsCollection operator+(const cWebshotsPicture& pic, const cWebshotsCollection& col)
00884 {
00885 cWebshotsCollection Collection;
00886
00887 Collection = pic;
00888 Collection += col;
00889
00890 return Collection;
00891 }
00892
00898 cWebshotsCollection operator+(const cWebshotsCollection& col, const cWebshotsPicture& pic)
00899 {
00900 cWebshotsCollection Collection;
00901
00902 Collection = col;
00903 Collection += pic;
00904
00905 return Collection;
00906 }
00907
00913 cWebshotsCollection operator+(const cWebshotsPicture& pic1, const cWebshotsPicture& pic2)
00914 {
00915 cWebshotsCollection Collection;
00916
00917 Collection = pic1;
00918 Collection += pic2;
00919
00920 return Collection;
00921 }
00922
00923 }