glc_cachemanager.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023  *****************************************************************************/
00024 
00025 #include "glc_cachemanager.h"
00026 #include <QtDebug>
00027 
00028 
00029 GLC_CacheManager::GLC_CacheManager(const QString& path)
00030 : m_Dir()
00031 , m_UseCompression(true)
00032 , m_CompressionLevel(-1)
00033 {
00034         if (! path.isEmpty())
00035         {
00036                 QFileInfo pathInfo(path);
00037                 if (pathInfo.isDir() && pathInfo.isReadable())
00038                 {
00039                         m_Dir.setPath(path);
00040                 }
00041         }
00042 }
00043 
00044 // Copy constructor
00045 GLC_CacheManager::GLC_CacheManager(const GLC_CacheManager& cacheManager)
00046 :m_Dir(cacheManager.m_Dir)
00047 , m_UseCompression(cacheManager.m_UseCompression)
00048 , m_CompressionLevel(cacheManager.m_CompressionLevel)
00049 {
00050 
00051 }
00052 
00053 // Assignement operator
00054 GLC_CacheManager& GLC_CacheManager::operator=(const GLC_CacheManager& cacheManager)
00055 {
00056         m_Dir= cacheManager.m_Dir;
00057         m_UseCompression= cacheManager.m_UseCompression;
00058         m_CompressionLevel= cacheManager.m_CompressionLevel;
00059 
00060         return *this;
00061 }
00062 
00063 GLC_CacheManager::~GLC_CacheManager()
00064 {
00065 
00066 }
00067 
00069 // Get Functions
00071 
00072 // Return true if the cache is is readable
00073 bool GLC_CacheManager::isReadable() const
00074 {
00075         bool isReadable= true;
00076         isReadable= isReadable && m_Dir.exists();
00077         QFileInfo dirInfo(m_Dir.absolutePath());
00078         isReadable= isReadable && dirInfo.isReadable();
00079 
00080         return isReadable;
00081 
00082 }
00083 
00084 // Return true if the cache is writable
00085 bool GLC_CacheManager::isWritable() const
00086 {
00087         bool isWritable= true;
00088         isWritable= isWritable && m_Dir.exists();
00089         QFileInfo dirInfo(m_Dir.absolutePath());
00090         isWritable= isWritable && dirInfo.isWritable();
00091 
00092         return isWritable;
00093 }
00094 
00095 // Return True if the specified file is cashed in the specified context
00096 bool GLC_CacheManager::isCashed(const QString& context, const QString& fileName) const
00097 {
00098         if (! isReadable()) return false;
00099 
00100         QFileInfo fileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName + '.' + GLC_BSRep::suffix());
00101         return fileInfo.exists();
00102 }
00103 
00104 // Return True if the cached file is usable
00105 bool GLC_CacheManager::isUsable(const QDateTime& timeStamp, const QString& context, const QString& fileName) const
00106 {
00107         bool result= isCashed(context, fileName);
00108 
00109         if (result)
00110         {
00111                 QFileInfo cacheFileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName+ '.' + GLC_BSRep::suffix());
00112                 //result= result && (timeStamp == cacheFileInfo.lastModified());
00113                 result= result && cacheFileInfo.isReadable();
00114                 if (result)
00115                 {
00116                         GLC_BSRep binaryRep;
00117                         binaryRep.setAbsoluteFileName(cacheFileInfo.absoluteFilePath());
00118                         result= result && binaryRep.repIsUpToDate(timeStamp);
00119                 }
00120                 else
00121                 {
00122                         qDebug() << "file " << fileName << " not usable";
00123                 }
00124         }
00125         else
00126         {
00127                 QFileInfo cacheFileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName+ '.' + GLC_BSRep::suffix());
00128         }
00129         if (! result) qDebug() << "NOT USABLE";
00130 
00131         return result;
00132 }
00133 
00134 // Return the binary serialized representation of the specified file
00135 GLC_BSRep GLC_CacheManager::binary3DRep(const QString& context, const QString& fileName) const
00136 {
00137         const QString absoluteFileName(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName+ '.' + GLC_BSRep::suffix());
00138         GLC_BSRep binaryRep(absoluteFileName);
00139 
00140         return binaryRep;
00141 }
00142 
00143 // Add the specified file in the cache
00144 bool GLC_CacheManager::addToCache(const QString& context, const GLC_3DRep& rep)
00145 {
00146         Q_ASSERT(!rep.fileName().isEmpty());
00147         bool addedToCache= isWritable();
00148         if (addedToCache)
00149         {
00150                 QFileInfo contextCacheInfo(m_Dir.absolutePath() + QDir::separator() + context);
00151                 if (! contextCacheInfo.exists())
00152                 {
00153                         addedToCache= m_Dir.mkdir(context);
00154                 }
00155                 if (addedToCache)
00156                 {
00157 
00158                         const QString binaryFileName= contextCacheInfo.filePath() + QDir::separator() + rep.fileName();
00159                         GLC_BSRep binariRep(binaryFileName, m_UseCompression);
00160                         binariRep.setCompressionLevel(m_CompressionLevel);
00161                         addedToCache= binariRep.save(rep);
00162                 }
00163         }
00164 
00165         return addedToCache;
00166 }
00167 
00169 //Set Functions
00171 
00172 // Set the cache file path
00173 bool GLC_CacheManager::setCachePath(const QString& path)
00174 {
00175         QFileInfo pathInfo(path);
00176         bool result= pathInfo.isDir();
00177         result= result && pathInfo.isReadable();
00178 
00179         if (result)
00180         {
00181                 m_Dir.setPath(path);
00182         }
00183         return result;
00184 }
00185 

SourceForge.net Logo

©2005 Laurent Ribon