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 <AK/SharedBuffer.h>
  27. #include <LibCore/ProcessStatisticsReader.h>
  28. #include <LibGUI/FileIconProvider.h>
  29. #include <LibGUI/RunningProcessesModel.h>
  30. namespace GUI {
  31. NonnullRefPtr<RunningProcessesModel> RunningProcessesModel::create()
  32. {
  33. return adopt(*new RunningProcessesModel);
  34. }
  35. RunningProcessesModel::RunningProcessesModel()
  36. {
  37. }
  38. RunningProcessesModel::~RunningProcessesModel()
  39. {
  40. }
  41. void RunningProcessesModel::update()
  42. {
  43. m_processes.clear();
  44. Core::ProcessStatisticsReader reader;
  45. auto processes = reader.get_all();
  46. for (auto& it : processes) {
  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. did_update();
  55. }
  56. int RunningProcessesModel::row_count(const GUI::ModelIndex&) const
  57. {
  58. return m_processes.size();
  59. }
  60. int RunningProcessesModel::column_count(const GUI::ModelIndex&) const
  61. {
  62. return Column::__Count;
  63. }
  64. String RunningProcessesModel::column_name(int column_index) const
  65. {
  66. switch (column_index) {
  67. case Column::Icon:
  68. return {};
  69. case Column::PID:
  70. return "PID";
  71. case Column::UID:
  72. return "UID";
  73. case Column::Name:
  74. return "Name";
  75. }
  76. ASSERT_NOT_REACHED();
  77. }
  78. GUI::Variant RunningProcessesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  79. {
  80. auto& process = m_processes[index.row()];
  81. if (role == ModelRole::Custom) {
  82. return process.pid;
  83. }
  84. if (role == ModelRole::Display) {
  85. switch (index.column()) {
  86. case Column::Icon:
  87. if (!process.icon)
  88. return GUI::Icon();
  89. return GUI::Icon(*process.icon);
  90. case Column::PID:
  91. return process.pid;
  92. case Column::UID:
  93. return process.uid;
  94. case Column::Name:
  95. return process.name;
  96. }
  97. ASSERT_NOT_REACHED();
  98. }
  99. return {};
  100. }
  101. }