glc_cachemanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00069
00070
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
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
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
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
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
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
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
00168
00169
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