| 1 |
/* |
| 2 |
* Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]> |
| 3 |
* Copyright (C) 2011 Jens Georg <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Zeeshan Ali (Khattak) <[email protected]> |
| 6 |
* Authors: Jens Georg <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along |
| 19 |
* with this program; if not, write to the Free Software Foundation, |
| 20 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <config.h> |
| 24 |
|
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
#include <gmodule.h> |
| 29 |
#include <glib/gi18n.h> |
| 30 |
|
| 31 |
#include "gui.h" |
| 32 |
#include "device-treeview.h" |
| 33 |
#include "icons.h" |
| 34 |
#include "main.h" |
| 35 |
|
| 36 |
#include "action-dialog.h" |
| 37 |
|
| 38 |
#define DEFAULT_TEXTVIEW_HEIGHT 55 |
| 39 |
|
| 40 |
static GtkWidget *dialog; |
| 41 |
static GtkWidget *in_args_grid; |
| 42 |
static GtkWidget *out_args_grid; |
| 43 |
static GtkWidget *device_label; |
| 44 |
static GtkWidget *service_label; |
| 45 |
static GtkWidget *action_label; |
| 46 |
static GtkWidget *in_args_expander; |
| 47 |
static GtkWidget *out_args_expander; |
| 48 |
static GtkSizeGroup *label_container_group; |
| 49 |
|
| 50 |
void |
| 51 |
on_action_invocation (GtkButton *button, |
| 52 |
gpointer user_data); |
| 53 |
|
| 54 |
static void |
| 55 |
on_expander_clicked (GObject *expander, |
| 56 |
GParamSpec *param_spec, |
| 57 |
gpointer user_data) |
| 58 |
{ |
| 59 |
/* Unfortunately set_size_request doesn't here */ |
| 60 |
gtk_window_resize (GTK_WINDOW (user_data), 1, 1); |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* 1. Gets rid of all the existing arguments |
| 65 |
* 2. Only show the label if there are any arguments |
| 66 |
*/ |
| 67 |
static void |
| 68 |
prepare_action_arguments_grid (GtkContainer *grid, |
| 69 |
GUPnPServiceActionArgDirection direction, |
| 70 |
GList *arguments) |
| 71 |
{ |
| 72 |
GList *child_node, *children; |
| 73 |
|
| 74 |
/* reset expander state */ |
| 75 |
gtk_expander_set_expanded (GTK_EXPANDER (in_args_expander), TRUE); |
| 76 |
gtk_expander_set_expanded (GTK_EXPANDER (out_args_expander), FALSE); |
| 77 |
|
| 78 |
children = gtk_container_get_children (grid); |
| 79 |
for (child_node = children; |
| 80 |
child_node; |
| 81 |
child_node = child_node->next) { |
| 82 |
gtk_widget_destroy (GTK_WIDGET (child_node->data)); |
| 83 |
} |
| 84 |
g_list_free (children); |
| 85 |
|
| 86 |
if (direction == GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN) { |
| 87 |
gtk_widget_set_visible (in_args_expander, |
| 88 |
arguments != NULL); |
| 89 |
} else { |
| 90 |
gtk_widget_set_visible (out_args_expander, |
| 91 |
arguments != NULL); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static GtkWidget * |
| 96 |
create_widget_for_argument (GUPnPServiceActionArgInfo *arg_info, |
| 97 |
GUPnPServiceIntrospection *introspection) |
| 98 |
{ |
| 99 |
GtkWidget *widget; |
| 100 |
const GUPnPServiceStateVariableInfo *variable; |
| 101 |
GValue default_value; |
| 102 |
|
| 103 |
variable = gupnp_service_introspection_get_state_variable ( |
| 104 |
introspection, |
| 105 |
arg_info->related_state_variable); |
| 106 |
|
| 107 |
memset (&default_value, 0, sizeof (GValue)); |
| 108 |
if (variable->is_numeric) { |
| 109 |
GValue min, max, step; |
| 110 |
|
| 111 |
memset (&min, 0, sizeof (GValue)); |
| 112 |
memset (&max, 0, sizeof (GValue)); |
| 113 |
memset (&step, 0, sizeof (GValue)); |
| 114 |
|
| 115 |
g_value_init (&min, G_TYPE_DOUBLE); |
| 116 |
g_value_init (&max, G_TYPE_DOUBLE); |
| 117 |
g_value_init (&step, G_TYPE_DOUBLE); |
| 118 |
g_value_init (&default_value, G_TYPE_DOUBLE); |
| 119 |
|
| 120 |
g_value_transform (&variable->minimum, &min); |
| 121 |
g_value_transform (&variable->maximum, &max); |
| 122 |
g_value_transform (&variable->step, &step); |
| 123 |
g_value_transform (&variable->default_value, &default_value); |
| 124 |
|
| 125 |
widget = gtk_spin_button_new_with_range ( |
| 126 |
g_value_get_double (&min), |
| 127 |
g_value_get_double (&max), |
| 128 |
g_value_get_double (&step)); |
| 129 |
gtk_spin_button_set_value ( |
| 130 |
GTK_SPIN_BUTTON (widget), |
| 131 |
g_value_get_double (&default_value)); |
| 132 |
|
| 133 |
if (arg_info->direction == |
| 134 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT) |
| 135 |
gtk_widget_set_sensitive (widget, FALSE); |
| 136 |
|
| 137 |
g_value_unset (&min); |
| 138 |
g_value_unset (&max); |
| 139 |
g_value_unset (&step); |
| 140 |
g_value_unset (&default_value); |
| 141 |
} else if (variable->type == G_TYPE_BOOLEAN) { |
| 142 |
gboolean active; |
| 143 |
|
| 144 |
widget = gtk_check_button_new (); |
| 145 |
|
| 146 |
active = g_value_get_boolean (&variable->default_value); |
| 147 |
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), |
| 148 |
active); |
| 149 |
|
| 150 |
if (arg_info->direction == |
| 151 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT) |
| 152 |
gtk_widget_set_sensitive (widget, FALSE); |
| 153 |
|
| 154 |
} else { |
| 155 |
const gchar *default_str; |
| 156 |
|
| 157 |
g_value_init (&default_value, G_TYPE_STRING); |
| 158 |
g_value_transform (&variable->default_value, &default_value); |
| 159 |
|
| 160 |
default_str = g_value_get_string (&default_value); |
| 161 |
|
| 162 |
/* Use a combobox if we are restricted to a list of values and |
| 163 |
* it's an input argument */ |
| 164 |
if (variable->allowed_values && |
| 165 |
arg_info->direction == GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN) { |
| 166 |
GList *node; |
| 167 |
gint index = 0; |
| 168 |
|
| 169 |
widget = gtk_combo_box_text_new (); |
| 170 |
for (node = variable->allowed_values; |
| 171 |
node; |
| 172 |
node = node->next) { |
| 173 |
gtk_combo_box_text_insert ( |
| 174 |
GTK_COMBO_BOX_TEXT (widget), |
| 175 |
index, |
| 176 |
NULL, |
| 177 |
(const char *) node->data); |
| 178 |
|
| 179 |
if (node == variable->allowed_values || |
| 180 |
(default_str != NULL && |
| 181 |
strcmp (default_str, node->data) == 0)) { |
| 182 |
gtk_combo_box_set_active ( |
| 183 |
GTK_COMBO_BOX (widget), |
| 184 |
index); |
| 185 |
} |
| 186 |
|
| 187 |
index++; |
| 188 |
} |
| 189 |
} else { |
| 190 |
GtkTextView *text_view; |
| 191 |
GtkTextBuffer *text_buffer; |
| 192 |
|
| 193 |
text_view = GTK_TEXT_VIEW (gtk_text_view_new ()); |
| 194 |
widget = gtk_scrolled_window_new (NULL, NULL); |
| 195 |
g_object_set (widget, |
| 196 |
"shadow-type", GTK_SHADOW_IN, |
| 197 |
"hscrollbar-policy", GTK_POLICY_AUTOMATIC, |
| 198 |
"vscrollbar-policy", GTK_POLICY_AUTOMATIC, |
| 199 |
"min-content-height", DEFAULT_TEXTVIEW_HEIGHT, |
| 200 |
NULL); |
| 201 |
|
| 202 |
gtk_container_add (GTK_CONTAINER (widget), |
| 203 |
GTK_WIDGET (text_view)); |
| 204 |
gtk_text_view_set_wrap_mode (text_view, GTK_WRAP_CHAR); |
| 205 |
text_buffer = gtk_text_view_get_buffer (text_view); |
| 206 |
|
| 207 |
if (default_str != NULL) |
| 208 |
gtk_text_buffer_set_text (text_buffer, |
| 209 |
default_str, |
| 210 |
-1); |
| 211 |
|
| 212 |
if (arg_info->direction == |
| 213 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT) |
| 214 |
gtk_text_view_set_editable (text_view, FALSE); |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
g_value_unset (&default_value); |
| 219 |
} |
| 220 |
|
| 221 |
g_object_set_data_full (G_OBJECT (widget), |
| 222 |
"argument-name", |
| 223 |
g_strdup (arg_info->name), |
| 224 |
g_free); |
| 225 |
return widget; |
| 226 |
} |
| 227 |
|
| 228 |
static void |
| 229 |
populate_action_arguments_grid (GtkWidget *grid, |
| 230 |
GList *arguments, |
| 231 |
GUPnPServiceActionArgDirection direction, |
| 232 |
GUPnPServiceIntrospection *introspection) |
| 233 |
{ |
| 234 |
GList *arg_node; |
| 235 |
GtkWidget *last_aligner_grid = NULL; |
| 236 |
|
| 237 |
g_assert (introspection != NULL); |
| 238 |
|
| 239 |
prepare_action_arguments_grid (GTK_CONTAINER (grid), |
| 240 |
direction, |
| 241 |
arguments); |
| 242 |
|
| 243 |
gtk_widget_set_size_request (dialog, 0, 0); |
| 244 |
|
| 245 |
for (arg_node = arguments; |
| 246 |
arg_node; |
| 247 |
arg_node = arg_node->next) { |
| 248 |
GUPnPServiceActionArgInfo *arg_info; |
| 249 |
GtkWidget *aligner_grid; |
| 250 |
GtkWidget *label; |
| 251 |
GtkWidget *input_widget; |
| 252 |
|
| 253 |
arg_info = (GUPnPServiceActionArgInfo *) arg_node->data; |
| 254 |
|
| 255 |
/* First add the name */ |
| 256 |
/* GTK_ALIGN_START seems to have a bug in a size group: |
| 257 |
* use a container for alignment */ |
| 258 |
aligner_grid = gtk_grid_new (); |
| 259 |
gtk_grid_attach_next_to (GTK_GRID (grid), |
| 260 |
aligner_grid, |
| 261 |
last_aligner_grid, |
| 262 |
GTK_POS_BOTTOM, |
| 263 |
1, 1); |
| 264 |
last_aligner_grid = aligner_grid; |
| 265 |
|
| 266 |
label = gtk_label_new (arg_info->name); |
| 267 |
gtk_widget_set_valign (label, GTK_ALIGN_CENTER); |
| 268 |
gtk_widget_set_vexpand (label, TRUE); |
| 269 |
gtk_widget_set_halign (label, GTK_ALIGN_START); |
| 270 |
gtk_grid_attach (GTK_GRID (aligner_grid), label, |
| 271 |
0, 0, 1, 1); |
| 272 |
gtk_size_group_add_widget (label_container_group, |
| 273 |
aligner_grid); |
| 274 |
gtk_widget_show_all (aligner_grid); |
| 275 |
|
| 276 |
/* Then the input widget */ |
| 277 |
input_widget = create_widget_for_argument (arg_info, |
| 278 |
introspection); |
| 279 |
gtk_widget_set_hexpand (input_widget, TRUE); |
| 280 |
gtk_grid_attach_next_to (GTK_GRID (grid), |
| 281 |
input_widget, |
| 282 |
aligner_grid, |
| 283 |
GTK_POS_RIGHT, |
| 284 |
1,1); |
| 285 |
gtk_widget_show_all (input_widget); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
setup_action_dialog_labels (GUPnPServiceInfo *service_info, |
| 291 |
GUPnPServiceActionInfo *action_info) |
| 292 |
{ |
| 293 |
GUPnPDeviceInfo *device_info; |
| 294 |
gchar *friendly_name; |
| 295 |
gchar *id; |
| 296 |
|
| 297 |
device_info = get_service_device (service_info); |
| 298 |
friendly_name = gupnp_device_info_get_friendly_name (device_info); |
| 299 |
gtk_label_set_text (GTK_LABEL (device_label), friendly_name); |
| 300 |
g_free (friendly_name); |
| 301 |
|
| 302 |
id = gupnp_service_info_get_id (service_info); |
| 303 |
gtk_label_set_text (GTK_LABEL (service_label), id); |
| 304 |
g_free (id); |
| 305 |
|
| 306 |
gtk_label_set_text (GTK_LABEL (action_label), action_info->name); |
| 307 |
} |
| 308 |
|
| 309 |
static GList * |
| 310 |
find_arguments_by_direction (GList *arguments, |
| 311 |
GUPnPServiceActionArgDirection direction) |
| 312 |
{ |
| 313 |
GList *ret_list = NULL; |
| 314 |
GList *arg_node; |
| 315 |
|
| 316 |
for (arg_node = arguments; |
| 317 |
arg_node; |
| 318 |
arg_node = arg_node->next) { |
| 319 |
GUPnPServiceActionArgInfo *arg_info; |
| 320 |
|
| 321 |
arg_info = (GUPnPServiceActionArgInfo *) arg_node->data; |
| 322 |
if (arg_info->direction == direction) { |
| 323 |
ret_list = g_list_append (ret_list, arg_node->data); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return ret_list; |
| 328 |
} |
| 329 |
|
| 330 |
void |
| 331 |
run_action_dialog (GUPnPServiceActionInfo *action_info, |
| 332 |
GUPnPServiceProxy *proxy, |
| 333 |
GUPnPServiceIntrospection *introspection) |
| 334 |
{ |
| 335 |
GList *in_arguments = NULL; |
| 336 |
GList *out_arguments = NULL; |
| 337 |
|
| 338 |
setup_action_dialog_labels (GUPNP_SERVICE_INFO (proxy), action_info); |
| 339 |
|
| 340 |
in_arguments = find_arguments_by_direction ( |
| 341 |
action_info->arguments, |
| 342 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN); |
| 343 |
populate_action_arguments_grid ( |
| 344 |
in_args_grid, |
| 345 |
in_arguments, |
| 346 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN, |
| 347 |
introspection); |
| 348 |
|
| 349 |
out_arguments = find_arguments_by_direction ( |
| 350 |
action_info->arguments, |
| 351 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT); |
| 352 |
populate_action_arguments_grid ( |
| 353 |
out_args_grid, |
| 354 |
out_arguments, |
| 355 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT, |
| 356 |
introspection); |
| 357 |
|
| 358 |
/* If the action only has out arguments, open the out expander */ |
| 359 |
if (!in_arguments) |
| 360 |
gtk_expander_set_expanded (GTK_EXPANDER (out_args_expander), TRUE); |
| 361 |
|
| 362 |
gtk_dialog_run (GTK_DIALOG (dialog)); |
| 363 |
gtk_widget_hide (dialog); |
| 364 |
|
| 365 |
g_list_free (in_arguments); |
| 366 |
g_list_free (out_arguments); |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
g_value_free (gpointer data) |
| 371 |
{ |
| 372 |
g_value_unset ((GValue *) data); |
| 373 |
g_slice_free (GValue, data); |
| 374 |
} |
| 375 |
|
| 376 |
static GUPnPServiceActionArgInfo * |
| 377 |
get_action_argument (GUPnPServiceActionInfo *action_info, |
| 378 |
const gchar *argument_name) |
| 379 |
{ |
| 380 |
GUPnPServiceActionArgInfo *arg_info = NULL; |
| 381 |
GList *arg_node; |
| 382 |
|
| 383 |
g_assert (argument_name != NULL); |
| 384 |
|
| 385 |
for (arg_node = action_info->arguments; |
| 386 |
arg_node; |
| 387 |
arg_node = arg_node->next) { |
| 388 |
GUPnPServiceActionArgInfo *info; |
| 389 |
|
| 390 |
info = (GUPnPServiceActionArgInfo *) arg_node->data; |
| 391 |
if (strcmp (info->name, argument_name) == 0) { |
| 392 |
arg_info = info; |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return arg_info; |
| 398 |
} |
| 399 |
|
| 400 |
static gchar * |
| 401 |
get_action_arg_widget_info (GtkWidget *arg_widget, |
| 402 |
GUPnPServiceIntrospection *introspection, |
| 403 |
GUPnPServiceActionInfo *action_info, |
| 404 |
GValue **value) |
| 405 |
{ |
| 406 |
GUPnPServiceActionArgInfo *arg_info; |
| 407 |
const GUPnPServiceStateVariableInfo *variable_info; |
| 408 |
gchar *arg_name; |
| 409 |
|
| 410 |
g_assert (value != NULL); |
| 411 |
|
| 412 |
arg_name = g_object_get_data (G_OBJECT (arg_widget), |
| 413 |
"argument-name"); |
| 414 |
if (arg_name == NULL) |
| 415 |
return NULL; |
| 416 |
|
| 417 |
arg_info = get_action_argument (action_info, arg_name); |
| 418 |
variable_info = gupnp_service_introspection_get_state_variable ( |
| 419 |
introspection, |
| 420 |
arg_info->related_state_variable); |
| 421 |
|
| 422 |
if (arg_info->direction == GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT) { |
| 423 |
*value = NULL; |
| 424 |
return arg_info->name; |
| 425 |
} |
| 426 |
|
| 427 |
*value = g_slice_alloc0 (sizeof (GValue)); |
| 428 |
g_value_init (*value, variable_info->type); |
| 429 |
|
| 430 |
if (variable_info->is_numeric) { |
| 431 |
GValue double_value; |
| 432 |
gdouble num; |
| 433 |
|
| 434 |
memset (&double_value, 0, sizeof (GValue)); |
| 435 |
g_value_init (&double_value, G_TYPE_DOUBLE); |
| 436 |
|
| 437 |
num = gtk_spin_button_get_value (GTK_SPIN_BUTTON (arg_widget)); |
| 438 |
g_value_set_double (&double_value, num); |
| 439 |
g_value_transform (&double_value, *value); |
| 440 |
|
| 441 |
g_value_unset (&double_value); |
| 442 |
} else if (variable_info->type == G_TYPE_BOOLEAN) { |
| 443 |
gboolean active; |
| 444 |
|
| 445 |
active = gtk_toggle_button_get_active ( |
| 446 |
GTK_TOGGLE_BUTTON (arg_widget)); |
| 447 |
g_value_set_boolean (*value, |
| 448 |
active); |
| 449 |
} else { |
| 450 |
GValue str_value; |
| 451 |
|
| 452 |
memset (&str_value, 0, sizeof (GValue)); |
| 453 |
g_value_init (&str_value, G_TYPE_STRING); |
| 454 |
|
| 455 |
if (GTK_IS_SCROLLED_WINDOW (arg_widget)) { |
| 456 |
GtkWidget *text_view; |
| 457 |
GtkTextBuffer *text_buffer; |
| 458 |
GtkTextIter start, end; |
| 459 |
const gchar *text; |
| 460 |
|
| 461 |
text_view = gtk_bin_get_child (GTK_BIN (arg_widget)); |
| 462 |
text_buffer = gtk_text_view_get_buffer ( |
| 463 |
GTK_TEXT_VIEW (text_view)); |
| 464 |
gtk_text_buffer_get_bounds (text_buffer, |
| 465 |
&start, |
| 466 |
&end); |
| 467 |
text = gtk_text_buffer_get_text (text_buffer, |
| 468 |
&start, |
| 469 |
&end, |
| 470 |
FALSE); |
| 471 |
g_value_set_string (&str_value, text); |
| 472 |
} |
| 473 |
else { |
| 474 |
gchar *text; |
| 475 |
|
| 476 |
text = gtk_combo_box_text_get_active_text ( |
| 477 |
GTK_COMBO_BOX_TEXT (arg_widget)); |
| 478 |
g_value_set_string (&str_value, text); |
| 479 |
g_free (text); |
| 480 |
} |
| 481 |
|
| 482 |
g_value_transform (&str_value, *value); |
| 483 |
|
| 484 |
g_value_unset (&str_value); |
| 485 |
} |
| 486 |
|
| 487 |
return arg_info->name; |
| 488 |
} |
| 489 |
|
| 490 |
static void |
| 491 |
update_out_action_argument_widget (GtkWidget *arg_widget, |
| 492 |
GUPnPServiceIntrospection *introspection, |
| 493 |
GUPnPServiceActionInfo *action_info, |
| 494 |
GValue *value) |
| 495 |
{ |
| 496 |
GUPnPServiceActionArgInfo *arg_info; |
| 497 |
const GUPnPServiceStateVariableInfo *variable_info; |
| 498 |
gchar *argument_name; |
| 499 |
|
| 500 |
g_assert (value != NULL); |
| 501 |
|
| 502 |
argument_name = g_object_get_data (G_OBJECT (arg_widget), |
| 503 |
"argument-name"); |
| 504 |
|
| 505 |
if (argument_name == NULL) |
| 506 |
return; |
| 507 |
|
| 508 |
arg_info = get_action_argument (action_info, argument_name); |
| 509 |
variable_info = gupnp_service_introspection_get_state_variable ( |
| 510 |
introspection, |
| 511 |
arg_info->related_state_variable); |
| 512 |
|
| 513 |
g_assert (arg_info->direction == |
| 514 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_OUT); |
| 515 |
|
| 516 |
if (variable_info->is_numeric) { |
| 517 |
GValue double_value; |
| 518 |
gdouble num; |
| 519 |
|
| 520 |
memset (&double_value, 0, sizeof (GValue)); |
| 521 |
g_value_init (&double_value, G_TYPE_DOUBLE); |
| 522 |
|
| 523 |
g_value_transform (value, &double_value); |
| 524 |
num = g_value_get_double (&double_value); |
| 525 |
gtk_spin_button_set_value (GTK_SPIN_BUTTON (arg_widget), num); |
| 526 |
|
| 527 |
g_value_unset (&double_value); |
| 528 |
} else if (variable_info->type == G_TYPE_BOOLEAN) { |
| 529 |
gboolean active; |
| 530 |
|
| 531 |
active = g_value_get_boolean (value); |
| 532 |
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (arg_widget), |
| 533 |
active); |
| 534 |
} else { |
| 535 |
GValue str_value; |
| 536 |
const gchar *text; |
| 537 |
|
| 538 |
memset (&str_value, 0, sizeof (GValue)); |
| 539 |
g_value_init (&str_value, G_TYPE_STRING); |
| 540 |
g_value_transform (value, &str_value); |
| 541 |
|
| 542 |
text = g_value_get_string (&str_value); |
| 543 |
if (text != NULL) { |
| 544 |
GtkWidget *text_view; |
| 545 |
GtkTextBuffer *text_buffer; |
| 546 |
|
| 547 |
text_view = gtk_bin_get_child (GTK_BIN (arg_widget)); |
| 548 |
text_buffer = gtk_text_view_get_buffer |
| 549 |
(GTK_TEXT_VIEW (text_view)); |
| 550 |
gtk_text_buffer_set_text (text_buffer, text, -1); |
| 551 |
} |
| 552 |
|
| 553 |
g_value_unset (&str_value); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
static void |
| 558 |
retrieve_in_action_arguments (GUPnPServiceIntrospection *introspection, |
| 559 |
GUPnPServiceActionInfo *action_info, |
| 560 |
GList **in_names, |
| 561 |
GList **in_values) |
| 562 |
{ |
| 563 |
GList *arg_node; |
| 564 |
|
| 565 |
arg_node = gtk_container_get_children (GTK_CONTAINER (in_args_grid)); |
| 566 |
|
| 567 |
for (; arg_node; arg_node = arg_node->next) { |
| 568 |
GtkWidget *arg_widget; |
| 569 |
gchar *name; |
| 570 |
GValue *value; |
| 571 |
|
| 572 |
arg_widget = GTK_WIDGET (arg_node->data); |
| 573 |
|
| 574 |
name = get_action_arg_widget_info (arg_widget, |
| 575 |
introspection, |
| 576 |
action_info, |
| 577 |
&value); |
| 578 |
if (name == NULL) |
| 579 |
continue; |
| 580 |
|
| 581 |
*in_names = g_list_append (*in_names, name); |
| 582 |
*in_values = g_list_append (*in_values, value); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
static GHashTable * |
| 587 |
retrieve_out_action_arguments (GUPnPServiceIntrospection *introspection, |
| 588 |
GUPnPServiceActionInfo *action_info) |
| 589 |
{ |
| 590 |
GHashTable *out_args; |
| 591 |
GList *arg_node; |
| 592 |
|
| 593 |
out_args = g_hash_table_new_full (g_str_hash, |
| 594 |
g_direct_equal, |
| 595 |
NULL, |
| 596 |
g_value_free); |
| 597 |
|
| 598 |
for (arg_node = action_info->arguments; |
| 599 |
arg_node; |
| 600 |
arg_node = arg_node->next) { |
| 601 |
GUPnPServiceActionArgInfo *arg_info; |
| 602 |
const GUPnPServiceStateVariableInfo *variable_info; |
| 603 |
GValue *value; |
| 604 |
|
| 605 |
arg_info = (GUPnPServiceActionArgInfo *) arg_node->data; |
| 606 |
if (arg_info->direction == |
| 607 |
GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN) |
| 608 |
continue; |
| 609 |
|
| 610 |
variable_info = |
| 611 |
gupnp_service_introspection_get_state_variable ( |
| 612 |
introspection, |
| 613 |
arg_info->related_state_variable); |
| 614 |
|
| 615 |
value = g_slice_alloc0 (sizeof (GValue)); |
| 616 |
g_value_init (value, variable_info->type); |
| 617 |
g_hash_table_insert (out_args, |
| 618 |
arg_info->name, |
| 619 |
value); |
| 620 |
} |
| 621 |
|
| 622 |
return out_args; |
| 623 |
} |
| 624 |
|
| 625 |
static void |
| 626 |
display_action_out_arguments (GHashTable *out_args) |
| 627 |
{ |
| 628 |
GUPnPServiceIntrospection *introspection; |
| 629 |
GUPnPServiceActionInfo *action_info; |
| 630 |
GList *arg_node; |
| 631 |
GList *it; |
| 632 |
|
| 633 |
action_info = get_selected_action (NULL, &introspection); |
| 634 |
if (action_info == NULL) |
| 635 |
return; |
| 636 |
|
| 637 |
arg_node = gtk_container_get_children (GTK_CONTAINER (out_args_grid)); |
| 638 |
|
| 639 |
for (it = arg_node; it; it = it->next) { |
| 640 |
GtkWidget *arg_widget; |
| 641 |
gchar *name; |
| 642 |
GValue *value = NULL; |
| 643 |
|
| 644 |
arg_widget = GTK_WIDGET (it->data); |
| 645 |
|
| 646 |
name = get_action_arg_widget_info (arg_widget, |
| 647 |
introspection, |
| 648 |
action_info, |
| 649 |
&value); |
| 650 |
if (name == NULL) |
| 651 |
continue; |
| 652 |
|
| 653 |
value = g_hash_table_lookup (out_args, name); |
| 654 |
update_out_action_argument_widget (arg_widget, |
| 655 |
introspection, |
| 656 |
action_info, |
| 657 |
value); |
| 658 |
} |
| 659 |
|
| 660 |
g_object_unref (introspection); |
| 661 |
g_list_free (arg_node); |
| 662 |
} |
| 663 |
|
| 664 |
static void |
| 665 |
on_action_complete (GObject *object, GAsyncResult *result, gpointer user_data) |
| 666 |
{ |
| 667 |
GUPnPServiceIntrospection *introspection; |
| 668 |
GUPnPServiceActionInfo *action_info; |
| 669 |
GHashTable *out_args; |
| 670 |
GError *error = NULL; |
| 671 |
GUPnPServiceProxy *proxy = GUPNP_SERVICE_PROXY (object); |
| 672 |
GUPnPServiceProxyAction *action; |
| 673 |
|
| 674 |
action_info = get_selected_action (NULL, &introspection); |
| 675 |
if (action_info == NULL) |
| 676 |
return; |
| 677 |
|
| 678 |
out_args = retrieve_out_action_arguments (introspection, action_info); |
| 679 |
|
| 680 |
action = gupnp_service_proxy_call_action_finish (proxy, result, &error); |
| 681 |
if (error != NULL) { |
| 682 |
GtkWidget *error_dialog; |
| 683 |
|
| 684 |
error_dialog = |
| 685 |
gtk_message_dialog_new ( |
| 686 |
GTK_WINDOW (dialog), |
| 687 |
GTK_DIALOG_MODAL, |
| 688 |
GTK_MESSAGE_ERROR, |
| 689 |
GTK_BUTTONS_CLOSE, |
| 690 |
_("Action failed.\n\nError %d: %s"), |
| 691 |
error->code, |
| 692 |
error->message); |
| 693 |
|
| 694 |
gtk_dialog_run (GTK_DIALOG (error_dialog)); |
| 695 |
gtk_widget_destroy (error_dialog); |
| 696 |
|
| 697 |
goto out; |
| 698 |
} |
| 699 |
|
| 700 |
gupnp_service_proxy_action_get_result_hash (action, out_args, &error); |
| 701 |
if (error != NULL) { |
| 702 |
GtkWidget *error_dialog; |
| 703 |
|
| 704 |
error_dialog = gtk_message_dialog_new ( |
| 705 |
GTK_WINDOW (dialog), |
| 706 |
GTK_DIALOG_MODAL, |
| 707 |
GTK_MESSAGE_ERROR, |
| 708 |
GTK_BUTTONS_CLOSE, |
| 709 |
_ ("Action failed.\n\nError %d: %s"), |
| 710 |
error->code, |
| 711 |
error->message); |
| 712 |
|
| 713 |
gtk_dialog_run (GTK_DIALOG (error_dialog)); |
| 714 |
gtk_widget_destroy (error_dialog); |
| 715 |
} else { |
| 716 |
display_action_out_arguments (out_args); |
| 717 |
} |
| 718 |
|
| 719 |
out: |
| 720 |
g_clear_error (&error); |
| 721 |
g_hash_table_destroy (out_args); |
| 722 |
g_object_unref (introspection); |
| 723 |
} |
| 724 |
|
| 725 |
G_MODULE_EXPORT |
| 726 |
void |
| 727 |
on_action_invocation (GtkButton *button, |
| 728 |
gpointer user_data) |
| 729 |
{ |
| 730 |
GUPnPServiceProxy *proxy; |
| 731 |
GUPnPServiceIntrospection *introspection; |
| 732 |
GUPnPServiceActionInfo *action_info; |
| 733 |
GList *in_names = NULL, *in_values = NULL; |
| 734 |
GUPnPServiceProxyAction *action; |
| 735 |
|
| 736 |
action_info = get_selected_action (&proxy, &introspection); |
| 737 |
if (action_info == NULL) |
| 738 |
return; |
| 739 |
|
| 740 |
retrieve_in_action_arguments (introspection, |
| 741 |
action_info, |
| 742 |
&in_names, |
| 743 |
&in_values); |
| 744 |
|
| 745 |
action = gupnp_service_proxy_action_new_from_list (action_info->name, |
| 746 |
in_names, |
| 747 |
in_values); |
| 748 |
|
| 749 |
gupnp_service_proxy_call_action_async (proxy, |
| 750 |
action, |
| 751 |
NULL, |
| 752 |
on_action_complete, |
| 753 |
NULL); |
| 754 |
gupnp_service_proxy_action_unref (action); |
| 755 |
|
| 756 |
g_list_free (in_names); |
| 757 |
g_list_free_full (in_values, g_value_free); |
| 758 |
|
| 759 |
g_object_unref (proxy); |
| 760 |
g_object_unref (introspection); |
| 761 |
} |
| 762 |
|
| 763 |
void |
| 764 |
init_action_dialog (GtkBuilder *builder) |
| 765 |
{ |
| 766 |
GtkWidget *image; |
| 767 |
|
| 768 |
label_container_group = |
| 769 |
gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); |
| 770 |
|
| 771 |
/* Dialog box and grids */ |
| 772 |
dialog = GTK_WIDGET (gtk_builder_get_object ( |
| 773 |
builder, |
| 774 |
"action-invocation-dialog")); |
| 775 |
g_assert (dialog != NULL); |
| 776 |
in_args_grid = GTK_WIDGET (gtk_builder_get_object ( |
| 777 |
builder, |
| 778 |
"in-action-arguments-grid")); |
| 779 |
g_assert (in_args_grid != NULL); |
| 780 |
out_args_grid = GTK_WIDGET (gtk_builder_get_object ( |
| 781 |
builder, |
| 782 |
"out-action-arguments-grid")); |
| 783 |
g_assert (out_args_grid != NULL); |
| 784 |
|
| 785 |
/* All the labels */ |
| 786 |
in_args_expander = GTK_WIDGET (gtk_builder_get_object ( |
| 787 |
builder, |
| 788 |
"in-action-arguments-expander")); |
| 789 |
g_assert (in_args_expander != NULL); |
| 790 |
out_args_expander = GTK_WIDGET (gtk_builder_get_object ( |
| 791 |
builder, |
| 792 |
"out-action-arguments-expander")); |
| 793 |
g_assert (out_args_expander != NULL); |
| 794 |
device_label = GTK_WIDGET (gtk_builder_get_object (builder, |
| 795 |
"device-label")); |
| 796 |
g_assert (device_label != NULL); |
| 797 |
service_label = GTK_WIDGET (gtk_builder_get_object (builder, |
| 798 |
"service-label")); |
| 799 |
g_assert (service_label != NULL); |
| 800 |
action_label = GTK_WIDGET (gtk_builder_get_object (builder, |
| 801 |
"action-label")); |
| 802 |
g_assert (action_label != NULL); |
| 803 |
|
| 804 |
/* the images */ |
| 805 |
image = GTK_WIDGET (gtk_builder_get_object (builder, "device-image")); |
| 806 |
g_assert (image != NULL); |
| 807 |
gtk_image_set_from_pixbuf (GTK_IMAGE (image), |
| 808 |
get_icon_by_id (ICON_DEVICE)); |
| 809 |
|
| 810 |
image = GTK_WIDGET (gtk_builder_get_object (builder, "service-image")); |
| 811 |
g_assert (image != NULL); |
| 812 |
gtk_image_set_from_pixbuf (GTK_IMAGE (image), |
| 813 |
get_icon_by_id (ICON_SERVICE)); |
| 814 |
|
| 815 |
image = GTK_WIDGET (gtk_builder_get_object (builder, "action-image")); |
| 816 |
g_assert (image != NULL); |
| 817 |
gtk_image_set_from_pixbuf (GTK_IMAGE (image), |
| 818 |
get_icon_by_id (ICON_ACTION)); |
| 819 |
|
| 820 |
g_signal_connect (in_args_expander, |
| 821 |
"notify::expanded", |
| 822 |
G_CALLBACK (on_expander_clicked), |
| 823 |
dialog); |
| 824 |
|
| 825 |
g_signal_connect (out_args_expander, |
| 826 |
"notify::expanded", |
| 827 |
G_CALLBACK (on_expander_clicked), |
| 828 |
dialog); |
| 829 |
} |
| 830 |
|
| 831 |
void |
| 832 |
deinit_action_dialog (void) |
| 833 |
{ |
| 834 |
g_object_unref (label_container_group); |
| 835 |
gtk_widget_destroy (dialog); |
| 836 |
} |
| 837 |
|