platform_util_linux.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/files/file_util.h"
  7. #include "base/process/kill.h"
  8. #include "base/process/launch.h"
  9. #include "url/gurl.h"
  10. namespace {
  11. bool XDGUtil(const std::string& util,
  12. const std::string& arg,
  13. const bool wait_for_exit) {
  14. std::vector<std::string> argv;
  15. argv.push_back(util);
  16. argv.push_back(arg);
  17. base::LaunchOptions options;
  18. options.allow_new_privs = true;
  19. // xdg-open can fall back on mailcap which eventually might plumb through
  20. // to a command that needs a terminal. Set the environment variable telling
  21. // it that we definitely don't have a terminal available and that it should
  22. // bring up a new terminal if necessary. See "man mailcap".
  23. options.environ["MM_NOTTTY"] = "1";
  24. base::Process process = base::LaunchProcess(argv, options);
  25. if (!process.IsValid())
  26. return false;
  27. if (!wait_for_exit) {
  28. base::EnsureProcessGetsReaped(process.Pid());
  29. return true;
  30. }
  31. int exit_code = -1;
  32. if (!process.WaitForExit(&exit_code))
  33. return false;
  34. return (exit_code == 0);
  35. }
  36. bool XDGOpen(const std::string& path, const bool wait_for_exit) {
  37. return XDGUtil("xdg-open", path, wait_for_exit);
  38. }
  39. bool XDGEmail(const std::string& email, const bool wait_for_exit) {
  40. return XDGUtil("xdg-email", email, wait_for_exit);
  41. }
  42. } // namespace
  43. namespace platform_util {
  44. // TODO(estade): It would be nice to be able to select the file in the file
  45. // manager, but that probably requires extending xdg-open. For now just
  46. // show the folder.
  47. bool ShowItemInFolder(const base::FilePath& full_path) {
  48. base::FilePath dir = full_path.DirName();
  49. if (!base::DirectoryExists(dir))
  50. return false;
  51. return XDGOpen(dir.value(), true);
  52. }
  53. bool OpenItem(const base::FilePath& full_path) {
  54. return XDGOpen(full_path.value(), true);
  55. }
  56. bool OpenExternal(const GURL& url, bool activate) {
  57. // Don't wait for exit, since we don't want to wait for the browser/email
  58. // client window to close before returning
  59. if (url.SchemeIs("mailto"))
  60. return XDGEmail(url.spec(), false);
  61. else
  62. return XDGOpen(url.spec(), false);
  63. }
  64. bool MoveItemToTrash(const base::FilePath& full_path) {
  65. return XDGUtil("gvfs-trash", full_path.value(), true);
  66. }
  67. void Beep() {
  68. // echo '\a' > /dev/console
  69. FILE* console = fopen("/dev/console", "r");
  70. if (console == NULL)
  71. return;
  72. fprintf(console, "\a");
  73. fclose(console);
  74. }
  75. } // namespace platform_util