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

SourceForge.net Logo

©2005-2011 Laurent Ribon