Debian Codegraph @

src/procproperties.cpp

raw symbols SCIP
1 /* Process properties dialog
2 * Copyright (C) 2010 Krishnan Parthasarathi <[email protected]>
3 * Robert Ancell <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include <config.h>
21
22 #include <glib/gi18n.h>
23 #include <glibtop/procmem.h>
24 #include <glibtop/procmap.h>
25 #include <glibtop/procstate.h>
26
27 #include "application.h"
28 #include "procactions.h"
29 #include "procproperties.h"
30 #include "procinfo.h"
31 #include "proctable.h"
32 #include "util.h"
33 #include "legacy/e_date.h"
34
35 typedef struct _proc_arg
36 {
37 const gchar *prop;
38 gchar *val;
39 } proc_arg;
40
41 static char *
42 format_memsize (guint64 size)
43 {
44 if (size == 0)
45 return g_strdup ("—");
46 else
47 return g_format_size_full (size, G_FORMAT_SIZE_IEC_UNITS);
48 }
49
50 static void
51 fill_proc_properties (GtkBuilder *builder,
52 ProcInfo *info)
53 {
54 if (!info)
55 return;
56
57 get_process_memory_writable (info);
58
59 GtkLabel *label;
60 AdwActionRow *row;
61 char *text;
62
63 label = GTK_LABEL (gtk_builder_get_object (builder, "pid_label"));
64 text = g_strdup_printf ("%d", info->pid);
65 gtk_label_set_label (label, text);
66 g_free (text);
67
68 label = GTK_LABEL (gtk_builder_get_object (builder, "user_label"));
69 text = g_strdup_printf ("%s (%d)", info->user.c_str (), info->uid);
70 gtk_label_set_label (label, text);
71 g_free (text);
72
73 label = GTK_LABEL (gtk_builder_get_object (builder, "started_label"));
74 text = procman_format_date_for_display (info->start_time);
75 gtk_label_set_label (label, text);
76 g_free (text);
77
78 label = GTK_LABEL (gtk_builder_get_object (builder, "priority_label"));
79 text = g_strdup_printf ("%s (%d)", procman::get_nice_level (info->nice), info->nice);
80 gtk_label_set_label (label, text);
81 g_free (text);
82
83 label = GTK_LABEL (gtk_builder_get_object (builder, "status_label"));
84 gtk_label_set_label (label, format_process_state (info->status));
85
86 label = GTK_LABEL (gtk_builder_get_object (builder, "cpu_label"));
87 text = g_strdup_printf ("%.2f%%", info->pcpu);
88 gtk_label_set_label (label, text);
89 g_free (text);
90
91 label = GTK_LABEL (gtk_builder_get_object (builder, "memory_label"));
92 text = format_memsize (info->mem);
93 gtk_label_set_label (label, text);
94 g_free (text);
95
96 label = GTK_LABEL (gtk_builder_get_object (builder, "cputime_label"));
97 text = procman::format_duration_for_display (100 * info->cpu_time / GsmApplication::get ().frequency);
98 gtk_label_set_label (label, text);
99 g_free (text);
100
101 label = GTK_LABEL (gtk_builder_get_object (builder, "vmemory_label"));
102 text = format_memsize (info->vmsize);
103 gtk_label_set_label (label, text);
104 g_free (text);
105
106 label = GTK_LABEL (gtk_builder_get_object (builder, "rmemory_label"));
107 text = format_memsize (info->memres);
108 gtk_label_set_label (label, text);
109 g_free (text);
110
111 label = GTK_LABEL (gtk_builder_get_object (builder, "wmemory_label"));
112 text = format_memsize (info->memwritable);
113 gtk_label_set_label (label, text);
114 g_free (text);
115
116 label = GTK_LABEL (gtk_builder_get_object (builder, "smemory_label"));
117 text = format_memsize (info->memshared);
118 gtk_label_set_label (label, text);
119 g_free (text);
120
121 row = ADW_ACTION_ROW (gtk_builder_get_object (builder, "securitycontext_row"));
122 text = not info->security_context.empty ()
123 ? g_strdup_printf ("%s", info->security_context.c_str ())
124 : g_strdup ("—");
125 adw_action_row_set_subtitle (row, text);
126 g_free (text);
127
128 row = ADW_ACTION_ROW (gtk_builder_get_object (builder, "commandline_row"));
129 text = g_strdup_printf ("%s", info->arguments.c_str ());
130 adw_action_row_set_subtitle (row, text);
131 g_free (text);
132
133 row = ADW_ACTION_ROW (gtk_builder_get_object (builder, "waitingchannel_row"));
134 text = g_strdup_printf ("%s", info->wchan.c_str ());
135 adw_action_row_set_subtitle (row, text);
136 g_free (text);
137
138 row = ADW_ACTION_ROW (gtk_builder_get_object (builder, "controlgroup_row"));
139 text = not info->cgroup_name.empty ()
140 ? g_strdup_printf ("%s", info->cgroup_name.c_str ())
141 : g_strdup ("—");
142 adw_action_row_set_subtitle (row, text);
143 g_free (text);
144 }
145
146 static void
147 update_procproperties_dialog (GtkBuilder *builder)
148 {
149 ProcInfo *info;
150 GtkWidget *widget;
151
152 widget = GTK_WIDGET (gtk_builder_get_object (builder, "procprop_dialog"));
153
154 pid_t pid = GPOINTER_TO_UINT (static_cast<pid_t*>(g_object_get_data (G_OBJECT (widget), "selected_info")));
155
156 info = GsmApplication::get ().processes.find (pid);
157
158 fill_proc_properties (builder, info);
159 }
160
161 static void
162 close_dialog_action (GSimpleAction*,
163 GVariant*,
164 GtkWindow *dialog)
165 {
166 guint timer;
167
168 timer = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (dialog), "timer"));
169 g_source_remove (timer);
170
171 gtk_window_destroy (dialog);
172 }
173
174 static void
175 close_procprop_dialog (GObject*,
176 gpointer data)
177 {
178 GtkWindow *window = static_cast<GtkWindow*>(data);
179 guint timer;
180
181 timer = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (window), "timer"));
182 g_source_remove (timer);
183
184 gtk_window_destroy (window);
185 }
186
187 static gboolean
188 procprop_timer (gpointer data)
189 {
190 GtkBuilder *builder = static_cast<GtkBuilder*>(data);
191
192 update_procproperties_dialog (builder);
193
194 return TRUE;
195 }
196
197 static void
198 create_single_procproperties_dialog (GtkTreeModel *model,
199 GtkTreePath*,
200 GtkTreeIter *iter,
201 gpointer data)
202 {
203 GsmApplication *app = static_cast<GsmApplication *>(data);
204
205 ProcInfo *info;
206 guint timer;
207 GAction *action;
208 GSimpleActionGroup *action_group;
209 GMenuItem *item;
210
211 gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);
212
213 if (!info)
214 return;
215
216 GtkBuilder *builder = gtk_builder_new ();
217 GError *err = NULL;
218
219 gtk_builder_add_from_resource (builder, "/org/gnome/gnome-system-monitor/data/procproperties.ui", &err);
220 if (err != NULL)
221 g_error ("%s", err->message);
222
223 GtkWindow *procpropdialog = GTK_WINDOW (gtk_builder_get_object (builder, "procprop_dialog"));
224 GtkButton *stop_button = GTK_BUTTON (gtk_builder_get_object (builder, "stop_button"));
225 GtkButton *force_stop_button = GTK_BUTTON (gtk_builder_get_object (builder, "force_stop_button"));
226 GMenu *menu = G_MENU (gtk_builder_get_object (builder, "menu"));
227
228 gtk_window_set_title (GTK_WINDOW (procpropdialog), info->name.c_str ());
229
230 fill_proc_properties (builder, info);
231 g_object_set_data (G_OBJECT (procpropdialog), "selected_info", GUINT_TO_POINTER (info->pid));
232
233 action_group = g_simple_action_group_new ();
234
235 GActionEntry win_action_entries[] = {
236 { "send-signal-stop", on_activate_send_signal, "i", NULL, NULL, { 0, 0, 0 } },
237 { "send-signal-cont", on_activate_send_signal, "i", NULL, NULL, { 0, 0, 0 } },
238 { "send-signal-term", on_activate_send_signal, "i", NULL, NULL, { 0, 0, 0 } },
239 { "send-signal-kill", on_activate_send_signal, "i", NULL, NULL, { 0, 0, 0 } },
240 };
241
242 gtk_actionable_set_action_target_value (GTK_ACTIONABLE (stop_button), g_variant_new_int32 (info->pid));
243 gtk_actionable_set_action_target_value (GTK_ACTIONABLE (force_stop_button), g_variant_new_int32 (info->pid));
244
245 item = g_menu_item_new (_("_Pause…"), "procprop.send-signal-stop");
246 g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_TARGET, g_variant_new_int32 (info->pid));
247 g_menu_append_item (menu, item);
248 item = g_menu_item_new (_("_Resume"), "procprop.send-signal-cont");
249 g_menu_item_set_attribute_value (item, G_MENU_ATTRIBUTE_TARGET, g_variant_new_int32 (info->pid));
250 g_menu_append_item (menu, item);
251
252 g_action_map_add_action_entries (G_ACTION_MAP (action_group),
253 win_action_entries,
254 G_N_ELEMENTS (win_action_entries),
255 app);
256
257 GSimpleAction *close_action = g_simple_action_new ("close", NULL);
258 g_signal_connect (close_action, "activate", G_CALLBACK (close_dialog_action), procpropdialog);
259 g_action_map_add_action (G_ACTION_MAP (action_group), G_ACTION (close_action));
260
261 gtk_widget_insert_action_group (GTK_WIDGET (procpropdialog), "procprop", G_ACTION_GROUP (action_group));
262
263 g_signal_connect (procpropdialog, "close-request",
264 G_CALLBACK (close_procprop_dialog), procpropdialog);
265
266 gtk_window_present (GTK_WINDOW (procpropdialog));
267
268 timer = g_timeout_add_seconds (5, procprop_timer, builder);
269 g_object_set_data (G_OBJECT (procpropdialog), "timer", GUINT_TO_POINTER (timer));
270
271 update_procproperties_dialog (builder);
272 }
273
274 void
275 create_procproperties_dialog (GsmApplication *app)
276 {
277 gtk_tree_selection_selected_foreach (app->selection, create_single_procproperties_dialog,
278 app);
279 }