Debian Codegraph @

src/openfiles.cpp

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include <config.h>
6
7 #include <glib/gi18n.h>
8
9 #include <glibtop/procopenfiles.h>
10
11 #include "open-file.h"
12 #include "procinfo.h"
13
14 #include "openfiles.h"
15
16
17 struct _GsmOpenFiles {
18 AdwWindow parent;
19
20 ProcInfo *info;
21
22 GtkWidget *window_title;
23
24 GListStore *store;
25
26 GHashTable *known_files;
27 guint timer;
28 };
29
30
31 G_DEFINE_FINAL_TYPE (GsmOpenFiles, gsm_open_files, ADW_TYPE_WINDOW)
32
33
34 enum {
35 PROP_0,
36 PROP_INFO,
37 LAST_PROP
38 };
39 static GParamSpec *pspecs[LAST_PROP] = { NULL, };
40
41
42 static void
43 gsm_open_files_dispose (GObject *object)
44 {
45 GsmOpenFiles *self = GSM_OPEN_FILES (object);
46
47 g_clear_pointer (&self->known_files, g_hash_table_unref);
48 g_clear_handle_id (&self->timer, g_source_remove);
49
50 G_OBJECT_CLASS (gsm_open_files_parent_class)->dispose (object);
51 }
52
53
54 static void
55 gsm_open_files_get_property (GObject *object,
56 guint property_id,
57 GValue *value,
58 GParamSpec *pspec)
59 {
60 GsmOpenFiles *self = GSM_OPEN_FILES (object);
61
62 switch (property_id) {
63 case PROP_INFO:
64 g_value_set_pointer (value, self->info);
65 break;
66 default:
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 break;
69 }
70 }
71
72
73 static inline GHashTable *
74 get_current_keys (GsmOpenFiles *self)
75 {
76 /* the strings in the hash set are borrowed from the hash table, so
77 * we don't need to free them */
78 g_autoptr (GHashTable) old_keys =
79 g_hash_table_new (g_str_hash, g_str_equal);
80 GHashTableIter iter;
81 const char *key;
82
83 g_hash_table_iter_init (&iter, self->known_files);
84 while (g_hash_table_iter_next (&iter, (gpointer *) &key, NULL)) {
85 g_hash_table_add (old_keys, (gpointer *) key);
86 }
87
88 return g_steal_pointer (&old_keys);
89 }
90
91
92 static inline void
93 drop_leftover_keys (GsmOpenFiles *self, GHashTable *leftover_keys)
94 {
95 GHashTableIter iter;
96 const char *key;
97
98 g_hash_table_iter_init (&iter, leftover_keys);
99 while (g_hash_table_iter_next (&iter, (gpointer *) &key, NULL)) {
100 g_autoptr (GsmOpenFile) file = NULL;
101 g_autofree char *stolen_key = NULL;
102 guint position;
103
104 if (!g_hash_table_steal_extended (self->known_files,
105 key,
106 (gpointer *) &stolen_key,
107 (gpointer *) &file)) {
108 continue;
109 }
110
111 if (g_list_store_find (self->store, file, &position)) {
112 g_list_store_remove (self->store, position);
113 }
114 }
115 }
116
117
118 static void
119 update_dialog (GsmOpenFiles *self)
120 {
121 g_autofree glibtop_open_files_entry *openfiles = NULL;
122 glibtop_proc_open_files procmap;
123 g_autoptr (GHashTable) leftover_keys = NULL;
124 g_autoptr (GPtrArray) new_files =
125 g_ptr_array_new_null_terminated (100, g_object_unref, TRUE);
126
127
128 // Translators: process name and id
129 char *subtitle = g_strdup_printf (_("%s (PID %u)"), self->info->name.c_str (), self->info->pid);
130 adw_window_title_set_subtitle (ADW_WINDOW_TITLE (self->window_title), subtitle);
131 g_free (subtitle);
132
133 openfiles = glibtop_get_proc_open_files (&procmap, self->info->pid);
134
135 if (!openfiles) {
136 g_hash_table_remove_all (self->known_files);
137 g_list_store_remove_all (self->store);
138 return;
139 }
140
141 /* Get a copy of the keys we already know about so we can
142 * derive which keys closed in the mean time, and drop them */
143 leftover_keys = get_current_keys (self);
144
145 /* Update or insert files */
146 for (size_t i = 0; i < procmap.number; i++) {
147 glibtop_open_files_entry *entry = openfiles + i;
148 g_autofree char *key = g_strdup_printf ("%d#%d", entry->fd, entry->type);
149 GsmOpenFile *existing_file;
150
151 /* If this was already a known key, save it from being cleaned up */
152 g_hash_table_remove (leftover_keys, key);
153
154 existing_file = static_cast<GsmOpenFile *>(g_hash_table_lookup (self->known_files, key));
155 if (existing_file) {
156 gsm_open_file_set_entry (existing_file, entry);
157 } else {
158 g_autoptr (GsmOpenFile) file = gsm_open_file_new (self->info, entry);
159
160 g_hash_table_insert (self->known_files,
161 g_steal_pointer (&key),
162 g_object_ref (file));
163 g_ptr_array_add (new_files, g_steal_pointer (&file));
164 }
165 }
166
167 g_list_store_splice (self->store, 0, 0, new_files->pdata, new_files->len);
168
169 /* Kill closed files */
170 drop_leftover_keys (self, leftover_keys);
171 }
172
173
174 static void
175 gsm_open_files_set_property (GObject *object,
176 guint property_id,
177 const GValue *value,
178 GParamSpec *pspec)
179 {
180 GsmOpenFiles *self = GSM_OPEN_FILES (object);
181
182 switch (property_id) {
183 case PROP_INFO:
184 self->info = static_cast<ProcInfo *>(g_value_get_pointer (value));
185 update_dialog (self);
186 break;
187 default:
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
189 break;
190 }
191 }
192
193
194 static void
195 gsm_open_files_class_init (GsmOpenFilesClass *klass)
196 {
197 GObjectClass *object_class = G_OBJECT_CLASS (klass);
198 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
199
200 object_class->dispose = gsm_open_files_dispose;
201 object_class->get_property = gsm_open_files_get_property;
202 object_class->set_property = gsm_open_files_set_property;
203
204 pspecs[PROP_INFO] =
205 g_param_spec_pointer ("info", NULL, NULL,
206 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
207
208 g_object_class_install_properties (object_class, LAST_PROP, pspecs);
209
210 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/gnome-system-monitor/data/openfiles.ui");
211
212 gtk_widget_class_bind_template_child (widget_class, GsmOpenFiles, window_title);
213 gtk_widget_class_bind_template_child (widget_class, GsmOpenFiles, store);
214
215 g_type_ensure (GSM_TYPE_OPEN_FILE);
216 }
217
218
219 static gboolean
220 openfiles_timer (gpointer data)
221 {
222 GsmOpenFiles *self = GSM_OPEN_FILES (data);
223
224 update_dialog (self);
225
226 return G_SOURCE_CONTINUE;
227 }
228
229
230 static void
231 gsm_open_files_init (GsmOpenFiles *self)
232 {
233 self->known_files = g_hash_table_new_full (g_str_hash,
234 g_str_equal,
235 g_free,
236 g_object_unref);
237
238 gtk_widget_init_template (GTK_WIDGET (self));
239
240 self->timer = g_timeout_add_seconds (5, openfiles_timer, self);
241 g_source_set_name_by_id (self->timer, "refresh openfiles");
242 }
243
244
245 GsmOpenFiles *
246 gsm_open_files_new (GtkApplication *app, ProcInfo *info)
247 {
248 return GSM_OPEN_FILES (g_object_new (GSM_TYPE_OPEN_FILES,
249 "application", app,
250 "info", info,
251 NULL));
252 }