QuickLaunchWidget.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2021, Fabian Blatz <fabianblatz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "QuickLaunchWidget.h"
  7. #include <AK/LexicalPath.h>
  8. #include <AK/OwnPtr.h>
  9. #include <Kernel/API/InodeWatcherFlags.h>
  10. #include <LibConfig/Client.h>
  11. #include <LibCore/FileWatcher.h>
  12. #include <LibCore/MimeData.h>
  13. #include <LibCore/Process.h>
  14. #include <LibCore/System.h>
  15. #include <LibDesktop/Launcher.h>
  16. #include <LibGUI/BoxLayout.h>
  17. #include <LibGUI/FileIconProvider.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/MessageBox.h>
  20. #include <serenity.h>
  21. #include <sys/stat.h>
  22. namespace Taskbar {
  23. constexpr auto quick_launch = "QuickLaunch"sv;
  24. constexpr int quick_launch_button_size = 24;
  25. ErrorOr<void> QuickLaunchEntryAppFile::launch() const
  26. {
  27. auto executable = m_app_file->executable();
  28. pid_t pid = TRY(Core::System::fork());
  29. if (pid == 0) {
  30. if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
  31. perror("chdir");
  32. exit(1);
  33. }
  34. if (m_app_file->run_in_terminal())
  35. execl("/bin/Terminal", "Terminal", "-e", executable.characters(), nullptr);
  36. else
  37. execl(executable.characters(), executable.characters(), nullptr);
  38. perror("execl");
  39. VERIFY_NOT_REACHED();
  40. } else
  41. TRY(Core::System::disown(pid));
  42. return {};
  43. }
  44. ErrorOr<void> QuickLaunchEntryExecutable::launch() const
  45. {
  46. TRY(Core::Process::spawn(m_path));
  47. return {};
  48. }
  49. GUI::Icon QuickLaunchEntryExecutable::icon() const
  50. {
  51. return GUI::FileIconProvider::icon_for_executable(m_path);
  52. }
  53. DeprecatedString QuickLaunchEntryExecutable::name() const
  54. {
  55. return LexicalPath { m_path }.basename();
  56. }
  57. ErrorOr<void> QuickLaunchEntryFile::launch() const
  58. {
  59. if (!Desktop::Launcher::open(URL::create_with_url_or_path(m_path))) {
  60. // FIXME: LaunchServer doesn't inform us about errors
  61. return Error::from_string_literal("Failed to open file");
  62. }
  63. return {};
  64. }
  65. GUI::Icon QuickLaunchEntryFile::icon() const
  66. {
  67. return GUI::FileIconProvider::icon_for_path(m_path);
  68. }
  69. DeprecatedString QuickLaunchEntryFile::name() const
  70. {
  71. // '=' is a special character in config files
  72. return m_path;
  73. }
  74. ErrorOr<NonnullRefPtr<QuickLaunchWidget>> QuickLaunchWidget::create()
  75. {
  76. Vector<NonnullOwnPtr<QuickLaunchEntry>> entries;
  77. auto keys = Config::list_keys("Taskbar"sv, quick_launch);
  78. for (auto& name : keys) {
  79. auto value = Config::read_string("Taskbar"sv, quick_launch, name);
  80. auto entry = QuickLaunchEntry::create_from_config_value(value);
  81. if (!entry)
  82. continue;
  83. entries.append(entry.release_nonnull());
  84. }
  85. auto widget = TRY(AK::adopt_nonnull_ref_or_enomem(new (nothrow) QuickLaunchWidget()));
  86. TRY(widget->create_context_menu());
  87. TRY(widget->add_quick_launch_buttons(move(entries)));
  88. return widget;
  89. }
  90. QuickLaunchWidget::QuickLaunchWidget()
  91. {
  92. set_shrink_to_fit(true);
  93. set_layout<GUI::HorizontalBoxLayout>(GUI::Margins {}, 0);
  94. set_frame_thickness(0);
  95. set_fixed_height(24);
  96. }
  97. ErrorOr<void> QuickLaunchWidget::create_context_menu()
  98. {
  99. auto icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"sv));
  100. m_context_menu = GUI::Menu::construct();
  101. m_context_menu_default_action = GUI::Action::create("&Remove", icon, [this](auto&) {
  102. Config::remove_key("Taskbar"sv, quick_launch, m_context_menu_app_name);
  103. auto button = find_child_of_type_named<GUI::Button>(m_context_menu_app_name);
  104. if (button) {
  105. remove_child(*button);
  106. }
  107. });
  108. m_context_menu->add_action(*m_context_menu_default_action);
  109. return {};
  110. }
  111. ErrorOr<void> QuickLaunchWidget::add_quick_launch_buttons(Vector<NonnullOwnPtr<QuickLaunchEntry>> entries)
  112. {
  113. for (auto& entry : entries) {
  114. auto name = entry->name();
  115. TRY(add_or_adjust_button(name, move(entry)));
  116. }
  117. return {};
  118. }
  119. OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_config_value(StringView value)
  120. {
  121. if (!value.starts_with('/') && value.ends_with(".af"sv)) {
  122. auto af_path = DeprecatedString::formatted("{}/{}", Desktop::AppFile::APP_FILES_DIRECTORY, value);
  123. return make<QuickLaunchEntryAppFile>(Desktop::AppFile::open(af_path));
  124. }
  125. return create_from_path(value);
  126. }
  127. OwnPtr<QuickLaunchEntry> QuickLaunchEntry::create_from_path(StringView path)
  128. {
  129. if (path.ends_with(".af"sv))
  130. return make<QuickLaunchEntryAppFile>(Desktop::AppFile::open(path));
  131. auto stat_or_error = Core::System::stat(path);
  132. if (stat_or_error.is_error()) {
  133. dbgln("Failed to stat quick launch entry file: {}", stat_or_error.release_error());
  134. return {};
  135. }
  136. auto stat = stat_or_error.release_value();
  137. if (S_ISREG(stat.st_mode) && (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
  138. return make<QuickLaunchEntryExecutable>(path);
  139. return make<QuickLaunchEntryFile>(path);
  140. }
  141. static DeprecatedString sanitize_entry_name(DeprecatedString const& name)
  142. {
  143. return name.replace(" "sv, ""sv, ReplaceMode::All).replace("="sv, ""sv, ReplaceMode::All);
  144. }
  145. ErrorOr<void> QuickLaunchWidget::add_or_adjust_button(DeprecatedString const& button_name, NonnullOwnPtr<QuickLaunchEntry>&& entry)
  146. {
  147. auto file_name_to_watch = entry->file_name_to_watch();
  148. if (!file_name_to_watch.is_null()) {
  149. if (!m_watcher) {
  150. m_watcher = TRY(Core::FileWatcher::create());
  151. m_watcher->on_change = [this](Core::FileWatcherEvent const& event) {
  152. auto name = sanitize_entry_name(event.event_path);
  153. dbgln("Removing QuickLaunch entry {}", name);
  154. auto button = find_child_of_type_named<GUI::Button>(name);
  155. if (button)
  156. remove_child(*button);
  157. };
  158. }
  159. TRY(m_watcher->add_watch(file_name_to_watch, Core::FileWatcherEvent::Type::Deleted));
  160. }
  161. auto button = find_child_of_type_named<GUI::Button>(button_name);
  162. if (!button)
  163. button = &add<GUI::Button>();
  164. button->set_fixed_size(quick_launch_button_size, quick_launch_button_size);
  165. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  166. auto icon = entry->icon();
  167. button->set_icon(icon.bitmap_for_size(16));
  168. button->set_tooltip(entry->name());
  169. button->set_name(button_name);
  170. button->on_click = [entry = move(entry), this](auto) {
  171. auto result = entry->launch();
  172. if (result.is_error()) {
  173. // FIXME: This message box is displayed in a weird position
  174. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to open quick launch entry: {}", result.release_error()));
  175. }
  176. };
  177. button->on_context_menu_request = [this, button_name](auto& context_menu_event) {
  178. m_context_menu_app_name = button_name;
  179. m_context_menu->popup(context_menu_event.screen_position(), m_context_menu_default_action);
  180. };
  181. return {};
  182. }
  183. void QuickLaunchWidget::config_key_was_removed(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key)
  184. {
  185. if (domain == "Taskbar" && group == quick_launch) {
  186. auto button = find_child_of_type_named<GUI::Button>(key);
  187. if (button)
  188. remove_child(*button);
  189. }
  190. }
  191. void QuickLaunchWidget::config_string_did_change(DeprecatedString const& domain, DeprecatedString const& group, DeprecatedString const& key, DeprecatedString const& value)
  192. {
  193. if (domain == "Taskbar" && group == quick_launch) {
  194. auto entry = QuickLaunchEntry::create_from_config_value(value);
  195. if (!entry)
  196. return;
  197. auto result = add_or_adjust_button(key, entry.release_nonnull());
  198. if (result.is_error())
  199. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to change quick launch entry: {}", result.release_error()));
  200. }
  201. }
  202. void QuickLaunchWidget::drag_enter_event(GUI::DragEvent& event)
  203. {
  204. auto const& mime_types = event.mime_types();
  205. if (mime_types.contains_slow("text/uri-list"))
  206. event.accept();
  207. }
  208. void QuickLaunchWidget::drop_event(GUI::DropEvent& event)
  209. {
  210. event.accept();
  211. if (event.mime_data().has_urls()) {
  212. auto urls = event.mime_data().urls();
  213. for (auto& url : urls) {
  214. auto entry = QuickLaunchEntry::create_from_path(url.path());
  215. if (entry) {
  216. auto item_name = sanitize_entry_name(entry->name());
  217. auto result = add_or_adjust_button(item_name, entry.release_nonnull());
  218. if (result.is_error())
  219. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to add quick launch entry: {}", result.release_error()));
  220. Config::write_string("Taskbar"sv, quick_launch, item_name, url.path());
  221. }
  222. }
  223. }
  224. }
  225. }