FileUtils.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Poco/File.h>
  23. #include <Atomic/Core/Context.h>
  24. #include <Atomic/Core/StringUtils.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include "FileUtils.h"
  28. #include "nfd.h"
  29. namespace AtomicEditor
  30. {
  31. FileUtils::FileUtils(Context* context) :
  32. Object(context)
  33. {
  34. }
  35. FileUtils::~FileUtils()
  36. {
  37. }
  38. String FileUtils::OpenProjectFileDialog()
  39. {
  40. nfdchar_t *outPath = NULL;
  41. String upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  42. nfdresult_t result = NFD_OpenDialog( "atomic",
  43. upath.CString(),
  44. &outPath);
  45. String fullpath;
  46. if (outPath && result == NFD_OKAY)
  47. {
  48. fullpath = outPath;
  49. }
  50. GetSubsystem<Graphics>()->RaiseWindow();
  51. if (outPath)
  52. free(outPath);
  53. return fullpath;
  54. }
  55. bool FileUtils::CreateDirs(const String& folder)
  56. {
  57. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  58. Poco::File dirs(folder.CString());
  59. dirs.createDirectories();
  60. return fileSystem->DirExists(folder);
  61. }
  62. String FileUtils::NewProjectFileDialog()
  63. {
  64. String projectPath;
  65. nfdchar_t *outPath = NULL;
  66. String upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  67. nfdresult_t result = NFD_ChooseDirectory( "Please choose the root folder for your project",
  68. upath.CString(),
  69. &outPath);
  70. if (outPath && result == NFD_OKAY)
  71. {
  72. projectPath = outPath;
  73. }
  74. GetSubsystem<Graphics>()->RaiseWindow();
  75. if (outPath)
  76. free(outPath);
  77. return projectPath;
  78. }
  79. String FileUtils::GetBuildPath(const String& defaultPath)
  80. {
  81. String buildPath;
  82. nfdchar_t *outPath = NULL;
  83. String ppath = GetSubsystem<FileSystem>()->GetProgramDir();
  84. if ( defaultPath.Length() > 0) ppath = defaultPath;
  85. nfdresult_t result = NFD_ChooseDirectory( "Please choose the build folder",
  86. ppath.CString(),
  87. &outPath);
  88. if (outPath && result == NFD_OKAY)
  89. {
  90. buildPath = outPath;
  91. }
  92. if (outPath)
  93. free(outPath);
  94. GetSubsystem<Graphics>()->RaiseWindow();
  95. return GetInternalPath(buildPath);
  96. }
  97. String FileUtils::GetAntPath(const String& defaultPath)
  98. {
  99. String antPath;
  100. nfdchar_t *outPath = NULL;
  101. #ifdef ATOMIC_PLATFORM_WINDOWS
  102. String msg = "Please select the folder which contains ant.bat";
  103. #else
  104. String msg = "Please select the folder which contains the ant executable";
  105. #endif
  106. String ppath = GetSubsystem<FileSystem>()->GetProgramDir();
  107. if ( defaultPath.Length() > 0) ppath = defaultPath;
  108. nfdresult_t result = NFD_ChooseDirectory(msg.CString(),
  109. ppath.CString(),
  110. &outPath);
  111. if (outPath && result == NFD_OKAY)
  112. {
  113. antPath = outPath;
  114. }
  115. if (outPath)
  116. free(outPath);
  117. GetSubsystem<Graphics>()->RaiseWindow();
  118. return GetInternalPath(antPath);
  119. }
  120. String FileUtils::GetMobileProvisionPath()
  121. {
  122. nfdchar_t *outPath = NULL;
  123. String upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  124. nfdresult_t result = NFD_OpenDialog( "mobileprovision",
  125. upath.CString(),
  126. &outPath);
  127. String fullpath;
  128. if (outPath && result == NFD_OKAY)
  129. {
  130. fullpath = outPath;
  131. }
  132. GetSubsystem<Graphics>()->RaiseWindow();
  133. if (outPath)
  134. free(outPath);
  135. return fullpath;
  136. }
  137. void FileUtils::RevealInFinder(const String& fullpath)
  138. {
  139. FileSystem* fs = GetSubsystem<FileSystem>();
  140. if (fs->DirExists(fullpath))
  141. fs->SystemOpen(fullpath);
  142. else if (fs->FileExists(fullpath))
  143. fs->SystemOpen(GetPath(fullpath));
  144. }
  145. String FileUtils::FindPath(const String& title, const String& defaultPath)
  146. {
  147. String resultPath;
  148. nfdchar_t *outPath = NULL;
  149. String upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  150. if ( defaultPath.Length() > 0) upath = defaultPath;
  151. nfdresult_t result = NFD_ChooseDirectory(title.CString(),
  152. upath.CString(),
  153. &outPath);
  154. if (outPath && result == NFD_OKAY)
  155. {
  156. resultPath = outPath;
  157. }
  158. if (outPath)
  159. free(outPath);
  160. GetSubsystem<Graphics>()->RaiseWindow();
  161. return GetInternalPath(resultPath);
  162. }
  163. String FileUtils::FindFile (const String& filterlist, const String& defaultPath)
  164. {
  165. String fullpath;
  166. nfdchar_t *outPath = NULL;
  167. String upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  168. if ( defaultPath.Length() > 0) upath = defaultPath;
  169. nfdresult_t result = NFD_OpenDialog( filterlist.CString(),
  170. upath.CString(),
  171. &outPath);
  172. if (outPath && result == NFD_OKAY)
  173. {
  174. fullpath = outPath;
  175. }
  176. GetSubsystem<Graphics>()->RaiseWindow();
  177. if (outPath)
  178. free(outPath);
  179. return fullpath;
  180. }
  181. }