QTemporaryFile Class Reference

#include <qtemporaryfile.h>

Inheritance diagram for QTemporaryFile:

Inheritance graph
[legend]
Collaboration diagram for QTemporaryFile:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QTemporaryFile class is an I/O device that operates on temporary files.

QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling open(). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed to QTemporaryFile's constructor.

Example:

        {
            QTemporaryFile file;
            if (file.open()) {
                // file.fileName() returns the unique file name
            }

            // the QTemporaryFile destructor removes the temporary file
        }

Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.

A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename qt_temp will be placed into the temporary path as returned by QDir::tempPath().

See also:
QDir::tempPath(), QFile

Definition at line 40 of file qtemporaryfile.h.

Public Member Functions

 QTemporaryFile ()
 QTemporaryFile (const QString &templateName)
 QTemporaryFile (QObject *parent)
 QTemporaryFile (const QString &templateName, QObject *parent)
 ~QTemporaryFile ()
bool autoRemove () const
void setAutoRemove (bool b)
bool open ()
QString fileName () const
QString fileTemplate () const
void setFileTemplate (const QString &name)
virtual QAbstractFileEnginefileEngine () const

Static Public Member Functions

static QTemporaryFilecreateLocalFile (const QString &fileName)
static QTemporaryFilecreateLocalFile (QFile &file)

Protected Member Functions

bool open (OpenMode flags)


Constructor & Destructor Documentation

QTemporaryFile::QTemporaryFile (  ) 

Constructs a QTemporaryFile in QDir::tempPath(), using the file template "qt_temp.XXXXXX". The file is stored in the system's temporary directory.

See also:
setFileTemplate(), QDir::tempPath()

Definition at line 364 of file qtemporaryfile.cpp.

References d, and QDir::tempPath().

00365     : QFile(*new QTemporaryFilePrivate, 0)
00366 {
00367     Q_D(QTemporaryFile);
00368     d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");
00369 }

Here is the call graph for this function:

QTemporaryFile::QTemporaryFile ( const QString templateName  )  [explicit]

Constructs a QTemporaryFile with a template filename of templateName. Upon opening the temporary file this will be used to create a unique filename. If the templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

If templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct templateName if you want use the system's temporary directory.

See also:
open(), fileTemplate()

Definition at line 383 of file qtemporaryfile.cpp.

References setFileTemplate().

00384     : QFile(*new QTemporaryFilePrivate, 0)
00385 {
00386     setFileTemplate(templateName);
00387 }

Here is the call graph for this function:

QTemporaryFile::QTemporaryFile ( QObject parent  )  [explicit]

Constructs a QTemporaryFile (with the given parent) in QDir::tempPath(), using the file template "qt_temp.XXXXXX".

See also:
setFileTemplate()

Definition at line 395 of file qtemporaryfile.cpp.

References d, and QDir::tempPath().

00396     : QFile(*new QTemporaryFilePrivate, parent)
00397 {
00398     Q_D(QTemporaryFile);
00399     d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");
00400 }

Here is the call graph for this function:

QTemporaryFile::QTemporaryFile ( const QString templateName,
QObject parent 
)

Constructs a QTemporaryFile with a template filename of templateName and the specified parent. Upon opening the temporary file this will be used to create a unique filename. If the templateName does end in XXXXXX it will automatically be appended and used as the dynamic portion of the filename.

If templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct templateName if you want use the system's temporary directory.

See also:
open(), fileTemplate()

Definition at line 416 of file qtemporaryfile.cpp.

References setFileTemplate().

00417     : QFile(*new QTemporaryFilePrivate, parent)
00418 {
00419     setFileTemplate(templateName);
00420 }

Here is the call graph for this function:

QTemporaryFile::~QTemporaryFile (  ) 

Destroys the temporary file object, the file is automatically closed if necessary and if in auto remove mode it will automatically delete the file.

See also:
autoRemove()

Definition at line 430 of file qtemporaryfile.cpp.

References QFile::close(), d, and QFile::remove().

00431 {
00432     Q_D(QTemporaryFile);
00433     close();
00434     if (!d->fileName.isEmpty() && d->autoRemove)
00435         remove();
00436 }

Here is the call graph for this function:


Member Function Documentation

bool QTemporaryFile::autoRemove (  )  const

Returns true if the QTemporaryFile is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your QTemporaryFile object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself.

Auto-remove is on by default.

See also:
setAutoRemove(), remove()

Definition at line 461 of file qtemporaryfile.cpp.

References d.

00462 {
00463     Q_D(const QTemporaryFile);
00464     return d->autoRemove;
00465 }

void QTemporaryFile::setAutoRemove ( bool  b  ) 

Sets the QTemporaryFile into auto-remove mode if b is true.

Auto-remove is on by default.

See also:
autoRemove(), remove()

Definition at line 474 of file qtemporaryfile.cpp.

References d.

00475 {
00476     Q_D(QTemporaryFile);
00477     d->autoRemove = b;
00478 }

bool QTemporaryFile::open (  )  [inline]

A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the fileName() to the unique filename used.

See also:
fileName()

Definition at line 60 of file qtemporaryfile.h.

References QIODevice::ReadWrite.

Referenced by QFile::copy(), createLocalFile(), defaultMacros(), QConfFileSettingsPrivate::isWritable(), and registerFont().

00060 { return open(QIODevice::ReadWrite); }

QString QTemporaryFile::fileName (  )  const

Returns the complete unique filename backing the QTemporaryFile object. This string is null before the QTemporaryFile is opened, afterwards it will contain the fileTemplate() plus additional characters to make it unique.

See also:
fileTemplate()

Reimplemented from QFile.

Definition at line 489 of file qtemporaryfile.cpp.

References QAbstractFileEngine::DefaultName, fileEngine(), QAbstractFileEngine::fileName(), and QIODevice::isOpen().

Referenced by defaultMacros(), and registerFont().

00490 {
00491     if(!isOpen())
00492         return QString();
00493     return fileEngine()->fileName(QAbstractFileEngine::DefaultName);
00494 }

Here is the call graph for this function:

QString QTemporaryFile::fileTemplate (  )  const

Returns the set file template. The default file template will be called qt_temp and be placed in QDir::tempPath().

See also:
setFileTemplate()

Definition at line 502 of file qtemporaryfile.cpp.

References d.

00503 {
00504     Q_D(const QTemporaryFile);
00505     return d->templateName;
00506 }

void QTemporaryFile::setFileTemplate ( const QString name  ) 

Sets the static portion of the file name to name. If the file template ends in XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be determined automatically based on the static portion specified.

If name contains a relative file path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct name if you want use the system's temporary directory.

See also:
fileTemplate()

Definition at line 520 of file qtemporaryfile.cpp.

References d, fileEngine(), QIODevice::isOpen(), name, and QAbstractFileEngine::setFileName().

Referenced by QTemporaryFile().

00521 {
00522     Q_ASSERT(!isOpen());
00523     Q_D(QTemporaryFile);
00524     fileEngine()->setFileName(name);
00525     d->templateName = name;
00526 }

Here is the call graph for this function:

QTemporaryFile * QTemporaryFile::createLocalFile ( const QString fileName  )  [inline, static]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Works on the given fileName rather than an existing QFile object.

Definition at line 66 of file qtemporaryfile.h.

00067         { QFile file(fileName); return createLocalFile(file); }

QTemporaryFile * QTemporaryFile::createLocalFile ( QFile file  )  [static]

Creates and returns a local temporary file whose contents are a copy of the contents of the given file.

Definition at line 541 of file qtemporaryfile.cpp.

References buffer, QFile::close(), QFile::fileEngine(), QAbstractFileEngine::FlagsMask, QIODevice::isOpen(), len, QAbstractFileEngine::LocalDiskFlag, open(), QFile::open(), QFile::pos(), QIODevice::read(), QIODevice::ReadOnly, QFile::seek(), and QIODevice::write().

00542 {
00543     if (QAbstractFileEngine *engine = file.fileEngine()) {
00544         if(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag)
00545             return 0; //local already
00546         //cache
00547         bool wasOpen = file.isOpen();
00548         qint64 old_off = 0;
00549         if(wasOpen)
00550             old_off = file.pos();
00551         else
00552             file.open(QIODevice::ReadOnly);
00553         //dump data
00554         QTemporaryFile *ret = new QTemporaryFile;
00555         ret->open();
00556         file.seek(0);
00557         char buffer[1024];
00558         while(true) {
00559             qint64 len = file.read(buffer, 1024);
00560             if(len < 1)
00561                 break;
00562             ret->write(buffer, len);
00563         }
00564         ret->seek(0);
00565         //restore
00566         if(wasOpen)
00567             file.seek(old_off);
00568         else
00569             file.close();
00570         //done
00571         return ret;
00572     }
00573     return 0;
00574 }

Here is the call graph for this function:

QAbstractFileEngine * QTemporaryFile::fileEngine (  )  const [virtual]

Reimplemented from QFile.

Definition at line 580 of file qtemporaryfile.cpp.

References d.

Referenced by fileName(), and setFileTemplate().

00581 {
00582     Q_D(const QTemporaryFile);
00583     if(!d->fileEngine)
00584         d->fileEngine = new QTemporaryFileEngine(d->templateName);
00585     return d->fileEngine;
00586 }

bool QTemporaryFile::open ( OpenMode  flags  )  [protected, virtual]

Creates a unique file name for the temporary file, and opens it. You can get the unique name later by calling fileName(). The file is guaranteed to have been created by this function (i.e., it has never existed before).

Reimplemented from QFile.

Definition at line 595 of file qtemporaryfile.cpp.

References d, QAbstractFileEngine::DefaultName, QFile::open(), and QIODevice::setOpenMode().

00596 {
00597     Q_D(QTemporaryFile);
00598     if (!d->fileName.isEmpty()) {
00599         setOpenMode(flags);
00600         return true;
00601     }
00602 
00603     if (QFile::open(flags)) {
00604         d->fileName = d->fileEngine->fileName(QAbstractFileEngine::DefaultName);
00605         return true;
00606     }
00607     return false;
00608 }

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 19:19:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1