Qt 4.8
Public Functions | Public Variables | List of all members
QCacheItem Class Reference

#include <qnetworkdiskcache_p.h>

Public Functions

bool canCompress () const
 We compress small text and JavaScript files. More...
 
 QCacheItem ()
 
bool read (QFile *device, bool readData)
 Returns false if the file is a cache file, but is an older version and should be removed otherwise true. More...
 
void reset ()
 
qint64 size () const
 
void writeCompressedData (QFile *device) const
 
void writeHeader (QFile *device) const
 
 ~QCacheItem ()
 

Public Variables

QBuffer data
 
QTemporaryFilefile
 
QNetworkCacheMetaData metaData
 

Detailed Description

Definition at line 68 of file qnetworkdiskcache_p.h.

Constructors and Destructors

◆ QCacheItem()

QCacheItem::QCacheItem ( )
inline

Definition at line 71 of file qnetworkdiskcache_p.h.

71  : file(0)
72  {
73  }
QTemporaryFile * file

◆ ~QCacheItem()

QCacheItem::~QCacheItem ( )
inline

Definition at line 74 of file qnetworkdiskcache_p.h.

75  {
76  reset();
77  }

Functions

◆ canCompress()

bool QCacheItem::canCompress ( ) const

We compress small text and JavaScript files.

Definition at line 638 of file qnetworkdiskcache.cpp.

Referenced by QNetworkDiskCache::prepare(), and reset().

639 {
640  bool sizeOk = false;
641  bool typeOk = false;
643  if (header.first.toLower() == "content-length") {
644  qint64 size = header.second.toLongLong();
645  if (size > MAX_COMPRESSION_SIZE)
646  return false;
647  else
648  sizeOk = true;
649  }
650 
651  if (header.first.toLower() == "content-type") {
652  QByteArray type = header.second;
653  if (type.startsWith("text/")
654  || (type.startsWith("application/")
655  && (type.endsWith("javascript") || type.endsWith("ecmascript"))))
656  typeOk = true;
657  else
658  return false;
659  }
660  if (sizeOk && typeOk)
661  return true;
662  }
663  return false;
664 }
qint64 size() const
int type
Definition: qmetatype.cpp:239
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
RawHeaderList rawHeaders() const
Returns a list of all raw headers that are set in this meta data.
QNetworkCacheMetaData metaData
#define MAX_COMPRESSION_SIZE
__int64 qint64
Definition: qglobal.h:942
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...

◆ read()

bool QCacheItem::read ( QFile device,
bool  readData 
)

Returns false if the file is a cache file, but is an older version and should be removed otherwise true.

Definition at line 694 of file qnetworkdiskcache.cpp.

Referenced by reset().

695 {
696  reset();
697 
698  QDataStream in(device);
699 
700  qint32 marker;
701  qint32 v;
702  in >> marker;
703  in >> v;
704  if (marker != CacheMagic)
705  return true;
706 
707  // If the cache magic is correct, but the version is not we should remove it
708  if (v != CurrentCacheVersion)
709  return false;
710 
711  bool compressed;
712  QByteArray dataBA;
713  in >> metaData;
714  in >> compressed;
715  if (readData && compressed) {
716  in >> dataBA;
717  data.setData(qUncompress(dataBA));
719  }
720 
721  // quick and dirty check if metadata's URL field and the file's name are in synch
722  QString expectedFilename = QNetworkDiskCachePrivate::uniqueFileName(metaData.url());
723  if (!device->fileName().endsWith(expectedFilename))
724  return false;
725 
726  return metaData.isValid();
727 }
static QString uniqueFileName(const QUrl &url)
Given a URL, generates a unique enough filename (and subdirectory)
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
int qint32
Definition: qglobal.h:937
Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes)
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
QNetworkCacheMetaData metaData
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796

◆ reset()

void QCacheItem::reset ( )
inline

Definition at line 85 of file qnetworkdiskcache_p.h.

Referenced by ~QCacheItem().

85  {
87  data.close();
88  delete file;
89  file = 0;
90  }
void close()
Reimplemented Function
Definition: qbuffer.cpp:359
QNetworkCacheMetaData metaData
QTemporaryFile * file
The QNetworkCacheMetaData class provides cache information.

◆ size()

qint64 QCacheItem::size ( ) const
inline

Definition at line 82 of file qnetworkdiskcache_p.h.

Referenced by QNetworkDiskCachePrivate::storeItem().

83  { return file ? file->size() : data.size(); }
qint64 size() const
Reimplemented Function
Definition: qbuffer.cpp:375
QTemporaryFile * file
qint64 size() const
Returns the size of the file.
Definition: qfile.cpp:1707

◆ writeCompressedData()

void QCacheItem::writeCompressedData ( QFile device) const

Definition at line 683 of file qnetworkdiskcache.cpp.

Referenced by reset(), and QNetworkDiskCachePrivate::storeItem().

684 {
685  QDataStream out(device);
686 
687  out << qCompress(data.data());
688 }
const QByteArray & data() const
Returns the data contained in the buffer.
Definition: qbuffer.cpp:301
Q_CORE_EXPORT QByteArray qCompress(const uchar *data, int nbytes, int compressionLevel=-1)
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71

◆ writeHeader()

void QCacheItem::writeHeader ( QFile device) const

Definition at line 672 of file qnetworkdiskcache.cpp.

Referenced by QNetworkDiskCache::prepare(), reset(), and QNetworkDiskCachePrivate::storeItem().

673 {
674  QDataStream out(device);
675 
676  out << qint32(CacheMagic);
677  out << qint32(CurrentCacheVersion);
678  out << metaData;
679  bool compressed = canCompress();
680  out << compressed;
681 }
int qint32
Definition: qglobal.h:937
QNetworkCacheMetaData metaData
bool canCompress() const
We compress small text and JavaScript files.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71

Properties

◆ data

QBuffer QCacheItem::data

◆ file

QTemporaryFile* QCacheItem::file

◆ metaData

QNetworkCacheMetaData QCacheItem::metaData

The documentation for this class was generated from the following files: