Hydra 0.20
hydra.src/hydra/Engine.h
00001 
00002 /*
00003  * Copyright (c) 2010 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_ENGINE_H__
00009 #define __INCLUDED_HYDRA_ENGINE_H__
00010 
00011 #include <hydra/TR1.h>
00012 
00013 #include <QString>
00014 #include <QDateTime>
00015 
00016 #define HYDRA_VERSION_STRING "0.20"
00017 #define HYDRA_COPYRIGHT_STRING "2007-2011"
00018 
00019 /**
00020  * The core hydra namespace contains all the classes
00021  * of the core hydra library.
00022  *
00023  * @author Aleksander Demko
00024  */ 
00025 namespace hydra
00026 {
00027   class Engine;
00028   class DB;//forward
00029   class FilePathRecord; //forward
00030   class FileHashRecord; //forward
00031   class FileItemRecord; //forward
00032 }
00033 
00034 /**
00035  * The main processing engine for hydra applications.
00036  * It is a singleton class that should be instatiated
00037  * by your main or similar function.
00038  *
00039  * @author Aleksander Demko
00040  */
00041 class hydra::Engine
00042 {
00043   public:
00044     // carious codes
00045     enum {
00046       Add_New = 10,
00047       Add_Exists,
00048       Add_NewPath,
00049       Add_UpdatedPath,
00050       Add_Error,
00051 
00052       Load_OK = 100,        // typically, you should just use Load_OK
00053       Load_ErrorNeedsUpdate,    // its in the DB, but it needs an update
00054       Load_ErrorNotFound, // not in db
00055       Load_ErrorFatal,    // some serious error
00056       Load_ErrorFileMissing,  // file missing or access error
00057     };
00058   public:
00059     /// constructor
00060     Engine(void);
00061     /// destructor
00062     ~Engine();
00063 
00064     /**
00065      * Returns the static instance, if any
00066      *
00067      * @author Aleksander Demko
00068      */
00069     static Engine * instance(void) { return dm_instance; }
00070 
00071     /**
00072      * Gets the path to the user's home directory, or "."
00073      * if not found.
00074      *
00075      * @author Aleksander Demko
00076      */ 
00077     static QString homeDir(void);
00078 
00079     /**
00080      * Returns the directory where the database files are stored.
00081      * (This is usually ~/.hydradb)
00082      *
00083      * This will also create it, if necesary.
00084      *
00085      * @author Aleksander Demko
00086      */ 
00087     static QString dbDir(void);
00088 
00089     /**
00090      * Adds a file to the db.
00091      * This will also repair/update any outof date records.
00092      * If precalchash is provided, then it is assumed to be the hash of the given
00093      * file.
00094      *
00095      * @author Aleksander Demko
00096      */ 
00097     int addFile(const QString &fullfilename, const QString *precalchash = 0);
00098 
00099     /**
00100      * Erase the given path from the database.
00101      * Returns true on success (which for now, is always)
00102      *
00103      * @author Aleksander Demko
00104      */ 
00105     bool erasePath(const QString &fullfilename);
00106 
00107     /**
00108      * Erase the given hash from the database.
00109      * Note, that this will UNTIE the hash from its current item record,
00110      * effectivly forgetting its tags, etc. Upon rediscovery, the hash
00111      * will get a new item record.
00112      * Returns true on success (which for now, is always)
00113      *
00114      * @author Aleksander Demko
00115      */ 
00116     bool eraseHash(const QString &hash);
00117 
00118     /**
00119      * Loads the given file from the DB
00120      * Any of the pointers may be null.
00121      *
00122      * @return a Load_* error code
00123      * @author Aleksander Demko
00124      */ 
00125     int getFileItem(const QString &fullfilename, hydra::FileItemRecord *item, hydra::FileHashRecord *hash,
00126         hydra::FilePathRecord *path);
00127     /**
00128      * Loads the given file from the DB
00129      *
00130      * This is a shorter version of the above.
00131      *
00132      * @return a Load_* error code
00133      * @author Aleksander Demko
00134      */ 
00135     int getFileItem(const QString &fullfilename, hydra::FileItemRecord &rec);
00136 
00137     /**
00138      * Loads the given file from the DB, by hash
00139      *
00140      * @return a Load_* error code
00141      * @author Aleksander Demko
00142      */ 
00143     int getFileItemByHash(const QString &hashkey, hydra::FileItemRecord *item, hydra::FileHashRecord *hash);
00144 
00145     /**
00146      * Reloads the given file item from db.
00147      * The fileitem must have previously been loaded from the db
00148      * (ie. its id field must be valid).
00149      *
00150      * Returns the same codes as getFileItem()
00151      *
00152      * @author Aleksander Demko
00153      */ 
00154     int regetFileItem(hydra::FileItemRecord &rec);
00155 
00156     /**
00157      * Saves the given item to the db.
00158      *
00159      *
00160      * @param rec the record to save
00161      * @param newmodtime the modification time to use. If this isValid, rec.modtime will be set to this before saving
00162      * @return true on success
00163      * @author Aleksander Demko
00164      */
00165     bool saveFileItem(hydra::FileItemRecord &rec, const QDateTime &newmodtime);
00166 
00167     // future, uses the internal cache bank
00168     //std::tr1::shared_ptr<FileItemRecord> getFileItem(const QString &fullfilename);
00169 
00170     hydra::DB & filePathDB(void) { return *dm_filepathdb; }
00171     hydra::DB & fileHashDB(void) { return *dm_filehashdb; }
00172     hydra::DB & fileItemDB(void) { return *dm_fileitemdb; }
00173 
00174     /**
00175      * This converts any of the errors codes into a ascii char
00176      * that may be suitable for status output.
00177      *
00178      * @author Aleksander Demko
00179      */ 
00180     static char codeToChar(int code);
00181 
00182     /// gets the mod time and file size of the given file
00183     /// returns true on success
00184     static bool statFile(const QString &fullfilename, double &modtime, qint64 &filesize);
00185 
00186   private:
00187     static Engine *dm_instance;
00188 
00189     std::shared_ptr<DB> dm_filepathdb, dm_filehashdb, dm_fileitemdb;
00190 };
00191 
00192 #endif
00193 
 All Classes Namespaces Functions Variables