KSaveFile Class Reference
from PyKDE4.kdecore import *
Inherits: QFile → QIODevice → QObject
Detailed Description
\class KSaveFile ksavefile.h <KSaveFile>
Class to allow for atomic file I/O, as well as utility functions.
The KSaveFile class has been made to write out changes to an existing file atomically. This means that either ALL changes will be written to the file, or NO changes have been written, and the original file (if any) has been unchanged. This is useful if you have lots of time-consuming processing to perform during which an interruption could occur, or if any error in the file structure will cause the entire file to be corrupt.
When you create a KSaveFile for a given file, a temporary file is instead created and all your I/O occurs in the save file. Once you call finalize() the temporary file is renamed to the target file, so that all your changes happen at once. If abort() is called then the temporary file is removed and the target file is untouched. KSaveFile derives from QFile so you can use it just as you would a normal QFile.
This class also includes several static utility functions available that can help ensure data integrity. See the individual functions for details.
Here is a quick example of how to use KSaveFile:
First we create the KSaveFile and open it.
KSaveFile saveFile; saveFile.setFileName("/lib/foo/bar.dat"); if ( !saveFile.open() ) { //Handle error }
At this point the file "/lib/foo/bar.dat" has not been altered in any way. Now, let's write out some data to the file.
QTextStream stream ( &saveFile ); stream << "Add some data."; // Perform long processing stream << "Add some more data."; stream.flush();
Even after writing this data, the target file "/lib/foo/bar.dat" still has not been altered in any way. Now that we are done writing our data, we can write out all the changes that we have made by calling finalize().
if ( !saveFile.finalize() ) { //Handle error }
If a user interruption or error occurred while we were writing out our changes, we would instead call abort() to cancel all the I/O without affecting the target file.
- See also:
- QFile
Methods | |
__init__ (self) | |
__init__ (self, QString filename, KComponentData componentData=KGlobal.mainComponent()) | |
__init__ (self, KSaveFile a0) | |
abort (self) | |
QFile.FileError | error (self) |
QString | errorString (self) |
QString | fileName (self) |
bool | finalize (self) |
bool | open (self, QIODevice.OpenMode flags=QIODevice.ReadWrite) |
setFileName (self, QString filename) | |
Static Methods | |
bool | backupFile (QString filename, QString backupDir=QString()) |
bool | numberedBackupFile (QString filename, QString backupDir=QString(), QString backupExtension=QString.fromLatin1("~"), long maxBackups=10) |
bool | rcsBackupFile (QString filename, QString backupDir=QString(), QString backupMessage=QString()) |
bool | simpleBackupFile (QString filename, QString backupDir=QString(), QString backupExtension=QLatin1String("~")) |
Method Documentation
__init__ | ( | self ) |
Default constructor.
__init__ | ( | self, | ||
QString | filename, | |||
KComponentData | componentData=KGlobal.mainComponent() | |||
) |
Creates a new KSaveFile and sets the target file to filename.
- Parameters:
-
filename the path of the file componentData The KComponentData to use for the temporary file.
__init__ | ( | self, | ||
KSaveFile | a0 | |||
) |
Default constructor.
abort | ( | self ) |
Discard changes without affecting the target file. This will discard all changes that have been made to this file. The target file will not be altered in any way.
QFile.FileError error | ( | self ) |
Returns the last error that occurred. Use this function to check for errors.
- Returns:
- The last error that occurred, or QFile.NoError.
QString errorString | ( | self ) |
Returns a human-readable description of the last error. Use this function to get a human-readable description of the last error that occurred.
- Returns:
- A string describing the last error that occurred.
QString fileName | ( | self ) |
Returns the name of the target file. This function returns the name of the target file, or an empty QString if it has not yet been set.
- Returns:
- The name of the target file.
bool finalize | ( | self ) |
Finalize changes to the file. This will commit all the changes that have been made to the file.
- Returns:
- true if successful, or false if an error has occurred.
bool open | ( | self, | ||
QIODevice.OpenMode | flags=QIODevice.ReadWrite | |||
) |
Open the save file. This function will open the save file by creating a temporary file to write to. It will also check to ensure that there are sufficient permissions to write to the target file.
- Parameters:
-
flags Sets the QIODevice.OpenMode. It should contain the write flag, otherwise you have a save file you cannot save to.
- Returns:
- true if successful, or false if an error has occurred.
setFileName | ( | self, | ||
QString | filename | |||
) |
Set the target filename for the save file. You must use this to set the filename of the target file if you do not use the contructor that does so.
- Parameters:
-
filename Name of the target file.
Static Method Documentation
Static method to create a backup file before saving.
If empty (the default), the backup will be in the same directory as filename. The backup type (simple, rcs, or numbered), extension string, and maximum number of backup files are read from the user's global configuration. Use simpleBackupFile() or numberedBackupFile() to force one of these specific backup styles. You can use this method even if you don't use KSaveFile.
- Parameters:
-
filename the file to backup backupDir optional directory where to save the backup file in.
- Returns:
- true if successful, or false if an error has occurred.
bool numberedBackupFile | ( | QString | filename, | |
QString | backupDir=QString(), | |||
QString | backupExtension=QString.fromLatin1("~"), | |||
long | maxBackups=10 | |||
) |
Static method to create a backup file for a given filename.
This function creates a series of numbered backup files from the given filename.
The backup file names will be of the form: <name>.<number><extension> for instance <pre> chat.3.log </pre>
The new backup file will be have the backup number 1. Each existing backup file will have its number incremented by 1. Any backup files with numbers greater than the maximum number permitted (@p maxBackups) will be removed. You can use this method even if you don't use KSaveFile.
- Parameters:
-
filename the file to backup backupDir optional directory where to save the backup file in. If empty (the default), the backup will be in the same directory as filename. backupExtension the extension to append to filename, which is "~" by default. Do not use an extension containing digits. maxBackups the maximum number of backup files permitted. For best performance a small number (10) is recommended.
- Returns:
- true if successful, or false if an error has occurred.
bool rcsBackupFile | ( | QString | filename, | |
QString | backupDir=QString(), | |||
QString | backupMessage=QString() | |||
) |
Static method to create an rcs backup file for a given filename.
This function creates a rcs-formatted backup file from the given filename.
The backup file names will be of the form: <name>,v for instance <pre> photo.jpg,v </pre>
The new backup file will be in RCS format. Each existing backup file will be committed as a new revision. You can use this method even if you don't use KSaveFile.
- Parameters:
-
filename the file to backup backupDir optional directory where to save the backup file in. If empty (the default), the backup will be in the same directory as filename. backupMessage is the RCS commit message for this revision.
- Returns:
- true if successful, or false if an error has occurred.
bool simpleBackupFile | ( | QString | filename, | |
QString | backupDir=QString(), | |||
QString | backupExtension=QLatin1String("~") | |||
) |
Static method to create a backup file for a given filename.
This function creates a backup file from the given filename. You can use this method even if you don't use KSaveFile.
- Parameters:
-
filename the file to backup backupDir optional directory where to save the backup file in. If empty (the default), the backup will be in the same directory as filename. backupExtension the extension to append to filename, "~" by default.
- Returns:
- true if successful, or false if an error has occurred.