AppFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Function.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/Process.h>
  13. #include <LibDesktop/AppFile.h>
  14. namespace Desktop {
  15. NonnullRefPtr<AppFile> AppFile::get_for_app(StringView app_name)
  16. {
  17. auto path = String::formatted("{}/{}.af", APP_FILES_DIRECTORY, app_name);
  18. return open(path);
  19. }
  20. NonnullRefPtr<AppFile> AppFile::open(StringView path)
  21. {
  22. return adopt_ref(*new AppFile(path));
  23. }
  24. void AppFile::for_each(Function<void(NonnullRefPtr<AppFile>)> callback, StringView directory)
  25. {
  26. Core::DirIterator di(directory, Core::DirIterator::SkipDots);
  27. if (di.has_error())
  28. return;
  29. while (di.has_next()) {
  30. auto name = di.next_path();
  31. if (!name.ends_with(".af"sv))
  32. continue;
  33. auto path = String::formatted("{}/{}", directory, name);
  34. auto af = AppFile::open(path);
  35. if (!af->is_valid())
  36. continue;
  37. callback(af);
  38. }
  39. }
  40. AppFile::AppFile(StringView path)
  41. : m_config(Core::ConfigFile::open(path).release_value_but_fixme_should_propagate_errors())
  42. , m_valid(validate())
  43. {
  44. }
  45. bool AppFile::validate() const
  46. {
  47. if (m_config->read_entry("App", "Name").trim_whitespace().is_empty())
  48. return false;
  49. if (m_config->read_entry("App", "Executable").trim_whitespace().is_empty())
  50. return false;
  51. return true;
  52. }
  53. String AppFile::name() const
  54. {
  55. auto name = m_config->read_entry("App", "Name").trim_whitespace();
  56. VERIFY(!name.is_empty());
  57. return name;
  58. }
  59. String AppFile::executable() const
  60. {
  61. auto executable = m_config->read_entry("App", "Executable").trim_whitespace();
  62. VERIFY(!executable.is_empty());
  63. return executable;
  64. }
  65. String AppFile::description() const
  66. {
  67. return m_config->read_entry("App", "Description").trim_whitespace();
  68. }
  69. String AppFile::category() const
  70. {
  71. return m_config->read_entry("App", "Category").trim_whitespace();
  72. }
  73. String AppFile::working_directory() const
  74. {
  75. return m_config->read_entry("App", "WorkingDirectory").trim_whitespace();
  76. }
  77. String AppFile::icon_path() const
  78. {
  79. return m_config->read_entry("App", "IconPath").trim_whitespace();
  80. }
  81. GUI::Icon AppFile::icon() const
  82. {
  83. auto override_icon = icon_path();
  84. // FIXME: support pointing to actual .ico files
  85. if (!override_icon.is_empty())
  86. return GUI::FileIconProvider::icon_for_path(override_icon);
  87. return GUI::FileIconProvider::icon_for_path(executable());
  88. }
  89. bool AppFile::run_in_terminal() const
  90. {
  91. return m_config->read_bool_entry("App", "RunInTerminal", false);
  92. }
  93. bool AppFile::requires_root() const
  94. {
  95. return m_config->read_bool_entry("App", "RequiresRoot", false);
  96. }
  97. Vector<String> AppFile::launcher_mime_types() const
  98. {
  99. Vector<String> mime_types;
  100. for (auto& entry : m_config->read_entry("Launcher", "MimeTypes").split(',')) {
  101. entry = entry.trim_whitespace();
  102. if (!entry.is_empty())
  103. mime_types.append(entry);
  104. }
  105. return mime_types;
  106. }
  107. Vector<String> AppFile::launcher_file_types() const
  108. {
  109. Vector<String> file_types;
  110. for (auto& entry : m_config->read_entry("Launcher", "FileTypes").split(',')) {
  111. entry = entry.trim_whitespace();
  112. if (!entry.is_empty())
  113. file_types.append(entry);
  114. }
  115. return file_types;
  116. }
  117. Vector<String> AppFile::launcher_protocols() const
  118. {
  119. Vector<String> protocols;
  120. for (auto& entry : m_config->read_entry("Launcher", "Protocols").split(',')) {
  121. entry = entry.trim_whitespace();
  122. if (!entry.is_empty())
  123. protocols.append(entry);
  124. }
  125. return protocols;
  126. }
  127. bool AppFile::spawn() const
  128. {
  129. if (!is_valid())
  130. return false;
  131. auto pid = Core::Process::spawn(executable(), Span<String const> {}, working_directory());
  132. if (pid.is_error())
  133. return false;
  134. return true;
  135. }
  136. }