AppFile.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <AK/Vector.h>
  9. #include <LibCore/ConfigFile.h>
  10. #include <LibCore/DirIterator.h>
  11. #include <LibDesktop/AppFile.h>
  12. #include <errno.h>
  13. #include <serenity.h>
  14. #include <spawn.h>
  15. namespace Desktop {
  16. NonnullRefPtr<AppFile> AppFile::get_for_app(const StringView& app_name)
  17. {
  18. auto path = String::formatted("{}/{}.af", APP_FILES_DIRECTORY, app_name);
  19. return open(path);
  20. }
  21. NonnullRefPtr<AppFile> AppFile::open(const StringView& path)
  22. {
  23. return adopt_ref(*new AppFile(path));
  24. }
  25. void AppFile::for_each(Function<void(NonnullRefPtr<AppFile>)> callback, const StringView& directory)
  26. {
  27. Core::DirIterator di(directory, Core::DirIterator::SkipDots);
  28. if (di.has_error())
  29. return;
  30. while (di.has_next()) {
  31. auto name = di.next_path();
  32. if (!name.ends_with(".af"))
  33. continue;
  34. auto path = String::formatted("{}/{}", directory, name);
  35. auto af = AppFile::open(path);
  36. if (!af->is_valid())
  37. continue;
  38. callback(af);
  39. }
  40. }
  41. AppFile::AppFile(const StringView& path)
  42. : m_config(Core::ConfigFile::open(path))
  43. , m_valid(validate())
  44. {
  45. }
  46. AppFile::~AppFile()
  47. {
  48. }
  49. bool AppFile::validate() const
  50. {
  51. if (m_config->read_entry("App", "Name").trim_whitespace().is_empty())
  52. return false;
  53. if (m_config->read_entry("App", "Executable").trim_whitespace().is_empty())
  54. return false;
  55. return true;
  56. }
  57. String AppFile::name() const
  58. {
  59. auto name = m_config->read_entry("App", "Name").trim_whitespace();
  60. VERIFY(!name.is_empty());
  61. return name;
  62. }
  63. String AppFile::executable() const
  64. {
  65. auto executable = m_config->read_entry("App", "Executable").trim_whitespace();
  66. VERIFY(!executable.is_empty());
  67. return executable;
  68. }
  69. String AppFile::category() const
  70. {
  71. return m_config->read_entry("App", "Category").trim_whitespace();
  72. }
  73. Vector<String> AppFile::launcher_file_types() const
  74. {
  75. Vector<String> file_types;
  76. for (auto& entry : m_config->read_entry("Launcher", "FileTypes").split(',')) {
  77. entry = entry.trim_whitespace();
  78. if (!entry.is_empty())
  79. file_types.append(entry);
  80. }
  81. return file_types;
  82. }
  83. Vector<String> AppFile::launcher_protocols() const
  84. {
  85. Vector<String> protocols;
  86. for (auto& entry : m_config->read_entry("Launcher", "Protocols").split(',')) {
  87. entry = entry.trim_whitespace();
  88. if (!entry.is_empty())
  89. protocols.append(entry);
  90. }
  91. return protocols;
  92. }
  93. bool AppFile::spawn() const
  94. {
  95. if (!is_valid())
  96. return false;
  97. pid_t child_pid;
  98. const char* argv[] = { executable().characters(), nullptr };
  99. if ((errno = posix_spawn(&child_pid, executable().characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
  100. perror("posix_spawn");
  101. return false;
  102. } else {
  103. if (disown(child_pid) < 0) {
  104. perror("disown");
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. }