ProcessFileDescriptorMapWidget.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2018-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 "ProcessFileDescriptorMapWidget.h"
  27. #include <LibGUI/GBoxLayout.h>
  28. #include <LibGUI/GJsonArrayModel.h>
  29. #include <LibGUI/GTableView.h>
  30. ProcessFileDescriptorMapWidget::ProcessFileDescriptorMapWidget(GUI::Widget* parent)
  31. : GUI::Widget(parent)
  32. {
  33. set_layout(make<GUI::VBoxLayout>());
  34. layout()->set_margins({ 4, 4, 4, 4 });
  35. m_table_view = GUI::TableView::construct(this);
  36. m_table_view->set_size_columns_to_fit_content(true);
  37. Vector<GUI::JsonArrayModel::FieldSpec> pid_fds_fields;
  38. pid_fds_fields.empend("fd", "FD", Gfx::TextAlignment::CenterRight);
  39. pid_fds_fields.empend("class", "Class", Gfx::TextAlignment::CenterLeft);
  40. pid_fds_fields.empend("offset", "Offset", Gfx::TextAlignment::CenterRight);
  41. pid_fds_fields.empend("absolute_path", "Path", Gfx::TextAlignment::CenterLeft);
  42. pid_fds_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  43. return object.get("seekable").to_bool() ? "Seekable" : "Sequential";
  44. });
  45. pid_fds_fields.empend("Blocking", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  46. return object.get("blocking").to_bool() ? "Blocking" : "Nonblocking";
  47. });
  48. pid_fds_fields.empend("On exec", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  49. return object.get("cloexec").to_bool() ? "Close" : "Keep";
  50. });
  51. pid_fds_fields.empend("Can read", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  52. return object.get("can_read").to_bool() ? "Yes" : "No";
  53. });
  54. pid_fds_fields.empend("Can write", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  55. return object.get("can_write").to_bool() ? "Yes" : "No";
  56. });
  57. m_table_view->set_model(GUI::JsonArrayModel::create({}, move(pid_fds_fields)));
  58. }
  59. ProcessFileDescriptorMapWidget::~ProcessFileDescriptorMapWidget()
  60. {
  61. }
  62. void ProcessFileDescriptorMapWidget::set_pid(pid_t pid)
  63. {
  64. if (m_pid == pid)
  65. return;
  66. m_pid = pid;
  67. static_cast<GUI::JsonArrayModel*>(m_table_view->model())->set_json_path(String::format("/proc/%d/fds", m_pid));
  68. }