main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibCore/System.h>
  8. #include <LibDesktop/AppFile.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Icon.h>
  12. #include <LibGUI/IconView.h>
  13. #include <LibGUI/Menu.h>
  14. #include <LibGUI/Model.h>
  15. #include <LibGUI/Process.h>
  16. #include <LibGUI/Statusbar.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibMain/Main.h>
  19. #include <serenity.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. // Modeled after PlaylistWidget.
  23. enum class SettingsAppsModelCustomRole {
  24. _DONOTUSE = (int)GUI::ModelRole::Custom,
  25. RequiresRoot
  26. };
  27. class SettingsAppsModel final : public GUI::Model {
  28. public:
  29. SettingsAppsModel()
  30. {
  31. Desktop::AppFile::for_each([&](Desktop::AppFile& app_file) {
  32. if (app_file.category() != "Settings")
  33. return;
  34. m_apps.append(app_file);
  35. });
  36. quick_sort(m_apps, [](auto& a, auto& b) { return a->name() < b->name(); });
  37. }
  38. virtual int row_count(GUI::ModelIndex const&) const override { return m_apps.size(); }
  39. virtual int column_count(GUI::ModelIndex const&) const override { return 1; }
  40. virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const&) const override
  41. {
  42. if (row < 0 || row >= (int)m_apps.size())
  43. return {};
  44. return create_index(row, column, &m_apps[row]);
  45. }
  46. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  47. {
  48. auto& app = m_apps[index.row()];
  49. if (role == GUI::ModelRole::Icon)
  50. return app->icon();
  51. if (role == GUI::ModelRole::Display) {
  52. DeprecatedString name;
  53. if (app->name().ends_with(" Settings"sv))
  54. name = app->name().substring(0, app->name().length() - " Settings"sv.length());
  55. else
  56. name = app->name();
  57. return name;
  58. }
  59. if (role == GUI::ModelRole::Custom)
  60. return app->executable();
  61. if (role == static_cast<GUI::ModelRole>(SettingsAppsModelCustomRole::RequiresRoot))
  62. return app->requires_root();
  63. return {};
  64. }
  65. private:
  66. Vector<NonnullRefPtr<Desktop::AppFile>> m_apps;
  67. };
  68. ErrorOr<int> serenity_main(Main::Arguments arguments)
  69. {
  70. TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath unix proc exec"));
  71. auto app = TRY(GUI::Application::try_create(arguments));
  72. TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath proc exec"));
  73. auto app_icon = GUI::Icon::default_icon("app-settings"sv);
  74. auto window = TRY(GUI::Window::try_create());
  75. window->set_title("Settings");
  76. window->resize(420, 265);
  77. auto file_menu = TRY(window->try_add_menu("&File"));
  78. file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  79. app->quit();
  80. }));
  81. auto help_menu = TRY(window->try_add_menu("&Help"));
  82. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
  83. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Settings", app_icon, window)));
  84. auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
  85. main_widget->set_fill_with_background_color(true);
  86. main_widget->set_layout<GUI::VerticalBoxLayout>();
  87. auto icon_view = TRY(main_widget->try_add<GUI::IconView>());
  88. icon_view->set_should_hide_unnecessary_scrollbars(true);
  89. auto model = adopt_ref(*new SettingsAppsModel);
  90. icon_view->set_model(*model);
  91. icon_view->on_activation = [&](GUI::ModelIndex const& index) {
  92. auto executable = model->data(index, GUI::ModelRole::Custom).as_string();
  93. auto requires_root = model->data(index, static_cast<GUI::ModelRole>(SettingsAppsModelCustomRole::RequiresRoot)).as_bool();
  94. auto launch_origin_rect = icon_view->to_widget_rect(icon_view->content_rect(index)).translated(icon_view->screen_relative_rect().location());
  95. setenv("__libgui_launch_origin_rect", DeprecatedString::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1);
  96. if (requires_root)
  97. GUI::Process::spawn_or_show_error(window, "/bin/Escalator"sv, Array { executable });
  98. else
  99. GUI::Process::spawn_or_show_error(window, executable);
  100. };
  101. auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>());
  102. icon_view->on_selection_change = [&] {
  103. auto index = icon_view->selection().first();
  104. if (!index.is_valid()) {
  105. statusbar->set_text({});
  106. return;
  107. }
  108. auto& app = *(NonnullRefPtr<Desktop::AppFile>*)index.internal_data();
  109. statusbar->set_text(app->description());
  110. };
  111. window->set_icon(app_icon.bitmap_for_size(16));
  112. window->show();
  113. return app->exec();
  114. }