AppFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <LibCore/Process.h>
  12. #include <LibDesktop/AppFile.h>
  13. namespace Desktop {
  14. NonnullRefPtr<AppFile> AppFile::get_for_app(const StringView& app_name)
  15. {
  16. auto path = String::formatted("{}/{}.af", APP_FILES_DIRECTORY, app_name);
  17. return open(path);
  18. }
  19. NonnullRefPtr<AppFile> AppFile::open(const StringView& path)
  20. {
  21. return adopt_ref(*new AppFile(path));
  22. }
  23. void AppFile::for_each(Function<void(NonnullRefPtr<AppFile>)> callback, const StringView& directory)
  24. {
  25. Core::DirIterator di(directory, Core::DirIterator::SkipDots);
  26. if (di.has_error())
  27. return;
  28. while (di.has_next()) {
  29. auto name = di.next_path();
  30. if (!name.ends_with(".af"))
  31. continue;
  32. auto path = String::formatted("{}/{}", directory, name);
  33. auto af = AppFile::open(path);
  34. if (!af->is_valid())
  35. continue;
  36. callback(af);
  37. }
  38. }
  39. AppFile::AppFile(const StringView& path)
  40. : m_config(Core::ConfigFile::open(path))
  41. , m_valid(validate())
  42. {
  43. }
  44. AppFile::~AppFile()
  45. {
  46. }
  47. bool AppFile::validate() const
  48. {
  49. if (m_config->read_entry("App", "Name").trim_whitespace().is_empty())
  50. return false;
  51. if (m_config->read_entry("App", "Executable").trim_whitespace().is_empty())
  52. return false;
  53. return true;
  54. }
  55. String AppFile::name() const
  56. {
  57. auto name = m_config->read_entry("App", "Name").trim_whitespace();
  58. VERIFY(!name.is_empty());
  59. return name;
  60. }
  61. String AppFile::executable() const
  62. {
  63. auto executable = m_config->read_entry("App", "Executable").trim_whitespace();
  64. VERIFY(!executable.is_empty());
  65. return executable;
  66. }
  67. String AppFile::description() const
  68. {
  69. return m_config->read_entry("App", "Description").trim_whitespace();
  70. }
  71. String AppFile::category() const
  72. {
  73. return m_config->read_entry("App", "Category").trim_whitespace();
  74. }
  75. String AppFile::icon_path() const
  76. {
  77. return m_config->read_entry("App", "IconPath").trim_whitespace();
  78. }
  79. GUI::Icon AppFile::icon() const
  80. {
  81. auto override_icon = icon_path();
  82. // FIXME: support pointing to actual .ico files
  83. if (!override_icon.is_empty())
  84. return GUI::FileIconProvider::icon_for_path(override_icon);
  85. return GUI::FileIconProvider::icon_for_path(executable());
  86. }
  87. bool AppFile::run_in_terminal() const
  88. {
  89. return m_config->read_bool_entry("App", "RunInTerminal", false);
  90. }
  91. Vector<String> AppFile::launcher_file_types() const
  92. {
  93. Vector<String> file_types;
  94. for (auto& entry : m_config->read_entry("Launcher", "FileTypes").split(',')) {
  95. entry = entry.trim_whitespace();
  96. if (!entry.is_empty())
  97. file_types.append(entry);
  98. }
  99. return file_types;
  100. }
  101. Vector<String> AppFile::launcher_protocols() const
  102. {
  103. Vector<String> protocols;
  104. for (auto& entry : m_config->read_entry("Launcher", "Protocols").split(',')) {
  105. entry = entry.trim_whitespace();
  106. if (!entry.is_empty())
  107. protocols.append(entry);
  108. }
  109. return protocols;
  110. }
  111. bool AppFile::spawn() const
  112. {
  113. if (!is_valid())
  114. return false;
  115. auto pid = Core::Process::spawn(executable());
  116. if (pid < 0)
  117. return false;
  118. return true;
  119. }
  120. }