1 /* ovirt viewer console application
2 * Copyright (C) 2008 Red Hat Inc.
3 * Written by Richard W.M. Jones <rjones@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <vncdisplay.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
44 /*#define HTTPS "https"*/
49 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
50 * configure or on the command line.
52 const char *cainfo = CAINFO;
53 gboolean check_cert = TRUE;
55 /* Private functions. */
56 static void start_ui (void);
57 static GtkWidget *menu_item_new (int which_menu);
58 static void connect_to_wui (GtkWidget *, gpointer);
59 static void login_to_wui (GtkWidget *, gpointer);
60 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
61 static void destroy (GtkWidget *widget, gpointer data);
62 static void clear_connectmenu (void);
63 static void help_about (GtkWidget *menu);
64 static void viewer_shutdown (GtkWidget *src, void *dummy, GtkWidget *vnc);
66 static void viewer_quit (GtkWidget *src, GtkWidget *vnc);
68 static void viewer_connected (GtkWidget *vnc);
69 static void viewer_initialized (GtkWidget *vnc, GtkWidget *data);
70 static void viewer_disconnected (GtkWidget *vnc);
71 static void viewer_credential (GtkWidget *vnc, GValueArray *credList);
72 static int viewer_open_tunnel_ssh (const char *sshhost, int sshport, const char *sshuser, int vncport);
74 /* For any widgets accessed from multiple functions. */
75 static GtkWidget *window;
76 static GtkWidget *connectitem;
77 static GtkWidget *connectmenu;
78 static GtkWidget *no_connections;
79 static GtkWidget *refresh_vmlist;
80 static GtkWidget *refresh_vmlist_separator;
81 static GtkWidget *connection_area;
82 static GtkWidget *ca_hostname;
83 static GtkWidget *ca_button;
84 static GtkWidget *ca_error;
85 static GtkWidget *login_area;
86 static GtkWidget *la_username;
87 static GtkWidget *la_password;
88 static GtkWidget *la_button;
89 static GtkWidget *notebook;
90 static GtkWidget *statusbar;
91 static guint statusbar_ctx;
92 static GdkCursor *busy_cursor;
106 const char *ungrabbed_text;
107 const char *grabbed_text;
110 static struct menuItem menuItems[] = {
111 { CONNECT_MENU, NULL, "_Connect", "Connect" },
112 { VIEW_MENU, NULL, "_View", "View" },
113 { SEND_KEY_MENU, NULL, "_Send Key", "Send Key" },
114 { WINDOW_MENU, NULL, "_Window", "Window" },
115 { HELP_MENU, NULL, "_Help", "Help" }
119 static const char *title = "oVirt Viewer";
121 /* Gtk widget styles. Avoid installation hassles by keeping this
122 * inside the binary. It can still be overridden by the user (who
125 static const char *styles =
126 "style \"ovirt-viewer-yellow-box\"\n"
128 " bg[NORMAL] = shade (1.5, \"yellow\")\n"
130 "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
133 /* Command-line arguments. */
134 static int print_version = 0;
136 static const char *help_msg =
137 "Use '" PACKAGE " --help' to see a list of available command line options";
139 static const GOptionEntry options[] = {
140 { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
141 "set the path of the CA certificate bundle", NULL },
142 { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
143 "if --no-check-certificate is passed we don't check the SSL certificate of the server", NULL },
144 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
145 "turn on debugging messages", NULL },
146 { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
147 "display version and exit", NULL },
148 { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
152 main (int argc, char *argv[])
154 GOptionContext *context;
155 GError *error = NULL;
157 /* Initialize GLib threads before anything else.
159 * There is one main thread, which is used for all Gtk interactions
160 * and to keep the UI responsive, and one WUI thread. The WUI
161 * thread is used to connect to the WUI, log in, and maintain the list
162 * of virtual machines. The WUI thread is the only thread allowed
163 * to use the CURL library.
165 * The main thread sends instructions to the WUI thread (like "connect",
166 * "disconnect", etc.) using a simple message-passing protocol and
169 * The WUI thread keeps the UI updated by adding idle events which are
170 * processed in the main thread - see:
171 * http://mail.gnome.org/archives/gtk-app-devel-list/2007-March/msg00232.html
173 * Note that under Win32 you must confine all Gtk/Gdk interactions
174 * to a single thread - see:
175 * http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html
177 if (!g_thread_supported ()) {
178 g_thread_init (NULL);
183 fprintf (stderr, "GLib threads not supported or not working.");
187 gtk_init (&argc, &argv);
189 /* Parse command-line options. */
190 context = g_option_context_new ("oVirt viewer");
191 g_option_context_add_main_entries (context, options, NULL);
192 g_option_context_add_group (context, gtk_get_option_group (TRUE));
193 g_option_context_add_group (context, vnc_display_get_option_group ());
194 g_option_context_parse (context, &argc, &argv, &error);
197 g_print ("%s\n%s\n", error->message, help_msg);
198 g_error_free (error);
203 printf ("%s %s\n", PACKAGE, VERSION);
210 DEBUG ("entering the Gtk main loop");
219 /* Create the viewer window, menus. */
228 GtkWidget *sendkeymenu;
239 DEBUG ("creating viewer windows and menus");
242 gtk_rc_parse_string (styles);
244 /* Busy cursor, used by main_busy() function.
245 * XXX This cursor is crap - how can we use the Bluecurve/theme cursor?
247 busy_cursor = gdk_cursor_new (GDK_WATCH);
250 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
251 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
252 gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
253 gtk_window_set_title (GTK_WINDOW (window), title);
255 g_signal_connect (G_OBJECT (window), "delete_event",
256 G_CALLBACK (delete_event), NULL);
257 g_signal_connect (G_OBJECT (window), "destroy",
258 G_CALLBACK (destroy), NULL);
260 /* VBox for layout within the window. */
261 vbox = gtk_vbox_new (FALSE, 0);
264 menubar = gtk_menu_bar_new ();
265 connectitem = menu_item_new (CONNECT_MENU);
266 connectmenu = gtk_menu_new ();
267 gtk_menu_item_set_submenu (GTK_MENU_ITEM (connectitem), connectmenu);
269 no_connections = gtk_menu_item_new_with_label ("Not connected");
270 gtk_menu_append (GTK_MENU (connectmenu), no_connections);
271 gtk_widget_set_sensitive (no_connections, FALSE);
273 /* This is not added to the menu yet, but will be when we are
277 gtk_menu_item_new_with_label ("Refresh list of virtual machines");
278 g_object_ref (refresh_vmlist);
279 refresh_vmlist_separator = gtk_separator_menu_item_new ();
280 g_object_ref (refresh_vmlist_separator);
283 screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
284 gtk_menu_append (GTK_MENU (filemenu), screenshot);
285 g_signal_connect (screenshot, "activate",
286 GTK_SIGNAL_FUNC (take_screenshot), NULL);
289 view = menu_item_new (VIEW_MENU);
290 viewmenu = gtk_menu_new ();
291 gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
293 sendkey = menu_item_new (SEND_KEY_MENU);
294 sendkeymenu = gtk_menu_new ();
295 gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
297 wind = menu_item_new (WINDOW_MENU);
298 windmenu = gtk_menu_new ();
299 gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
301 help = menu_item_new (HELP_MENU);
302 helpmenu = gtk_menu_new ();
303 gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
305 about = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, NULL);
306 gtk_menu_append(GTK_MENU(helpmenu), about);
307 g_signal_connect(about, "activate", GTK_SIGNAL_FUNC (help_about), NULL);
309 gtk_menu_bar_append (GTK_MENU_BAR (menubar), connectitem);
310 gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
311 gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
312 gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
313 gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
315 /* For login dialogs, etc., usually invisible. */
316 connection_area = gtk_event_box_new ();
317 ca_vbox = gtk_vbox_new (FALSE, 0);
318 ca_label = gtk_label_new ("Give the name of the oVirt management server:");
319 ca_hbox = gtk_hbox_new (FALSE, 0);
320 ca_hostname = gtk_entry_new ();
321 gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
322 ca_button = gtk_button_new_with_label ("Connect");
323 ca_error = gtk_label_new (NULL);
324 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
325 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button, FALSE, FALSE, 0);
326 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label, FALSE, FALSE, 4);
327 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox, TRUE, FALSE, 4);
328 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_error, TRUE, FALSE, 4);
329 gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
331 gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
333 g_signal_connect (G_OBJECT (ca_button), "clicked",
334 G_CALLBACK (connect_to_wui), NULL);
336 login_area = gtk_event_box_new ();
337 la_hbox = gtk_hbox_new (FALSE, 0);
338 la_username = gtk_entry_new ();
339 gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
340 la_password = gtk_entry_new ();
341 gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
342 gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
343 la_button = gtk_button_new_with_label ("Login");
344 gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
345 gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
346 gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
347 gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
349 gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
351 g_signal_connect (G_OBJECT (la_button), "clicked",
352 G_CALLBACK (login_to_wui), NULL);
354 /* Tabbed notebook. */
355 notebook = gtk_notebook_new ();
356 /*gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);*/
357 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
358 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
361 statusbar = gtk_statusbar_new ();
362 statusbar_ctx = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar),
364 gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, "");
367 gtk_container_add (GTK_CONTAINER (window), vbox);
368 gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
369 "expand", FALSE, NULL);
370 gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
371 "expand", FALSE, "fill", TRUE, NULL);
372 gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
373 "expand", FALSE, "fill", TRUE, NULL);
374 gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
375 "expand", TRUE, NULL);
376 gtk_container_add_with_properties (GTK_CONTAINER (vbox), statusbar,
377 "expand", FALSE, NULL);
380 gtk_widget_show_all (window);
382 if (wui_thread_is_connected ())
383 gtk_widget_hide (connection_area);
384 if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
385 gtk_widget_hide (login_area);
389 menu_item_new (int which_menu)
395 text = menuItems[which_menu].ungrabbed_text;
397 widget = gtk_menu_item_new ();
398 label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
399 gtk_label_set_text_with_mnemonic (GTK_LABEL (label), text);
400 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
402 gtk_container_add (GTK_CONTAINER (widget), label);
403 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), widget);
404 gtk_widget_show (label);
406 menuItems[which_menu].label = label;
412 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
414 DEBUG ("delete_event");
419 destroy (GtkWidget *widget, gpointer data)
426 help_about (GtkWidget *menu)
429 const char *authors[] = {
430 "Richard W.M. Jones <rjones@redhat.com>",
431 "Daniel P. Berrange <berrange@redhat.com>",
435 about = gtk_about_dialog_new();
437 gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(about), "oVirt Viewer");
438 gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about), VERSION);
439 gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about), "http://ovirt.org/");
440 gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(about), "oVirt website");
441 gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about), authors);
442 gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(about),
443 "This program is free software; you can redistribute it and/or modify\n" \
444 "it under the terms of the GNU General Public License as published by\n" \
445 "the Free Software Foundation; either version 2 of the License, or\n" \
446 "(at your option) any later version.\n" \
448 "This program is distributed in the hope that it will be useful,\n" \
449 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
450 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
451 "GNU General Public License for more details.\n" \
453 "You should have received a copy of the GNU General Public License\n" \
454 "along with this program; if not, write to the Free Software\n" \
455 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
457 gtk_dialog_run(GTK_DIALOG(about));
458 gtk_widget_destroy(about);
462 connect_to_wui (GtkWidget *widget, gpointer data)
464 const char *hostname;
468 hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
469 if (STREQ (hostname, "")) return;
471 /* https:// + hostname + /ovirt + \0 */
472 len = 8 + strlen (hostname) + 6 + 1;
473 uri = g_alloca (len);
474 snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
476 wui_thread_send_connect (uri);
480 login_to_wui (GtkWidget *widget, gpointer data)
482 const char *username, *password;
484 username = gtk_entry_get_text (GTK_ENTRY (la_username));
485 if (STREQ (username, "")) return;
486 password = gtk_entry_get_text (GTK_ENTRY (la_password));
488 wui_thread_send_login (username, password);
491 /* Connect to a virtual machine. This callback is called from the
492 * connect menu. It searches the notebook of gtk-vnc widgets to see
493 * if we have already connected to this machine, and if not it
494 * makes a new connection.
497 connect_to_vm (GtkWidget *widget, gpointer _vm)
499 struct vm *vm = (struct vm *) _vm;
500 int n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
501 int i, uuidlen, len, fd;
506 DEBUG ("searching tabs for uuid %s", vm->uuid);
508 uuidlen = strlen (vm->uuid);
510 /* Search the tabs for this UUID, and if found, switch to it and return. */
511 for (i = 0; i < n; ++i) {
512 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), i);
513 label = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (notebook), child);
514 len = strlen (label);
515 if (len >= uuidlen &&
516 STREQ (label + len - uuidlen, vm->uuid)) {
517 DEBUG ("found on tab %d", i);
518 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
523 DEBUG ("not found, creating new tab");
525 /* This VM isn't in the notebook already, so create a new console. */
526 fd = viewer_open_tunnel_ssh (/*vm->host XXX*/ "192.168.50.6",
527 0, /* Default SSH port. */
528 "root", /* Root account. */
530 if (fd == -1) return; /* We've already given an error. */
532 child = vnc_display_new ();
533 if (! vnc_display_open_fd (VNC_DISPLAY (child), fd)) {
534 main_status_error (g_strdup ("internal error in Gtk-VNC widget"));
539 gtk_signal_connect(GTK_OBJECT(child), "vnc-pointer-grab",
540 GTK_SIGNAL_FUNC(viewer_grab), window);
541 gtk_signal_connect(GTK_OBJECT(child), "vnc-pointer-ungrab",
542 GTK_SIGNAL_FUNC(viewer_ungrab), window);
545 gtk_signal_connect(GTK_OBJECT(child), "delete-event",
546 GTK_SIGNAL_FUNC(viewer_shutdown), child);
548 gtk_signal_connect(GTK_OBJECT(child), "vnc-connected",
549 GTK_SIGNAL_FUNC(viewer_connected), NULL);
550 gtk_signal_connect(GTK_OBJECT(child), "vnc-initialized",
551 GTK_SIGNAL_FUNC(viewer_initialized), NULL);
552 gtk_signal_connect(GTK_OBJECT(child), "vnc-disconnected",
553 GTK_SIGNAL_FUNC(viewer_disconnected), NULL);
555 g_signal_connect(GTK_OBJECT(child), "vnc-auth-credential",
556 GTK_SIGNAL_FUNC(viewer_credential), NULL);
558 /* NB. We have to do this before adding it to the notebook. */
559 gtk_widget_show (child);
561 /* Choose a tab label, which MUST end with the uuid string, since
562 * we use the tab label to store uuid.
564 len = strlen (vm->description) + 1 + strlen (vm->uuid) + 1;
565 label2 = g_alloca (len);
566 snprintf (label2, len, "%s %s", vm->description, vm->uuid);
568 i = gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child, NULL);
569 gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (notebook), child, label2);
570 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
572 DEBUG ("finished creating new tab");
576 static void viewer_grab(GtkWidget *vnc, GtkWidget *window)
580 viewer_set_title(VNC_DISPLAY(vnc), window, TRUE);
582 for (i = 0 ; i < LAST_MENU; i++) {
583 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].grabbed_text);
587 static void viewer_ungrab(GtkWidget *vnc, GtkWidget *window)
591 viewer_set_title(VNC_DISPLAY(vnc), window, FALSE);
593 for (i = 0 ; i < LAST_MENU; i++) {
594 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].ungrabbed_text);
600 viewer_shutdown (GtkWidget *src, void *dummy, GtkWidget *vnc)
602 vnc_display_close (VNC_DISPLAY(vnc));
604 /* Just close the notebook tab for now. XXX */
605 gtk_notebook_remove_page (GTK_NOTEBOOK (notebook),
606 gtk_notebook_page_num (GTK_NOTEBOOK (notebook),
612 viewer_quit (GtkWidget *src, GtkWidget *vnc)
614 viewer_shutdown (src, NULL, vnc);
619 viewer_connected (GtkWidget *vnc)
621 DEBUG ("Connected to server");
625 viewer_initialized (GtkWidget *vnc, GtkWidget *data)
627 DEBUG ("Connection initialized");
631 viewer_disconnected (GtkWidget *vnc)
633 DEBUG ("Disconnected from server");
637 viewer_credential (GtkWidget *vnc, GValueArray *credList)
639 GtkWidget *dialog = NULL;
641 unsigned int i, prompt = 0;
644 DEBUG ("Got credential request for %d credential(s)",
647 data = g_new0(const char *, credList->n_values);
649 for (i = 0 ; i < credList->n_values ; i++) {
650 GValue *cred = g_value_array_get_nth(credList, i);
651 switch (g_value_get_enum(cred)) {
652 case VNC_DISPLAY_CREDENTIAL_USERNAME:
653 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
656 case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
664 GtkWidget **label, **entry, *box, *vbox;
666 dialog = gtk_dialog_new_with_buttons("Authentication required",
674 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
676 box = gtk_table_new(credList->n_values, 2, FALSE);
677 label = g_new(GtkWidget *, prompt);
678 entry = g_new(GtkWidget *, prompt);
680 for (i = 0, row =0 ; i < credList->n_values ; i++) {
681 GValue *cred = g_value_array_get_nth(credList, i);
682 switch (g_value_get_enum(cred)) {
683 case VNC_DISPLAY_CREDENTIAL_USERNAME:
684 label[row] = gtk_label_new("Username:");
686 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
687 label[row] = gtk_label_new("Password:");
692 entry[row] = gtk_entry_new();
693 if (g_value_get_enum(cred) == VNC_DISPLAY_CREDENTIAL_PASSWORD)
694 gtk_entry_set_visibility(GTK_ENTRY(entry[row]), FALSE);
696 gtk_table_attach(GTK_TABLE(box), label[i], 0, 1, row, row+1, GTK_SHRINK, GTK_SHRINK, 3, 3);
697 gtk_table_attach(GTK_TABLE(box), entry[i], 1, 2, row, row+1, GTK_SHRINK, GTK_SHRINK, 3, 3);
701 vbox = gtk_bin_get_child(GTK_BIN(dialog));
702 gtk_container_add(GTK_CONTAINER(vbox), box);
704 gtk_widget_show_all(dialog);
705 response = gtk_dialog_run(GTK_DIALOG(dialog));
706 gtk_widget_hide(GTK_WIDGET(dialog));
708 if (response == GTK_RESPONSE_OK) {
709 for (i = 0, row = 0 ; i < credList->n_values ; i++) {
710 GValue *cred = g_value_array_get_nth(credList, i);
711 switch (g_value_get_enum(cred)) {
712 case VNC_DISPLAY_CREDENTIAL_USERNAME:
713 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
714 data[i] = gtk_entry_get_text(GTK_ENTRY(entry[row]));
721 for (i = 0 ; i < credList->n_values ; i++) {
722 GValue *cred = g_value_array_get_nth(credList, i);
724 if (vnc_display_set_credential(VNC_DISPLAY(vnc),
725 g_value_get_enum(cred),
727 DEBUG("Failed to set credential type %d",
728 g_value_get_enum(cred));
729 vnc_display_close(VNC_DISPLAY(vnc));
732 DEBUG("Unsupported credential type %d",
733 g_value_get_enum(cred));
734 vnc_display_close(VNC_DISPLAY(vnc));
740 gtk_widget_destroy(GTK_WIDGET(dialog));
743 #if defined(HAVE_SOCKETPAIR) && defined(HAVE_FORK)
745 static int viewer_open_tunnel(const char **cmd)
750 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) < 0)
760 if (pid == 0) { /* child */
769 execvp("ssh", (char *const*)cmd);
777 viewer_open_tunnel_ssh (const char *sshhost, int sshport, const char *sshuser,
781 char portstr[50], portstr2[50];
787 snprintf (portstr, sizeof portstr, "%d", sshport);
788 snprintf (portstr2, sizeof portstr2, "%d", vncport);
799 cmd[n++] = "localhost";
803 return viewer_open_tunnel(cmd);
806 #endif /* defined(HAVE_SOCKETPAIR) && defined(HAVE_FORK) */
808 /* Remove all menu items from the Connect menu. */
810 remove_menu_item (GtkWidget *menu_item, gpointer data)
812 gtk_container_remove (GTK_CONTAINER (connectmenu), menu_item);
816 clear_connectmenu (void)
818 DEBUG ("clear Connect menu");
819 gtk_container_foreach (GTK_CONTAINER (connectmenu), remove_menu_item, NULL);
822 /* The WUI thread has changed its state to connected. */
824 main_connected (gpointer data)
827 ASSERT_IS_MAIN_THREAD ();
829 gtk_label_set_text (GTK_LABEL (ca_error), NULL);
831 gtk_widget_hide (connection_area);
832 if (!wui_thread_is_logged_in ())
833 gtk_widget_show (login_area);
837 /* The WUI thread has changed its state to disconnected. */
839 main_disconnected (gpointer data)
841 DEBUG ("disconnected");
842 ASSERT_IS_MAIN_THREAD ();
844 gtk_widget_show (connection_area);
845 gtk_widget_hide (login_area);
847 clear_connectmenu ();
848 gtk_menu_append (GTK_MENU (connectmenu), no_connections);
849 gtk_widget_show_all (connectmenu);
854 /* The WUI thread has changed its state to logged in. */
856 main_logged_in (gpointer data)
859 ASSERT_IS_MAIN_THREAD ();
861 gtk_widget_hide (login_area);
865 /* The WUI thread has changed its state to logged out. */
867 main_logged_out (gpointer data)
869 DEBUG ("logged out");
870 ASSERT_IS_MAIN_THREAD ();
872 if (wui_thread_is_connected ())
873 gtk_widget_show (login_area);
877 /* The WUI thread has changed its state to busy. */
879 main_busy (gpointer data)
881 GdkWindow *gdk_window;
884 ASSERT_IS_MAIN_THREAD ();
886 gdk_window = gtk_widget_get_window (window);
888 gdk_window_set_cursor (gdk_window, busy_cursor);
895 /* The WUI thread has changed its state to idle. */
897 main_idle (gpointer data)
899 GdkWindow *gdk_window;
902 ASSERT_IS_MAIN_THREAD ();
904 gdk_window = gtk_widget_get_window (window);
906 gdk_window_set_cursor (gdk_window, NULL);
913 /* The WUI thread had a connection error. This function must
917 main_connection_error (gpointer _str)
919 char *str = (char *) _str;
921 DEBUG ("connection error: %s", str);
922 ASSERT_IS_MAIN_THREAD ();
924 gtk_label_set_text (GTK_LABEL (ca_error), str);
930 /* The WUI thread had a login error. This function must
934 main_login_error (gpointer _str)
936 char *str = (char *) _str;
938 DEBUG ("login error: %s", str);
939 ASSERT_IS_MAIN_THREAD ();
942 gtk_label_set_text (GTK_LABEL (la_error), str);
949 /* The WUI thread reports a general status error. This function
950 * must free the string.
953 main_status_error (gpointer _str)
955 char *str = (char *) _str;
957 DEBUG ("status error: %s", str);
958 ASSERT_IS_MAIN_THREAD ();
960 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), statusbar_ctx);
961 gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, str);
967 /* The WUI thread has updated the vm list. Here in the main thread
968 * we keep our own copy of the vmlist.
970 static GSList *vmlist = NULL;
972 static void add_vm_to_connectmenu (gpointer _vm, gpointer data);
975 main_vmlist_updated (gpointer data)
979 DEBUG ("vmlist updated");
980 ASSERT_IS_MAIN_THREAD ();
982 /* Get the new vmlist. */
983 if (wui_thread_get_vmlist (&new_vmlist)) {
984 /* Free the previous vmlist. This invalidates all the vm pointers
985 * contained in the Connect menu callbacks, but we're going to
986 * delete those callbacks and create news ones in a moment anyway ...
988 free_vmlist (vmlist);
992 clear_connectmenu ();
994 gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist);
996 if (vmlist != NULL) {
997 gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist_separator);
998 g_slist_foreach (vmlist, add_vm_to_connectmenu, NULL);
1001 /* Grrrr Gtk is stupid. */
1002 gtk_widget_show_all (connectmenu);
1009 add_vm_to_connectmenu (gpointer _vm, gpointer data)
1011 struct vm *vm = (struct vm *) _vm;
1014 DEBUG ("adding %s to Connect menu", vm->description);
1016 item = gtk_menu_item_new_with_label (vm->description);
1017 gtk_menu_append (GTK_MENU (connectmenu), item);
1019 g_signal_connect (G_OBJECT (item), "activate",
1020 G_CALLBACK (connect_to_vm), vm);