FileUtils.cpp 5.3 KB

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