| 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 "gui.h" |
| 30 |
#include "icons.h" |
| 31 |
#include "action-dialog.h" |
| 32 |
#include "details-treeview.h" |
| 33 |
#include "main.h" |
| 34 |
|
| 35 |
/* default details */ |
| 36 |
#define SOFTWARE_INFO N_("GUPnP Universal Control Point") |
| 37 |
#define AUTHOR_INFO "Zeeshan Ali (Khattak) <[email protected]>" |
| 38 |
|
| 39 |
static GtkWidget *treeview; |
| 40 |
static GtkWidget *popup; |
| 41 |
static GtkWidget *subscribe_menuitem; |
| 42 |
static GtkWidget *action_menuitem; |
| 43 |
static GtkWidget *separator; |
| 44 |
|
| 45 |
static gboolean expanded; |
| 46 |
|
| 47 |
gboolean |
| 48 |
on_device_treeview_button_release (GtkWidget *widget, |
| 49 |
GdkEventButton *event, |
| 50 |
gpointer user_data); |
| 51 |
|
| 52 |
void |
| 53 |
on_device_treeview_row_activate (GtkMenuItem *menuitem, |
| 54 |
gpointer user_data); |
| 55 |
|
| 56 |
void |
| 57 |
on_expand_devices_activate (GtkMenuItem *menuitem, |
| 58 |
gpointer user_data); |
| 59 |
|
| 60 |
void |
| 61 |
on_collapse_devices_activate (GtkMenuItem *menuitem, |
| 62 |
gpointer user_data); |
| 63 |
static void |
| 64 |
show_default_details (void) |
| 65 |
{ |
| 66 |
const char *details[32]; |
| 67 |
int i = 0; |
| 68 |
|
| 69 |
details[i++] = _("Software"); |
| 70 |
details[i++] = _(SOFTWARE_INFO); |
| 71 |
details[i++] = _("Version"); |
| 72 |
details[i++] = VERSION; |
| 73 |
details[i++] = _("Author"); |
| 74 |
details[i++] = AUTHOR_INFO; |
| 75 |
details[i] = NULL; |
| 76 |
update_details (details); |
| 77 |
} |
| 78 |
|
| 79 |
gboolean |
| 80 |
find_device (GtkTreeModel *model, |
| 81 |
const char *udn, |
| 82 |
GtkTreeIter *root_iter, |
| 83 |
GtkTreeIter *iter) |
| 84 |
{ |
| 85 |
gboolean found = FALSE; |
| 86 |
gboolean more = TRUE; |
| 87 |
|
| 88 |
if (udn == NULL || root_iter == NULL) |
| 89 |
return found; |
| 90 |
|
| 91 |
more = gtk_tree_model_iter_children (model, iter, root_iter); |
| 92 |
|
| 93 |
while (more) { |
| 94 |
GUPnPDeviceInfo *info; |
| 95 |
GtkTreeIter tmp; |
| 96 |
guint icon_type; |
| 97 |
|
| 98 |
gtk_tree_model_get (model, iter, |
| 99 |
2, &info, |
| 100 |
5, &icon_type, -1); |
| 101 |
|
| 102 |
if (info) { |
| 103 |
if (icon_type == ICON_DEVICE) { |
| 104 |
const char *device_udn; |
| 105 |
|
| 106 |
device_udn = gupnp_device_info_get_udn (info); |
| 107 |
|
| 108 |
if (device_udn && |
| 109 |
strcmp (device_udn, udn) == 0) |
| 110 |
found = TRUE; |
| 111 |
} |
| 112 |
|
| 113 |
g_object_unref (info); |
| 114 |
|
| 115 |
if (found) |
| 116 |
break; |
| 117 |
|
| 118 |
if (icon_type == ICON_DEVICE) { |
| 119 |
/* recurse into embedded-devices */ |
| 120 |
found = find_device (model, udn, iter, &tmp); |
| 121 |
if (found) { |
| 122 |
*iter = tmp; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
more = gtk_tree_model_iter_next (model, iter); |
| 129 |
} |
| 130 |
|
| 131 |
return found; |
| 132 |
} |
| 133 |
|
| 134 |
GUPnPDeviceInfo * |
| 135 |
get_service_device (GUPnPServiceInfo *service_info) |
| 136 |
{ |
| 137 |
GUPnPDeviceInfo *info = NULL; |
| 138 |
GtkTreeModel *model; |
| 139 |
GtkTreeIter root_iter; |
| 140 |
GtkTreeIter iter; |
| 141 |
const char *udn; |
| 142 |
|
| 143 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 144 |
g_assert (model != NULL); |
| 145 |
|
| 146 |
if (!gtk_tree_model_get_iter_first (model, &root_iter)) |
| 147 |
return NULL; |
| 148 |
|
| 149 |
udn = gupnp_service_info_get_udn (service_info); |
| 150 |
if (find_device (model, udn, &root_iter, &iter)) |
| 151 |
gtk_tree_model_get (model, &iter, 2, &info, -1); |
| 152 |
|
| 153 |
return info; |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
setup_device_popup (GtkWidget *widget) |
| 158 |
{ |
| 159 |
GUPnPServiceProxy *proxy; |
| 160 |
|
| 161 |
/* See if a service is selected */ |
| 162 |
proxy = get_selected_service (); |
| 163 |
if (proxy != NULL) { |
| 164 |
g_object_set (subscribe_menuitem, |
| 165 |
"visible", |
| 166 |
TRUE, |
| 167 |
"active", |
| 168 |
gupnp_service_proxy_get_subscribed (proxy), |
| 169 |
NULL); |
| 170 |
|
| 171 |
g_object_set (action_menuitem, |
| 172 |
"visible", |
| 173 |
FALSE, |
| 174 |
NULL); |
| 175 |
} else { |
| 176 |
GUPnPServiceActionInfo *action; |
| 177 |
|
| 178 |
g_object_set (subscribe_menuitem, |
| 179 |
"visible", |
| 180 |
FALSE, |
| 181 |
NULL); |
| 182 |
|
| 183 |
/* See if an action is selected */ |
| 184 |
action = get_selected_action (NULL, NULL); |
| 185 |
g_object_set (action_menuitem, |
| 186 |
"visible", |
| 187 |
action != NULL, |
| 188 |
NULL); |
| 189 |
} |
| 190 |
|
| 191 |
/* Separator should be visible only if either service or action row is |
| 192 |
* selected |
| 193 |
*/ |
| 194 |
g_object_set (separator, |
| 195 |
"visible", |
| 196 |
(proxy != NULL), |
| 197 |
NULL); |
| 198 |
if (proxy) |
| 199 |
g_object_unref (proxy); |
| 200 |
} |
| 201 |
|
| 202 |
G_MODULE_EXPORT |
| 203 |
gboolean |
| 204 |
on_device_treeview_button_release (GtkWidget *widget, |
| 205 |
GdkEventButton *event, |
| 206 |
gpointer user_data) |
| 207 |
{ |
| 208 |
if (event->type != GDK_BUTTON_RELEASE || |
| 209 |
event->button != 3) |
| 210 |
return FALSE; |
| 211 |
|
| 212 |
setup_device_popup (popup); |
| 213 |
|
| 214 |
#if GTK_CHECK_VERSION(3,22,0) |
| 215 |
gtk_menu_popup_at_pointer (GTK_MENU (popup), (GdkEvent *)event); |
| 216 |
#else |
| 217 |
gtk_menu_popup (GTK_MENU (popup), |
| 218 |
NULL, |
| 219 |
NULL, |
| 220 |
NULL, |
| 221 |
NULL, |
| 222 |
event->button, |
| 223 |
event->time); |
| 224 |
#endif |
| 225 |
return TRUE; |
| 226 |
} |
| 227 |
|
| 228 |
G_MODULE_EXPORT |
| 229 |
void |
| 230 |
on_device_treeview_row_activate (GtkMenuItem *menuitem, |
| 231 |
gpointer user_data) |
| 232 |
{ |
| 233 |
GUPnPServiceProxy *proxy; |
| 234 |
GUPnPServiceIntrospection *introspection; |
| 235 |
|
| 236 |
/* See if a service is selected */ |
| 237 |
proxy = get_selected_service (); |
| 238 |
if (proxy != NULL) { |
| 239 |
gboolean subscribed; |
| 240 |
|
| 241 |
subscribed = gupnp_service_proxy_get_subscribed (proxy); |
| 242 |
gupnp_service_proxy_set_subscribed (proxy, !subscribed); |
| 243 |
} else { |
| 244 |
GUPnPServiceActionInfo *action_info; |
| 245 |
|
| 246 |
/* See if an action is selected */ |
| 247 |
action_info = get_selected_action (&proxy, &introspection); |
| 248 |
if (action_info != NULL) { |
| 249 |
run_action_dialog (action_info, |
| 250 |
proxy, |
| 251 |
introspection); |
| 252 |
g_object_unref (introspection); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if (proxy != NULL) |
| 257 |
g_object_unref (proxy); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
on_something_selected (GtkTreeSelection *selection, |
| 262 |
gpointer user_data) |
| 263 |
{ |
| 264 |
GtkTreeModel *model; |
| 265 |
GtkTreeIter iter; |
| 266 |
|
| 267 |
if (gtk_tree_selection_get_selected (selection, &model, &iter)) { |
| 268 |
guint icon_type; |
| 269 |
|
| 270 |
gtk_tree_model_get (model, &iter, 5, &icon_type, -1); |
| 271 |
|
| 272 |
/* We recognise things by how they look, don't we? */ |
| 273 |
if (icon_type == ICON_DEVICE) { |
| 274 |
GUPnPDeviceInfo *info; |
| 275 |
|
| 276 |
gtk_tree_model_get (model, &iter, 2, &info, -1); |
| 277 |
show_device_details (info); |
| 278 |
g_object_unref (info); |
| 279 |
} else if (icon_type == ICON_SERVICE) { |
| 280 |
GUPnPServiceInfo *info; |
| 281 |
|
| 282 |
gtk_tree_model_get (model, &iter, 2, &info, -1); |
| 283 |
show_service_details (info); |
| 284 |
g_object_unref (info); |
| 285 |
} else if (icon_type == ICON_VARIABLE) { |
| 286 |
GUPnPServiceStateVariableInfo *info; |
| 287 |
|
| 288 |
gtk_tree_model_get (model, &iter, 4, &info, -1); |
| 289 |
show_state_variable_details (info); |
| 290 |
} else if (icon_type == ICON_ACTION) { |
| 291 |
GUPnPServiceActionInfo *info; |
| 292 |
|
| 293 |
gtk_tree_model_get (model, &iter, 4, &info, -1); |
| 294 |
show_action_details (info); |
| 295 |
} else if (icon_type == ICON_ACTION_ARG_IN || |
| 296 |
icon_type == ICON_ACTION_ARG_OUT) { |
| 297 |
GUPnPServiceActionArgInfo *info; |
| 298 |
|
| 299 |
gtk_tree_model_get (model, &iter, 4, &info, -1); |
| 300 |
show_action_arg_details (info); |
| 301 |
} else |
| 302 |
show_default_details (); |
| 303 |
} |
| 304 |
|
| 305 |
else |
| 306 |
show_default_details (); |
| 307 |
} |
| 308 |
|
| 309 |
void |
| 310 |
remove_device (GUPnPDeviceInfo *info) |
| 311 |
{ |
| 312 |
GtkTreeModel *model; |
| 313 |
GtkTreeIter root_iter; |
| 314 |
GtkTreeIter iter; |
| 315 |
const char *udn; |
| 316 |
|
| 317 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 318 |
g_assert (model != NULL); |
| 319 |
|
| 320 |
udn = gupnp_device_info_get_udn (info); |
| 321 |
|
| 322 |
if (!gtk_tree_model_get_iter_first (model, &root_iter)) |
| 323 |
return; |
| 324 |
|
| 325 |
if (find_device (model, udn, &root_iter, &iter)) { |
| 326 |
GCancellable *cancellable = |
| 327 |
g_object_get_data (G_OBJECT (info), |
| 328 |
"icon-download-cancellable"); |
| 329 |
if (cancellable != NULL) { |
| 330 |
g_cancellable_cancel (cancellable); |
| 331 |
|
| 332 |
g_object_set_data (G_OBJECT (info), |
| 333 |
"icon-download-cancellable", |
| 334 |
NULL); |
| 335 |
} |
| 336 |
gtk_tree_store_remove (GTK_TREE_STORE (model), &iter); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
static void |
| 341 |
on_state_variable_changed (GUPnPServiceProxy *proxy, |
| 342 |
const char *variable_name, |
| 343 |
GValue *value, |
| 344 |
gpointer user_data) |
| 345 |
{ |
| 346 |
GUPnPServiceInfo *info = GUPNP_SERVICE_INFO (proxy); |
| 347 |
GUPnPDeviceInfo *device_info; |
| 348 |
GValue str_value; |
| 349 |
char *id; |
| 350 |
char *friendly_name; |
| 351 |
char *notified_at; |
| 352 |
struct tm *tm; |
| 353 |
time_t current_time; |
| 354 |
|
| 355 |
/* Get the parent device */ |
| 356 |
device_info = get_service_device (info); |
| 357 |
g_return_if_fail (device_info != NULL); |
| 358 |
|
| 359 |
friendly_name = gupnp_device_info_get_friendly_name (device_info); |
| 360 |
g_object_unref (device_info); |
| 361 |
id = gupnp_service_info_get_id (info); |
| 362 |
/* We neither keep devices without friendlyname |
| 363 |
* nor services without an id |
| 364 |
*/ |
| 365 |
g_assert (friendly_name != NULL); |
| 366 |
g_assert (id != NULL); |
| 367 |
|
| 368 |
memset (&str_value, 0, sizeof (GValue)); |
| 369 |
g_value_init (&str_value, G_TYPE_STRING); |
| 370 |
g_value_transform (value, &str_value); |
| 371 |
|
| 372 |
current_time = time (NULL); |
| 373 |
tm = localtime (¤t_time); |
| 374 |
notified_at = g_strdup_printf ("%02d:%02d", |
| 375 |
tm->tm_hour, |
| 376 |
tm->tm_min); |
| 377 |
|
| 378 |
display_event (notified_at, |
| 379 |
friendly_name, |
| 380 |
id, |
| 381 |
variable_name, |
| 382 |
g_value_get_string (&str_value)); |
| 383 |
|
| 384 |
g_free (notified_at); |
| 385 |
g_free (friendly_name); |
| 386 |
g_free (id); |
| 387 |
g_value_unset (&str_value); |
| 388 |
} |
| 389 |
|
| 390 |
static void |
| 391 |
on_device_icon_available (GObject *source, |
| 392 |
GAsyncResult *res, |
| 393 |
gpointer user_data) |
| 394 |
{ |
| 395 |
GtkTreeModel *model; |
| 396 |
GtkTreeIter root_iter; |
| 397 |
GtkTreeIter device_iter; |
| 398 |
const char *udn; |
| 399 |
g_autoptr (GError) error = NULL; |
| 400 |
g_autoptr (GdkPixbuf) icon = NULL; |
| 401 |
|
| 402 |
icon = update_icon_finish (GUPNP_DEVICE_INFO (source), res, &error); |
| 403 |
if (error != NULL) { |
| 404 |
g_debug ("Failed to download icon: %s", error->message); |
| 405 |
} |
| 406 |
|
| 407 |
// There was no icon or there was an error getting that |
| 408 |
// icon. Just keep the default then |
| 409 |
if (icon == NULL) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data)); |
| 414 |
g_assert (model != NULL); |
| 415 |
|
| 416 |
udn = gupnp_device_info_get_udn (GUPNP_DEVICE_INFO (source)); |
| 417 |
|
| 418 |
if (!gtk_tree_model_get_iter_first (model, &root_iter)) |
| 419 |
return; |
| 420 |
|
| 421 |
if (find_device (model, udn, &root_iter, &device_iter)) { |
| 422 |
g_object_set_data (source, "icon-download-cancellable", NULL); |
| 423 |
|
| 424 |
gtk_tree_store_set (GTK_TREE_STORE (model), |
| 425 |
&device_iter, |
| 426 |
0, icon, |
| 427 |
-1); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
static void |
| 432 |
append_action_arguments (GList *arguments, |
| 433 |
GtkTreeStore *store, |
| 434 |
GtkTreeIter *action_iter) |
| 435 |
{ |
| 436 |
const GList *iter; |
| 437 |
|
| 438 |
for (iter = arguments; iter; iter = iter->next) { |
| 439 |
GUPnPServiceActionArgInfo *info; |
| 440 |
IconID icon_id; |
| 441 |
|
| 442 |
info = iter->data; |
| 443 |
|
| 444 |
if (info->direction == GUPNP_SERVICE_ACTION_ARG_DIRECTION_IN) { |
| 445 |
icon_id = ICON_ACTION_ARG_IN; |
| 446 |
} else { |
| 447 |
icon_id = ICON_ACTION_ARG_OUT; |
| 448 |
} |
| 449 |
|
| 450 |
gtk_tree_store_insert_with_values |
| 451 |
(store, |
| 452 |
NULL, action_iter, -1, |
| 453 |
0, get_icon_by_id (icon_id), |
| 454 |
1, info->name, |
| 455 |
4, info, |
| 456 |
5, icon_id, |
| 457 |
-1); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
static void |
| 462 |
append_actions (GUPnPServiceProxy *proxy, |
| 463 |
GUPnPServiceIntrospection *introspection, |
| 464 |
const GList *actions, |
| 465 |
GtkTreeStore *store, |
| 466 |
GtkTreeIter *service_iter) |
| 467 |
{ |
| 468 |
const GList *iter; |
| 469 |
|
| 470 |
for (iter = actions; iter; iter = iter->next) { |
| 471 |
GUPnPServiceActionInfo *info; |
| 472 |
GtkTreeIter action_iter; |
| 473 |
|
| 474 |
info = iter->data; |
| 475 |
|
| 476 |
gtk_tree_store_insert_with_values |
| 477 |
(store, &action_iter, |
| 478 |
service_iter, -1, |
| 479 |
0, get_icon_by_id (ICON_ACTION), |
| 480 |
1, info->name, |
| 481 |
2, proxy, |
| 482 |
3, introspection, |
| 483 |
4, info, |
| 484 |
5, ICON_ACTION, |
| 485 |
-1); |
| 486 |
append_action_arguments (info->arguments, |
| 487 |
store, |
| 488 |
&action_iter); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
static void |
| 493 |
append_state_variables (GUPnPServiceProxy *proxy, |
| 494 |
GUPnPServiceIntrospection *introspection, |
| 495 |
const GList *variables, |
| 496 |
GtkTreeStore *store, |
| 497 |
GtkTreeIter *service_iter) |
| 498 |
{ |
| 499 |
const GList *iter; |
| 500 |
GtkTreeIter variables_iter; |
| 501 |
|
| 502 |
gtk_tree_store_insert_with_values (store, |
| 503 |
&variables_iter, |
| 504 |
service_iter, -1, |
| 505 |
0, get_icon_by_id (ICON_VARIABLES), |
| 506 |
1, _("State variables"), |
| 507 |
5, ICON_VARIABLES, |
| 508 |
-1); |
| 509 |
|
| 510 |
for (iter = variables; iter; iter = iter->next) { |
| 511 |
GUPnPServiceStateVariableInfo *info; |
| 512 |
|
| 513 |
info = iter->data; |
| 514 |
|
| 515 |
gtk_tree_store_insert_with_values |
| 516 |
(store, |
| 517 |
NULL, &variables_iter, -1, |
| 518 |
0, get_icon_by_id (ICON_VARIABLE), |
| 519 |
1, info->name, |
| 520 |
2, proxy, |
| 521 |
3, introspection, |
| 522 |
4, info, |
| 523 |
5, ICON_VARIABLE, |
| 524 |
-1); |
| 525 |
|
| 526 |
/* Set-up event notitications for variable */ |
| 527 |
gupnp_service_proxy_add_notify (proxy, |
| 528 |
info->name, |
| 529 |
info->type, |
| 530 |
on_state_variable_changed, |
| 531 |
NULL); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
static void |
| 536 |
append_introspection (GUPnPServiceProxy *proxy, |
| 537 |
GUPnPServiceIntrospection *introspection, |
| 538 |
GtkTreeStore *store, |
| 539 |
GtkTreeIter *service_iter) |
| 540 |
{ |
| 541 |
const GList *list; |
| 542 |
|
| 543 |
if (introspection == NULL) { |
| 544 |
gtk_tree_store_insert_with_values (store, |
| 545 |
NULL, service_iter, -1, |
| 546 |
0, get_icon_by_id (ICON_MISSING), |
| 547 |
1, _("Information not available"), |
| 548 |
5, ICON_MISSING, |
| 549 |
-1); |
| 550 |
|
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
list = gupnp_service_introspection_list_state_variables ( |
| 555 |
introspection); |
| 556 |
if (list) |
| 557 |
append_state_variables (proxy, |
| 558 |
introspection, |
| 559 |
list, |
| 560 |
store, |
| 561 |
service_iter); |
| 562 |
|
| 563 |
list = gupnp_service_introspection_list_actions (introspection); |
| 564 |
if (list) |
| 565 |
append_actions (proxy, |
| 566 |
introspection, |
| 567 |
list, |
| 568 |
store, |
| 569 |
service_iter); |
| 570 |
} |
| 571 |
|
| 572 |
static void |
| 573 |
got_introspection (GObject *source, GAsyncResult *res, gpointer user_data) |
| 574 |
{ |
| 575 |
GtkTreeModel *model; |
| 576 |
GtkTreeIter *service_iter; |
| 577 |
g_autoptr (GError) error = NULL; |
| 578 |
|
| 579 |
GUPnPServiceIntrospection *introspection = |
| 580 |
gupnp_service_info_introspect_finish ( |
| 581 |
GUPNP_SERVICE_INFO (source), |
| 582 |
res, |
| 583 |
&error); |
| 584 |
|
| 585 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { |
| 586 |
return; |
| 587 |
} |
| 588 |
|
| 589 |
if (error != NULL) { |
| 590 |
g_warning ("Failed to get introspection for device: %s", |
| 591 |
error->message); |
| 592 |
|
| 593 |
return; |
| 594 |
} |
| 595 |
|
| 596 |
service_iter = (GtkTreeIter *) user_data; |
| 597 |
|
| 598 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 599 |
g_assert (model != NULL); |
| 600 |
|
| 601 |
if (!gtk_tree_store_iter_is_valid (GTK_TREE_STORE (model), |
| 602 |
service_iter)) { |
| 603 |
goto out; |
| 604 |
} |
| 605 |
|
| 606 |
append_introspection (GUPNP_SERVICE_PROXY (source), |
| 607 |
introspection, |
| 608 |
GTK_TREE_STORE (model), |
| 609 |
service_iter); |
| 610 |
|
| 611 |
if (introspection != NULL) { |
| 612 |
g_object_unref (introspection); |
| 613 |
|
| 614 |
/* Services are subscribed to by default */ |
| 615 |
gupnp_service_proxy_set_subscribed ( |
| 616 |
GUPNP_SERVICE_PROXY (source), |
| 617 |
TRUE); |
| 618 |
} |
| 619 |
out: |
| 620 |
g_slice_free (GtkTreeIter, service_iter); |
| 621 |
} |
| 622 |
|
| 623 |
static void |
| 624 |
append_service_tree (GUPnPServiceInfo *info, |
| 625 |
GtkTreeStore *store, |
| 626 |
GtkTreeIter *device_iter) |
| 627 |
{ |
| 628 |
char *id; |
| 629 |
|
| 630 |
id = gupnp_service_info_get_id (info); |
| 631 |
if (id) { |
| 632 |
GtkTreeIter *service_iter; |
| 633 |
|
| 634 |
service_iter = g_slice_new (GtkTreeIter); |
| 635 |
gtk_tree_store_insert_with_values |
| 636 |
(store, |
| 637 |
service_iter, |
| 638 |
device_iter, -1, |
| 639 |
0, get_icon_by_id (ICON_SERVICE), |
| 640 |
1, id, |
| 641 |
2, info, |
| 642 |
5, ICON_SERVICE, |
| 643 |
-1); |
| 644 |
|
| 645 |
gupnp_service_info_introspect_async (info, |
| 646 |
NULL, |
| 647 |
got_introspection, |
| 648 |
service_iter); |
| 649 |
|
| 650 |
g_free (id); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
static void |
| 655 |
append_device_tree (GUPnPDeviceInfo *info, |
| 656 |
GtkTreeModel *model, |
| 657 |
GtkTreeIter *parent_iter) |
| 658 |
{ |
| 659 |
char *friendly_name; |
| 660 |
|
| 661 |
friendly_name = gupnp_device_info_get_friendly_name (info); |
| 662 |
if (friendly_name) { |
| 663 |
GtkTreeIter device_iter; |
| 664 |
GList *child; |
| 665 |
GCancellable *cancellable = g_cancellable_new (); |
| 666 |
|
| 667 |
gtk_tree_store_insert_with_values |
| 668 |
(GTK_TREE_STORE (model), |
| 669 |
&device_iter, parent_iter, -1, |
| 670 |
0, get_icon_by_id (ICON_DEVICE), |
| 671 |
1, friendly_name, |
| 672 |
2, info, |
| 673 |
5, ICON_DEVICE, |
| 674 |
-1); |
| 675 |
g_free (friendly_name); |
| 676 |
|
| 677 |
g_object_set_data_full (G_OBJECT (info), |
| 678 |
"icon-download-cancellable", |
| 679 |
cancellable, |
| 680 |
g_object_unref); |
| 681 |
update_icon_async (info, |
| 682 |
cancellable, |
| 683 |
on_device_icon_available, |
| 684 |
treeview); |
| 685 |
|
| 686 |
/* Append the embedded devices */ |
| 687 |
child = gupnp_device_info_list_devices (info); |
| 688 |
while (child) { |
| 689 |
append_device_tree (GUPNP_DEVICE_INFO (child->data), |
| 690 |
model, |
| 691 |
&device_iter); |
| 692 |
g_object_unref (child->data); |
| 693 |
child = g_list_delete_link (child, child); |
| 694 |
} |
| 695 |
|
| 696 |
/* Append the services */ |
| 697 |
child = gupnp_device_info_list_services (info); |
| 698 |
while (child) { |
| 699 |
append_service_tree (GUPNP_SERVICE_INFO (child->data), |
| 700 |
GTK_TREE_STORE (model), |
| 701 |
&device_iter); |
| 702 |
g_object_unref (child->data); |
| 703 |
child = g_list_delete_link (child, child); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
void |
| 709 |
append_device (GUPnPDeviceInfo *info) |
| 710 |
{ |
| 711 |
GtkTreeModel *model; |
| 712 |
GtkTreeIter root_iter; |
| 713 |
GtkTreeIter iter; |
| 714 |
const char *udn; |
| 715 |
|
| 716 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 717 |
g_assert (model != NULL); |
| 718 |
|
| 719 |
udn = gupnp_device_info_get_udn (info); |
| 720 |
|
| 721 |
if (!gtk_tree_model_get_iter_first (model, &root_iter)) |
| 722 |
return; |
| 723 |
|
| 724 |
if (!find_device (model, udn, &root_iter, &iter)) { |
| 725 |
|
| 726 |
append_device_tree (info, model, &root_iter); |
| 727 |
|
| 728 |
if (!expanded) { |
| 729 |
GtkTreePath *first_row; |
| 730 |
|
| 731 |
first_row = gtk_tree_model_get_path ( |
| 732 |
model, |
| 733 |
&root_iter); |
| 734 |
expanded = gtk_tree_view_expand_row ( |
| 735 |
GTK_TREE_VIEW (treeview), |
| 736 |
first_row, |
| 737 |
FALSE); |
| 738 |
gtk_tree_path_free (first_row); |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
static GtkTreeModel * |
| 744 |
create_device_treemodel (void) |
| 745 |
{ |
| 746 |
GtkTreeStore *store; |
| 747 |
|
| 748 |
store = gtk_tree_store_new (6, |
| 749 |
GDK_TYPE_PIXBUF, /* Icon */ |
| 750 |
G_TYPE_STRING, /* Name */ |
| 751 |
G_TYPE_OBJECT, /* Device/Service Info */ |
| 752 |
G_TYPE_OBJECT, /* Introspection */ |
| 753 |
G_TYPE_POINTER, /* non-object */ |
| 754 |
G_TYPE_UINT); /* icon type */ |
| 755 |
|
| 756 |
gtk_tree_store_insert_with_values (store, NULL, NULL, 0, |
| 757 |
0, get_icon_by_id (ICON_NETWORK), |
| 758 |
1, _("UPnP Network"), |
| 759 |
5, ICON_NETWORK, |
| 760 |
-1); |
| 761 |
return GTK_TREE_MODEL (store); |
| 762 |
} |
| 763 |
|
| 764 |
void |
| 765 |
setup_device_treeview (GtkBuilder *builder) |
| 766 |
{ |
| 767 |
GtkTreeModel *model; |
| 768 |
GtkTreeSelection *selection; |
| 769 |
GtkCellRenderer *renderer; |
| 770 |
GtkTreeViewColumn *column; |
| 771 |
char *headers[] = { _("Device"), NULL}; |
| 772 |
|
| 773 |
treeview = GTK_WIDGET (gtk_builder_get_object (builder, |
| 774 |
"device-treeview")); |
| 775 |
g_assert (treeview != NULL); |
| 776 |
popup = GTK_WIDGET (gtk_builder_get_object (builder, "device-popup")); |
| 777 |
g_assert (popup != NULL); |
| 778 |
|
| 779 |
subscribe_menuitem = GTK_WIDGET (gtk_builder_get_object ( |
| 780 |
builder, |
| 781 |
"subscribe-to-events")); |
| 782 |
g_assert (subscribe_menuitem != NULL); |
| 783 |
action_menuitem = GTK_WIDGET (gtk_builder_get_object (builder, |
| 784 |
"invoke-action")); |
| 785 |
g_assert (action_menuitem != NULL); |
| 786 |
separator = GTK_WIDGET (gtk_builder_get_object ( |
| 787 |
builder, |
| 788 |
"device-popup-separator")); |
| 789 |
g_assert (separator != NULL); |
| 790 |
|
| 791 |
model = create_device_treemodel (); |
| 792 |
g_assert (model != NULL); |
| 793 |
|
| 794 |
setup_treeview (treeview, model, headers, 1); |
| 795 |
g_object_unref (model); |
| 796 |
|
| 797 |
column = gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 0); |
| 798 |
renderer = gtk_cell_renderer_pixbuf_new (); |
| 799 |
gtk_tree_view_column_pack_start (column, renderer, FALSE); |
| 800 |
gtk_tree_view_column_add_attribute (column, |
| 801 |
renderer, |
| 802 |
"pixbuf", 0); |
| 803 |
|
| 804 |
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); |
| 805 |
g_assert (selection != NULL); |
| 806 |
g_signal_connect (selection, |
| 807 |
"changed", |
| 808 |
G_CALLBACK (on_something_selected), |
| 809 |
NULL); |
| 810 |
|
| 811 |
expanded = FALSE; |
| 812 |
} |
| 813 |
|
| 814 |
GUPnPServiceProxy * |
| 815 |
get_selected_service (void) |
| 816 |
{ |
| 817 |
GUPnPServiceProxy *proxy = NULL; |
| 818 |
GtkTreeModel *model; |
| 819 |
GtkTreeSelection *selection; |
| 820 |
GtkTreeIter iter; |
| 821 |
|
| 822 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 823 |
g_assert (model != NULL); |
| 824 |
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); |
| 825 |
g_assert (selection != NULL); |
| 826 |
|
| 827 |
if (gtk_tree_selection_get_selected (selection, &model, &iter)) { |
| 828 |
guint icon_type; |
| 829 |
|
| 830 |
gtk_tree_model_get (model, &iter, |
| 831 |
2, &proxy, |
| 832 |
5, &icon_type, -1); |
| 833 |
|
| 834 |
if (icon_type != ICON_SERVICE) |
| 835 |
proxy = NULL; |
| 836 |
} |
| 837 |
|
| 838 |
return proxy; |
| 839 |
} |
| 840 |
|
| 841 |
GUPnPServiceActionInfo * |
| 842 |
get_selected_action (GUPnPServiceProxy **ret_proxy, |
| 843 |
GUPnPServiceIntrospection **ret_introspection) |
| 844 |
{ |
| 845 |
GUPnPServiceProxy *proxy; |
| 846 |
GUPnPServiceIntrospection *introspection; |
| 847 |
GUPnPServiceActionInfo *action_info = NULL; |
| 848 |
GtkTreeModel *model; |
| 849 |
GtkTreeSelection *selection; |
| 850 |
GtkTreeIter iter; |
| 851 |
|
| 852 |
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); |
| 853 |
g_assert (model != NULL); |
| 854 |
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); |
| 855 |
g_assert (selection != NULL); |
| 856 |
|
| 857 |
if (gtk_tree_selection_get_selected (selection, &model, &iter)) { |
| 858 |
guint icon_type; |
| 859 |
|
| 860 |
gtk_tree_model_get (model, &iter, |
| 861 |
2, &proxy, |
| 862 |
3, &introspection, |
| 863 |
4, &action_info, |
| 864 |
5, &icon_type, |
| 865 |
-1); |
| 866 |
|
| 867 |
if (icon_type != ICON_ACTION) { |
| 868 |
if (proxy != NULL) { |
| 869 |
g_object_unref (proxy); |
| 870 |
proxy = NULL; |
| 871 |
} |
| 872 |
|
| 873 |
if (introspection != NULL) { |
| 874 |
g_object_unref (introspection); |
| 875 |
introspection = NULL; |
| 876 |
} |
| 877 |
|
| 878 |
action_info = NULL; |
| 879 |
} |
| 880 |
|
| 881 |
if (ret_proxy) |
| 882 |
*ret_proxy = proxy; |
| 883 |
else if (proxy != NULL) |
| 884 |
g_object_unref (proxy); |
| 885 |
|
| 886 |
if (ret_introspection) |
| 887 |
*ret_introspection = introspection; |
| 888 |
else if (introspection != NULL) |
| 889 |
g_object_unref (introspection); |
| 890 |
} |
| 891 |
|
| 892 |
return action_info; |
| 893 |
} |
| 894 |
|
| 895 |
G_MODULE_EXPORT |
| 896 |
void |
| 897 |
on_expand_devices_activate (GtkMenuItem *menuitem, |
| 898 |
gpointer user_data) |
| 899 |
{ |
| 900 |
gtk_tree_view_expand_all (GTK_TREE_VIEW (treeview)); |
| 901 |
} |
| 902 |
|
| 903 |
G_MODULE_EXPORT |
| 904 |
void |
| 905 |
on_collapse_devices_activate (GtkMenuItem *menuitem, |
| 906 |
gpointer user_data) |
| 907 |
{ |
| 908 |
gtk_tree_view_collapse_all (GTK_TREE_VIEW (treeview)); |
| 909 |
} |
| 910 |
|