Hydra 0.20
hydra.src/hydra/WebExport.h
00001 
00002 /*
00003  * Copyright (c) 2009 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_HYDRA_WEBEXPORT_H__
00009 #define __INCLUDED_HYDRA_WEBEXPORT_H__
00010 
00011 #include <QString>
00012 #include <QUuid>
00013 #include <QTextStream>
00014 
00015 #include <vector>
00016 #include <map>
00017 #include <hydra/TR1.h>
00018 
00019 namespace hydra
00020 {
00021   class WebExport;
00022   class FileItemRecord; //forward
00023 };
00024 
00025 /**
00026  * A class that exports a collection pics for web site building.
00027  *
00028  * @author Aleksander Demko
00029  */ 
00030 class hydra::WebExport
00031 {
00032   public:
00033     /**
00034      * Export a site to the given website output directory.
00035      *
00036      * @author Aleksander Demko
00037      */ 
00038     WebExport(const QString &outputdir, QTextStream &out);
00039 
00040     /**
00041      * Sets the title of the exported web site
00042      *
00043      * @author Aleksander Demko
00044      */ 
00045     void setTitle(const QString &title);
00046 
00047     /**
00048      * Adds a given file to the output list.
00049      *
00050      * @author Aleksander Demko
00051      */ 
00052     void addFile(const QString &fullfilename, const QString &basedir,
00053         hydra::FileItemRecord &item, const QString &filehash);
00054 
00055     /**
00056      * Commit and actually build the web site.
00057      *
00058      * Returns the number of files made, or -1 on error.
00059      *
00060      * @author Aleksander Demko
00061      */ 
00062     int commitWebSite(void);
00063 
00064     /**
00065      * Commit and actually build the web site.
00066      *
00067      * Returns the number of files made, or -1 on error.
00068      *
00069      * @author Aleksander Demko
00070      */ 
00071     int commitFileCopy(void);
00072     
00073   private:
00074     struct DirEntry;
00075     struct FileEntry;
00076 
00077     // returns the full file name of the given thumbnail
00078     QString thumbFileName(const QString &hash, unsigned long w, unsigned long h);
00079 
00080     void addDirComponents(const QString &dir);
00081 
00082     // connects the parents to children in dm_basedirs
00083     void buildDirTree(void);
00084 
00085     void computeDirCounts(DirEntry &dir);
00086 
00087     int writeImageFiles(void);
00088 
00089     bool writeFileIndex(int myid, int numpeers, FileEntry &entry);
00090     bool writeDirIndex(DirEntry &entry);
00091     void writeIndexFiles(void);
00092 
00093     void fileCopyMakeDirs(const QString dir = "");
00094     int fileCopyCopyFiles(void);
00095 
00096   private:
00097     QTextStream &dm_out;
00098 
00099     QString dm_outdir, dm_title;
00100 
00101     typedef std::vector<std::shared_ptr<DirEntry> > DirSet;
00102 
00103     static bool DirEntryLT(const std::shared_ptr<DirEntry> &lhs,
00104         const std::shared_ptr<DirEntry> &rhs);
00105 
00106     typedef std::vector<std::shared_ptr<FileEntry> > FileSet;
00107 
00108     static bool FileEntryLT(const std::shared_ptr<FileEntry> &lhs,
00109         const std::shared_ptr<FileEntry> &rhs);
00110 
00111     struct DirEntry {
00112       bool isroot;
00113       ptrdiff_t totalfiles;
00114 
00115       QString basedir;       // the map KEY
00116       // derived:
00117       QString urlname;       // basedir, but suitable for url display
00118       QString justname;      // just the last name
00119 
00120       std::weak_ptr<DirEntry> parent;    // parent dir, null for the root node
00121 
00122       // they all store map keys, ofcourse
00123       DirSet subdirs;       // sub dirs
00124       FileSet subimages;     // images (thumbable) in this dir
00125       FileSet subfiles;      // no-image files in this dir
00126 
00127       DirEntry(const QString &_basedir);
00128     };
00129 
00130     typedef std::map<QString, std::shared_ptr<DirEntry> > DirMap;
00131 
00132     struct FileEntry {
00133       QUuid id;       // db id
00134 
00135       std::weak_ptr<DirEntry> parent;    // parent dir
00136 
00137       int rotateCode;        // rotattion to apply, if any
00138 
00139       QString fullfilename;  // full on disk filename
00140       QString justname;      // just the filename (as on disk)
00141       QString basedir;       // basic web path (the parent directory)
00142       QString basejustname;  // justname, but possibly adjusted for conflicts
00143       QString basefilename;  // the baedir + filename, also, this is the map KEY
00144       QString title;         // title from the db
00145       QString desc;          // desc from the db
00146       int filetype;          // same as in the item record
00147       QString filehash;      // binary hash for this file
00148 
00149       // these are all set by calc()
00150       QString urlnamebase;   // core url name (/ -> , substiution), from basefilename
00151       QString urlhtml;       // urlnamebase + .html
00152       QString urlorigimage;  // urlnamebase
00153       QString urlviewimage;  // VIEW, + urlnamebase
00154       QString urlthumbimage; // THUMB, + urlnamebase
00155 
00156       void calc(void);        // calculates various computed strings after the others are set
00157     };
00158 
00159     typedef std::map<QString, std::shared_ptr<FileEntry> > FileMap;
00160 
00161     FileMap dm_basefiles;   // built incrementally when caller adds files
00162 
00163     DirMap dm_basedirs; // built from dm_basefiles upton BuildDirTree()
00164 };
00165 
00166 #endif
00167 
 All Classes Namespaces Functions Variables