2020-05-15 22:37:31 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-15 22:37:31 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ClipboardHistoryModel.h"
|
2021-08-27 05:24:09 +03:00
|
|
|
#include <LibConfig/Client.h>
|
2021-12-03 01:36:40 +03:00
|
|
|
#include <LibCore/System.h>
|
2020-11-24 23:37:04 +03:00
|
|
|
#include <LibGUI/Action.h>
|
2020-05-15 22:37:31 +03:00
|
|
|
#include <LibGUI/Application.h>
|
2020-07-22 16:29:51 +03:00
|
|
|
#include <LibGUI/ImageWidget.h>
|
2020-11-24 23:37:04 +03:00
|
|
|
#include <LibGUI/Menu.h>
|
2020-05-15 22:37:31 +03:00
|
|
|
#include <LibGUI/TableView.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2021-12-03 01:36:40 +03:00
|
|
|
#include <LibMain/Main.h>
|
2020-05-15 22:37:31 +03:00
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-05-15 22:37:31 +03:00
|
|
|
{
|
2022-10-03 16:32:18 +03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
|
2022-04-16 19:17:39 +03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2020-05-15 22:37:31 +03:00
|
|
|
|
2022-02-11 19:33:09 +03:00
|
|
|
Config::pledge_domain("ClipboardHistory");
|
2021-08-27 06:08:06 +03:00
|
|
|
Config::monitor_domain("ClipboardHistory");
|
2021-08-27 05:24:09 +03:00
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
|
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2022-07-11 20:32:29 +03:00
|
|
|
auto app_icon = TRY(GUI::Icon::try_create_default_icon("edit-copy"sv));
|
2020-12-18 18:46:31 +03:00
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
auto main_window = TRY(GUI::Window::try_create());
|
2020-05-15 22:37:31 +03:00
|
|
|
main_window->set_title("Clipboard history");
|
|
|
|
main_window->set_rect(670, 65, 325, 500);
|
2020-12-18 18:46:31 +03:00
|
|
|
main_window->set_icon(app_icon.bitmap_for_size(16));
|
2020-05-15 22:37:31 +03:00
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
auto table_view = TRY(main_window->try_set_main_widget<GUI::TableView>());
|
2020-05-15 22:37:31 +03:00
|
|
|
auto model = ClipboardHistoryModel::create();
|
2021-12-03 01:36:40 +03:00
|
|
|
table_view->set_model(model);
|
2020-05-15 22:37:31 +03:00
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
table_view->on_activation = [&](GUI::ModelIndex const& index) {
|
2022-02-22 05:07:28 +03:00
|
|
|
auto& data_and_type = model->item_at(index.row()).data_and_type;
|
2020-09-05 18:06:34 +03:00
|
|
|
GUI::Clipboard::the().set_data(data_and_type.data, data_and_type.mime_type, data_and_type.metadata);
|
2020-05-15 22:37:31 +03:00
|
|
|
};
|
|
|
|
|
2020-11-24 23:37:04 +03:00
|
|
|
auto delete_action = GUI::CommonActions::make_delete_action([&](const GUI::Action&) {
|
2021-12-03 01:36:40 +03:00
|
|
|
model->remove_item(table_view->selection().first().row());
|
2020-11-24 23:37:04 +03:00
|
|
|
});
|
|
|
|
|
2021-11-28 22:45:23 +03:00
|
|
|
auto debug_dump_action = GUI::Action::create("Dump to debug console", [&](const GUI::Action&) {
|
2021-12-03 01:36:40 +03:00
|
|
|
table_view->selection().for_each_index([&](GUI::ModelIndex& index) {
|
2021-11-28 22:45:23 +03:00
|
|
|
dbgln("{}", model->data(index, GUI::ModelRole::Display).as_string());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
auto entry_context_menu = TRY(GUI::Menu::try_create());
|
|
|
|
TRY(entry_context_menu->try_add_action(delete_action));
|
|
|
|
TRY(entry_context_menu->try_add_action(debug_dump_action));
|
|
|
|
table_view->on_context_menu_request = [&](GUI::ModelIndex const&, GUI::ContextMenuEvent const& event) {
|
|
|
|
delete_action->set_enabled(!table_view->selection().is_empty());
|
|
|
|
debug_dump_action->set_enabled(!table_view->selection().is_empty());
|
2020-11-24 23:37:04 +03:00
|
|
|
entry_context_menu->popup(event.screen_position());
|
|
|
|
};
|
|
|
|
|
2021-12-03 01:36:40 +03:00
|
|
|
auto applet_window = TRY(GUI::Window::try_create());
|
2020-05-20 21:24:44 +03:00
|
|
|
applet_window->set_title("ClipboardHistory");
|
2021-04-04 18:55:50 +03:00
|
|
|
applet_window->set_window_type(GUI::WindowType::Applet);
|
2021-04-05 14:51:49 +03:00
|
|
|
applet_window->set_has_alpha_channel(true);
|
2021-12-03 01:36:40 +03:00
|
|
|
auto icon_widget = TRY(applet_window->try_set_main_widget<GUI::ImageWidget>());
|
|
|
|
icon_widget->set_tooltip("Clipboard History");
|
2022-07-11 20:32:29 +03:00
|
|
|
icon_widget->load_from_file("/res/icons/16x16/edit-copy.png"sv);
|
2021-12-03 01:36:40 +03:00
|
|
|
icon_widget->on_click = [&main_window = *main_window] {
|
2020-05-15 22:37:31 +03:00
|
|
|
main_window.show();
|
2020-11-11 22:31:35 +03:00
|
|
|
main_window.move_to_front();
|
2020-05-15 22:37:31 +03:00
|
|
|
};
|
|
|
|
applet_window->resize(16, 16);
|
|
|
|
applet_window->show();
|
|
|
|
|
2020-07-04 15:05:19 +03:00
|
|
|
return app->exec();
|
2020-05-15 22:37:31 +03:00
|
|
|
}
|