FileUtil.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "FileUtil_Common.h"
  10. #include "../Include/SandboxAPI.h"
  11. #include <QString>
  12. #include <QFileInfo>
  13. #include "../Include/IFileUtil.h"
  14. class QStringList;
  15. class QMenu;
  16. class SANDBOX_API CFileUtil
  17. {
  18. public:
  19. static bool ScanDirectory(const QString& path, const QString& fileSpec, IFileUtil::FileArray& files,
  20. bool recursive = true, bool addDirAlso = false, IFileUtil::ScanDirectoryUpdateCallBack updateCB = nullptr, bool bSkipPaks = false);
  21. static void ShowInExplorer(const QString& path);
  22. static bool ExtractFile(QString& file, bool bMsgBoxAskForExtraction = true, const char* pDestinationFilename = nullptr);
  23. static void EditTextFile(const char* txtFile, int line = 0, IFileUtil::ETextFileType fileType = IFileUtil::FILE_TYPE_SCRIPT);
  24. static void EditTextureFile(const char* txtureFile, bool bUseGameFolder);
  25. //! Reformat filter string for (MFC) CFileDialog style file filtering
  26. static void FormatFilterString(QString& filter);
  27. //! Open file selection dialog.
  28. static bool SelectFile(const QString& fileSpec, const QString& searchFolder, QString& fullFileName);
  29. //! Open file selection dialog.
  30. static bool SelectFiles(const QString& fileSpec, const QString& searchFolder, QStringList& files);
  31. //! Display OpenFile dialog and allow to select multiple files.
  32. //! @return true if selected, false if canceled.
  33. //! @outputFile Inputs and Outputs filename.
  34. /*
  35. static bool SelectSingleFile(IFileUtil::ECustomFileType fileType, QString& outputFile, const QString& filter = "", const QString& initialDir = "");
  36. static bool SelectSingleFile(IFileUtil::ECustomFileType fileType, char* outputFile, int outputSize, const char* filter = "", const char* initialDir = "");
  37. static bool SelectSingleFile(IFileUtil::ECustomFileType fileType, QString& outputFile, const QString& filter = {}, const QString& initialDir = {});
  38. */
  39. static bool SelectSaveFile(const QString& fileFilter, const QString& defaulExtension, const QString& startFolder, QString& fileName);
  40. //! Attempt to make a file writable
  41. static bool OverwriteFile(const QString& filename);
  42. //! Checks out the file from source control API. Blocks until completed
  43. static bool CheckoutFile(const char* filename, QWidget* parentWindow = nullptr);
  44. //! Discard changes to a file from source control API. Blocks until completed
  45. static bool RevertFile(const char* filename, QWidget* parentWindow = nullptr);
  46. //! Renames (moves) a file through the source control API. Blocks until completed
  47. static bool RenameFile(const char* sourceFile, const char* targetFile, QWidget* parentWindow = nullptr);
  48. //! Deletes a file using source control API. Blocks until completed.
  49. static bool DeleteFromSourceControl(const char* filename, QWidget* parentWindow = nullptr);
  50. //! Attempts to get the latest version of a file from source control. Blocks until completed
  51. static bool GetLatestFromSourceControl(const char* filename, QWidget* parentWindow = nullptr);
  52. //! Gather information about a file using the source control API. Blocks until completed
  53. static bool GetFileInfoFromSourceControl(const char* filename, AzToolsFramework::SourceControlFileInfo& fileInfo, QWidget* parentWindow = nullptr);
  54. //! Creates this directory if it doesn't exist. Returns false if the director doesn't exist and couldn't be created.
  55. static bool CreateDirectory(const char* dir);
  56. //! Makes a backup file.
  57. static void BackupFile(const char* filename);
  58. //! Makes a backup file, marked with a datestamp, e.g. myfile.20071014.093320.xml
  59. //! If bUseBackupSubDirectory is true, moves backup file into a relative subdirectory "backups"
  60. static void BackupFileDated(const char* filename, bool bUseBackupSubDirectory = false);
  61. // ! Added deltree as a copy from the function found in Crypak.
  62. static bool Deltree(const char* szFolder, bool bRecurse);
  63. // Checks if a file or directory exist.
  64. // We are using 3 functions here in order to make the names more instructive for the programmers.
  65. // Those functions only work for OS files and directories.
  66. static bool Exists(const QString& strPath, bool boDirectory, IFileUtil::FileDesc* pDesc = nullptr);
  67. static bool FileExists(const QString& strFilePath, IFileUtil::FileDesc* pDesc = nullptr);
  68. static bool PathExists(const QString& strPath);
  69. static bool GetDiskFileSize(const char* pFilePath, uint64& rOutSize);
  70. // This function should be used only with physical files.
  71. static bool IsFileExclusivelyAccessable(const QString& strFilePath);
  72. // Creates the entire path, if needed.
  73. static bool CreatePath(const QString& strPath);
  74. // Attempts to delete a file (if read only it will set its attributes to normal first).
  75. static bool DeleteFile(const QString& strPath);
  76. // Attempts to remove a directory (if read only it will set its attributes to normal first).
  77. static bool RemoveDirectory(const QString& strPath);
  78. // Calls predicate() with each entry in the directory
  79. static void ForEach(const QString& path, std::function<void(const QString&)> predicate, bool recurse = true);
  80. //! Copies all the elements from the source directory to the target directory.
  81. //! It doesn't copy the source folder to the target folder, only it's contents.
  82. //! THIS FUNCTION IS NOT DESIGNED FOR MULTI-THREADED USAGE
  83. //! The ignore list can either take a single string of a file name or folder name or it can take PIPE separated names ("something|somethingelse")
  84. //! It is not a pattern match, merely a file or folder name match.
  85. static IFileUtil::ECopyTreeResult CopyTree(const QString& strSourceDirectory, const QString& strTargetDirectory, bool boRecurse = true, bool boConfirmOverwrite = false, const char* const ignoreFilesAndFolders = nullptr);
  86. //////////////////////////////////////////////////////////////////////////
  87. // @param LPPROGRESS_ROUTINE pfnProgress - called by the system to notify of file copy progress
  88. // @param LPBOOL pbCancel - when the contents of this BOOL are set to true, the system cancels the copy operation
  89. static IFileUtil::ECopyTreeResult CopyFile(const QString& strSourceFile, const QString& strTargetFile, bool boConfirmOverwrite = false, ProgressRoutine pfnProgress = nullptr, bool* pbCancel = nullptr);
  90. // As we don't have a FileUtil interface here, we have to duplicate some code :-( in order to keep
  91. // function calls clean.
  92. // Moves all the elements from the source directory to the target directory.
  93. // It doesn't move the source folder to the target folder, only it's contents.
  94. // THIS FUNCTION IS NOT DESIGNED FOR MULTI-THREADED USAGE
  95. static IFileUtil::ECopyTreeResult MoveTree(const QString& strSourceDirectory, const QString& strTargetDirectory, bool boRecurse = true, bool boConfirmOverwrite = false);
  96. static void PopulateQMenu(QWidget* caller, QMenu* menu, AZStd::string_view fullGamePath);
  97. // Get file attributes include source control attributes if available
  98. static uint32 GetAttributes(const char* filename, bool bUseSourceControl = true);
  99. // Returns true if the files have the same content, false otherwise
  100. static bool CompareFiles(const QString& strFilePath1, const QString& strFilePath2);
  101. // Sort Columns( Ascending/Descending )
  102. static bool SortAscendingFileNames(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  103. static bool SortDescendingFileNames(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  104. static bool SortAscendingDates(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  105. static bool SortDescendingDates(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  106. static bool SortAscendingSizes(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  107. static bool SortDescendingSizes(const IFileUtil::FileDesc& desc1, const IFileUtil::FileDesc& desc2);
  108. // Return true is the filepath is a absolute path
  109. static bool IsAbsPath(const QString& filepath);
  110. private:
  111. // True means to use the custom file dialog, false means to use the smart file open dialog.
  112. static bool s_singleFileDlgPref[IFileUtil::EFILE_TYPE_LAST];
  113. static bool s_multiFileDlgPref[IFileUtil::EFILE_TYPE_LAST];
  114. // Keep this variant of this method private! pIsSelected is captured in a lambda, and so requires menu use exec() and never use show()
  115. static void PopulateQMenu(QWidget* caller, QMenu* menu, AZStd::string_view fullGamePath, bool* pIsSelected);
  116. static void HandlePrefsDialogForFileType(const Common::EditFileType fileType);
  117. static QString GetEditorForFileTypeFromPreferences(const Common::EditFileType fileType);
  118. static QString HandleNoEditorAssigned(const Common::EditFileType fileType);
  119. static QString HandleEditorOpenFailure(const Common::EditFileType fileType, const QString& currentEditor);
  120. static AZStd::string GetSettingsKeyForFileType(const Common::EditFileType fileType);
  121. static void EditFile(const QString&, const Common::EditFileType fileType);
  122. };
  123. class CAutoRestorePrimaryCDRoot
  124. {
  125. public:
  126. ~CAutoRestorePrimaryCDRoot();
  127. };
  128. //
  129. // A helper for creating a temp file to write to, then copying that over the destination
  130. // file only if it changes (to avoid requiring the user to check out source controlled
  131. // file unnecessarily)
  132. //
  133. class SANDBOX_API CTempFileHelper
  134. {
  135. public:
  136. CTempFileHelper(const char* pFileName);
  137. ~CTempFileHelper();
  138. // Gets the path to the temp file that should be written to
  139. const QString& GetTempFilePath() { return m_tempFileName; }
  140. // After the temp file has been written and closed, this should be called to update
  141. // the destination file.
  142. // If bBackup is true CFileUtil::BackupFile will be called if the file has changed.
  143. bool UpdateFile(bool bBackup);
  144. private:
  145. QString m_fileName;
  146. QString m_tempFileName;
  147. };