Debian Codegraph @

src/av-cp/search-dialog.c

raw
1 /*
2 * Copyright (C) 2016 Jens Georg <[email protected]>
3 *
4 * Authors: Jens Georg <[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 <glib/gi18n.h>
24 #include <libgupnp-av/gupnp-av.h>
25
26 #include <string.h>
27
28 #include "entry-completion.h"
29 #include "search-dialog.h"
30 #include "server-device.h"
31 #include "didl-dialog.h"
32 #include "icons.h"
33
34 /* DLNA recommends something between 10 and 30, let's just use 30
35 * cf. 7.4.1.4.10.9 in the 2014 version of the architecture and protocols
36 * guideline
37 * dlna-guidelines-march-2014---part-1-1-architectures-and-protocols.pdf
38 */
39 #define SEARCH_DIALOG_DEFAULT_SLICE 30
40
41 #define DEFAULT_SEARCH_FILTER "upnp:class,dc:title"
42
43 #define DIALOG_RESOURCE_PATH "/org/gupnp/Tools/AV-CP/search-dialog.ui"
44
45 typedef struct _SearchTask SearchTask;
46
47 struct _SearchDialog {
48 GtkDialog parent;
49 };
50
51 struct _SearchDialogClass {
52 GtkDialogClass parent_class;
53 };
54
55 struct _SearchDialogPrivate {
56 GtkListStore *search_dialog_liststore;
57 GtkEntry *search_dialog_entry;
58 GtkTreeView *search_dialog_treeview;
59 char *id;
60 char *title;
61 AVCPMediaServer *server;
62 guint pulse_timer;
63 SearchTask *task;
64 GUPnPSearchCriteriaParser *parser;
65 GRegex *position_re;
66 GtkWidget *popup_menu;
67 };
68
69 typedef struct _SearchDialogPrivate SearchDialogPrivate;
70 G_DEFINE_TYPE_WITH_PRIVATE (SearchDialog, search_dialog, GTK_TYPE_DIALOG)
71
72 static void
73 search_dialog_on_search_activate (SearchDialog *self, GtkEntry *entry);
74
75 static gboolean
76 search_dialog_on_listview_button_release (GtkWidget *widget,
77 GdkEventButton *event,
78 gpointer user_data);
79
80 static void
81 search_dialog_on_didl_popup_activate (SearchDialog *self, GVariant *parameter, gpointer user_data);
82
83 static void
84 search_dialog_on_icon_release (SearchDialog *self, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer user_data);
85
86 static void
87 search_dialog_finalize (GObject *object);
88
89 static void
90 search_dialog_dispose (GObject *object);
91
92 struct _SearchTask {
93 AVCPMediaServer *server;
94 char *search_expression;
95 char *container_id;
96 GtkListStore *target;
97 int start;
98 int count;
99 int total;
100 gboolean running;
101 GUPnPDIDLLiteParser *parser;
102 GError *error;
103 GSourceFunc callback;
104 gpointer user_data;
105 GCancellable *cancellable;
106 };
107
108 static void
109 search_task_on_didl_object_available (GUPnPDIDLLiteParser *parser,
110 GUPnPDIDLLiteObject *object,
111 gpointer user_data);
112
113 static SearchTask *
114 search_task_new (AVCPMediaServer *server,
115 GtkListStore *target,
116 const char *container_id,
117 const char *search_expression,
118 int count,
119 GSourceFunc callback,
120 gpointer user_data)
121 {
122 SearchTask *task = g_new0 (SearchTask, 1);
123
124 task->search_expression = g_strdup (search_expression);
125 task->target = g_object_ref (target);
126 task->server = g_object_ref (server);
127 task->container_id = g_strdup (container_id);
128 task->start = 0;
129 task->count = count;
130 task->total = -1;
131 task->parser = gupnp_didl_lite_parser_new ();
132 task->error = NULL;
133 task->callback = callback;
134 task->user_data = user_data;
135 task->cancellable = g_cancellable_new ();
136
137 g_signal_connect (G_OBJECT (task->parser),
138 "object-available",
139 G_CALLBACK (search_task_on_didl_object_available),
140 task);
141
142 return task;
143 }
144
145 static void
146 search_task_free (SearchTask *task) {
147 g_clear_object (&task->target);
148 g_clear_object (&task->server);
149 g_clear_object (&task->parser);
150 g_free (task->search_expression);
151 g_free (task->container_id);
152 if (task->error != NULL) {
153 g_error_free (task->error);
154 }
155 g_free (task);
156 }
157
158 static void
159 search_task_cancel (SearchTask *task) {
160 g_cancellable_cancel (task->cancellable);
161 }
162
163 static gboolean
164 search_task_idle_callback (gpointer user_data)
165 {
166 SearchTask *task = (SearchTask *)user_data;
167 if (task->callback != NULL) {
168 task->callback (task->user_data);
169 }
170
171 return FALSE;
172 }
173
174 static void
175 search_task_set_finished (SearchTask *task, GError *error)
176 {
177 task->running = FALSE;
178 task->error = error;
179
180 g_idle_add (search_task_idle_callback, task);
181 }
182
183 static void
184 search_task_on_search_ready (GObject *source, GAsyncResult *res, gpointer user_data)
185 {
186 SearchTask *task = (SearchTask *)user_data;
187 GError *error = NULL;
188 char *didl_xml = NULL;
189 guint32 total = 0;
190 guint32 returned = 0;
191 gboolean result;
192 gboolean finished = FALSE;
193
194 result = av_cp_media_server_search_finish (AV_CP_MEDIA_SERVER (source),
195 res,
196 &didl_xml,
197 &total,
198 &returned,
199 &error);
200
201 g_debug ("Received search slice result for %s with expression %s, result is %s",
202 task->container_id,
203 task->search_expression,
204 result ? "TRUE" : "FALSE");
205
206
207 if (!result) {
208 finished = TRUE;
209
210 goto out;
211 }
212
213 if (g_cancellable_is_cancelled (task->cancellable)) {
214 finished = TRUE;
215
216 goto out;
217 }
218
219 /* Nothing returned by the server */
220 if (returned == 0) {
221 finished = TRUE;
222
223 goto out;
224 }
225
226 if (didl_xml == NULL) {
227 finished = TRUE;
228 goto out;
229 }
230
231 gupnp_didl_lite_parser_parse_didl (task->parser, didl_xml, &error);
232 if (error != NULL) {
233 finished = TRUE;
234
235 goto out;
236 }
237
238 if (total != 0) {
239 task->total = total;
240 }
241
242 task->start += returned;
243
244 out:
245 g_clear_pointer (&didl_xml, g_free);
246 if (finished) {
247 g_debug ("Finished search, error: %s",
248 error ? error->message : "none");
249 search_task_set_finished (task, error);
250 } else {
251 g_debug ("Starting new slice %u/%u (total %u)",
252 task->start,
253 task->count,
254 task->total == -1 ? 0 : task->total);
255
256 av_cp_media_server_search_async (task->server,
257 task->cancellable,
258 search_task_on_search_ready,
259 task->container_id,
260 task->search_expression,
261 DEFAULT_SEARCH_FILTER,
262 task->start,
263 task->count,
264 task);
265 }
266 }
267
268 static void
269 search_task_run (SearchTask *task) {
270 if (task->running) {
271 g_debug ("Search task is already running, not doing anything.");
272
273 return;
274 }
275
276 g_debug ("Starting search task for %s with expression %s",
277 task->container_id,
278 task->search_expression);
279
280 task->running = TRUE;
281
282 av_cp_media_server_search_async (task->server,
283 NULL,
284 search_task_on_search_ready,
285 task->container_id,
286 task->search_expression,
287 DEFAULT_SEARCH_FILTER,
288 task->start,
289 task->count,
290 task);
291 }
292
293 #define ITEM_CLASS_IMAGE "object.item.imageItem"
294 #define ITEM_CLASS_AUDIO "object.item.audioItem"
295 #define ITEM_CLASS_VIDEO "object.item.videoItem"
296 #define ITEM_CLASS_TEXT "object.item.textItem"
297 #define CONTAINER_CLASS "object.container"
298
299 static GdkPixbuf *
300 get_item_icon (GUPnPDIDLLiteObject *object)
301 {
302 GdkPixbuf *icon;
303 const char *class_name;
304
305 class_name = gupnp_didl_lite_object_get_upnp_class (object);
306 if (G_UNLIKELY (class_name == NULL)) {
307 return get_icon_by_id (ICON_FILE);
308 }
309
310 if (g_str_has_prefix (class_name, ITEM_CLASS_IMAGE)) {
311 icon = get_icon_by_id (ICON_IMAGE_ITEM);
312 } else if (g_str_has_prefix (class_name, ITEM_CLASS_AUDIO)) {
313 icon = get_icon_by_id (ICON_AUDIO_ITEM);
314 } else if (g_str_has_prefix (class_name, ITEM_CLASS_VIDEO)) {
315 icon = get_icon_by_id (ICON_VIDEO_ITEM);
316 } else if (g_str_has_prefix (class_name, ITEM_CLASS_TEXT)) {
317 icon = get_icon_by_id (ICON_TEXT_ITEM);
318 } else if (g_str_has_prefix (class_name, CONTAINER_CLASS) ){
319 icon = get_icon_by_id (ICON_CONTAINER);
320 } else {
321 icon = get_icon_by_id (ICON_FILE);
322 }
323
324 return icon;
325 }
326
327 static void
328 search_task_on_didl_object_available (GUPnPDIDLLiteParser *parser,
329 GUPnPDIDLLiteObject *object,
330 gpointer user_data)
331 {
332 SearchTask *task = (SearchTask *)user_data;
333 GtkTreeIter iter;
334
335 gtk_list_store_insert_with_values (task->target,
336 &iter,
337 -1,
338 0, get_item_icon (object),
339 1, gupnp_didl_lite_object_get_title (object),
340 3, gupnp_didl_lite_object_get_id (object),
341 -1);
342 }
343
344 static void
345 search_dialog_class_init (SearchDialogClass *klass)
346 {
347 GObjectClass *object_class = G_OBJECT_CLASS (klass);
348 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
349
350 gtk_widget_class_set_template_from_resource (widget_class,
351 DIALOG_RESOURCE_PATH);
352 gtk_widget_class_bind_template_child_private (widget_class,
353 SearchDialog,
354 search_dialog_liststore);
355 gtk_widget_class_bind_template_child_private (widget_class,
356 SearchDialog,
357 search_dialog_entry);
358 gtk_widget_class_bind_template_child_private (widget_class,
359 SearchDialog,
360 search_dialog_treeview);
361 gtk_widget_class_bind_template_callback (widget_class,
362 search_dialog_on_search_activate);
363 gtk_widget_class_bind_template_callback (widget_class,
364 search_dialog_on_listview_button_release);
365 gtk_widget_class_bind_template_callback (widget_class, gtk_widget_hide);
366 gtk_widget_class_bind_template_callback (widget_class, search_dialog_on_icon_release);
367
368
369 object_class->finalize = search_dialog_finalize;
370 object_class->dispose = search_dialog_dispose;
371 }
372
373 static void
374 search_dialog_init (SearchDialog *self)
375 {
376 SearchDialogPrivate *priv = NULL;
377
378 gtk_widget_init_template (GTK_WIDGET (self));
379 priv = search_dialog_get_instance_private (self);
380
381 priv->parser = gupnp_search_criteria_parser_new ();
382 gtk_entry_set_completion (priv->search_dialog_entry,
383 entry_completion_new ());
384
385 GMenu *menu = g_menu_new ();
386 g_menu_insert (menu, 0, _("Show _DIDL…"), "search.show-didl");
387 priv->popup_menu = gtk_menu_new_from_model (G_MENU_MODEL (menu));
388 g_object_unref (menu);
389
390 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
391 GTK_WIDGET (self),
392 NULL);
393 GSimpleActionGroup *group = g_simple_action_group_new ();
394 GSimpleAction *action = g_simple_action_new ("show-didl", NULL);
395 g_signal_connect_swapped (G_OBJECT (action),
396 "activate",
397 G_CALLBACK (search_dialog_on_didl_popup_activate),
398 self);
399 g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action));
400
401 gtk_widget_insert_action_group (GTK_WIDGET (self),
402 "search",
403 G_ACTION_GROUP (group));
404 g_object_unref (group);
405 g_object_unref (action);
406 }
407
408 static void
409 search_dialog_dispose (GObject *object)
410 {
411 SearchDialog *self = SEARCH_DIALOG (object);
412 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
413 GObjectClass *parent_class =
414 G_OBJECT_CLASS (search_dialog_parent_class);
415
416 if (priv->task != NULL) {
417 search_task_cancel (priv->task);
418 }
419
420 if (priv->pulse_timer != 0) {
421 g_source_remove (priv->pulse_timer);
422 priv->pulse_timer = 0;
423 }
424
425 g_clear_object (&priv->parser);
426 g_clear_object (&priv->popup_menu);
427
428 if (parent_class->dispose != NULL) {
429 parent_class->dispose (object);
430 }
431 }
432
433 static void
434 search_dialog_finalize (GObject *object)
435 {
436 SearchDialog *self = SEARCH_DIALOG (object);
437 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
438 GObjectClass *parent_class =
439 G_OBJECT_CLASS (search_dialog_parent_class);
440
441 g_clear_pointer (&priv->id, g_free);
442 g_clear_pointer (&priv->title, g_free);
443 g_clear_pointer (&priv->task, search_task_free);
444 g_clear_pointer (&priv->position_re, g_regex_unref);
445
446 if (parent_class->finalize != NULL) {
447 parent_class->finalize (object);
448 }
449 }
450
451 static gboolean
452 search_dialog_on_search_task_done (gpointer user_data)
453 {
454 SearchDialog *self = SEARCH_DIALOG (user_data);
455 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
456
457 g_source_remove (priv->pulse_timer);
458 gtk_entry_set_progress_fraction (priv->search_dialog_entry, 0);
459 gtk_editable_set_editable (GTK_EDITABLE (priv->search_dialog_entry), TRUE);
460 g_object_set (G_OBJECT (priv->search_dialog_entry),
461 "primary-icon-name",
462 "edit-find-symbolic",
463 "secondary-icon-activatable",
464 TRUE,
465 NULL);
466
467 /* Only show visible error if dialog is visible. */
468 if (priv->task->error != NULL &&
469 !g_error_matches (priv->task->error,
470 G_IO_ERROR,
471 G_IO_ERROR_CANCELLED) &&
472 gtk_widget_is_visible (GTK_WIDGET (self))) {
473 GtkWidget *dialog = NULL;
474
475 dialog = gtk_message_dialog_new (GTK_WINDOW (self),
476 GTK_DIALOG_DESTROY_WITH_PARENT,
477 GTK_MESSAGE_WARNING,
478 GTK_BUTTONS_CLOSE,
479 "%s",
480 _("Search failed"));
481
482 gtk_message_dialog_format_secondary_text
483 (GTK_MESSAGE_DIALOG (dialog),
484 _("Error message was: %s"),
485 priv->task->error->message);
486 gtk_dialog_run (GTK_DIALOG (dialog));
487 gtk_widget_destroy (dialog);
488
489 g_critical ("Failed to search: %s", priv->task->error->message);
490 }
491
492 g_clear_pointer (&priv->task, search_task_free);
493
494 return FALSE;
495 }
496
497 void
498 search_dialog_set_server (SearchDialog *self, AVCPMediaServer *server)
499 {
500 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
501 GtkEntryCompletion *completion = gtk_entry_get_completion (priv->search_dialog_entry);
502
503 priv->server = server;
504
505 entry_completion_set_search_criteria (ENTRY_COMPLETION (completion),
506 av_cp_media_server_get_search_caps (server));
507 }
508
509 void
510 search_dialog_set_container_id (SearchDialog *self, char *id)
511 {
512 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
513 g_free (priv->id);
514
515 priv->id = id;
516 }
517
518 void
519 search_dialog_set_container_title (SearchDialog *self, char *title)
520 {
521 char *name = NULL;
522 char *window_title = NULL;
523
524 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
525 g_free (priv->title);
526
527 priv->title = title;
528 name = gupnp_device_info_get_friendly_name
529 (GUPNP_DEVICE_INFO (priv->server));
530
531 if (g_str_equal (priv->id, "0")) {
532 window_title = g_strdup_printf (_("Searching on %s"),
533 name);
534 } else {
535 window_title = g_strdup_printf (_("Searching in %s on %s"),
536 title,
537 name);
538 }
539
540 gtk_window_set_title (GTK_WINDOW (self), window_title);
541
542 g_free (name);
543 g_free (window_title);
544 }
545
546 void
547 search_dialog_run (SearchDialog *self)
548 {
549 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
550 gtk_dialog_run (GTK_DIALOG (self));
551 gtk_widget_hide (GTK_WIDGET (self));
552
553 if (priv->task != NULL &&
554 priv->task->running) {
555 search_task_cancel (priv->task);
556 }
557 }
558
559 static gboolean
560 pulse_timer (gpointer user_data)
561 {
562 SearchDialog *self = SEARCH_DIALOG (user_data);
563 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
564 if (priv->task->total == -1) {
565 gtk_entry_progress_pulse (GTK_ENTRY (priv->search_dialog_entry));
566 } else {
567 gdouble progress = (gdouble) priv->task->start /
568 (gdouble) priv->task->total;
569 gtk_entry_set_progress_fraction
570 (GTK_ENTRY (priv->search_dialog_entry),
571 progress);
572 }
573
574 return TRUE;
575 }
576
577 static void
578 search_dialog_on_search_activate (SearchDialog *self, GtkEntry *entry)
579 {
580 GError *error = NULL;
581 const char *text = gtk_entry_get_text (entry);
582 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
583
584 gupnp_search_criteria_parser_parse_text (priv->parser, text, &error);
585 if (error == NULL) {
586
587 g_object_set (G_OBJECT (entry),
588 "primary-icon-name",
589 "media-playback-stop-symbolic",
590 "secondary-icon-activatable",
591 FALSE,
592 NULL);
593 gtk_list_store_clear (priv->search_dialog_liststore);
594 gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
595 priv->pulse_timer = g_timeout_add_seconds (1, pulse_timer, self);
596
597 g_clear_pointer (&priv->task, search_task_free);
598
599 priv->task = search_task_new (priv->server,
600 priv->search_dialog_liststore,
601 priv->id,
602 gtk_entry_get_text (entry),
603 SEARCH_DIALOG_DEFAULT_SLICE,
604 search_dialog_on_search_task_done,
605 self);
606 search_task_run (priv->task);
607 } else {
608 GtkWidget *dialog = NULL;
609 GMatchInfo *info = NULL;
610 char *position = NULL;
611
612 if (priv->position_re == NULL) {
613 priv->position_re = g_regex_new ("([0-9]+)$", 0, 0, NULL);
614 }
615
616 if (!g_regex_match (priv->position_re, error->message, 0, &info)) {
617 position = g_strdup ("-1");
618 } else {
619 position = g_match_info_fetch (info, 0);
620 }
621
622 g_match_info_free (info);
623
624 dialog = gtk_message_dialog_new (GTK_WINDOW (self),
625 GTK_DIALOG_DESTROY_WITH_PARENT,
626 GTK_MESSAGE_WARNING,
627 GTK_BUTTONS_CLOSE,
628 "%s",
629 _("Search failed"));
630
631 gtk_message_dialog_format_secondary_text
632 (GTK_MESSAGE_DIALOG (dialog),
633 _("Search criteria invalid: %s"),
634 error->message);
635 gtk_dialog_run (GTK_DIALOG (dialog));
636 gtk_widget_destroy (dialog);
637
638 g_error_free (error);
639 error = NULL;
640
641 gtk_editable_set_position (GTK_EDITABLE (entry),
642 atoi (position));
643 g_free (position);
644
645 return;
646 }
647
648 }
649
650 static void
651 do_popup_menu (GtkMenu *menu, GtkWidget *widget, GdkEventButton *event)
652 {
653 #if GTK_CHECK_VERSION(3,22,0)
654 gtk_menu_popup_at_pointer (menu,
655 event != NULL ? (GdkEvent *) event
656 : gtk_get_current_event ());
657 #else
658 int button = 0;
659 int event_time;
660 if (event) {
661 button = event->button;
662 event_time = event->time;
663 } else {
664 event_time = gtk_get_current_event_time ();
665 }
666
667 gtk_menu_popup (menu, NULL, NULL, NULL, NULL, button, event_time);
668 #endif
669 }
670
671 static gboolean
672 search_dialog_on_listview_button_release (GtkWidget *widget,
673 GdkEventButton *event,
674 gpointer user_data)
675 {
676 SearchDialog *self = SEARCH_DIALOG (user_data);
677 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
678 GtkTreeSelection *selection = NULL;
679 GtkTreeModel *model = NULL;
680 GtkTreeIter iter;
681 GtkTreeView *treeview = priv->search_dialog_treeview;
682
683 if (event->type != GDK_BUTTON_RELEASE || event->button != 3) {
684 return FALSE;
685 }
686
687 selection = gtk_tree_view_get_selection (treeview);
688 g_assert (selection != NULL);
689
690 /* Only show the popup menu when a row is selected */
691 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
692 return FALSE;
693 }
694
695 do_popup_menu (GTK_MENU (priv->popup_menu),
696 GTK_WIDGET (treeview),
697 event);
698
699 return TRUE;
700 }
701
702 static void
703 on_object (GUPnPDIDLLiteParser *parser,
704 GUPnPDIDLLiteObject *object,
705 gpointer user_data)
706 {
707 GUPnPDIDLLiteObject **result = (GUPnPDIDLLiteObject **)user_data;
708 if (*result == NULL) {
709 *result = g_object_ref (object);
710 }
711 }
712
713 static void
714 search_dialog_on_metadata_ready (GObject *source,
715 GAsyncResult *res,
716 gpointer user_data)
717 {
718 SearchDialog *self = SEARCH_DIALOG (user_data);
719 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
720 AVCPMediaServer *server = AV_CP_MEDIA_SERVER (source);
721 GtkTreeView *treeview = priv->search_dialog_treeview;
722 GtkTreeModel *model = NULL;
723 GtkTreeSelection *selection = NULL;
724 char *xml;
725 GError *error = NULL;
726 GtkTreeIter iter;
727
728 if (!av_cp_media_server_browse_metadata_finish (server,
729 res,
730 &xml,
731 &error)) {
732 GtkWidget *message = NULL;
733
734 message = gtk_message_dialog_new (GTK_WINDOW (self),
735 GTK_DIALOG_MODAL,
736 GTK_MESSAGE_WARNING,
737 GTK_BUTTONS_CLOSE,
738 _("Error fetching detailed information: %s"),
739 error->message);
740 gtk_dialog_run (GTK_DIALOG (message));
741 gtk_widget_destroy (message);
742 } else {
743 selection = gtk_tree_view_get_selection (treeview);
744 if (gtk_tree_selection_get_selected (selection,
745 &model,
746 &iter)) {
747 GUPnPDIDLLiteParser *parser = NULL;
748 GUPnPDIDLLiteObject *didl_object = NULL;
749
750 parser = gupnp_didl_lite_parser_new ();
751 g_signal_connect (G_OBJECT (parser),
752 "object-available",
753 G_CALLBACK (on_object),
754 &didl_object);
755 gupnp_didl_lite_parser_parse_didl (parser, xml, NULL);
756 gtk_list_store_set (GTK_LIST_STORE (model),
757 &iter,
758 2, didl_object,
759 -1);
760 g_object_unref (parser);
761 g_object_unref (didl_object);
762 {
763 AVCPDidlDialog *dialog = av_cp_didl_dialog_new ();
764
765 av_cp_didl_dialog_set_xml (dialog, xml);
766 gtk_window_set_transient_for (GTK_WINDOW (dialog),
767 GTK_WINDOW (self));
768 gtk_dialog_run (GTK_DIALOG (dialog));
769 gtk_widget_destroy (GTK_WIDGET (dialog));
770 }
771 }
772 g_free (xml);
773 }
774 }
775
776 static void
777 search_dialog_on_didl_popup_activate (SearchDialog *self, GVariant *parameter, gpointer user_data)
778 {
779 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
780 GtkTreeView *treeview = priv->search_dialog_treeview;
781 GtkTreeModel *model = NULL;
782 GUPnPDIDLLiteObject *didl_object = NULL;
783 GtkTreeSelection *selection = NULL;
784 GtkTreeIter iter;
785 char *id = NULL;
786
787 selection = gtk_tree_view_get_selection (treeview);
788
789 /* Only show the popup menu when a row is selected */
790 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
791 return;
792 }
793
794 gtk_tree_model_get (model,
795 &iter,
796 2, &didl_object,
797 3, &id,
798 -1);
799
800 if (didl_object != NULL) {
801 AVCPDidlDialog *dialog = av_cp_didl_dialog_new ();
802 char *xml = NULL;
803
804 g_free (id);
805 xml = gupnp_didl_lite_object_get_xml_string (didl_object);
806 av_cp_didl_dialog_set_xml (dialog, xml);
807 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (self));
808 gtk_dialog_run (GTK_DIALOG (dialog));
809 gtk_widget_destroy (GTK_WIDGET (dialog));
810 g_free (xml);
811 g_object_unref (didl_object);
812 } else {
813 av_cp_media_server_browse_metadata_async (priv->server,
814 NULL,
815 search_dialog_on_metadata_ready,
816 id,
817 self);
818 g_free (id);
819 }
820 }
821
822 GtkDialog *
823 search_dialog_new (void)
824 {
825 GtkSettings *settings = gtk_settings_get_default ();
826 int use_header;
827
828 g_object_get (G_OBJECT (settings),
829 "gtk-dialogs-use-header",
830 &use_header,
831 NULL);
832
833 return (GtkDialog *) g_object_new (SEARCH_DIALOG_TYPE,
834 "use-header-bar",
835 use_header == 1 ? TRUE : FALSE,
836 NULL);
837 }
838
839 static void
840 search_dialog_on_icon_release (SearchDialog *self, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer user_data)
841 {
842 SearchDialogPrivate *priv = search_dialog_get_instance_private (self);
843
844 if (icon_pos == GTK_ENTRY_ICON_PRIMARY) {
845 if (priv->task != NULL && priv->task->running) {
846 search_task_cancel (priv->task);
847 } else {
848 search_dialog_on_search_activate (self, user_data);
849 }
850 } else {
851 gtk_entry_set_text (GTK_ENTRY (user_data), "");
852 }
853 }