AppFile.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <LibFileSystem/FileSystem.h>
  15. namespace Desktop {
  16. DeprecatedString AppFile::app_file_path_for_app(StringView app_name)
  17. {
  18. return DeprecatedString::formatted("{}/{}.af", APP_FILES_DIRECTORY, app_name);
  19. }
  20. bool AppFile::exists_for_app(StringView app_name)
  21. {
  22. return FileSystem::exists(app_file_path_for_app(app_name));
  23. }
  24. NonnullRefPtr<AppFile> AppFile::get_for_app(StringView app_name)
  25. {
  26. return open(app_file_path_for_app(app_name));
  27. }
  28. NonnullRefPtr<AppFile> AppFile::open(StringView path)
  29. {
  30. return adopt_ref(*new AppFile(path));
  31. }
  32. void AppFile::for_each(Function<void(NonnullRefPtr<AppFile>)> callback, StringView directory)
  33. {
  34. Core::DirIterator di(directory, Core::DirIterator::SkipDots);
  35. if (di.has_error())
  36. return;
  37. while (di.has_next()) {
  38. auto name = di.next_path();
  39. if (!name.ends_with(".af"sv))
  40. continue;
  41. auto path = DeprecatedString::formatted("{}/{}", directory, name);
  42. auto af = AppFile::open(path);
  43. if (!af->is_valid())
  44. continue;
  45. callback(af);
  46. }
  47. }
  48. AppFile::AppFile(StringView path)
  49. : m_config(Core::ConfigFile::open(path).release_value_but_fixme_should_propagate_errors())
  50. , m_valid(validate())
  51. {
  52. }
  53. bool AppFile::validate() const
  54. {
  55. if (m_config->read_entry("App", "Name").trim_whitespace().is_empty())
  56. return false;
  57. if (m_config->read_entry("App", "Executable").trim_whitespace().is_empty())
  58. return false;
  59. return true;
  60. }
  61. DeprecatedString AppFile::name() const
  62. {
  63. auto name = m_config->read_entry("App", "Name").trim_whitespace();
  64. VERIFY(!name.is_empty());
  65. return name;
  66. }
  67. DeprecatedString AppFile::executable() const
  68. {
  69. auto executable = m_config->read_entry("App", "Executable").trim_whitespace();
  70. VERIFY(!executable.is_empty());
  71. return executable;
  72. }
  73. DeprecatedString AppFile::description() const
  74. {
  75. return m_config->read_entry("App", "Description").trim_whitespace();
  76. }
  77. DeprecatedString AppFile::category() const
  78. {
  79. return m_config->read_entry("App", "Category").trim_whitespace();
  80. }
  81. DeprecatedString AppFile::working_directory() const
  82. {
  83. return m_config->read_entry("App", "WorkingDirectory").trim_whitespace();
  84. }
  85. DeprecatedString AppFile::icon_path() const
  86. {
  87. return m_config->read_entry("App", "IconPath").trim_whitespace();
  88. }
  89. GUI::Icon AppFile::icon() const
  90. {
  91. auto override_icon = icon_path();
  92. // FIXME: support pointing to actual .ico files
  93. if (!override_icon.is_empty())
  94. return GUI::FileIconProvider::icon_for_path(override_icon);
  95. return GUI::FileIconProvider::icon_for_path(executable());
  96. }
  97. bool AppFile::run_in_terminal() const
  98. {
  99. return m_config->read_bool_entry("App", "RunInTerminal", false);
  100. }
  101. bool AppFile::requires_root() const
  102. {
  103. return m_config->read_bool_entry("App", "RequiresRoot", false);
  104. }
  105. bool AppFile::exclude_from_system_menu() const
  106. {
  107. return m_config->read_bool_entry("App", "ExcludeFromSystemMenu", false);
  108. }
  109. Vector<DeprecatedString> AppFile::launcher_mime_types() const
  110. {
  111. Vector<DeprecatedString> mime_types;
  112. for (auto& entry : m_config->read_entry("Launcher", "MimeTypes").split(',')) {
  113. entry = entry.trim_whitespace();
  114. if (!entry.is_empty())
  115. mime_types.append(entry);
  116. }
  117. return mime_types;
  118. }
  119. Vector<DeprecatedString> AppFile::launcher_file_types() const
  120. {
  121. Vector<DeprecatedString> file_types;
  122. for (auto& entry : m_config->read_entry("Launcher", "FileTypes").split(',')) {
  123. entry = entry.trim_whitespace();
  124. if (!entry.is_empty())
  125. file_types.append(entry);
  126. }
  127. return file_types;
  128. }
  129. Vector<DeprecatedString> AppFile::launcher_protocols() const
  130. {
  131. Vector<DeprecatedString> protocols;
  132. for (auto& entry : m_config->read_entry("Launcher", "Protocols").split(',')) {
  133. entry = entry.trim_whitespace();
  134. if (!entry.is_empty())
  135. protocols.append(entry);
  136. }
  137. return protocols;
  138. }
  139. bool AppFile::spawn(ReadonlySpan<StringView> arguments) const
  140. {
  141. if (!is_valid())
  142. return false;
  143. auto pid = Core::Process::spawn(executable(), arguments, working_directory());
  144. if (pid.is_error())
  145. return false;
  146. return true;
  147. }
  148. }