AppFile.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = DeprecatedString::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 = DeprecatedString::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. DeprecatedString 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. DeprecatedString 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. DeprecatedString AppFile::description() const
  66. {
  67. return m_config->read_entry("App", "Description").trim_whitespace();
  68. }
  69. DeprecatedString AppFile::category() const
  70. {
  71. return m_config->read_entry("App", "Category").trim_whitespace();
  72. }
  73. DeprecatedString AppFile::working_directory() const
  74. {
  75. return m_config->read_entry("App", "WorkingDirectory").trim_whitespace();
  76. }
  77. DeprecatedString 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. bool AppFile::exclude_from_system_menu() const
  98. {
  99. return m_config->read_bool_entry("App", "ExcludeFromSystemMenu", false);
  100. }
  101. Vector<DeprecatedString> AppFile::launcher_mime_types() const
  102. {
  103. Vector<DeprecatedString> mime_types;
  104. for (auto& entry : m_config->read_entry("Launcher", "MimeTypes").split(',')) {
  105. entry = entry.trim_whitespace();
  106. if (!entry.is_empty())
  107. mime_types.append(entry);
  108. }
  109. return mime_types;
  110. }
  111. Vector<DeprecatedString> AppFile::launcher_file_types() const
  112. {
  113. Vector<DeprecatedString> file_types;
  114. for (auto& entry : m_config->read_entry("Launcher", "FileTypes").split(',')) {
  115. entry = entry.trim_whitespace();
  116. if (!entry.is_empty())
  117. file_types.append(entry);
  118. }
  119. return file_types;
  120. }
  121. Vector<DeprecatedString> AppFile::launcher_protocols() const
  122. {
  123. Vector<DeprecatedString> protocols;
  124. for (auto& entry : m_config->read_entry("Launcher", "Protocols").split(',')) {
  125. entry = entry.trim_whitespace();
  126. if (!entry.is_empty())
  127. protocols.append(entry);
  128. }
  129. return protocols;
  130. }
  131. bool AppFile::spawn(ReadonlySpan<StringView> arguments) const
  132. {
  133. if (!is_valid())
  134. return false;
  135. auto pid = Core::Process::spawn(executable(), arguments, working_directory());
  136. if (pid.is_error())
  137. return false;
  138. return true;
  139. }
  140. }