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 m_layout_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/layout.png"));
41 m_timer_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/timer.png"));
42}
43
44RemoteObjectGraphModel::~RemoteObjectGraphModel()
45{
46}
47
48GUI::ModelIndex RemoteObjectGraphModel::index(int row, int column, const GUI::ModelIndex& parent) const
49{
50 if (!parent.is_valid()) {
51 if (m_process.roots().is_empty())
52 return {};
53 return create_index(row, column, &m_process.roots().at(row));
54 }
55 auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
56 return create_index(row, column, &remote_parent.children.at(row));
57}
58
59GUI::ModelIndex RemoteObjectGraphModel::parent_index(const GUI::ModelIndex& index) const
60{
61 if (!index.is_valid())
62 return {};
63 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
64 if (!remote_object.parent)
65 return {};
66
67 // NOTE: If the parent has no parent, it's a root, so we have to look among the remote roots.
68 if (!remote_object.parent->parent) {
69 for (size_t row = 0; row < m_process.roots().size(); ++row) {
70 if (&m_process.roots()[row] == remote_object.parent)
71 return create_index(row, 0, remote_object.parent);
72 }
73 ASSERT_NOT_REACHED();
74 return {};
75 }
76
77 for (size_t row = 0; row < remote_object.parent->parent->children.size(); ++row) {
78 if (&remote_object.parent->parent->children[row] == remote_object.parent)
79 return create_index(row, 0, remote_object.parent);
80 }
81
82 ASSERT_NOT_REACHED();
83 return {};
84}
85
86int RemoteObjectGraphModel::row_count(const GUI::ModelIndex& index) const
87{
88 if (!index.is_valid())
89 return m_process.roots().size();
90 auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
91 return remote_object.children.size();
92}
93
94int RemoteObjectGraphModel::column_count(const GUI::ModelIndex&) const
95{
96 return 1;
97}
98
99GUI::Variant RemoteObjectGraphModel::data(const GUI::ModelIndex& index, Role role) const
100{
101 auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
102 if (role == Role::Icon) {
103 if (remote_object->class_name == "Window")
104 return m_window_icon;
105 if (remote_object->class_name == "Timer")
106 return m_timer_icon;
107 if (remote_object->class_name.ends_with("Layout"))
108 return m_layout_icon;
109 return m_object_icon;
110 }
111 if (role == Role::Display) {
112 return String::format("%s{%p}", remote_object->class_name.characters(), remote_object->address);
113 }
114 return {};
115}
116
117void RemoteObjectGraphModel::update()
118{
119 did_update();
120}