• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

Public Member Functions | Static Public Member Functions | List of all members
KAutoSaveFile Class Reference

#include <KAutoSaveFile>

Inheritance diagram for KAutoSaveFile:
Inheritance graph
[legend]

Public Member Functions

 KAutoSaveFile (const KUrl &filename, QObject *parent=0)
 
 KAutoSaveFile (QObject *parent=0)
 
 ~KAutoSaveFile ()
 
KUrl managedFile () const
 
virtual bool open (OpenMode openmode)
 
virtual void releaseLock ()
 
void setManagedFile (const KUrl &filename)
 
- Public Member Functions inherited from QFile
 QFile (const QString &name)
 
 QFile (QObject *parent)
 
 QFile (const QString &name, QObject *parent)
 
 ~QFile ()
 
virtual bool atEnd () const
 
virtual void close ()
 
bool copy (const QString &newName)
 
FileError error () const
 
bool exists () const
 
QString fileName () const
 
bool flush ()
 
int handle () const
 
virtual bool isSequential () const
 
bool link (const QString &linkName)
 
uchar * map (qint64 offset, qint64 size, MemoryMapFlags flags)
 
QString name () const
 
bool open (QFlags< QIODevice::OpenModeFlag > aFlags, int fd)
 
bool open (FILE *fh, QFlags< QIODevice::OpenModeFlag > mode)
 
bool open (int fd, QFlags< QIODevice::OpenModeFlag > mode)
 
bool open (const RFile &f, QFlags< QIODevice::OpenModeFlag > mode, QFlags< QFile::FileHandleFlag > handleFlags)
 
bool open (FILE *fh, QFlags< QIODevice::OpenModeFlag > mode, QFlags< QFile::FileHandleFlag > handleFlags)
 
bool open (int fd, QFlags< QIODevice::OpenModeFlag > mode, QFlags< QFile::FileHandleFlag > handleFlags)
 
bool open (QFlags< QIODevice::OpenModeFlag > aFlags, FILE *f)
 
virtual bool open (QFlags< QIODevice::OpenModeFlag > mode)
 
Permissions permissions () const
 
virtual qint64 pos () const
 
QString readLink () const
 
bool remove ()
 
bool rename (const QString &newName)
 
bool resize (qint64 sz)
 
virtual bool seek (qint64 pos)
 
void setFileName (const QString &name)
 
void setName (const QString &name)
 
bool setPermissions (QFlags< QFile::Permission > permissions)
 
virtual qint64 size () const
 
QString symLinkTarget () const
 
bool unmap (uchar *address)
 
void unsetError ()
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 
 QIODevice (QObject *parent)
 
virtual  ~QIODevice ()
 
void aboutToClose ()
 
Offset at () const
 
bool at (Offset offset)
 
virtual qint64 bytesAvailable () const
 
virtual qint64 bytesToWrite () const
 
void bytesWritten (qint64 bytes)
 
virtual bool canReadLine () const
 
QString errorString () const
 
int flags () const
 
int getch ()
 
bool getChar (char *c)
 
bool isAsynchronous () const
 
bool isBuffered () const
 
bool isCombinedAccess () const
 
bool isDirectAccess () const
 
bool isInactive () const
 
bool isOpen () const
 
bool isRaw () const
 
bool isReadable () const
 
bool isSequentialAccess () const
 
bool isSynchronous () const
 
bool isTextModeEnabled () const
 
bool isTranslated () const
 
bool isWritable () const
 
int mode () const
 
OpenMode openMode () const
 
qint64 peek (char *data, qint64 maxSize)
 
QByteArray peek (qint64 maxSize)
 
int putch (int ch)
 
bool putChar (char c)
 
qint64 read (char *data, qint64 maxSize)
 
QByteArray read (qint64 maxSize)
 
QByteArray readAll ()
 
qint64 readBlock (char *data, quint64 size)
 
void readChannelFinished ()
 
QByteArray readLine (qint64 maxSize)
 
qint64 readLine (char *data, qint64 maxSize)
 
void readyRead ()
 
virtual bool reset ()
 
void resetStatus ()
 
void setTextModeEnabled (bool enabled)
 
int state () const
 
Status status () const
 
int ungetch (int ch)
 
void ungetChar (char c)
 
virtual bool waitForBytesWritten (int msecs)
 
virtual bool waitForReadyRead (int msecs)
 
qint64 write (const char *data)
 
qint64 write (const char *data, qint64 maxSize)
 
qint64 write (const QByteArray &byteArray)
 
qint64 writeBlock (const QByteArray &data)
 
qint64 writeBlock (const char *data, quint64 size)
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
 QObject (QObject *parent, const char *name)
 
virtual  ~QObject ()
 
bool blockSignals (bool block)
 
QObject * child (const char *objName, const char *inheritsClass, bool recursiveSearch) const
 
const QObjectList & children () const
 
const char * className () const
 
bool connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const QObject *receiver, const char *method)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method)
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QByteArray > dynamicPropertyNames () const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
T findChild (const QString &name) const
 
QList< T > findChildren (const QRegExp &regExp) const
 
QList< T > findChildren (const QString &name) const
 
bool inherits (const char *className) const
 
void insertChild (QObject *object)
 
void installEventFilter (QObject *filterObj)
 
bool isA (const char *className) const
 
bool isWidgetType () const
 
void killTimer (int id)
 
virtual const QMetaObject * metaObject () const
 
void moveToThread (QThread *targetThread)
 
const char * name () const
 
const char * name (const char *defaultName) const
 
QString objectName () const
 
QObject * parent () const
 
QVariant property (const char *name) const
 
void removeChild (QObject *object)
 
void removeEventFilter (QObject *obj)
 
void setName (const char *name)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval)
 
QThread * thread () const
 

Static Public Member Functions

static QList< KAutoSaveFile * > allStaleFiles (const QString &applicationName=QString())
 
static QList< KAutoSaveFile * > staleFiles (const KUrl &filename, const QString &applicationName=QString())
 
- Static Public Member Functions inherited from QFile
bool copy (const QString &fileName, const QString &newName)
 
QString decodeName (const QByteArray &localFileName)
 
QString decodeName (const char *localFileName)
 
QByteArray encodeName (const QString &fileName)
 
bool exists (const QString &fileName)
 
bool link (const QString &fileName, const QString &linkName)
 
Permissions permissions (const QString &fileName)
 
QString readLink (const QString &fileName)
 
bool remove (const QString &fileName)
 
bool rename (const QString &oldName, const QString &newName)
 
bool resize (const QString &fileName, qint64 sz)
 
void setDecodingFunction (DecoderFn function)
 
void setEncodingFunction (EncoderFn function)
 
bool setPermissions (const QString &fileName, QFlags< QFile::Permission > permissions)
 
QString symLinkTarget (const QString &fileName)
 
- Static Public Member Functions inherited from QObject
bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 

Additional Inherited Members

- Public Attributes inherited from QFile
typedef DecoderFn
 
typedef EncoderFn
 
typedef FileHandleFlags
 
typedef Permissions
 
typedef PermissionSpec
 
- Public Attributes inherited from QIODevice
typedef Offset
 
typedef OpenMode
 
typedef Status
 
- Protected Member Functions inherited from QFile
virtual qint64 readData (char *data, qint64 len)
 
virtual qint64 readLineData (char *data, qint64 maxlen)
 
virtual qint64 writeData (const char *data, qint64 len)
 
- Protected Member Functions inherited from QIODevice
virtual qint64 readData (char *data, qint64 maxSize)=0
 
void setErrorString (const QString &str)
 
void setOpenMode (QFlags< QIODevice::OpenModeFlag > openMode)
 
virtual qint64 writeData (const char *data, qint64 maxSize)=0
 
- Protected Member Functions inherited from QObject
bool checkConnectArgs (const char *signal, const QObject *object, const char *method)
 
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const char *signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const char *signal)
 
int receivers (const char *signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Static Protected Member Functions inherited from QObject
QByteArray normalizeSignalSlot (const char *signalSlot)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

Creates and manages a temporary "auto-save" file.

Autosave files are temporary files that applications use to store the unsaved data in a file they have open for editing. KAutoSaveFile allows you to easily create and manage such files, as well as to recover the unsaved data left over by a crashed or otherwise gone process.

Each KAutoSaveFile object is associated with one specific file that the application holds open. KAutoSaveFile is also a QObject, so it can be reparented to the actual opened file object, so as to manage the lifetime of the temporary file.

Typical use consists of:

  • verifying whether stale autosave files exist for the opened file
  • deciding whether to recover the old, autosaved data
  • if not recovering, creating a KAutoSaveFile object for the opened file
  • during normal execution of the program, periodically save unsaved data into the KAutoSaveFile file.

KAutoSaveFile holds a lock on the autosave file, so it's safe to delete the file and recreate it later. Because of that, disposing of stale autosave files should be done with releaseLock(). No lock is held on the managed file.

Examples: Opening a new file:

void Document::open(const KUrl &url)
{
// check whether autosave files exist:
QList<KAutoSaveFile *> staleFiles = KAutoSaveFile::staleFiles(url);
if (!staleFiles.isEmpty()) {
if (KMessageBox::questionYesNo(parent,
"Auto-saved files exist. Do you want to recover them now?",
"File Recovery",
"Recover", "Don't recover") == KMessage::Yes) {
recoverFiles(staleFiles);
return;
} else {
// remove the stale files
foreach (KAutoSaveFile *stale, staleFiles) {
stale->open(QIODevice::ReadWrite);
delete stale;
}
}
}
// create new autosave object
m_autosave = new KAutoSaveFile(url, this);
// continue the process of opening file 'url'
...
}

The function recoverFiles could loop over the list of files and do this:

foreach (KAutoSaveFile *stale, staleFiles) {
if (!stale->open(QIODevice::ReadWrite)) {
// show an error message; we could not steal the lockfile
// maybe another application got to the file before us?
delete stale;
continue;
}
Document *doc = new Document;
doc->m_autosave = stale;
stale->setParent(doc); // reparent
doc->setUrl(stale->managedFile());
doc->setContents(stale->readAll());
doc->setState(Document::Modified); // mark it as modified and unsaved
documentManager->addDocument(doc);
}

If the file is unsaved, periodically write the contents to the save file:

if (!m_autosave->isOpen() && !m_autosave->open(QIODevice::ReadWrite)) {
// show error: could not open the autosave file
}
m_autosave->write(contents());

When the user saves the file, the autosaved file is no longer necessary and can be removed or emptied.

m_autosave->resize(0); // leaves the file open
m_autosave->remove(); // closes the file
Author
Jacob R Rideout kde@j.nosp@m.acob.nosp@m.rideo.nosp@m.ut.n.nosp@m.et

Definition at line 129 of file kautosavefile.h.

Constructor & Destructor Documentation

KAutoSaveFile::KAutoSaveFile ( const KUrl &  filename,
QObject *  parent = 0 
)
explicit

Constructs a KAutoSaveFile for file filename.

The temporary file is not opened or created until actually needed. The file filename does not have to exist for KAutoSaveFile to be constructed (if it exists, it will not be touched).

Parameters
filenamethe filename that this KAutoSaveFile refers to
parentthe parent object

Definition at line 71 of file kautosavefile.cpp.

KAutoSaveFile::KAutoSaveFile ( QObject *  parent = 0)
explicit

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Constructs a KAutoSaveFile object.

Note that you need to call setManagedFile() before calling open().

Parameters
parentthe parent object

Definition at line 79 of file kautosavefile.cpp.

KAutoSaveFile::~KAutoSaveFile ( )

Destroys the KAutoSaveFile object, removes the autosave file and drops the lock being held (if any).

Definition at line 86 of file kautosavefile.cpp.

Member Function Documentation

QList< KAutoSaveFile * > KAutoSaveFile::allStaleFiles ( const QString &  applicationName = QString())
static

Returns all stale autosave files left behind by crashed or otherwise gone instances of this application.

If not given, the application name is obtained from QCoreApplication, so be sure to have set it correctly before calling this function.

See staleFiles() for information on the returned objects.

Definition at line 190 of file kautosavefile.cpp.

KUrl KAutoSaveFile::managedFile ( ) const

Retrieves the URL of the file managed by KAutoSaveFile.

This is the same URL that was given to setManagedFile() or the KAutoSaveFile constructor.

This is the name of the real file being edited by the application. To get the name of the temporary file where data can be saved, use fileName() (after you have called open()).

Definition at line 92 of file kautosavefile.cpp.

bool KAutoSaveFile::open ( OpenMode  openmode)
virtual

Opens the autosave file and locks it if it wasn't already locked.

The name of the temporary file where data can be saved to will be set by this function and can be retrieved with fileName(). It will not change unless releaseLock() is called. No other application will attempt to edit such a file either while the lock is held.

Parameters
openmodethe mode that should be used to open the file, probably QIODevice::ReadWrite
Returns
true if the file could be opened (= locked and created), false if the operation failed

Definition at line 115 of file kautosavefile.cpp.

void KAutoSaveFile::releaseLock ( )
virtual

Closes the autosave file resource and removes the lock file.

The file name returned by fileName() will no longer be protected and can be overwritten by another application at any time. To obtain a new lock, call open() again.

This function calls remove(), so the autosave temporary file will be removed too.

Definition at line 105 of file kautosavefile.cpp.

void KAutoSaveFile::setManagedFile ( const KUrl &  filename)

Sets the URL of the file managed by KAutoSaveFile.

This should be the name of the real file being edited by the application. If the file was previously set, this function calls releaseLock().

Parameters
filenamethe filename that this KAutoSaveFile refers to

Definition at line 97 of file kautosavefile.cpp.

QList< KAutoSaveFile * > KAutoSaveFile::staleFiles ( const KUrl &  filename,
const QString &  applicationName = QString() 
)
static

Checks for stale autosave files for filename.

Returns a list of autosave files that contain autosaved data left behind by other instances of the application, due to crashing or otherwise uncleanly exiting.

It is the application's job to determine what to do with such unsaved data. Generally, this is done by asking the user if he wants to see the recovered data, and then allowing the user to save if he wants to.

If not given, the application name is obtained from QCoreApplication, so be sure to have set it correctly before calling this function.

This function returns a list of unopened KAutoSaveFile objects. By calling open() on them, the application will steal the lock. Subsequent releaseLock() or deleting of the object will then erase the stale autosave file.

Definition at line 151 of file kautosavefile.cpp.


The documentation for this class was generated from the following files:
  • kautosavefile.h
  • kautosavefile.cpp
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal