RunningProcessesModel.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ProcessStatisticsReader.h>
  27. #include <LibGUI/FileIconProvider.h>
  28. #include <LibGUI/RunningProcessesModel.h>
  29. namespace GUI {
  30. NonnullRefPtr<RunningProcessesModel> RunningProcessesModel::create()
  31. {
  32. return adopt(*new RunningProcessesModel);
  33. }
  34. RunningProcessesModel::RunningProcessesModel()
  35. {
  36. }
  37. RunningProcessesModel::~RunningProcessesModel()
  38. {
  39. }
  40. void RunningProcessesModel::update()
  41. {
  42. m_processes.clear();
  43. Core::ProcessStatisticsReader reader;
  44. auto processes = reader.get_all();
  45. if (processes.has_value()) {
  46. for (auto& it : processes.value()) {
  47. Process process;
  48. process.pid = it.value.pid;
  49. process.uid = it.value.uid;
  50. process.icon = FileIconProvider::icon_for_executable(it.value.executable).bitmap_for_size(16);
  51. process.name = it.value.name;
  52. m_processes.append(move(process));
  53. }
  54. }
  55. did_update();
  56. }
  57. int RunningProcessesModel::row_count(const GUI::ModelIndex&) const
  58. {
  59. return m_processes.size();
  60. }
  61. int RunningProcessesModel::column_count(const GUI::ModelIndex&) const
  62. {
  63. return Column::__Count;
  64. }
  65. String RunningProcessesModel::column_name(int column_index) const
  66. {
  67. switch (column_index) {
  68. case Column::Icon:
  69. return {};
  70. case Column::PID:
  71. return "PID";
  72. case Column::UID:
  73. return "UID";
  74. case Column::Name:
  75. return "Name";
  76. }
  77. VERIFY_NOT_REACHED();
  78. }
  79. GUI::Variant RunningProcessesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  80. {
  81. auto& process = m_processes[index.row()];
  82. if (role == ModelRole::Custom) {
  83. return process.pid;
  84. }
  85. if (role == ModelRole::Display) {
  86. switch (index.column()) {
  87. case Column::Icon:
  88. if (!process.icon)
  89. return GUI::Icon();
  90. return GUI::Icon(*process.icon);
  91. case Column::PID:
  92. return process.pid;
  93. case Column::UID:
  94. return process.uid;
  95. case Column::Name:
  96. return process.name;
  97. }
  98. VERIFY_NOT_REACHED();
  99. }
  100. return {};
  101. }
  102. }