FileUtils.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_PickFolder(upath.Length() ? GetNativePath(upath).CString() : "", &outPath);
  74. if (outPath && result == NFD_OKAY)
  75. {
  76. projectPath = outPath;
  77. }
  78. GetSubsystem<Graphics>()->RaiseWindow();
  79. if (outPath)
  80. free(outPath);
  81. return projectPath;
  82. }
  83. String FileUtils::GetBuildPath(const String& defaultPath)
  84. {
  85. String buildPath;
  86. nfdchar_t *outPath = NULL;
  87. nfdresult_t result = NFD_PickFolder(defaultPath.Length() ? GetNativePath(defaultPath).CString() : "", &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. nfdresult_t result = NFD_PickFolder(defaultPath.Length() ? GetNativePath(defaultPath).CString() : "", &outPath);
  102. if (outPath && result == NFD_OKAY)
  103. {
  104. antPath = outPath;
  105. }
  106. if (outPath)
  107. free(outPath);
  108. GetSubsystem<Graphics>()->RaiseWindow();
  109. return GetInternalPath(antPath);
  110. }
  111. String FileUtils::GetMobileProvisionPath()
  112. {
  113. nfdchar_t *outPath = NULL;
  114. nfdresult_t result = NFD_OpenDialog( "mobileprovision",
  115. "",
  116. &outPath);
  117. String fullpath;
  118. if (outPath && result == NFD_OKAY)
  119. {
  120. fullpath = outPath;
  121. }
  122. GetSubsystem<Graphics>()->RaiseWindow();
  123. if (outPath)
  124. free(outPath);
  125. return fullpath;
  126. }
  127. void FileUtils::RevealInFinder(const String& fullpath)
  128. {
  129. FileSystem* fs = GetSubsystem<FileSystem>();
  130. if (fs->DirExists(fullpath))
  131. fs->SystemOpen(fullpath);
  132. else if (fs->FileExists(fullpath))
  133. fs->SystemOpen(GetPath(fullpath));
  134. }
  135. String FileUtils::FindPath(const String& title, const String& defaultPath)
  136. {
  137. String resultPath;
  138. nfdchar_t *outPath = NULL;
  139. nfdresult_t result = NFD_PickFolder(defaultPath.Length() ? GetNativePath(defaultPath).CString() : "", &outPath);
  140. if (outPath && result == NFD_OKAY)
  141. {
  142. resultPath = outPath;
  143. }
  144. if (outPath)
  145. free(outPath);
  146. GetSubsystem<Graphics>()->RaiseWindow();
  147. return GetInternalPath(resultPath);
  148. }
  149. String FileUtils::FindFile (const String& filterlist, const String& defaultPath)
  150. {
  151. String fullpath;
  152. nfdchar_t *outPath = NULL;
  153. nfdresult_t result = NFD_OpenDialog( filterlist.CString(),
  154. defaultPath.Length() ? GetNativePath(defaultPath).CString() : "",
  155. &outPath);
  156. if (outPath && result == NFD_OKAY)
  157. {
  158. fullpath = outPath;
  159. }
  160. GetSubsystem<Graphics>()->RaiseWindow();
  161. if (outPath)
  162. free(outPath);
  163. return fullpath;
  164. }
  165. }