#include <qtemporaryfile.h>
Inheritance diagram for QTemporaryFile:


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().
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 QAbstractFileEngine * | fileEngine () const |
Static Public Member Functions | |
| static QTemporaryFile * | createLocalFile (const QString &fileName) |
| static QTemporaryFile * | createLocalFile (QFile &file) |
Protected Member Functions | |
| bool | open (OpenMode flags) |
| 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.
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.
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".
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:

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.
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.
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:

| 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.
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.
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.
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.
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().
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.
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:

1.5.1