Codebase list virt-viewer / fa02bc64-b963-439a-87b2-b721033a39c5/main src / remote-viewer.c
fa02bc64-b963-439a-87b2-b721033a39c5/main

Tree @fa02bc64-b963-439a-87b2-b721033a39c5/main (Download .tar.gz)

remote-viewer.c @fa02bc64-b963-439a-87b2-b721033a39c5/main

2a8ed05
 
 
a893d5e
 
2a8ed05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ce19f
2a8ed05
 
 
67c8c37
 
 
 
3df1e5a
7a47a4d
67c8c37
2a8ed05
6c6ec5f
392b69b
6c6ec5f
40ebfba
2a8ed05
67c8c37
09b9add
01532f9
40d1fff
2a8ed05
f4018a8
2a8ed05
06b57ee
 
3df1e5a
 
6c6ec5f
79255e8
2a8ed05
 
06b57ee
2a8ed05
6963650
 
 
 
 
 
 
7a47a4d
c3d24f8
 
 
 
7a47a4d
 
1bcd8d0
2a8ed05
9cad9ae
 
 
140cb84
9cad9ae
69eccb5
9cad9ae
3df1e5a
06b57ee
 
 
3df1e5a
 
 
9cad9ae
 
 
240baee
 
 
 
 
06b57ee
1bcd8d0
30bb401
 
240baee
 
 
 
 
a9ce19f
 
db827de
a9ce19f
 
 
 
 
 
 
db827de
 
a9ce19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b57ee
a9ce19f
 
 
 
 
 
 
 
 
 
 
140cb84
a9ce19f
 
db827de
 
 
a9ce19f
 
 
 
 
 
 
6963650
 
87c63b9
 
6963650
87c63b9
6963650
87c63b9
6963650
 
 
 
06b57ee
6963650
 
 
 
 
 
 
 
2a8ed05
 
 
e1283a9
 
a9ce19f
2a8ed05
6963650
40ebfba
2a8ed05
a9ce19f
 
e1283a9
240baee
a9ce19f
6963650
 
 
 
 
 
 
 
 
 
2a8ed05
 
 
06b57ee
2a8ed05
 
 
 
a9ce19f
2a8ed05
e1283a9
5b44064
a9ce19f
e1283a9
2a8ed05
 
140cb84
392b69b
 
 
e1283a9
 
392b69b
e1283a9
 
392b69b
e1283a9
392b69b
e1283a9
392b69b
e1283a9
392b69b
2c9cdb6
392b69b
67c8c37
 
ad8966c
67c8c37
 
 
 
 
 
 
 
 
 
 
 
7a47a4d
67c8c37
 
 
 
 
 
7a47a4d
 
 
67c8c37
7a47a4d
45c6fc9
7a47a4d
 
 
 
 
67c8c37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45c6fc9
67c8c37
 
 
 
 
 
 
 
 
 
 
 
247d91d
67c8c37
 
49abd71
 
67c8c37
2561c17
0cd836d
247d91d
67c8c37
49abd71
 
 
 
0cd836d
 
9886055
 
 
67c8c37
247d91d
0cd836d
247d91d
 
0cd836d
 
 
 
 
2561c17
67c8c37
 
 
 
0443bd1
247d91d
67c8c37
b7c8049
247d91d
b7c8049
 
2561c17
67c8c37
 
69eccb5
a9ce19f
69eccb5
6963650
a9ce19f
6963650
d451d6e
 
 
 
69eccb5
 
 
4537047
69eccb5
 
a9ce19f
 
 
69eccb5
 
 
 
 
4537047
69eccb5
 
 
4537047
69eccb5
67c8c37
3df1e5a
 
 
 
 
06b57ee
3df1e5a
 
 
06b57ee
 
69eccb5
 
6963650
5f50c14
3df1e5a
67c8c37
7a47a4d
67c8c37
 
8df64ae
 
67c8c37
 
 
 
 
 
ad8966c
67c8c37
 
 
f9577b9
67c8c37
 
 
 
 
 
 
7c0967f
7a47a4d
67c8c37
 
 
7a47a4d
 
8db413e
67c8c37
7a47a4d
49abd71
7a47a4d
49abd71
 
 
9eb3747
67c8c37
 
 
8df64ae
 
 
0443bd1
 
 
 
 
 
8df64ae
 
 
 
67c8c37
7a71432
67c8c37
 
7a47a4d
 
c3d24f8
 
 
 
 
7a47a4d
 
 
 
67c8c37
 
7a47a4d
 
041a39f
67c8c37
 
7a47a4d
67c8c37
 
 
 
 
 
7a47a4d
67c8c37
7a47a4d
 
67c8c37
 
 
7a47a4d
 
 
 
67c8c37
 
 
 
 
 
7c0967f
f9577b9
67c8c37
1eafd04
9e16ee0
1eafd04
 
9e16ee0
1eafd04
67c8c37
ccafa32
 
 
 
 
 
 
67c8c37
 
 
 
 
7a47a4d
9e16ee0
041a39f
67c8c37
 
 
3df1e5a
 
 
 
 
 
67c8c37
 
 
f1204f0
67c8c37
f9c4a03
584f6cf
67c8c37
 
b63e90f
 
67c8c37
 
 
 
7c0967f
f9577b9
67c8c37
b63e90f
 
 
 
 
 
 
73a4e99
 
 
 
 
b63e90f
73a4e99
67c8c37
 
 
 
 
 
ad8966c
67c8c37
 
 
 
 
 
7c0967f
7a47a4d
f9577b9
67c8c37
 
7a47a4d
67c8c37
 
 
 
 
 
 
 
 
 
7a47a4d
c3d24f8
 
 
 
7a47a4d
 
 
 
 
 
 
 
 
bd6fd96
7a47a4d
e2abe95
 
7a47a4d
 
 
 
 
 
 
74d2574
7a47a4d
 
 
c3d24f8
 
 
7a47a4d
 
 
 
 
 
 
 
3dd077b
7a47a4d
a9ce19f
 
 
 
 
 
 
 
 
 
b983b6b
a9ce19f
 
 
 
 
 
 
 
 
a13173a
a9ce19f
 
 
 
a13173a
a9ce19f
 
 
ce7fd6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df42f78
b983b6b
df42f78
 
 
 
fe7aad2
 
 
 
 
 
 
 
 
 
 
 
b983b6b
df42f78
a13173a
df42f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a8ed05
1bcd8d0
2a8ed05
06b57ee
392b69b
e1283a9
09b9add
 
e1283a9
 
 
13f4932
2a8ed05
06b2c38
140cb84
06b57ee
58dd08f
 
1bcd8d0
 
 
 
79255e8
1bcd8d0
79255e8
 
 
 
e1283a9
 
ed939ad
e1283a9
ce7fd6e
 
 
 
09b9add
ce7fd6e
f289003
09b9add
 
ce7fd6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09b9add
 
1a619f2
 
 
e1283a9
 
fe7aad2
 
e1283a9
df42f78
1bcd8d0
2a8ed05
09b9add
 
 
e1283a9
e23f6fa
e1283a9
e23f6fa
79255e8
06b57ee
a450977
 
 
1bcd8d0
06b2c38
 
1bcd8d0
 
06b2c38
e1283a9
2a8ed05
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
 * Virt Viewer: A virtual machine console viewer
 *
 * Copyright (C) 2007-2012 Red Hat, Inc.
 * Copyright (C) 2009-2012 Daniel P. Berrange
 * Copyright (C) 2010 Marc-André Lureau
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Marc-André Lureau <marcandre.lureau@redhat.com>
 */

#include <config.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#include <libxml/uri.h>

#ifdef HAVE_OVIRT
#include <govirt/govirt.h>
#include "ovirt-foreign-menu.h"
#include "virt-viewer-vm-connection.h"
#endif

#ifdef HAVE_SPICE_GTK
#include "virt-viewer-session-spice.h"
#endif

#include "virt-viewer-app.h"
#include "virt-viewer-auth.h"
#include "virt-viewer-file.h"
#include "virt-viewer-session.h"
#include "virt-viewer-util.h"
#include "remote-viewer.h"
#include "remote-viewer-connect.h"

struct _RemoteViewer {
    VirtViewerApp parent;
#ifdef HAVE_OVIRT
    OvirtForeignMenu *ovirt_foreign_menu;
#endif
    gboolean open_recent_dialog;
};

G_DEFINE_TYPE(RemoteViewer, remote_viewer, VIRT_VIEWER_TYPE_APP)

enum RemoteViewerProperties {
    PROP_0,
#ifdef HAVE_OVIRT
    PROP_OVIRT_FOREIGN_MENU,
#endif
};

#ifdef HAVE_OVIRT
static OvirtVm * choose_vm(GtkWindow *main_window,
                           char **vm_name,
                           OvirtCollection *vms,
                           GError **error);
#endif

static gboolean remote_viewer_start(VirtViewerApp *self, GError **error);

static void
remote_viewer_dispose (GObject *object)
{
#if defined(HAVE_OVIRT)
    RemoteViewer *self = REMOTE_VIEWER(object);
#endif

#ifdef HAVE_OVIRT
    if (self->ovirt_foreign_menu) {
        g_object_unref(self->ovirt_foreign_menu);
        self->ovirt_foreign_menu = NULL;
    }
#endif

    G_OBJECT_CLASS(remote_viewer_parent_class)->dispose (object);
}

static void
remote_viewer_deactivated(VirtViewerApp *app, gboolean connect_error)
{
    RemoteViewer *self = REMOTE_VIEWER(app);

    if (connect_error && self->open_recent_dialog) {
        if (virt_viewer_app_start(app, NULL)) {
            return;
        }
    }

    VIRT_VIEWER_APP_CLASS(remote_viewer_parent_class)->deactivated(app, connect_error);
}

static gchar **opt_args = NULL;
static char *opt_title = NULL;
static gboolean opt_shared = FALSE;

static void
remote_viewer_add_option_entries(VirtViewerApp *self, GOptionContext *context, GOptionGroup *group)
{
    static const GOptionEntry options[] = {
        { "title", 't', 0, G_OPTION_ARG_STRING, &opt_title,
          N_("Set window title"), NULL },
        { "shared", 's', 0, G_OPTION_ARG_NONE,  &opt_shared,
          N_("Share client session"), NULL },
        { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_args,
          NULL, "URI|VV-FILE" },
        { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
    };

    VIRT_VIEWER_APP_CLASS(remote_viewer_parent_class)->add_option_entries(self, context, group);
    g_option_context_set_summary(context, _("Remote viewer client"));
    g_option_group_add_entries(group, options);

#ifdef HAVE_OVIRT
    g_option_context_add_group (context, ovirt_get_option_group ());
#endif
}

static gboolean
remote_viewer_local_command_line (GApplication   *gapp,
                                  gchar        ***args,
                                  int            *status)
{
    gboolean ret = FALSE;
    VirtViewerApp *app = VIRT_VIEWER_APP(gapp);
    RemoteViewer *self = REMOTE_VIEWER(app);

    ret = G_APPLICATION_CLASS(remote_viewer_parent_class)->local_command_line(gapp, args, status);
    if (ret)
        goto end;

    if (!opt_args) {
        self->open_recent_dialog = TRUE;
    } else {
        if (g_strv_length(opt_args) > 1) {
            g_printerr(_("\nError: can't handle multiple URIs\n\n"));
            ret = TRUE;
            *status = 1;
            goto end;
        }

        g_object_set(app, "guri", opt_args[0], NULL);
    }

    if (opt_title)
        g_object_set(app, "title", opt_title, NULL);

    virt_viewer_app_set_shared(app, opt_shared);

 end:
    if (ret && *status)
        g_printerr(_("Run '%s --help' to see a full list of available command line options\n"), g_get_prgname());

    g_strfreev(opt_args);
    return ret;
}

static void
remote_viewer_get_property(GObject *object, guint property_id,
                           GValue *value G_GNUC_UNUSED,
                           GParamSpec *pspec)
{
#ifdef HAVE_OVIRT
    RemoteViewer *self = REMOTE_VIEWER(object);
#endif

    switch (property_id) {
#ifdef HAVE_OVIRT
    case PROP_OVIRT_FOREIGN_MENU:
        g_value_set_object(value, self->ovirt_foreign_menu);
        break;
#endif

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
remote_viewer_class_init (RemoteViewerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    VirtViewerAppClass *app_class = VIRT_VIEWER_APP_CLASS (klass);
    GApplicationClass *g_app_class = G_APPLICATION_CLASS(klass);

    object_class->get_property = remote_viewer_get_property;
    object_class->dispose = remote_viewer_dispose;

    g_app_class->local_command_line = remote_viewer_local_command_line;

    app_class->start = remote_viewer_start;
    app_class->deactivated = remote_viewer_deactivated;
    app_class->add_option_entries = remote_viewer_add_option_entries;

#ifdef HAVE_OVIRT
    g_object_class_install_property(object_class,
                                    PROP_OVIRT_FOREIGN_MENU,
                                    g_param_spec_object("ovirt-foreign-menu",
                                                        "oVirt Foreign Menu",
                                                        "Object which is used as interface to oVirt",
                                                        OVIRT_TYPE_FOREIGN_MENU,
                                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
#endif
}

static void
remote_viewer_init(RemoteViewer *self G_GNUC_UNUSED)
{
}

RemoteViewer *
remote_viewer_new(void)
{
    return g_object_new(REMOTE_VIEWER_TYPE,
                        "application-id", "org.virt-manager.virt-viewer",
                        "flags", G_APPLICATION_NON_UNIQUE,
                        NULL);
}

#if defined(HAVE_SPICE_GTK) && defined(HAVE_OVIRT)
static SpiceSession *
remote_viewer_get_spice_session(RemoteViewer *self)
{
    VirtViewerSession *vsession = NULL;
    SpiceSession *session = NULL;

    g_object_get(self, "session", &vsession, NULL);
    g_return_val_if_fail(vsession != NULL, NULL);

    g_object_get(vsession, "spice-session", &session, NULL);

    g_object_unref(vsession);

    return session;
}
#endif

#ifdef HAVE_OVIRT
static gboolean
parse_ovirt_uri(const gchar *uri_str, char **rest_uri, char **name, char **username)
{
    char *vm_name = NULL;
    char *rel_path;
    xmlURIPtr uri;
    gchar **path_elements;
    guint element_count;

    g_return_val_if_fail(uri_str != NULL, FALSE);
    g_return_val_if_fail(rest_uri != NULL, FALSE);
    g_return_val_if_fail(name != NULL, FALSE);

    uri = xmlParseURI(uri_str);
    g_return_val_if_fail(uri != NULL, FALSE);

    if (g_strcmp0(uri->scheme, "ovirt") != 0) {
        xmlFreeURI(uri);
        return FALSE;
    }

    if (username && uri->user)
        *username = g_strdup(uri->user);

    if (uri->path == NULL) {
        *name = NULL;
        *rest_uri = g_strdup(uri->server);
        xmlFreeURI(uri);
        return TRUE;
    }

    if (*uri->path != '/') {
        xmlFreeURI(uri);
        return FALSE;
    }

    /* extract VM name from path */
    path_elements = g_strsplit(uri->path, "/", -1);

    element_count = g_strv_length(path_elements);
    if (element_count == 0) {
        g_strfreev(path_elements);
        return FALSE;
    }
    vm_name = path_elements[element_count-1];
    path_elements[element_count-1] = NULL;

    /* build final URI */
    rel_path = g_strjoinv("/", path_elements);
    *rest_uri = g_strdup_printf("%s%s", uri->server, rel_path);
    *name = vm_name;
    g_free(rel_path);
    g_strfreev(path_elements);
    xmlFreeURI(uri);

    g_debug("oVirt base URI: %s", *rest_uri);
    g_debug("oVirt VM name: %s", *name);

    return TRUE;
}

static gboolean
authenticate_cb(RestProxy *proxy, RestProxyAuth *rstauth,
                G_GNUC_UNUSED gboolean retrying, gpointer user_data)
{
    gchar *username = NULL;
    gchar *password = NULL;
    VirtViewerWindow *window;
    gboolean success = FALSE;
    gboolean kiosk = FALSE;
    VirtViewerAuth *auth;

    g_object_get(proxy,
                 "username", &username,
                 NULL);

    g_object_get(G_OBJECT(user_data), "kiosk", &kiosk, NULL);

    if (username == NULL || *username == '\0')
        username = g_strdup(g_get_user_name());

    window = virt_viewer_app_get_main_window(VIRT_VIEWER_APP(user_data));
    auth = virt_viewer_auth_new(virt_viewer_window_get_window(window));
    do {

        success = virt_viewer_auth_collect_credentials(auth,
                                                       "oVirt",
                                                       NULL,
                                                       &username, &password);
    } while (kiosk && !success);

    if (success) {
        g_object_set(G_OBJECT(proxy),
                     "username", username,
                     "password", password,
                     NULL);
    } else {
        rest_proxy_auth_cancel(rstauth);
    }

    gtk_widget_destroy(GTK_WIDGET(auth));
    g_free(username);
    g_free(password);
    return success;
}

static void
ovirt_foreign_menu_update(GtkApplication *gtkapp, GtkWindow *gtkwin, G_GNUC_UNUSED gpointer data)
{
    RemoteViewer *self = REMOTE_VIEWER(gtkapp);
    VirtViewerWindow *win = g_object_get_data(G_OBJECT(gtkwin), "virt-viewer-window");
    GtkBuilder *builder = virt_viewer_window_get_builder(win);
    GtkWidget *button = GTK_WIDGET(gtk_builder_get_object(builder, "toolbar-cd"));
    gtk_widget_set_visible(button, self->ovirt_foreign_menu != NULL);
    button = GTK_WIDGET(gtk_builder_get_object(builder, "header-cd"));
    gtk_widget_set_visible(button, self->ovirt_foreign_menu != NULL);
}

static void
ovirt_foreign_menu_update_each(gpointer value,
                               gpointer user_data)
{
    ovirt_foreign_menu_update(GTK_APPLICATION(user_data),
                              virt_viewer_window_get_window(VIRT_VIEWER_WINDOW(value)),
                              NULL);
}

static void
ovirt_foreign_menu_updated(RemoteViewer *self)
{
    GList *windows = virt_viewer_app_get_windows(VIRT_VIEWER_APP(self));

    g_debug("Spice foreign menu updated");

    g_list_foreach(windows, ovirt_foreign_menu_update_each, self);
}

static void
virt_viewer_app_set_ovirt_foreign_menu(VirtViewerApp *app,
                                       OvirtForeignMenu *foreign_menu)
{
    RemoteViewer *self;
    g_return_if_fail(REMOTE_IS_VIEWER(app));
    g_return_if_fail(OVIRT_IS_FOREIGN_MENU(foreign_menu));

    self = REMOTE_VIEWER(app);
    g_clear_object(&self->ovirt_foreign_menu);
    self->ovirt_foreign_menu = foreign_menu;
    g_signal_connect(G_OBJECT(app), "window-added",
                     (GCallback)ovirt_foreign_menu_update, NULL);
    ovirt_foreign_menu_updated(self);
}

static gboolean
create_ovirt_session(VirtViewerApp *app, const char *uri, GError **err)
{
    OvirtProxy *proxy = NULL;
    OvirtApi *api = NULL;
    OvirtCollection *vms;
    OvirtVm *vm = NULL;
    OvirtVmDisplay *display = NULL;
    OvirtVmState state;
    GError *error = NULL;
    char *rest_uri = NULL;
    char *vm_name = NULL;
    char *username = NULL;
    gboolean success = FALSE;
    guint port;
    guint secure_port;
    char *proxy_url = NULL;
    OvirtVmDisplayType type;
    const char *session_type;

    gchar *gport = NULL;
    gchar *gtlsport = NULL;
    gchar *ghost = NULL;
    gchar *ticket = NULL;
    gchar *host_subject = NULL;
    gchar *guid = NULL;

    g_return_val_if_fail(VIRT_VIEWER_IS_APP(app), FALSE);

    if (!parse_ovirt_uri(uri, &rest_uri, &vm_name, &username)) {
        g_set_error_literal(&error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                            _("failed to parse oVirt URI"));
        goto error;
    }

    proxy = ovirt_proxy_new(rest_uri);
    g_object_set(proxy,
                 "username", username,
                 NULL);
    ovirt_set_proxy_options(proxy);
    g_signal_connect(G_OBJECT(proxy), "authenticate",
                     G_CALLBACK(authenticate_cb), app);

    api = ovirt_proxy_fetch_api(proxy, &error);
    if (error != NULL) {
        g_debug("failed to get oVirt 'api' collection: %s", error->message);
        if (g_error_matches(error, OVIRT_REST_CALL_ERROR, OVIRT_REST_CALL_ERROR_CANCELLED)) {
            g_clear_error(&error);
            g_set_error_literal(&error,
                                VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_CANCELLED,
                                _("Authentication was cancelled"));
        }
        goto error;
    }
    vms = ovirt_api_get_vms(api);
    ovirt_collection_fetch(vms, proxy, &error);
    if (error != NULL) {
        g_debug("failed to fetch oVirt 'vms' collection: %s", error->message);
        goto error;
    }
    if (vm_name == NULL ||
        (vm = OVIRT_VM(ovirt_collection_lookup_resource(vms, vm_name))) == NULL) {
        VirtViewerWindow *main_window = virt_viewer_app_get_main_window(app);
        vm = choose_vm(virt_viewer_window_get_window(main_window),
                       &vm_name,
                       vms,
                       &error);
        if (vm == NULL) {
            goto error;
        }
    }
    g_object_get(G_OBJECT(vm), "state", &state, NULL);
    if (state != OVIRT_VM_STATE_UP) {
        g_set_error(&error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                    _("oVirt VM %s is not running"), vm_name);
        g_debug("%s", error->message);
        goto error;
    }
    g_object_set(app, "guest-name", vm_name, NULL);

    if (!ovirt_vm_get_ticket(vm, proxy, &error)) {
        g_debug("failed to get ticket for %s: %s", vm_name, error->message);
        goto error;
    }

    g_object_get(G_OBJECT(vm), "display", &display, "guid", &guid, NULL);
    if (display == NULL) {
        g_set_error(&error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                    _("oVirt VM %s has no display"), vm_name);
        goto error;
    }

    if (guid != NULL) {
        g_object_set(app, "uuid", guid, NULL);
    }

    g_object_get(G_OBJECT(display),
                 "type", &type,
                 "address", &ghost,
                 "port", &port,
                 "secure-port", &secure_port,
                 "ticket", &ticket,
                 "host-subject", &host_subject,
                 "proxy-url", &proxy_url,
                 NULL);
    if (port != 0) {
        gport = g_strdup_printf("%u", port);
    }
    if (secure_port != 0) {
        gtlsport = g_strdup_printf("%u", secure_port);
    }

    if (ghost == NULL) {
        g_set_error(&error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                    _("oVirt VM %s has no host information"), vm_name);
        g_debug("%s", error->message);
        goto error;
    }

    if (type == OVIRT_VM_DISPLAY_SPICE) {
        session_type = "spice";
    } else if (type == OVIRT_VM_DISPLAY_VNC) {
        session_type = "vnc";
    } else {
        g_set_error(&error, VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                    _("oVirt VM %s has unknown display type: %u"), vm_name, type);
        g_debug("%s", error->message);
        goto error;
    }

    {
        OvirtForeignMenu *ovirt_menu = ovirt_foreign_menu_new(proxy);
        g_object_set(G_OBJECT(ovirt_menu), "api", api, "vm", vm, NULL);
        virt_viewer_app_set_ovirt_foreign_menu(app, ovirt_menu);
    }

    virt_viewer_app_set_connect_info(app, NULL, ghost, gport, gtlsport,
                                     session_type, NULL, NULL, 0, NULL);

    if (!virt_viewer_app_create_session(app, session_type, &error))
        goto error;

#ifdef HAVE_SPICE_GTK
    if (type == OVIRT_VM_DISPLAY_SPICE) {
        SpiceSession *session;
        GByteArray *ca_cert = NULL;
        const char *from = "display";

        session = remote_viewer_get_spice_session(REMOTE_VIEWER(app));
        g_object_set(G_OBJECT(session),
                     "password", ticket,
                     "cert-subject", host_subject,
                     "proxy", proxy_url,
                     NULL);

        g_object_get(G_OBJECT(display), "ca-cert", &ca_cert, NULL);
        if (ca_cert == NULL) {
            g_object_get(G_OBJECT(proxy), "ca-cert", &ca_cert, NULL);
            from = "proxy";
        }

        if (ca_cert != NULL) {
            g_object_set(G_OBJECT(session),
                    "ca", ca_cert,
                    NULL);
            g_byte_array_unref(ca_cert);
            g_debug("Using ca-cert from %s", from);
        }
    }
#endif

    success = TRUE;

error:
    g_free(username);
    g_free(rest_uri);
    g_free(vm_name);
    g_free(ticket);
    g_free(gport);
    g_free(gtlsport);
    g_free(ghost);
    g_free(host_subject);
    g_free(guid);
    g_free(proxy_url);

    if (error != NULL)
        g_propagate_error(err, error);
    if (display != NULL)
        g_object_unref(display);
    if (vm != NULL)
        g_object_unref(vm);
    if (proxy != NULL)
        g_object_unref(proxy);

    return success;
}

static OvirtVm *
choose_vm(GtkWindow *main_window,
          char **vm_name,
          OvirtCollection *vms_collection,
          GError **error)
{
    GtkListStore *model;
    GtkTreeIter iter;
    GHashTable *vms;
    GHashTableIter vms_iter;
    OvirtVmState state;
    OvirtVm *vm;

    g_return_val_if_fail(vm_name != NULL, NULL);
    free(*vm_name);

                               /* UI name      , key          , tooltip */
    model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

    vms = ovirt_collection_get_resources(vms_collection);
    g_hash_table_iter_init(&vms_iter, vms);
    while (g_hash_table_iter_next(&vms_iter, (gpointer *) vm_name, (gpointer *) &vm)) {
        g_object_get(G_OBJECT(vm), "state", &state, NULL);
        if (state == OVIRT_VM_STATE_UP) {
            gtk_list_store_append(model, &iter);
            gtk_list_store_set(model, &iter, 0, *vm_name, 1, *vm_name, -1);
       }
    }

    *vm_name = virt_viewer_vm_connection_choose_name_dialog(main_window,
                                                            GTK_TREE_MODEL(model),
                                                            error);
    g_object_unref(model);
    if (*vm_name == NULL)
        return NULL;

    vm = OVIRT_VM(ovirt_collection_lookup_resource(vms_collection, *vm_name));

    return vm;
}
#endif /* HAVE_OVIRT */

static void
remote_viewer_recent_add(gchar *uri, const gchar *mime_type)
{
    GtkRecentManager *recent;
    GtkRecentData meta = {
        .app_name     = (char*)"remote-viewer",
        .app_exec     = (char*)"remote-viewer %u",
        .mime_type    = (char*)mime_type,
    };

    g_return_if_fail(uri != NULL);

    recent = gtk_recent_manager_get_default();
    meta.display_name = uri;
    if (!gtk_recent_manager_add_full(recent, uri, &meta))
        g_warning("Recent item couldn't be added");
}

static void
remote_viewer_session_connected(VirtViewerSession *session,
                                const gchar *guri)
{
    gchar *uri = virt_viewer_session_get_uri(session);
    const gchar *mime = virt_viewer_session_mime_type(session);

    remote_viewer_recent_add(uri != NULL ? uri : (char *) guri, mime);
    g_free(uri);
}

static gchar *
read_all_stdin(gsize *len, GError **err)
{
    GIOChannel *ioc = g_io_channel_unix_new(fileno(stdin));
    gchar *content = NULL;
    GIOStatus status;

    status = g_io_channel_read_to_end(ioc, &content, len, err);
    g_assert(status != G_IO_STATUS_AGAIN);

    g_io_channel_unref(ioc);
    g_assert((content && !*err) || (!content && *err));

    return content;
}

static gboolean
remote_viewer_initial_connect(RemoteViewer *self, const gchar *type, const gchar *guri,
                              VirtViewerFile *vvfile, GError **error)
{
    VirtViewerApp *app = VIRT_VIEWER_APP(self);

#ifdef HAVE_OVIRT
    if (g_strcmp0(type, "ovirt") == 0) {
        if (!create_ovirt_session(app, guri, error)) {
            g_prefix_error(error, _("Couldn't open oVirt session: "));
            return FALSE;
        }
    } else
#endif
    {
        if (!virt_viewer_app_create_session(app, type, error))
            return FALSE;
    }

    g_signal_connect(virt_viewer_app_get_session(app), "session-connected",
                     G_CALLBACK(remote_viewer_session_connected), (gpointer) g_intern_string(guri));

    virt_viewer_session_set_file(virt_viewer_app_get_session(app), vvfile);
#ifdef HAVE_OVIRT
    if (vvfile != NULL) {
        OvirtForeignMenu *ovirt_menu;
        ovirt_menu = ovirt_foreign_menu_new_from_file(vvfile);
        if (ovirt_menu != NULL) {
            virt_viewer_app_set_ovirt_foreign_menu(app, ovirt_menu);
        }
    }
#endif

    if (!virt_viewer_app_initial_connect(app, error)) {
        if (*error == NULL) {
            g_set_error_literal(error,
                                VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                                _("Failed to initiate connection"));
        }
        return FALSE;
    }

    return TRUE;
}

static gboolean
remote_viewer_start(VirtViewerApp *app, GError **err)
{
    g_return_val_if_fail(REMOTE_IS_VIEWER(app), FALSE);

    RemoteViewer *self = REMOTE_VIEWER(app);
    GFile *file = NULL;
    VirtViewerFile *vvfile = NULL;
    gboolean ret = FALSE;
    gchar *guri = NULL;
    gchar *type = NULL;
    GError *error = NULL;

retry_dialog:
    {
        if (self->open_recent_dialog) {
            VirtViewerWindow *main_window = virt_viewer_app_get_main_window(app);
            if (!remote_viewer_connect_dialog(virt_viewer_window_get_window(main_window), &guri)) {
                g_set_error_literal(&error,
                            VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_CANCELLED,
                            _("No connection was chosen"));
                g_propagate_error(err, error);
                return FALSE;
            }
            g_object_set(app, "guri", guri, NULL);
        } else
            g_object_get(app, "guri", &guri, NULL);

        g_return_val_if_fail(guri != NULL, FALSE);

        g_debug("Opening display to %s", guri);

        if (!g_strcmp0(guri, "-")) {
            gsize len = 0;
            gchar *buf = read_all_stdin(&len, &error);

            if (error) {
                g_prefix_error(&error, _("Failed to read stdin: "));
                g_warning("%s", error->message);
                goto cleanup;
            }

            vvfile = virt_viewer_file_new_from_buffer(buf, len, &error);
            g_free(buf);
        } else {
            file = g_file_new_for_commandline_arg(guri);
            if (g_file_query_exists(file, NULL)) {
                gchar *path = g_file_get_path(file);
                vvfile = virt_viewer_file_new(path, &error);
                g_free(path);
            }
        }

        if (error) {
            g_prefix_error(&error, _("Invalid file %s: "), guri);
            g_warning("%s", error->message);
            goto cleanup;
        }

        if (vvfile) {
            g_object_get(G_OBJECT(vvfile), "type", &type, NULL);
        } else if (virt_viewer_util_extract_host(guri, &type, NULL, NULL, NULL, NULL) < 0 || type == NULL) {
            g_set_error_literal(&error,
                                VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
                                _("Cannot determine the connection type from URI"));
            goto cleanup;
        }
        if (!remote_viewer_initial_connect(self, type, guri, vvfile, &error))
            goto cleanup;
    }

    ret = VIRT_VIEWER_APP_CLASS(remote_viewer_parent_class)->start(app, &error);

cleanup:
    g_clear_object(&file);
    g_clear_object(&vvfile);
    g_free(guri);
    guri = NULL;
    g_free(type);
    type = NULL;

    if (!ret && self->open_recent_dialog) {
        if (error != NULL) {
            virt_viewer_app_simple_message_dialog(app, _("Unable to connect: %s"), error->message);
        }
        g_clear_error(&error);
        goto retry_dialog;
    }
    if (error != NULL)
        g_propagate_error(err, error);

    return ret;
}