Chris@674: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@674: 
Chris@674: /*
Chris@674:     Sonic Visualiser
Chris@674:     An audio file viewer and annotation editor.
Chris@674:     Centre for Digital Music, Queen Mary, University of London.
Chris@674:     
Chris@674:     This program is free software; you can redistribute it and/or
Chris@674:     modify it under the terms of the GNU General Public License as
Chris@674:     published by the Free Software Foundation; either version 2 of the
Chris@674:     License, or (at your option) any later version.  See the file
Chris@674:     COPYING included with this distribution for more information.
Chris@674: */
Chris@674: 
Chris@1359: #ifndef SV_TEMP_WRITE_FILE_H
Chris@1359: #define SV_TEMP_WRITE_FILE_H
Chris@674: 
Chris@674: #include <QTemporaryFile>
Chris@674: 
Chris@674: /**
Chris@674:  * A class that manages the creation of a temporary file with a given
Chris@674:  * prefix and the renaming of that file to the prefix after use.  For
Chris@674:  * use when saving a file over an existing one, to avoid clobbering
Chris@674:  * the original before the save is complete.
Chris@674:  */
Chris@674: class TempWriteFile
Chris@674: {
Chris@674: public:
Chris@674:     TempWriteFile(QString targetFileName); // may throw FileOperationFailed
Chris@674: 
Chris@674:     /**
Chris@674:      * Destroy the temporary file object.  If moveToTarget has not
Chris@674:      * been called, the associated temporary file will be deleted
Chris@674:      * without being copied to the target location.
Chris@674:      */
Chris@674:     ~TempWriteFile();
Chris@674: 
Chris@674:     /**
Chris@674:      * Return the name of the temporary file.  Unless the constructor
Chris@674:      * threw an exception, this file will have been created already
Chris@674:      * (but it will not be open).
Chris@674:      *
Chris@674:      * (If moveToTarget has already been called, return an empty
Chris@674:      * string.)
Chris@674:      */
Chris@674:     QString getTemporaryFilename();
Chris@674: 
Chris@674:     /**
Chris@674:      * Rename the temporary file to the target filename.
Chris@674:      */
Chris@674:     void moveToTarget();
Chris@674: 
Chris@674: protected:
Chris@674:     QString m_target;
Chris@674:     QString m_temp;
Chris@674: };
Chris@674: 
Chris@674: 
Chris@674: #endif