FileUtils.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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;
  42. #ifdef ATOMIC_PLATFORM_LINUX
  43. upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  44. #endif
  45. nfdresult_t result = NFD_OpenDialog( "atomic",
  46. upath.Length() ? GetNativePath(upath).CString() : "",
  47. &outPath);
  48. String fullpath;
  49. if (outPath && result == NFD_OKAY)
  50. {
  51. fullpath = outPath;
  52. }
  53. GetSubsystem<Graphics>()->RaiseWindow();
  54. if (outPath)
  55. free(outPath);
  56. return fullpath;
  57. }
  58. bool FileUtils::CreateDirs(const String& folder)
  59. {
  60. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  61. Poco::File dirs(folder.CString());
  62. dirs.createDirectories();
  63. return fileSystem->DirExists(folder);
  64. }
  65. String FileUtils::NewProjectFileDialog()
  66. {
  67. String projectPath;
  68. nfdchar_t *outPath = NULL;
  69. String upath;
  70. #ifdef ATOMIC_PLATFORM_LINUX
  71. upath = GetSubsystem<FileSystem>()->GetUserDocumentsDir();
  72. #endif
  73. nfdresult_t result = NFD_ChooseDirectory( "Please choose the root folder for your project",
  74. upath.Length() ? GetNativePath(upath).CString() : "",
  75. &outPath);
  76. if (outPath && result == NFD_OKAY)
  77. {
  78. projectPath = outPath;
  79. }
  80. GetSubsystem<Graphics>()->RaiseWindow();
  81. if (outPath)
  82. free(outPath);
  83. return projectPath;
  84. }
  85. String FileUtils::GetBuildPath(const String& defaultPath)
  86. {
  87. String buildPath;
  88. nfdchar_t *outPath = NULL;
  89. nfdresult_t result = NFD_ChooseDirectory( "Please choose the build folder",
  90. defaultPath.Length() ? GetNativePath(defaultPath).CString() : "",
  91. &outPath);
  92. if (outPath && result == NFD_OKAY)
  93. {
  94. buildPath = outPath;
  95. }
  96. if (outPath)
  97. free(outPath);
  98. GetSubsystem<Graphics>()->RaiseWindow();
  99. return GetInternalPath(buildPath);
  100. }
  101. String FileUtils::GetAntPath(const String& defaultPath)
  102. {
  103. String antPath;
  104. nfdchar_t *outPath = NULL;
  105. #ifdef ATOMIC_PLATFORM_WINDOWS
  106. String msg = "Please select the folder which contains ant.bat";
  107. #else
  108. String msg = "Please select the folder which contains the ant executable";
  109. #endif
  110. nfdresult_t result = NFD_ChooseDirectory(msg.CString(),
  111. defaultPath.Length() ? GetNativePath(defaultPath).CString() : "",
  112. &outPath);
  113. if (outPath && result == NFD_OKAY)
  114. {
  115. antPath = outPath;
  116. }
  117. if (outPath)
  118. free(outPath);
  119. GetSubsystem<Graphics>()->RaiseWindow();
  120. return GetInternalPath(antPath);
  121. }
  122. String FileUtils::GetMobileProvisionPath()
  123. {
  124. nfdchar_t *outPath = NULL;
  125. nfdresult_t result = NFD_OpenDialog( "mobileprovision",
  126. "",
  127. &outPath);
  128. String fullpath;
  129. if (outPath && result == NFD_OKAY)
  130. {
  131. fullpath = outPath;
  132. }
  133. GetSubsystem<Graphics>()->RaiseWindow();
  134. if (outPath)
  135. free(outPath);
  136. return fullpath;
  137. }
  138. void FileUtils::RevealInFinder(const String& fullpath)
  139. {
  140. FileSystem* fs = GetSubsystem<FileSystem>();
  141. if (fs->DirExists(fullpath))
  142. fs->SystemOpen(fullpath);
  143. else if (fs->FileExists(fullpath))
  144. fs->SystemOpen(GetPath(fullpath));
  145. }
  146. String FileUtils::FindPath(const String& title, const String& defaultPath)
  147. {
  148. String resultPath;
  149. nfdchar_t *outPath = NULL;
  150. nfdresult_t result = NFD_ChooseDirectory(title.CString(),
  151. defaultPath.Length() ? GetNativePath(defaultPath).CString() : "",
  152. &outPath);
  153. if (outPath && result == NFD_OKAY)
  154. {
  155. resultPath = outPath;
  156. }
  157. if (outPath)
  158. free(outPath);
  159. GetSubsystem<Graphics>()->RaiseWindow();
  160. return GetInternalPath(resultPath);
  161. }
  162. String FileUtils::FindFile (const String& filterlist, const String& defaultPath)
  163. {
  164. String fullpath;
  165. nfdchar_t *outPath = NULL;
  166. nfdresult_t result = NFD_OpenDialog( filterlist.CString(),
  167. defaultPath.Length() ? GetNativePath(defaultPath).CString() : "",
  168. &outPath);
  169. if (outPath && result == NFD_OKAY)
  170. {
  171. fullpath = outPath;
  172. }
  173. GetSubsystem<Graphics>()->RaiseWindow();
  174. if (outPath)
  175. free(outPath);
  176. return fullpath;
  177. }
  178. }