DesktopServices.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/URL.h>
  27. #include <LibCore/DesktopServices.h>
  28. #include <stdio.h>
  29. #include <sys/stat.h>
  30. namespace Core {
  31. static bool open_file_url(const URL&);
  32. static bool spawn(String executable, String argument);
  33. bool DesktopServices::open(const URL& url)
  34. {
  35. if (url.protocol() == "file")
  36. return open_file_url(url);
  37. if (url.protocol() == "irc")
  38. return spawn("/bin/IRCClient", url.to_string());
  39. return spawn("/bin/Browser", url.to_string());
  40. }
  41. bool spawn(String executable, String argument)
  42. {
  43. pid_t child_pid = fork();
  44. if (child_pid < 0) {
  45. perror("fork");
  46. return false;
  47. }
  48. if (child_pid == 0) {
  49. if (execl(executable.characters(), executable.characters(), argument.characters(), nullptr) < 0) {
  50. perror("execl");
  51. return false;
  52. }
  53. ASSERT_NOT_REACHED();
  54. }
  55. return true;
  56. }
  57. bool open_file_url(const URL& url)
  58. {
  59. struct stat st;
  60. if (stat(url.path().characters(), &st) < 0) {
  61. perror("stat");
  62. return false;
  63. }
  64. if (S_ISDIR(st.st_mode))
  65. return spawn("/bin/FileManager", url.path());
  66. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  67. return spawn(url.path(), {});
  68. if (url.path().to_lowercase().ends_with(".png"))
  69. return spawn("/bin/QuickShow", url.path());
  70. if (url.path().to_lowercase().ends_with(".html"))
  71. return spawn("/bin/Browser", url.path());
  72. if (url.path().to_lowercase().ends_with(".wav"))
  73. return spawn("/bin/SoundPlayer", url.path());
  74. return spawn("/bin/TextEditor", url.path());
  75. }
  76. }