File indexing completed on 2024-05-12 16:23:26

0001 /***************************************************************************
0002  *   Copyright (C) 2013-2017 by Linuxstopmotion contributors;              *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #ifndef WORKSPACEFILE_H_
0022 #define WORKSPACEFILE_H_
0023 
0024 #include <stdint.h>
0025 
0026 /**
0027  * Represents the filename of a file in the workspace (~/.stopmotion/).
0028  * The file is not held open, nor is it deleted on destruction.
0029  * @par
0030  * To be used by commands in the undo system and objects manipulated by such
0031  * commands.
0032  */
0033 class WorkspaceFile {
0034     char* fullPath;
0035     const char* namePart;
0036     void setFilename(const char* basename, bool inFrames = false);
0037 public:
0038     enum NewModelFile { newModelFile };
0039     enum CurrentModelFile { currentModelFile };
0040     enum CommandLogFile { commandLogFile };
0041     enum CapturedImage { capturedImage };
0042     enum PreferencesFile { preferencesFile };
0043     enum PreferencesFileOld { preferencesFileOld };
0044     enum AndClear { doNotClear, andClear };
0045     WorkspaceFile(const WorkspaceFile&);
0046     WorkspaceFile& operator=(const WorkspaceFile&);
0047     /**
0048      * Creates a WorkspaceFile referring to no file. Both @ref basename and
0049      * @ref path will return null until a @ref TemporaryWorkspaceFile is
0050      * assigned to it.
0051      */
0052     WorkspaceFile();
0053     /**
0054      * Creates a WorkspaceFile referring to a sound or image file already in
0055      * the workspace (having been created by {@c TemporaryWorkspaceFile}).
0056      * @param name The basename of the file (i.e. the return value of
0057      * {@ref TemporaryWorkspaceFile::basename}).
0058      */
0059     WorkspaceFile(const char* name);
0060     /**
0061      * Refers to the "new" model file, which should be used by the recovery
0062      * system if either the "current" model file or the command log cannot be
0063      * found. The command log should never be used in conjunction with this
0064      * file. This should only be necessary if a crash occurred during project
0065      * save.
0066      */
0067     WorkspaceFile(NewModelFile);
0068     /**
0069      * Refers to the "current" model file. If it and the command log are
0070      * present, the recovery system restores from this and then plays back the
0071      * commands from the command log.
0072      */
0073     WorkspaceFile(CurrentModelFile);
0074     /**
0075      * Refers to the command log. This holds the commands that were issued
0076      * since the "current" model file was written.
0077      */
0078     WorkspaceFile(CommandLogFile);
0079     /**
0080      * Refers to the image written to by the capture command.
0081      */
0082     WorkspaceFile(CapturedImage);
0083     /**
0084      * Refers to the preferences file.
0085      */
0086     WorkspaceFile(PreferencesFile);
0087     /**
0088      * Refers to the backup preferences file.
0089      */
0090     WorkspaceFile(PreferencesFileOld);
0091     ~WorkspaceFile();
0092     /**
0093      * Gets the file's basename.
0094      * @return The file's name (i.e. with no directory part but including any
0095      * extension).
0096      */
0097     const char* basename() const;
0098     /**
0099      * Gets the file's full path.
0100      * @return the file's full path, which will be in the workspace directory
0101      * (i.e. @c ~/.stopmotion but specified relative to /, not ~).
0102      */
0103     const char* path() const;
0104     /**
0105      * Swaps the content of this object with another.
0106      * @param w The object to swap contents with.
0107      */
0108     void swap(WorkspaceFile& w);
0109     /**
0110      * Ensures that the ~/.stopmotion and frames directories exist if they do
0111      * not already.
0112      * @param clear Pass @c andClear to clear the frames directory contents, if
0113      * any.
0114      */
0115     static void ensureStopmotionDirectoriesExist(AndClear clear = doNotClear);
0116     /**
0117      * Clears (creating if necessary) model files, frames, sounds and logs from
0118      * the workspace directory
0119      */
0120     static void clear();
0121     /**
0122      * Returns the current sound number counter. This is cleared by a call to
0123      * {@ref clear}. Remember to call {@ref nextSoundNumber} if the sound
0124      * number actually gets used. This is the number to be used in the default
0125      * readable name of a newly-added sound.
0126      * @return One more than the number of calls to {@ref nextSoundNumber}
0127      * since the last call to {@ref clear}.
0128      */
0129     static uint32_t getSoundNumber();
0130     /**
0131      * Increments the sound counter.
0132      */
0133     static void nextSoundNumber();
0134 };
0135 
0136 /**
0137  * Represents a type of file that can be copied into the workspace.
0138  */
0139 class WorkspaceFileType {
0140 public:
0141     virtual ~WorkspaceFileType() = 0;
0142     /**
0143      * Returns the preferred file name extension, including any dot.
0144      * @param path The original file path.
0145      * @return The extension, ownership is not returned.
0146      */
0147     virtual const char* preferredExtension(const char* path) const = 0;
0148     /**
0149      * Determines if the argument is (probably?) this type of file.
0150      * @param path The file path.
0151      * @return true if and only if this is a file that might be used
0152      * for this sort of file.
0153      */
0154     virtual bool isType(const char* path) const = 0;
0155     /**
0156      * Returns a representation of image files.
0157      * @return The type, ownership is not returned.
0158      */
0159     static const WorkspaceFileType& image();
0160     /**
0161      * Returns a representation of sound files.
0162      * @return The type, ownership is not returned.
0163      */
0164     static const WorkspaceFileType& sound();
0165 };
0166 
0167 /**
0168  * Represents the filename of a newly-created file in the workspace
0169  * (~/.stopmotion/). This file will be deleted upon destruction unless it
0170  * has been assigned to a @ref WorkspaceFile beforehand.
0171  * @par
0172  * To be used by the facade in front of the undo system.
0173  */
0174 class TemporaryWorkspaceFile {
0175     char* fullPath;
0176     const char* namePart;
0177     bool toBeDeleted;
0178     TemporaryWorkspaceFile(const TemporaryWorkspaceFile&);
0179     TemporaryWorkspaceFile& operator=(const TemporaryWorkspaceFile);
0180     friend class WorkspaceFile;
0181     /**
0182      * @throws CopyFailedException if the copy failed.
0183      */
0184     void copyToWorkspace(const char* filename, const WorkspaceFileType& type);
0185 public:
0186     enum ForceCopy {
0187         forceCopy
0188     };
0189     /**
0190      * Copy file with path @a filename into the workspace directory
0191      * ({@c ~/.stopmotion}) unless it is already in this directory.
0192      * A freshly-copied file will be deleted on destruction unless
0193      * @p retainFile has been called.
0194      * @note The file is not kept open by this class.
0195      * @param filename The full path to the file.
0196      * @param type The type of file to be copied.
0197      * @throws UiException with code failedToCopyFilesToWorkspace if
0198      * the copy failed.
0199      * @throws UiException with code unsupportedImageType if an image
0200      * type that is not supported is passed.
0201      */
0202     TemporaryWorkspaceFile(const char* filename, const WorkspaceFileType& type);
0203     /**
0204      * Copy file with path @a filename into the workspace directory,
0205      * even if it is already in this directory. The file will be deleted on
0206      * destruction unless @p retainFile has been called.
0207      * @note The file is not kept open by this class.
0208      * @param filename The full path to the file. Ownership is not passed.
0209      * @param type The type of file to be copied.
0210      * @throws UiException with code failedToCopyFilesToWorkspace if
0211      * the copy failed.
0212      * @throws UiException with code unsupportedImageType if an image
0213      * type that is not supported is passed.
0214      */
0215     TemporaryWorkspaceFile(const char* filename, const WorkspaceFileType& type,
0216             ForceCopy);
0217     ~TemporaryWorkspaceFile();
0218     /**
0219      * Prevents the file from being deleted on destruction.
0220      */
0221     void retainFile() {
0222         toBeDeleted = false;
0223     }
0224     /**
0225      * Returns the path of the file.
0226      * @return Ownership is not returned.
0227      */
0228     const char* path() const {
0229         return fullPath;
0230     }
0231     /**
0232      * Returns the basename (with extension) of the file.
0233      * @return Ownership is not returned.
0234      */
0235     const char* basename() const {
0236         return namePart;
0237     }
0238 };
0239 
0240 class ExportDirectory {
0241     char* p;
0242 public:
0243     ExportDirectory();
0244     ~ExportDirectory();
0245     const char* path() const {
0246         return p;
0247     }
0248     void makeEmpty();
0249 };
0250 
0251 #endif /* WORKSPACEFILE_H_ */