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
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
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
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
00071
00072
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
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
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
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
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
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
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
00171
00172
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