Chris@81: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@81: 
Chris@81: /*
Chris@81:     Sonic Visualiser
Chris@81:     An audio file viewer and annotation editor.
Chris@81:     Centre for Digital Music, Queen Mary, University of London.
Chris@81:     This file copyright 2006 Chris Cannam.
Chris@81:     
Chris@81:     This program is free software; you can redistribute it and/or
Chris@81:     modify it under the terms of the GNU General Public License as
Chris@81:     published by the Free Software Foundation; either version 2 of the
Chris@81:     License, or (at your option) any later version.  See the file
Chris@81:     COPYING included with this distribution for more information.
Chris@81: */
Chris@81: 
Chris@81: #ifndef _TEMP_DIRECTORY_H_
Chris@81: #define _TEMP_DIRECTORY_H_
Chris@81: 
Chris@81: #include <QString>
Chris@81: #include <QMutex>
Chris@81: 
Chris@81: #include <exception>
Chris@81: 
Chris@81: /**
Chris@81:  * A class that manages the creation and removal of a temporary
Chris@81:  * directory tree to store data during the program run.  There is one
Chris@81:  * root temporary directory for the program, created on demand and
Chris@81:  * deleted when the program exits.
Chris@81:  *
Chris@81:  * This class is thread safe.
Chris@81:  */
Chris@81: 
Chris@81: class TempDirectory
Chris@81: {
Chris@81: public:
Chris@145:     static TempDirectory *getInstance();
Chris@81:     
Chris@81:     virtual ~TempDirectory();
Chris@81: 
Chris@81:     /**
Chris@460:      * Return the path of the directory in which the temporary
Chris@460:      * directory has been or will be created.  This directory is
Chris@460:      * particular to this application, although not to this instance
Chris@460:      * of it, and it will not be removed when the application exits.
Chris@460:      * Persistent cache data or similar may be placed in this
Chris@460:      * directory or other, non-temporary subdirectories of it.
Chris@460:      *
Chris@460:      * If this directory does not exist, it will be created.  Throw
Chris@460:      * DirectoryCreationFailed if the directory cannot be created.
Chris@460:      */
Chris@460:     QString getContainingPath();
Chris@460: 
Chris@460:     /**
Chris@81:      * Create the root temporary directory if necessary, and return
Chris@460:      * its path.  This directory will be removed when the application
Chris@460:      * exits.
Chris@460:      *
Chris@460:      * Throw DirectoryCreationFailed if the directory cannot be
Chris@460:      * created.
Chris@81:      */
Chris@81:     QString getPath();
Chris@81: 
Chris@81:     /** 
Chris@81:      * Create an immediate subdirectory of the root temporary
Chris@81:      * directory of the given name, if it doesn't already exist, and
Chris@460:      * return its path.  This directory will be removed when the
Chris@460:      * application exits.
Chris@460:      * 
Chris@460:      * Throw DirectoryCreationFailed if the directory cannot be
Chris@460:      * created.
Chris@81:      */
Chris@81:     QString getSubDirectoryPath(QString subdir);
Chris@81: 
Chris@81:     /**
Chris@81:      * Delete the temporary directory (before exiting).
Chris@81:      */
Chris@81:     void cleanup();
Chris@81: 
Chris@81: protected:
Chris@81:     TempDirectory();
Chris@98: 
Chris@98:     QString createTempDirectoryIn(QString inDir);
Chris@81:     void cleanupDirectory(QString tmpDir);
Chris@98:     void cleanupAbandonedDirectories(QString svDir);
Chris@98: 
Chris@81:     QString m_tmpdir;
Chris@81:     QMutex m_mutex;
Chris@81: 
Chris@81:     static TempDirectory *m_instance;
Chris@81: };
Chris@81: 
Chris@81: 
Chris@81: #endif