Debian Codegraph @

src/universal-cp/details-treeview.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <gmodule.h>
27 #include <glib/gi18n.h>
28
29 #include "details-treeview.h"
30 #include "gui.h"
31
32 static GtkWidget *treeview;
33 static GtkWidget *copy_value_menuitem;
34 static GtkWidget *popup;
35
36 gboolean
37 on_details_treeview_button_release (GtkWidget *widget,
38 GdkEventButton *event,
39 gpointer user_data);
40 void
41 on_details_treeview_row_activate (GtkMenuItem *menuitem,
42 gpointer user_data);
43
44 void
45 on_copy_all_details_activate (GtkMenuItem *menuitem,
46 gpointer user_data);
47
48 static gboolean
49 get_selected_row (GtkTreeIter *iter)
50 {
51 GtkTreeModel *model;
52 GtkTreeSelection *selection;
53
54 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
55 g_assert (model != NULL);
56 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
57 g_assert (selection != NULL);
58
59 return gtk_tree_selection_get_selected (selection, &model, iter);
60 }
61
62 G_MODULE_EXPORT
63 gboolean
64 on_details_treeview_button_release (GtkWidget *widget,
65 GdkEventButton *event,
66 gpointer user_data)
67 {
68 if (event->type != GDK_BUTTON_RELEASE ||
69 event->button != 3)
70 return FALSE;
71
72 /* Only show "Copy Value" menuitem when a row is selected */
73 g_object_set (copy_value_menuitem,
74 "visible",
75 get_selected_row (NULL),
76 NULL);
77
78 #if GTK_CHECK_VERSION(3,22,0)
79 gtk_menu_popup_at_pointer (GTK_MENU (popup), (GdkEvent *)event);
80 #else
81 gtk_menu_popup (GTK_MENU (popup),
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 event->button,
87 event->time);
88 #endif
89 return TRUE;
90 }
91
92 G_MODULE_EXPORT
93 void
94 on_details_treeview_row_activate (GtkMenuItem *menuitem,
95 gpointer user_data)
96 {
97 GtkTreeModel *model;
98 GtkTreeIter iter;
99
100 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
101 g_assert (model != NULL);
102
103 if (get_selected_row (&iter)) {
104 char *val;
105
106 gtk_tree_model_get (model, &iter, 1, &val, -1);
107 if (val) {
108 GtkClipboard *clipboard;
109
110 clipboard = gtk_clipboard_get
111 (GDK_SELECTION_CLIPBOARD);
112 g_assert (clipboard != NULL);
113 gtk_clipboard_set_text (clipboard, val, -1);
114 g_free (val);
115 }
116 }
117 }
118
119 G_MODULE_EXPORT
120 void
121 on_copy_all_details_activate (GtkMenuItem *menuitem,
122 gpointer user_data)
123 {
124 GtkClipboard *clipboard;
125 GtkTreeModel *model;
126 GtkTreeIter iter;
127 char *copied;
128
129 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
130 g_assert (model != NULL);
131 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
132 g_assert (clipboard != NULL);
133
134 if (!gtk_tree_model_get_iter_first (model, &iter))
135 return;
136
137 copied = g_strdup ("");
138 do {
139 char *name;
140 char *val;
141 char *pair;
142
143 gtk_tree_model_get (model, &iter,
144 0, &name,
145 1, &val, -1);
146 if (!name)
147 continue;
148 if (!val) {
149 g_free (name);
150 continue;
151 }
152
153 pair = g_strjoin (" ", name, val, NULL);
154
155 g_free (val);
156 g_free (name);
157
158 val = copied;
159 copied = g_strjoin ("\n", copied, pair, NULL);
160
161 g_free (val);
162 g_free (pair);
163 } while (gtk_tree_model_iter_next (model, &iter));
164
165 gtk_clipboard_set_text (clipboard, copied, -1);
166
167 g_free (copied);
168 }
169
170 void
171 update_details (const char **tuples)
172 {
173 GtkTreeModel *model;
174 const char **tuple;
175
176 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
177 gtk_list_store_clear (GTK_LIST_STORE (model));
178
179 for (tuple = tuples; *tuple; tuple = tuple + 2) {
180 if (*(tuples + 1) == NULL)
181 continue;
182
183 gtk_list_store_insert_with_values (
184 GTK_LIST_STORE (model),
185 NULL, -1,
186 0, *tuple,
187 1, *(tuple + 1), -1);
188 }
189 }
190
191 void
192 show_action_arg_details (GUPnPServiceActionArgInfo *info)
193 {
194 char *details[32];
195 int i = 0;
196
197 details[i++] = _("Name");
198 details[i++] = g_strdup (info->name);
199 details[i++] = _("Direction");
200 if (info->direction == GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN)
201 details[i++] = g_strdup ("in");
202 else
203 details[i++] = g_strdup ("out");
204 details[i++] = _("Related State Variable");
205 details[i++] = g_strdup (info->related_state_variable);
206 details[i++] = _("Is Return Value");
207 details[i++] = g_strdup (info->retval? _("Yes"): _("No"));
208 details[i] = NULL;
209
210 update_details ((const char **) details);
211
212 /* Only free the values */
213 for (i = 1; details[i - 1]; i += 2) {
214 if (details[i])
215 g_free (details[i]);
216 }
217 }
218
219 void
220 show_action_details (GUPnPServiceActionInfo *info)
221 {
222 char *details[32];
223 int i = 0;
224
225 details[i++] = _("Name");
226 details[i++] = g_strdup (info->name);
227 details[i++] = _("Number of Arguments");
228 details[i++] = g_strdup_printf ("%u",
229 g_list_length (info->arguments));
230 details[i] = NULL;
231
232 update_details ((const char **) details);
233
234 /* Only free the values */
235 for (i = 1; details[i - 1]; i += 2) {
236 if (details[i])
237 g_free (details[i]);
238 }
239 }
240
241 void
242 show_state_variable_details (GUPnPServiceStateVariableInfo *info)
243 {
244 char *details[32];
245 GValue str_value;
246 int i = 0;
247
248 memset (&str_value, 0, sizeof (GValue));
249 g_value_init (&str_value, G_TYPE_STRING);
250
251 details[i++] = _("Name");
252 details[i++] = g_strdup (info->name);
253 details[i++] = _("Send Events");
254 details[i++] = g_strdup (info->send_events? _("Yes"): _("No"));
255 details[i++] = _("GType");
256 details[i++] = g_strdup (g_type_name (info->type));
257
258 details[i++] = _("Default Value");
259 g_value_transform (&info->default_value,
260 &str_value);
261 details[i++] = g_value_dup_string (&str_value);
262
263 if (info->is_numeric) {
264 details[i++] = _("Minimum");
265 g_value_transform (&info->minimum,
266 &str_value);
267 details[i++] = g_value_dup_string (&str_value);
268 details[i++] = _("Maximum");
269 g_value_transform (&info->maximum,
270 &str_value);
271 details[i++] = g_value_dup_string (&str_value);
272 details[i++] = _("Step");
273 g_value_transform (&info->step,
274 &str_value);
275 details[i++] = g_value_dup_string (&str_value);
276 } else if (info->allowed_values) {
277 GList *iter;
278 char **valuesv;
279 int j;
280
281 valuesv =
282 g_malloc (sizeof (char *) *
283 (g_list_length (info->allowed_values) + 1));
284
285 for (j = 0, iter = info->allowed_values;
286 iter;
287 iter = iter->next, j++) {
288 valuesv[j] = (char *) iter->data;
289 }
290 valuesv[j] = NULL;
291
292 details[i++] = _("Allowed Values");
293 details[i++] = g_strjoinv (", ", valuesv);
294 }
295
296 g_value_unset (&str_value);
297 details[i] = NULL;
298
299 update_details ((const char **) details);
300
301 /* Only free the values */
302 for (i = 1; details[i - 1]; i += 2) {
303 if (details[i])
304 g_free (details[i]);
305 }
306 }
307
308 void
309 show_service_details (GUPnPServiceInfo *info)
310 {
311 char *details[32];
312 const GUri *uri;
313 const char *str;
314 int i = 0;
315
316 details[i++] = _("Location");
317 str = gupnp_service_info_get_location (info);
318 if (str)
319 details[i++] = g_strdup (str);
320
321 details[i++] = _("UDN");
322 str = gupnp_service_info_get_udn (info);
323 if (str)
324 details[i++] = g_strdup (str);
325
326 details[i++] = _("Type");
327 str = gupnp_service_info_get_service_type (info);
328 if (str)
329 details[i++] = g_strdup (str);
330
331 details[i++] = _("Base URL");
332 uri = gupnp_service_info_get_url_base (info);
333 if (uri)
334 details[i++] = g_uri_to_string ((GUri *) uri);
335
336 details[i++] = _("Service ID");
337 details[i++] = gupnp_service_info_get_id (info);
338 details[i++] = _("Service URL");
339 details[i++] = gupnp_service_info_get_scpd_url (info);
340 details[i++] = _("Control URL");
341 details[i++] = gupnp_service_info_get_control_url (info);
342 details[i++] = _("Event Subscription URL");
343 details[i++] = gupnp_service_info_get_event_subscription_url (info);
344 details[i] = NULL;
345
346 update_details ((const char **) details);
347
348 /* Only free the values */
349 for (i = 1; details[i - 1]; i += 2) {
350 if (details[i])
351 g_free (details[i]);
352 }
353 }
354
355 void
356 show_device_details (GUPnPDeviceInfo *info)
357 {
358 char *details[32];
359 const GUri *uri;
360 const char *str;
361 int i = 0;
362
363 details[i++] = _("Location");
364 str = gupnp_device_info_get_location (info);
365 if (str)
366 details[i++] = g_strdup (str);
367
368 details[i++] = _("UDN");
369 str = gupnp_device_info_get_udn (info);
370 if (str)
371 details[i++] = g_strdup (str);
372
373 details[i++] = _("Type");
374 str = gupnp_device_info_get_device_type (info);
375 if (str)
376 details[i++] = g_strdup (str);
377
378 details[i++] = _("Base URL");
379 uri = gupnp_device_info_get_url_base (info);
380 if (uri)
381 details[i++] = g_uri_to_string ((GUri *) uri);
382
383 details[i++] = _("Friendly Name");
384 details[i++] = gupnp_device_info_get_friendly_name (info);
385 details[i++] = _("Manufacturer");
386 details[i++] = gupnp_device_info_get_manufacturer (info);
387 details[i++] = _("Manufacturer URL");
388 details[i++] = gupnp_device_info_get_manufacturer_url (info);
389 details[i++] = _("Model Description");
390 details[i++] = gupnp_device_info_get_model_description (info);
391 details[i++] = _("Model Name");
392 details[i++] = gupnp_device_info_get_model_name (info);
393 details[i++] = _("Model Number");
394 details[i++] = gupnp_device_info_get_model_number (info);
395 details[i++] = _("Model URL");
396 details[i++] = gupnp_device_info_get_model_url (info);
397 details[i++] = _("Serial Number");
398 details[i++] = gupnp_device_info_get_serial_number (info);
399 details[i++] = _("UPC");
400 details[i++] = gupnp_device_info_get_upc (info);
401 details[i++] = _("Presentation URL");
402 details[i++] = gupnp_device_info_get_presentation_url (info);
403 details[i] = NULL;
404
405 update_details ((const char **) details);
406
407 /* Only free the values */
408 for (i = 1; details[i - 1]; i += 2) {
409 if (details[i])
410 g_free (details[i]);
411 }
412 }
413
414 static GtkTreeModel *
415 create_details_treemodel (void)
416 {
417 GtkListStore *store;
418
419 store = gtk_list_store_new (2,
420 G_TYPE_STRING, /* Name */
421 G_TYPE_STRING); /* Value */
422
423 return GTK_TREE_MODEL (store);
424 }
425
426 void
427 setup_details_treeview (GtkBuilder *builder)
428 {
429 GtkTreeModel *model;
430 char *headers[3] = { _("Name"),
431 _("Value"),
432 NULL };
433
434 treeview = GTK_WIDGET (gtk_builder_get_object (builder,
435 "details-treeview"));
436 g_assert (treeview != NULL);
437 copy_value_menuitem = GTK_WIDGET (gtk_builder_get_object (builder,
438 "copy-value-menuitem"));
439 g_assert (copy_value_menuitem != NULL);
440 popup = GTK_WIDGET (gtk_builder_get_object (builder, "details-popup"));
441 g_assert (popup != NULL);
442
443 model = create_details_treemodel ();
444 g_assert (model != NULL);
445
446 setup_treeview (treeview, model, headers, 0);
447 g_object_unref (model);
448 }
449