From 2cda579b076a615e94be6ed922b987aa189b98e4 Mon Sep 17 00:00:00 2001 From: Maciej Date: Wed, 29 Dec 2021 18:15:21 +0100 Subject: [PATCH] Taskbar: Remove QuickLaunch entries if corresponding file was deleted --- .../Services/Taskbar/QuickLaunchWidget.cpp | 30 +++++++++++++++++-- Userland/Services/Taskbar/QuickLaunchWidget.h | 6 ++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Userland/Services/Taskbar/QuickLaunchWidget.cpp b/Userland/Services/Taskbar/QuickLaunchWidget.cpp index d37112ef1b7..ee445875962 100644 --- a/Userland/Services/Taskbar/QuickLaunchWidget.cpp +++ b/Userland/Services/Taskbar/QuickLaunchWidget.cpp @@ -5,13 +5,14 @@ */ #include "QuickLaunchWidget.h" -#include "LibCore/IODevice.h" -#include "LibDesktop/Launcher.h" #include +#include #include +#include #include #include #include +#include #include #include #include @@ -133,14 +134,37 @@ OwnPtr QuickLaunchEntry::create_from_path(StringView path) dbgln("Failed to stat quick launch entry file: {}", stat_or_error.release_error()); return {}; } + auto stat = stat_or_error.release_value(); if (S_ISREG(stat.st_mode) && (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) return make(path); return make(path); } +static String sanitize_entry_name(String const& name) +{ + return name.replace(" ", "", true).replace("=", "", true); +} + void QuickLaunchWidget::add_or_adjust_button(String const& button_name, NonnullOwnPtr&& entry) { + auto file_name_to_watch = entry->file_name_to_watch(); + if (!file_name_to_watch.is_null()) { + if (!m_watcher) { + // FIXME: Propagate errors + m_watcher = MUST(Core::FileWatcher::create()); + m_watcher->on_change = [this](Core::FileWatcherEvent const& event) { + auto name = sanitize_entry_name(event.event_path); + dbgln("Removing QuickLaunch entry {}", name); + auto button = find_child_of_type_named(name); + if (button) + remove_child(*button); + }; + } + // FIXME: Propagate errors + MUST(m_watcher->add_watch(file_name_to_watch, Core::FileWatcherEvent::Type::Deleted)); + } + auto button = find_child_of_type_named(button_name); if (!button) button = &add(); @@ -192,7 +216,7 @@ void QuickLaunchWidget::drop_event(GUI::DropEvent& event) for (auto& url : urls) { auto entry = QuickLaunchEntry::create_from_path(url.path()); if (entry) { - auto item_name = entry->name().replace(" ", "", true).replace("=", "", true); + auto item_name = sanitize_entry_name(entry->name()); add_or_adjust_button(item_name, entry.release_nonnull()); Config::write_string("Taskbar", quick_launch, item_name, url.path()); } diff --git a/Userland/Services/Taskbar/QuickLaunchWidget.h b/Userland/Services/Taskbar/QuickLaunchWidget.h index 3e68587c0e7..5896d97aaec 100644 --- a/Userland/Services/Taskbar/QuickLaunchWidget.h +++ b/Userland/Services/Taskbar/QuickLaunchWidget.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -19,6 +20,7 @@ public: virtual ErrorOr launch() const = 0; virtual GUI::Icon icon() const = 0; virtual String name() const = 0; + virtual String file_name_to_watch() const = 0; static OwnPtr create_from_config_value(StringView path); static OwnPtr create_from_path(StringView path); @@ -34,6 +36,7 @@ public: virtual ErrorOr launch() const override; virtual GUI::Icon icon() const override { return m_app_file->icon(); } virtual String name() const override { return m_app_file->name(); } + virtual String file_name_to_watch() const override { return {}; } private: NonnullRefPtr m_app_file; @@ -49,6 +52,7 @@ public: virtual ErrorOr launch() const override; virtual GUI::Icon icon() const override; virtual String name() const override; + virtual String file_name_to_watch() const override { return m_path; } private: String m_path; @@ -63,6 +67,7 @@ public: virtual ErrorOr launch() const override; virtual GUI::Icon icon() const override; virtual String name() const override; + virtual String file_name_to_watch() const override { return m_path; } private: String m_path; @@ -86,6 +91,7 @@ private: RefPtr m_context_menu; RefPtr m_context_menu_default_action; String m_context_menu_app_name; + RefPtr m_watcher; }; }