GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_cachemanager.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_cachemanager.h"
25 #include <QtDebug>
26 
27 
29 : m_Dir()
30 , m_UseCompression(true)
31 , m_CompressionLevel(-1)
32 {
33  if (! path.isEmpty())
34  {
35  QFileInfo pathInfo(path);
36  if (pathInfo.isDir() && pathInfo.isReadable())
37  {
38  m_Dir.setPath(path);
39  }
40  }
41 }
42 
43 // Copy constructor
45 :m_Dir(cacheManager.m_Dir)
46 , m_UseCompression(cacheManager.m_UseCompression)
47 , m_CompressionLevel(cacheManager.m_CompressionLevel)
48 {
49 
50 }
51 
52 // Assignement operator
54 {
55  m_Dir= cacheManager.m_Dir;
56  m_UseCompression= cacheManager.m_UseCompression;
58 
59  return *this;
60 }
61 
63 {
64 
65 }
66 
68 // Get Functions
70 
71 // Return true if the cache is is readable
73 {
74  bool isReadable= true;
75  isReadable= isReadable && m_Dir.exists();
76  QFileInfo dirInfo(m_Dir.absolutePath());
77  isReadable= isReadable && dirInfo.isReadable();
78 
79  return isReadable;
80 
81 }
82 
83 // Return true if the cache is writable
85 {
86  bool isWritable= true;
87  isWritable= isWritable && m_Dir.exists();
88  QFileInfo dirInfo(m_Dir.absolutePath());
89  isWritable= isWritable && dirInfo.isWritable();
90 
91  return isWritable;
92 }
93 
94 // Return True if the specified file is cashed in the specified context
95 bool GLC_CacheManager::isCashed(const QString& context, const QString& fileName) const
96 {
97  if (! isReadable()) return false;
98 
99  QFileInfo fileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName + '.' + GLC_BSRep::suffix());
100  return fileInfo.exists();
101 }
102 
103 // Return True if the cached file is usable
104 bool GLC_CacheManager::isUsable(const QDateTime& timeStamp, const QString& context, const QString& fileName) const
105 {
106  bool result= isCashed(context, fileName);
107 
108  if (result)
109  {
110  QFileInfo cacheFileInfo(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName+ '.' + GLC_BSRep::suffix());
111  //result= result && (timeStamp == cacheFileInfo.lastModified());
112  result= result && cacheFileInfo.isReadable();
113  if (result)
114  {
115  GLC_BSRep binaryRep;
116  binaryRep.setAbsoluteFileName(cacheFileInfo.absoluteFilePath());
117  result= result && binaryRep.isUsable(timeStamp);
118  }
119  }
120 
121  return result;
122 }
123 
124 // Return the binary serialized representation of the specified file
125 GLC_BSRep GLC_CacheManager::binary3DRep(const QString& context, const QString& fileName) const
126 {
127  const QString absoluteFileName(m_Dir.absolutePath() + QDir::separator() + context + QDir::separator() + fileName + '.' + GLC_BSRep::suffix());
128  GLC_BSRep binaryRep(absoluteFileName);
129 
130  return binaryRep;
131 }
132 
133 // Add the specified file in the cache
134 bool GLC_CacheManager::addToCache(const QString& context, const GLC_3DRep& rep)
135 {
136  Q_ASSERT(!rep.fileName().isEmpty());
137  bool addedToCache= isWritable();
138  if (addedToCache)
139  {
140  QFileInfo contextCacheInfo(m_Dir.absolutePath() + QDir::separator() + context);
141  if (! contextCacheInfo.exists())
142  {
143  addedToCache= m_Dir.mkdir(context);
144  }
145  if (addedToCache)
146  {
147  QString repFileName= rep.fileName();
148  if (glc::isArchiveString(repFileName))
149  {
150  repFileName= glc::archiveEntryFileName(repFileName);
151  }
152  else
153  {
154  repFileName= QFileInfo(repFileName).fileName();
155  }
156  const QString binaryFileName= contextCacheInfo.filePath() + QDir::separator() + repFileName;
157  GLC_BSRep binariRep(binaryFileName, m_UseCompression);
159  addedToCache= binariRep.save(rep);
160  }
161  }
162 
163  return addedToCache;
164 }
165 
167 //Set Functions
169 
170 // Set the cache file path
171 bool GLC_CacheManager::setCachePath(const QString& path)
172 {
173  QFileInfo pathInfo(path);
174  bool result= pathInfo.isDir();
175  result= result && pathInfo.isReadable();
176 
177  if (result)
178  {
179  m_Dir.setPath(path);
180  }
181  return result;
182 }
183 

©2005-2013 Laurent Ribon