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  *****************************************************************************/
00023 
00024 #include "glc_cachemanager.h"
00025 #include <QtDebug>
00026 
00027 
00028 GLC_CacheManager::GLC_CacheManager(const QString& path)
00029 : m_Dir()
00030 , m_UseCompression(true)
00031 , m_CompressionLevel(-1)
00032 {
00033         if (! path.isEmpty())
00034         {
00035                 QFileInfo pathInfo(path);
00036                 if (pathInfo.isDir() && pathInfo.isReadable())
00037                 {
00038                         m_Dir.setPath(path);
00039                 }
00040         }
00041 }
00042 
00043 // Copy constructor
00044 GLC_CacheManager::GLC_CacheManager(const GLC_CacheManager& cacheManager)
00045 :m_Dir(cacheManager.m_Dir)
00046 , m_UseCompression(cacheManager.m_UseCompression)
00047 , m_CompressionLevel(cacheManager.m_CompressionLevel)
00048 {
00049 
00050 }
00051 
00052 // Assignement operator
00053 GLC_CacheManager& GLC_CacheManager::operator=(const GLC_CacheManager& cacheManager)
00054 {
00055         m_Dir= cacheManager.m_Dir;
00056         m_UseCompression= cacheManager.m_UseCompression;
00057         m_CompressionLevel= cacheManager.m_CompressionLevel;
00058 
00059         return *this;
00060 }
00061 
00062 GLC_CacheManager::~GLC_CacheManager()
00063 {
00064 
00065 }
00066 
00068 // Get Functions
00070 
00071 // Return true if the cache is is readable
00072 bool GLC_CacheManager::isReadable() const
00073 {
00074         bool isReadable= true;
00075         isReadable= isReadable && m_Dir.exists();
00076         QFileInfo dirInfo(m_Dir.absolutePath());
00077         isReadable= isReadable && dirInfo.isReadable();
00078 
00079         return isReadable;
00080 
00081 }
00082 
00083 // Return true if the cache is writable
00084 bool GLC_CacheManager::isWritable() const
00085 {
00086         bool isWritable= true;
00087         isWritable= isWritable && m_Dir.exists();
00088         QFileInfo dirInfo(m_Dir.absolutePath());
00089         isWritable= isWritable && dirInfo.isWritable();
00090 
00091         return isWritable;
00092 }
00093 
00094 // Return True if the specified file is cashed in the specified context
00095 bool GLC_CacheManager::isCashed(const QString& context, const QString& fileName) const
00096 {
00097         if (! isReadable()) return false;
00098 
00099         QFileInfo fileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName + '.' + GLC_BSRep::suffix());
00100         return fileInfo.exists();
00101 }
00102 
00103 // Return True if the cached file is usable
00104 bool GLC_CacheManager::isUsable(const QDateTime& timeStamp, const QString& context, const QString& fileName) const
00105 {
00106         bool result= isCashed(context, fileName);
00107 
00108         if (result)
00109         {
00110                 QFileInfo cacheFileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName+ '.' + GLC_BSRep::suffix());
00111                 //result= result && (timeStamp == cacheFileInfo.lastModified());
00112                 result= result && cacheFileInfo.isReadable();
00113                 if (result)
00114                 {
00115                         GLC_BSRep binaryRep;
00116                         binaryRep.setAbsoluteFileName(cacheFileInfo.absoluteFilePath());
00117                         result= result && binaryRep.isUsable(timeStamp);
00118                 }
00119         }
00120 
00121         return result;
00122 }
00123 
00124 // Return the binary serialized representation of the specified file
00125 GLC_BSRep GLC_CacheManager::binary3DRep(const QString& context, const QString& fileName) const
00126 {
00127         const QString absoluteFileName(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName + '.' + GLC_BSRep::suffix());
00128         GLC_BSRep binaryRep(absoluteFileName);
00129 
00130         return binaryRep;
00131 }
00132 
00133 // Add the specified file in the cache
00134 bool GLC_CacheManager::addToCache(const QString& context, const GLC_3DRep& rep)
00135 {
00136         Q_ASSERT(!rep.fileName().isEmpty());
00137         bool addedToCache= isWritable();
00138         if (addedToCache)
00139         {
00140                 QFileInfo contextCacheInfo(m_Dir.absolutePath() + QDir::separator() + context);
00141                 if (! contextCacheInfo.exists())
00142                 {
00143                         addedToCache= m_Dir.mkdir(context);
00144                 }
00145                 if (addedToCache)
00146                 {
00147                         QString repFileName= rep.fileName();
00148                         if (glc::isArchiveString(repFileName))
00149                         {
00150                                 repFileName= glc::archiveEntryFileName(repFileName);
00151                         }
00152                         else
00153                         {
00154                                 repFileName= QFileInfo(repFileName).fileName();
00155                         }
00156                         const QString binaryFileName= contextCacheInfo.filePath() + QDir::separator() + repFileName;
00157                         GLC_BSRep binariRep(binaryFileName, m_UseCompression);
00158                         binariRep.setCompressionLevel(m_CompressionLevel);
00159                         addedToCache= binariRep.save(rep);
00160                 }
00161         }
00162 
00163         return addedToCache;
00164 }
00165 
00167 //Set Functions
00169 
00170 // Set the cache file path
00171 bool GLC_CacheManager::setCachePath(const QString& path)
00172 {
00173         QFileInfo pathInfo(path);
00174         bool result= pathInfo.isDir();
00175         result= result && pathInfo.isReadable();
00176 
00177         if (result)
00178         {
00179                 m_Dir.setPath(path);
00180         }
00181         return result;
00182 }
00183 

SourceForge.net Logo

©2005-2011 Laurent Ribon