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>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
52 /*#define HTTPS "https"*/
57 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
58 * configure or on the command line.
60 const char *cainfo = CAINFO;
61 gboolean check_cert = FALSE; // do we want this enabled by default ?
62 // would require a CA by default (self-signed wont work)
63 // (don't set to true, change var/flag to no_check_cert)
65 /* Private functions. */
66 static void start_ui (void);
67 static GtkWidget *menu_item_new (int which_menu);
68 static void connect_to_wui (GtkWidget *, gpointer);
69 static void login_to_wui (GtkWidget *, gpointer);
70 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
71 static void destroy (GtkWidget *widget, gpointer data);
72 static void clear_connectmenu (void);
73 static void help_about (GtkWidget *menu);
74 static void viewer_shutdown (GtkWidget *src, void *dummy, GtkWidget *vnc);
76 static void viewer_quit (GtkWidget *src, GtkWidget *vnc);
78 static void viewer_connected (GtkWidget *vnc);
79 static void viewer_initialized (GtkWidget *vnc, GtkWidget *data);
80 static void viewer_disconnected (GtkWidget *vnc);
81 static void viewer_credential (GtkWidget *vnc, GValueArray *credList);
82 static int viewer_open_vnc_socket (const char *vnchost, int vncport);
84 /* For any widgets accessed from multiple functions. */
85 static GtkWidget *window;
86 static GtkWidget *connectitem;
87 static GtkWidget *connectmenu;
88 static GtkWidget *no_connections;
89 static GtkWidget *refresh_vmlist;
90 static GtkWidget *refresh_vmlist_separator;
91 static GtkWidget *connection_area;
92 static GtkWidget *ca_hostname;
93 static GtkWidget *ca_button;
94 static GtkWidget *ca_error;
95 static GtkWidget *login_area;
96 static GtkWidget *la_username;
97 static GtkWidget *la_password;
98 static GtkWidget *la_button;
99 static GtkWidget *notebook;
100 static GtkWidget *statusbar;
101 static guint statusbar_ctx;
102 static GdkCursor *busy_cursor;
116 const char *ungrabbed_text;
117 const char *grabbed_text;
120 static struct menuItem menuItems[] = {
121 { CONNECT_MENU, NULL, "_Connect", "Connect" },
122 { VIEW_MENU, NULL, "_View", "View" },
123 { SEND_KEY_MENU, NULL, "_Send Key", "Send Key" },
124 { WINDOW_MENU, NULL, "_Window", "Window" },
125 { HELP_MENU, NULL, "_Help", "Help" }
129 static const char *title = "oVirt Viewer";
131 /* Gtk widget styles. Avoid installation hassles by keeping this
132 * inside the binary. It can still be overridden by the user (who
135 static const char *styles =
136 "style \"ovirt-viewer-yellow-box\"\n"
138 " bg[NORMAL] = shade (1.5, \"yellow\")\n"
140 "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
143 /* Command-line arguments. */
144 static int print_version = 0;
146 static const char *help_msg =
147 "Use '" PACKAGE " --help' to see a list of available command line options";
149 static const GOptionEntry options[] = {
150 { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
151 "set the path of the CA certificate bundle", NULL },
152 { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
153 "check the SSL certificate of the server", NULL },
154 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
155 "turn on debugging messages", NULL },
156 { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
157 "display version and exit", NULL },
158 { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
162 main (int argc, char *argv[])
164 GOptionContext *context;
165 GError *error = NULL;
167 /* Initialize GLib threads before anything else.
169 * There is one main thread, which is used for all Gtk interactions
170 * and to keep the UI responsive, and one WUI thread. The WUI
171 * thread is used to connect to the WUI, log in, and maintain the list
172 * of virtual machines. The WUI thread is the only thread allowed
173 * to use the CURL library.
175 * The main thread sends instructions to the WUI thread (like "connect",
176 * "disconnect", etc.) using a simple message-passing protocol and
179 * The WUI thread keeps the UI updated by adding idle events which are
180 * processed in the main thread - see:
181 * http://mail.gnome.org/archives/gtk-app-devel-list/2007-March/msg00232.html
183 * Note that under Win32 you must confine all Gtk/Gdk interactions
184 * to a single thread - see:
185 * http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html
187 if (!g_thread_supported ()) {
188 g_thread_init (NULL);
193 fprintf (stderr, "GLib threads not supported or not working.");
197 gtk_init (&argc, &argv);
199 /* Parse command-line options. */
200 context = g_option_context_new ("oVirt viewer");
201 g_option_context_add_main_entries (context, options, NULL);
202 g_option_context_add_group (context, gtk_get_option_group (TRUE));
203 g_option_context_add_group (context, vnc_display_get_option_group ());
204 g_option_context_parse (context, &argc, &argv, &error);
207 g_print ("%s\n%s\n", error->message, help_msg);
208 g_error_free (error);
213 printf ("%s %s\n", PACKAGE, VERSION);
220 DEBUG ("entering the Gtk main loop");
229 /* Create the viewer window, menus. */
238 GtkWidget *sendkeymenu;
249 DEBUG ("creating viewer windows and menus");
252 gtk_rc_parse_string (styles);
254 /* Busy cursor, used by main_busy() function.
255 * XXX This cursor is crap - how can we use the Bluecurve/theme cursor?
257 busy_cursor = gdk_cursor_new (GDK_WATCH);
260 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
261 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
262 gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
263 gtk_window_set_title (GTK_WINDOW (window), title);
265 g_signal_connect (G_OBJECT (window), "delete_event",
266 G_CALLBACK (delete_event), NULL);
267 g_signal_connect (G_OBJECT (window), "destroy",
268 G_CALLBACK (destroy), NULL);
270 /* VBox for layout within the window. */
271 vbox = gtk_vbox_new (FALSE, 0);
274 menubar = gtk_menu_bar_new ();
275 connectitem = menu_item_new (CONNECT_MENU);
276 connectmenu = gtk_menu_new ();
277 gtk_menu_item_set_submenu (GTK_MENU_ITEM (connectitem), connectmenu);
279 no_connections = gtk_menu_item_new_with_label ("Not connected");
280 gtk_menu_append (GTK_MENU (connectmenu), no_connections);
281 gtk_widget_set_sensitive (no_connections, FALSE);
283 /* This is not added to the menu yet, but will be when we are
287 gtk_menu_item_new_with_label ("Refresh list of virtual machines");
288 g_object_ref (refresh_vmlist);
289 refresh_vmlist_separator = gtk_separator_menu_item_new ();
290 g_object_ref (refresh_vmlist_separator);
293 screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
294 gtk_menu_append (GTK_MENU (filemenu), screenshot);
295 g_signal_connect (screenshot, "activate",
296 GTK_SIGNAL_FUNC (take_screenshot), NULL);
299 view = menu_item_new (VIEW_MENU);
300 viewmenu = gtk_menu_new ();
301 gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
303 sendkey = menu_item_new (SEND_KEY_MENU);
304 sendkeymenu = gtk_menu_new ();
305 gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
307 wind = menu_item_new (WINDOW_MENU);
308 windmenu = gtk_menu_new ();
309 gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
311 help = menu_item_new (HELP_MENU);
312 helpmenu = gtk_menu_new ();
313 gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
315 about = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, NULL);
316 gtk_menu_append(GTK_MENU(helpmenu), about);
317 g_signal_connect(about, "activate", GTK_SIGNAL_FUNC (help_about), NULL);
319 gtk_menu_bar_append (GTK_MENU_BAR (menubar), connectitem);
320 gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
321 gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
322 gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
323 gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
325 /* For login dialogs, etc., usually invisible. */
326 connection_area = gtk_event_box_new ();
327 ca_vbox = gtk_vbox_new (FALSE, 0);
328 ca_label = gtk_label_new ("Give the name of the oVirt management server:");
329 ca_hbox = gtk_hbox_new (FALSE, 0);
330 ca_hostname = gtk_entry_new ();
331 gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
332 ca_button = gtk_button_new_with_label ("Connect");
333 ca_error = gtk_label_new (NULL);
334 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
335 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button, FALSE, FALSE, 0);
336 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label, FALSE, FALSE, 4);
337 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox, TRUE, FALSE, 4);
338 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_error, TRUE, FALSE, 4);
339 gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
341 gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
343 g_signal_connect (G_OBJECT (ca_button), "clicked",
344 G_CALLBACK (connect_to_wui), NULL);
346 login_area = gtk_event_box_new ();
347 la_hbox = gtk_hbox_new (FALSE, 0);
348 la_username = gtk_entry_new ();
349 gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
350 la_password = gtk_entry_new ();
351 gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
352 gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
353 la_button = gtk_button_new_with_label ("Login");
354 gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
355 gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
356 gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
357 gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
359 gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
361 g_signal_connect (G_OBJECT (la_button), "clicked",
362 G_CALLBACK (login_to_wui), NULL);
364 /* Tabbed notebook. */
365 notebook = gtk_notebook_new ();
366 /*gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);*/
367 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
368 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
371 statusbar = gtk_statusbar_new ();
372 statusbar_ctx = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar),
374 gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, "");
377 gtk_container_add (GTK_CONTAINER (window), vbox);
378 gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
379 "expand", FALSE, NULL);
380 gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
381 "expand", FALSE, "fill", TRUE, NULL);
382 gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
383 "expand", FALSE, "fill", TRUE, NULL);
384 gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
385 "expand", TRUE, NULL);
386 gtk_container_add_with_properties (GTK_CONTAINER (vbox), statusbar,
387 "expand", FALSE, NULL);
390 gtk_widget_show_all (window);
392 if (wui_thread_is_connected ())
393 gtk_widget_hide (connection_area);
394 if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
395 gtk_widget_hide (login_area);
399 menu_item_new (int which_menu)
405 text = menuItems[which_menu].ungrabbed_text;
407 widget = gtk_menu_item_new ();
408 label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
409 gtk_label_set_text_with_mnemonic (GTK_LABEL (label), text);
410 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
412 gtk_container_add (GTK_CONTAINER (widget), label);
413 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), widget);
414 gtk_widget_show (label);
416 menuItems[which_menu].label = label;
422 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
424 DEBUG ("delete_event");
429 destroy (GtkWidget *widget, gpointer data)
436 help_about (GtkWidget *menu)
439 const char *authors[] = {
440 "Richard W.M. Jones <rjones@redhat.com>",
441 "Daniel P. Berrange <berrange@redhat.com>",
445 about = gtk_about_dialog_new();
447 gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(about), "oVirt Viewer");
448 gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about), VERSION);
449 gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about), "http://ovirt.org/");
450 gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(about), "oVirt website");
451 gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about), authors);
452 gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(about),
453 "This program is free software; you can redistribute it and/or modify\n" \
454 "it under the terms of the GNU General Public License as published by\n" \
455 "the Free Software Foundation; either version 2 of the License, or\n" \
456 "(at your option) any later version.\n" \
458 "This program is distributed in the hope that it will be useful,\n" \
459 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
460 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \
461 "GNU General Public License for more details.\n" \
463 "You should have received a copy of the GNU General Public License\n" \
464 "along with this program; if not, write to the Free Software\n" \
465 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
467 gtk_dialog_run(GTK_DIALOG(about));
468 gtk_widget_destroy(about);
472 connect_to_wui (GtkWidget *widget, gpointer data)
474 const char *hostname;
478 hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
479 if (STREQ (hostname, "")) return;
481 /* https:// + hostname + /ovirt + \0 */
482 len = 8 + strlen (hostname) + 6 + 1;
483 uri = g_alloca (len);
484 snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
486 wui_thread_send_connect (uri);
490 login_to_wui (GtkWidget *widget, gpointer data)
492 const char *username, *password;
494 username = gtk_entry_get_text (GTK_ENTRY (la_username));
495 if (STREQ (username, "")) return;
496 password = gtk_entry_get_text (GTK_ENTRY (la_password));
498 wui_thread_send_login (username, password);
501 /* Connect to a virtual machine. This callback is called from the
502 * connect menu. It searches the notebook of gtk-vnc widgets to see
503 * if we have already connected to this machine, and if not it
504 * makes a new connection.
507 connect_to_vm (GtkWidget *widget, gpointer _vm)
509 struct vm *vm = (struct vm *) _vm;
510 int n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
511 int i, uuidlen, len, fd;
514 const char* hostname;
517 DEBUG ("searching tabs for uuid %s", vm->uuid);
519 uuidlen = strlen (vm->uuid);
521 /* Search the tabs for this UUID, and if found, switch to it and return. */
522 for (i = 0; i < n; ++i) {
523 child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), i);
524 label = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (notebook), child);
525 len = strlen (label);
526 if (len >= uuidlen &&
527 STREQ (label + len - uuidlen, vm->uuid)) {
528 DEBUG ("found on tab %d", i);
529 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
534 DEBUG ("not found, creating new tab");
536 /* This VM isn't in the notebook already, so create a new console. */
537 hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
538 fd = viewer_open_vnc_socket(hostname, vm->forward_vnc_port);
539 if (fd == -1) return; /* We've already given an error. */
541 child = vnc_display_new ();
542 if (! vnc_display_open_fd (VNC_DISPLAY (child), fd)) {
543 main_status_error (g_strdup ("internal error in Gtk-VNC widget"));
548 gtk_signal_connect(GTK_OBJECT(child), "vnc-pointer-grab",
549 GTK_SIGNAL_FUNC(viewer_grab), window);
550 gtk_signal_connect(GTK_OBJECT(child), "vnc-pointer-ungrab",
551 GTK_SIGNAL_FUNC(viewer_ungrab), window);
554 gtk_signal_connect(GTK_OBJECT(child), "delete-event",
555 GTK_SIGNAL_FUNC(viewer_shutdown), child);
557 gtk_signal_connect(GTK_OBJECT(child), "vnc-connected",
558 GTK_SIGNAL_FUNC(viewer_connected), NULL);
559 gtk_signal_connect(GTK_OBJECT(child), "vnc-initialized",
560 GTK_SIGNAL_FUNC(viewer_initialized), NULL);
561 gtk_signal_connect(GTK_OBJECT(child), "vnc-disconnected",
562 GTK_SIGNAL_FUNC(viewer_disconnected), NULL);
564 g_signal_connect(GTK_OBJECT(child), "vnc-auth-credential",
565 GTK_SIGNAL_FUNC(viewer_credential), NULL);
567 /* NB. We have to do this before adding it to the notebook. */
568 gtk_widget_show (child);
570 /* Choose a tab label, which MUST end with the uuid string, since
571 * we use the tab label to store uuid.
573 len = strlen (vm->description) + 1 + strlen (vm->uuid) + 1;
574 label2 = g_alloca (len);
575 snprintf (label2, len, "%s %s", vm->description, vm->uuid);
577 i = gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child, NULL);
578 gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (notebook), child, label2);
579 gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
581 DEBUG ("finished creating new tab");
585 static void viewer_grab(GtkWidget *vnc, GtkWidget *window)
589 viewer_set_title(VNC_DISPLAY(vnc), window, TRUE);
591 for (i = 0 ; i < LAST_MENU; i++) {
592 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].grabbed_text);
596 static void viewer_ungrab(GtkWidget *vnc, GtkWidget *window)
600 viewer_set_title(VNC_DISPLAY(vnc), window, FALSE);
602 for (i = 0 ; i < LAST_MENU; i++) {
603 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].ungrabbed_text);
609 viewer_shutdown (GtkWidget *src, void *dummy, GtkWidget *vnc)
611 vnc_display_close (VNC_DISPLAY(vnc));
613 /* Just close the notebook tab for now. XXX */
614 gtk_notebook_remove_page (GTK_NOTEBOOK (notebook),
615 gtk_notebook_page_num (GTK_NOTEBOOK (notebook),
621 viewer_quit (GtkWidget *src, GtkWidget *vnc)
623 viewer_shutdown (src, NULL, vnc);
628 viewer_connected (GtkWidget *vnc)
630 DEBUG ("Connected to server");
634 viewer_initialized (GtkWidget *vnc, GtkWidget *data)
636 DEBUG ("Connection initialized");
640 viewer_disconnected (GtkWidget *vnc)
642 DEBUG ("Disconnected from server");
646 viewer_credential (GtkWidget *vnc, GValueArray *credList)
648 GtkWidget *dialog = NULL;
650 unsigned int i, prompt = 0;
653 DEBUG ("Got credential request for %d credential(s)",
656 data = g_new0(const char *, credList->n_values);
658 for (i = 0 ; i < credList->n_values ; i++) {
659 GValue *cred = g_value_array_get_nth(credList, i);
660 switch (g_value_get_enum(cred)) {
661 case VNC_DISPLAY_CREDENTIAL_USERNAME:
662 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
665 case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
673 GtkWidget **label, **entry, *box, *vbox;
675 dialog = gtk_dialog_new_with_buttons("Authentication required",
683 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
685 box = gtk_table_new(credList->n_values, 2, FALSE);
686 label = g_new(GtkWidget *, prompt);
687 entry = g_new(GtkWidget *, prompt);
689 for (i = 0, row =0 ; i < credList->n_values ; i++) {
690 GValue *cred = g_value_array_get_nth(credList, i);
691 switch (g_value_get_enum(cred)) {
692 case VNC_DISPLAY_CREDENTIAL_USERNAME:
693 label[row] = gtk_label_new("Username:");
695 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
696 label[row] = gtk_label_new("Password:");
701 entry[row] = gtk_entry_new();
702 if (g_value_get_enum(cred) == VNC_DISPLAY_CREDENTIAL_PASSWORD)
703 gtk_entry_set_visibility(GTK_ENTRY(entry[row]), FALSE);
705 gtk_table_attach(GTK_TABLE(box), label[i], 0, 1, row, row+1, GTK_SHRINK, GTK_SHRINK, 3, 3);
706 gtk_table_attach(GTK_TABLE(box), entry[i], 1, 2, row, row+1, GTK_SHRINK, GTK_SHRINK, 3, 3);
710 vbox = gtk_bin_get_child(GTK_BIN(dialog));
711 gtk_container_add(GTK_CONTAINER(vbox), box);
713 gtk_widget_show_all(dialog);
714 response = gtk_dialog_run(GTK_DIALOG(dialog));
715 gtk_widget_hide(GTK_WIDGET(dialog));
717 if (response == GTK_RESPONSE_OK) {
718 for (i = 0, row = 0 ; i < credList->n_values ; i++) {
719 GValue *cred = g_value_array_get_nth(credList, i);
720 switch (g_value_get_enum(cred)) {
721 case VNC_DISPLAY_CREDENTIAL_USERNAME:
722 case VNC_DISPLAY_CREDENTIAL_PASSWORD:
723 data[i] = gtk_entry_get_text(GTK_ENTRY(entry[row]));
730 for (i = 0 ; i < credList->n_values ; i++) {
731 GValue *cred = g_value_array_get_nth(credList, i);
733 if (vnc_display_set_credential(VNC_DISPLAY(vnc),
734 g_value_get_enum(cred),
736 DEBUG("Failed to set credential type %d",
737 g_value_get_enum(cred));
738 vnc_display_close(VNC_DISPLAY(vnc));
741 DEBUG("Unsupported credential type %d",
742 g_value_get_enum(cred));
743 vnc_display_close(VNC_DISPLAY(vnc));
749 gtk_widget_destroy(GTK_WIDGET(dialog));
752 #if defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME)
755 viewer_open_vnc_socket(const char* vnchost, int vncport)
758 struct hostent *serv;
759 struct sockaddr_in serv_addr;
761 socketfd = socket(PF_INET, SOCK_STREAM, 0);
766 serv = gethostbyname(vnchost);
771 serv_addr.sin_family = PF_INET;
772 serv_addr.sin_port = htons(vncport);
773 serv_addr.sin_addr.s_addr = ((struct in_addr *)(serv->h_addr))->s_addr;
775 if (connect(socketfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
782 #endif /* defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME) */
784 /* Remove all menu items from the Connect menu. */
786 remove_menu_item (GtkWidget *menu_item, gpointer data)
788 gtk_container_remove (GTK_CONTAINER (connectmenu), menu_item);
792 clear_connectmenu (void)
794 DEBUG ("clear Connect menu");
795 gtk_container_foreach (GTK_CONTAINER (connectmenu), remove_menu_item, NULL);
798 /* The WUI thread has changed its state to connected. */
800 main_connected (gpointer data)
803 ASSERT_IS_MAIN_THREAD ();
805 gtk_label_set_text (GTK_LABEL (ca_error), NULL);
807 gtk_widget_hide (connection_area);
808 if (!wui_thread_is_logged_in ())
809 gtk_widget_show (login_area);
813 /* The WUI thread has changed its state to disconnected. */
815 main_disconnected (gpointer data)
817 DEBUG ("disconnected");
818 ASSERT_IS_MAIN_THREAD ();
820 gtk_widget_show (connection_area);
821 gtk_widget_hide (login_area);
823 clear_connectmenu ();
824 gtk_menu_append (GTK_MENU (connectmenu), no_connections);
825 gtk_widget_show_all (connectmenu);
830 /* The WUI thread has changed its state to logged in. */
832 main_logged_in (gpointer data)
835 ASSERT_IS_MAIN_THREAD ();
837 gtk_widget_hide (login_area);
841 /* The WUI thread has changed its state to logged out. */
843 main_logged_out (gpointer data)
845 DEBUG ("logged out");
846 ASSERT_IS_MAIN_THREAD ();
848 if (wui_thread_is_connected ())
849 gtk_widget_show (login_area);
853 /* The WUI thread has changed its state to busy. */
855 main_busy (gpointer data)
857 GdkWindow *gdk_window;
860 ASSERT_IS_MAIN_THREAD ();
862 gdk_window = gtk_widget_get_window (window);
864 gdk_window_set_cursor (gdk_window, busy_cursor);
871 /* The WUI thread has changed its state to idle. */
873 main_idle (gpointer data)
875 GdkWindow *gdk_window;
878 ASSERT_IS_MAIN_THREAD ();
880 gdk_window = gtk_widget_get_window (window);
882 gdk_window_set_cursor (gdk_window, NULL);
889 /* The WUI thread had a connection error. This function must
893 main_connection_error (gpointer _str)
895 char *str = (char *) _str;
897 DEBUG ("connection error: %s", str);
898 ASSERT_IS_MAIN_THREAD ();
900 gtk_label_set_text (GTK_LABEL (ca_error), str);
906 /* The WUI thread had a login error. This function must
910 main_login_error (gpointer _str)
912 char *str = (char *) _str;
914 DEBUG ("login error: %s", str);
915 ASSERT_IS_MAIN_THREAD ();
918 gtk_label_set_text (GTK_LABEL (la_error), str);
925 /* The WUI thread reports a general status error. This function
926 * must free the string.
929 main_status_error (gpointer _str)
931 char *str = (char *) _str;
933 DEBUG ("status error: %s", str);
934 ASSERT_IS_MAIN_THREAD ();
936 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), statusbar_ctx);
937 gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, str);
943 /* The WUI thread has updated the vm list. Here in the main thread
944 * we keep our own copy of the vmlist.
946 static GSList *vmlist = NULL;
948 static void add_vm_to_connectmenu (gpointer _vm, gpointer data);
951 main_vmlist_updated (gpointer data)
955 DEBUG ("vmlist updated");
956 ASSERT_IS_MAIN_THREAD ();
958 /* Get the new vmlist. */
959 if (wui_thread_get_vmlist (&new_vmlist)) {
960 /* Free the previous vmlist. This invalidates all the vm pointers
961 * contained in the Connect menu callbacks, but we're going to
962 * delete those callbacks and create news ones in a moment anyway ...
964 free_vmlist (vmlist);
968 clear_connectmenu ();
970 gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist);
972 if (vmlist != NULL) {
973 gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist_separator);
974 g_slist_foreach (vmlist, add_vm_to_connectmenu, NULL);
977 /* Grrrr Gtk is stupid. */
978 gtk_widget_show_all (connectmenu);
985 add_vm_to_connectmenu (gpointer _vm, gpointer data)
987 struct vm *vm = (struct vm *) _vm;
990 DEBUG ("adding %s to Connect menu", vm->description);
992 item = gtk_menu_item_new_with_label (vm->description);
993 gtk_menu_append (GTK_MENU (connectmenu), item);
995 g_signal_connect (G_OBJECT (item), "activate",
996 G_CALLBACK (connect_to_vm), vm);