platform_util_linux.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/platform_util.h"
  5. #include <stdio.h>
  6. #include "base/cancelable_callback.h"
  7. #include "base/files/file_util.h"
  8. #include "base/process/kill.h"
  9. #include "base/process/launch.h"
  10. #include "url/gurl.h"
  11. #define ELECTRON_TRASH "ELECTRON_TRASH"
  12. #define ELECTRON_DEFAULT_TRASH "gvfs-trash"
  13. namespace {
  14. bool XDGUtilV(const std::vector<std::string>& argv,
  15. const bool wait_for_exit) {
  16. base::LaunchOptions options;
  17. options.allow_new_privs = true;
  18. // xdg-open can fall back on mailcap which eventually might plumb through
  19. // to a command that needs a terminal. Set the environment variable telling
  20. // it that we definitely don't have a terminal available and that it should
  21. // bring up a new terminal if necessary. See "man mailcap".
  22. options.environ["MM_NOTTTY"] = "1";
  23. base::Process process = base::LaunchProcess(argv, options);
  24. if (!process.IsValid())
  25. return false;
  26. if (!wait_for_exit) {
  27. base::EnsureProcessGetsReaped(process.Pid());
  28. return true;
  29. }
  30. int exit_code = -1;
  31. if (!process.WaitForExit(&exit_code))
  32. return false;
  33. return (exit_code == 0);
  34. }
  35. bool XDGUtil(const std::string& util,
  36. const std::string& arg,
  37. const bool wait_for_exit) {
  38. std::vector<std::string> argv;
  39. argv.push_back(util);
  40. argv.push_back(arg);
  41. return XDGUtilV(argv, wait_for_exit);
  42. }
  43. bool XDGOpen(const std::string& path, const bool wait_for_exit) {
  44. return XDGUtil("xdg-open", path, wait_for_exit);
  45. }
  46. bool XDGEmail(const std::string& email, const bool wait_for_exit) {
  47. return XDGUtil("xdg-email", email, wait_for_exit);
  48. }
  49. } // namespace
  50. namespace platform_util {
  51. // TODO(estade): It would be nice to be able to select the file in the file
  52. // manager, but that probably requires extending xdg-open. For now just
  53. // show the folder.
  54. bool ShowItemInFolder(const base::FilePath& full_path) {
  55. base::FilePath dir = full_path.DirName();
  56. if (!base::DirectoryExists(dir))
  57. return false;
  58. return XDGOpen(dir.value(), true);
  59. }
  60. bool OpenItem(const base::FilePath& full_path) {
  61. return XDGOpen(full_path.value(), true);
  62. }
  63. bool OpenExternal(const GURL& url, bool activate) {
  64. // Don't wait for exit, since we don't want to wait for the browser/email
  65. // client window to close before returning
  66. if (url.SchemeIs("mailto"))
  67. return XDGEmail(url.spec(), false);
  68. else
  69. return XDGOpen(url.spec(), false);
  70. }
  71. void OpenExternal(const GURL& url, bool activate,
  72. const OpenExternalCallback& callback) {
  73. // TODO(gabriel): Implement async open if callback is specified
  74. callback.Run(OpenExternal(url, activate) ? "" : "Failed to open");
  75. }
  76. bool MoveItemToTrash(const base::FilePath& full_path) {
  77. std::string trash;
  78. if (getenv(ELECTRON_TRASH) != NULL) {
  79. trash = getenv(ELECTRON_TRASH);
  80. } else {
  81. trash = ELECTRON_DEFAULT_TRASH;
  82. }
  83. std::vector<std::string> argv;
  84. if (trash.compare("kioclient5") == 0 || trash.compare("kioclient") == 0) {
  85. argv.push_back(trash);
  86. argv.push_back("move");
  87. argv.push_back(full_path.value());
  88. argv.push_back("trash:/");
  89. } else if (trash.compare("trash-cli") == 0) {
  90. argv.push_back("trash-put");
  91. argv.push_back(full_path.value());
  92. } else {
  93. argv.push_back(ELECTRON_DEFAULT_TRASH);
  94. argv.push_back(full_path.value());
  95. }
  96. return XDGUtilV(argv, true);
  97. }
  98. void Beep() {
  99. // echo '\a' > /dev/console
  100. FILE* console = fopen("/dev/console", "r");
  101. if (console == NULL)
  102. return;
  103. fprintf(console, "\a");
  104. fclose(console);
  105. }
  106. } // namespace platform_util