RunningProcessesModel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/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. for (auto& it : processes) {
  46. Process process;
  47. process.pid = it.value.pid;
  48. process.uid = it.value.uid;
  49. if (it.value.icon_id != -1) {
  50. if (auto icon_buffer = SharedBuffer::create_from_shbuf_id(it.value.icon_id)) {
  51. process.icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *icon_buffer, { 16, 16 });
  52. }
  53. }
  54. process.name = it.value.name;
  55. m_processes.append(move(process));
  56. }
  57. did_update();
  58. }
  59. int RunningProcessesModel::row_count(const GUI::ModelIndex&) const
  60. {
  61. return m_processes.size();
  62. }
  63. int RunningProcessesModel::column_count(const GUI::ModelIndex&) const
  64. {
  65. return Column::__Count;
  66. }
  67. String RunningProcessesModel::column_name(int column_index) const
  68. {
  69. switch (column_index) {
  70. case Column::Icon:
  71. return {};
  72. case Column::PID:
  73. return "PID";
  74. case Column::UID:
  75. return "UID";
  76. case Column::Name:
  77. return "Name";
  78. }
  79. ASSERT_NOT_REACHED();
  80. }
  81. GUI::Variant RunningProcessesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  82. {
  83. auto& process = m_processes[index.row()];
  84. if (role == ModelRole::Custom) {
  85. return process.pid;
  86. }
  87. if (role == ModelRole::Display) {
  88. switch (index.column()) {
  89. case Column::Icon:
  90. if (!process.icon)
  91. return GUI::Icon();
  92. return GUI::Icon(*process.icon);
  93. case Column::PID:
  94. return process.pid;
  95. case Column::UID:
  96. return process.uid;
  97. case Column::Name:
  98. return process.name;
  99. }
  100. ASSERT_NOT_REACHED();
  101. }
  102. return {};
  103. }
  104. }