Serenity Operating System
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
27#include "RemoteObjectGraphModel.h"
28#include "RemoteObject.h"
29#include "RemoteProcess.h"
30#include <AK/JsonObject.h>
31#include <AK/JsonValue.h>
32#include <LibGUI/Application.h>
33#include <stdio.h>
34
35RemoteObjectGraphModel::RemoteObjectGraphModel(RemoteProcess& process)
36 : m_process(process)
37{
38 m_object_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
39 m_window_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png"));
40}
41
42RemoteObjectGraphModel::~RemoteObjectGraphModel()
43{
44}
45
46GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
47{
48 if (!parent.is_valid()) {
49 if (m_process.roots().is_empty())
50 return {};
51 return create_index(row, column, &m_process.roots().at(row));
52 }
53 auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
54 return create_index(row, column, &remote_parent.children.at(row));
55}
56
57GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
58{
59 if (!index.is_valid())
60 return {};
61 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
62 if (!remote_object.parent)
63 return {};
64
65 // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
66 if (!remote_object.parent->parent) {
67 for (size_t row = 0; row < m_process.roots().size(); ++row) {
68 if (&m_process.roots()[row] == remote_object.parent)
69 return create_index(row, 0, remote_object.parent);
70 }
71 ASSERT_NOT_REACHED();
72 return {};
73 }
74
75 for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
76 if (&remote_object.parent->parent->children[row] == remote_object.parent)
77 return create_index(row, 0, remote_object.parent);
78 }
79
80 ASSERT_NOT_REACHED();
81 return {};
82}
83
84int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
85{
86 if (!index.is_valid())
87 return m_process.roots().size();
88 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
89 return remote_object.children.size();
90}
91
92int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
93{
94 return 1;
95}
96
97GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, Role role) const
98{
99 auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
100 if (role == Role::Icon) {
101 if (remote_object->class_name == "GWindow")
102 return m_window_icon;
103 return m_object_icon;
104 }
105 if (role == Role::Display) {
106 return String::format("%s{%s} (%d)", remote_object->class_name.characters(), remote_object->address.characters(), remote_object->children.size());
107 }
108 return {};
109}
110
111void RemoteObjectGraphModel::update()
112{
113 did_update();
114}