0a9209e531bad15c78361aa718afb8a85ed37f17
[ovirt-viewer.git] / main.c
1 /* ovirt viewer console application
2  * Copyright (C) 2008 Red Hat Inc.
3  * Written by Richard W.M. Jones <rjones@redhat.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <vncdisplay.h>
29
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41
42 #ifdef HAVE_SYS_UN_H
43 #include <sys/un.h>
44 #endif
45
46 #ifdef HAVE_WINDOWS_H
47 #include <windows.h>
48 #endif
49
50 #include "internal.h"
51
52 /*#define HTTPS "https"*/
53 #define HTTPS "http"
54
55 gboolean debug = 0;
56
57 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
58  * configure or on the command line.
59  */
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)
64
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);
75 #if 0
76 static void viewer_quit (GtkWidget *src, GtkWidget *vnc);
77 #endif
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);
83
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;
103
104 /* Menus. */
105 enum menuNums {
106   CONNECT_MENU,
107   VIEW_MENU,
108   SEND_KEY_MENU,
109   WINDOW_MENU,
110   HELP_MENU,
111 };
112
113 struct menuItem {
114   guint menu;
115   GtkWidget *label;
116   const char *ungrabbed_text;
117   const char *grabbed_text;
118 };
119
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" }
126 };
127
128 /* Window title. */
129 static const char *title = "oVirt Viewer";
130
131 /* Gtk widget styles.  Avoid installation hassles by keeping this
132  * inside the binary.  It can still be overridden by the user (who
133  * will do that?)
134  */
135 static const char *styles =
136   "style \"ovirt-viewer-yellow-box\"\n"
137   "{\n"
138   "  bg[NORMAL] = shade (1.5, \"yellow\")\n"
139   "}\n"
140   "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
141   ;
142
143 /* Command-line arguments. */
144 static int print_version = 0;
145
146 static const char *help_msg =
147   "Use '" PACKAGE " --help' to see a list of available command line options";
148
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 }
159 };
160
161 int
162 main (int argc, char *argv[])
163 {
164   GOptionContext *context;
165   GError *error = NULL;
166
167   /* Initialize GLib threads before anything else.
168    *
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.
174    *
175    * The main thread sends instructions to the WUI thread (like "connect",
176    * "disconnect", etc.) using a simple message-passing protocol and
177    * a GAsyncQueue.
178    *
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
182    *
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
186    */
187   if (!g_thread_supported ()) {
188     g_thread_init (NULL);
189 #ifndef WIN32
190     gdk_threads_init ();
191 #endif
192   } else {
193     fprintf (stderr, "GLib threads not supported or not working.");
194     exit (1);
195   }
196
197   gtk_init (&argc, &argv);
198
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);
205
206   if (error) {
207     g_print ("%s\n%s\n", error->message, help_msg);
208     g_error_free (error);
209     exit (1);
210   }
211
212   if (print_version) {
213     printf ("%s %s\n", PACKAGE, VERSION);
214     exit (0);
215   }
216
217   start_wui_thread ();
218   start_ui ();
219
220   DEBUG ("entering the Gtk main loop");
221
222   gtk_main ();
223
224   stop_wui_thread ();
225
226   exit (0);
227 }
228
229 /* Create the viewer window, menus. */
230 static void
231 start_ui (void)
232 {
233   GtkWidget *vbox;
234   GtkWidget *menubar;
235   GtkWidget *view;
236   GtkWidget *viewmenu;
237   GtkWidget *sendkey;
238   GtkWidget *sendkeymenu;
239   GtkWidget *wind;
240   GtkWidget *windmenu;
241   GtkWidget *help;
242   GtkWidget *helpmenu;
243   GtkWidget *about;
244   GtkWidget *ca_vbox;
245   GtkWidget *ca_hbox;
246   GtkWidget *ca_label;
247   GtkWidget *la_hbox;
248
249   DEBUG ("creating viewer windows and menus");
250
251   /* Parse styles. */
252   gtk_rc_parse_string (styles);
253
254   /* Busy cursor, used by main_busy() function.
255    * XXX This cursor is crap - how can we use the Bluecurve/theme cursor?
256    */
257   busy_cursor = gdk_cursor_new (GDK_WATCH);
258
259   /* Window. */
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);
264
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);
269
270   /* VBox for layout within the window. */
271   vbox = gtk_vbox_new (FALSE, 0);
272
273   /* Menubar. */
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);
278
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);
282
283   /* This is not added to the menu yet, but will be when we are
284    * connected.
285    */
286   refresh_vmlist =
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);
291
292 #if 0
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);
297 #endif
298
299   view = menu_item_new (VIEW_MENU);
300   viewmenu = gtk_menu_new ();
301   gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
302
303   sendkey = menu_item_new (SEND_KEY_MENU);
304   sendkeymenu = gtk_menu_new ();
305   gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
306
307   wind = menu_item_new (WINDOW_MENU);
308   windmenu = gtk_menu_new ();
309   gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
310
311   help = menu_item_new (HELP_MENU);
312   helpmenu = gtk_menu_new ();
313   gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
314
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);
318
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);
324
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);
340
341   gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
342
343   g_signal_connect (G_OBJECT (ca_button), "clicked",
344                     G_CALLBACK (connect_to_wui), NULL);
345
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);
358
359   gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
360
361   g_signal_connect (G_OBJECT (la_button), "clicked",
362                     G_CALLBACK (login_to_wui), NULL);
363
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);
369
370   /* Status bar. */
371   statusbar = gtk_statusbar_new ();
372   statusbar_ctx = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar),
373                                                 "context");
374   gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, "");
375
376   /* Packing. */
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);
388
389   /* Show widgets. */
390   gtk_widget_show_all (window);
391
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);
396 }
397
398 static GtkWidget *
399 menu_item_new (int which_menu)
400 {
401   GtkWidget *widget;
402   GtkWidget *label;
403   const char *text;
404
405   text = menuItems[which_menu].ungrabbed_text;
406
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);
411
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);
415
416   menuItems[which_menu].label = label;
417
418   return widget;
419 }
420
421 static gboolean
422 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
423 {
424   DEBUG ("delete_event");
425   return FALSE;
426 }
427
428 static void
429 destroy (GtkWidget *widget, gpointer data)
430 {
431   DEBUG ("destroy");
432   gtk_main_quit ();
433 }
434
435 static void
436 help_about (GtkWidget *menu)
437 {
438   GtkWidget *about;
439   const char *authors[] = {
440     "Richard W.M. Jones <rjones@redhat.com>",
441     "Daniel P. Berrange <berrange@redhat.com>",
442     NULL
443   };
444
445   about = gtk_about_dialog_new();
446
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" \
457  "\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" \
462  "\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");
466
467   gtk_dialog_run(GTK_DIALOG(about));
468   gtk_widget_destroy(about);
469 }
470
471 static void
472 connect_to_wui (GtkWidget *widget, gpointer data)
473 {
474   const char *hostname;
475   char *uri;
476   int len;
477
478   hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
479   if (STREQ (hostname, "")) return;
480
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);
485
486   wui_thread_send_connect (uri);
487 }
488
489 static void
490 login_to_wui (GtkWidget *widget, gpointer data)
491 {
492   const char *username, *password;
493
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));
497
498   wui_thread_send_login (username, password);
499 }
500
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.
505  */
506 static void
507 connect_to_vm (GtkWidget *widget, gpointer _vm)
508 {
509   struct vm *vm = (struct vm *) _vm;
510   int n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
511   int i, uuidlen, len, fd;
512   GtkWidget *child;
513   const char *label;
514   const char* hostname;
515   char *label2;
516
517   DEBUG ("searching tabs for uuid %s", vm->uuid);
518
519   uuidlen = strlen (vm->uuid);
520
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);
530       return;
531     }
532   }
533
534   DEBUG ("not found, creating new tab");
535
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. */
540
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"));
544     return;
545   }
546
547   /*
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);
552   */
553
554   gtk_signal_connect(GTK_OBJECT(child), "delete-event",
555                      GTK_SIGNAL_FUNC(viewer_shutdown), child);
556
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);
563
564   g_signal_connect(GTK_OBJECT(child), "vnc-auth-credential",
565                    GTK_SIGNAL_FUNC(viewer_credential), NULL);
566
567   /* NB. We have to do this before adding it to the notebook. */
568   gtk_widget_show (child);
569
570   /* Choose a tab label, which MUST end with the uuid string, since
571    * we use the tab label to store uuid.
572    */
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);
576
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);
580
581   DEBUG ("finished creating new tab");
582 }
583
584 /*
585 static void viewer_grab(GtkWidget *vnc, GtkWidget *window)
586 {
587         int i;
588
589         viewer_set_title(VNC_DISPLAY(vnc), window, TRUE);
590
591         for (i = 0 ; i < LAST_MENU; i++) {
592                 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].grabbed_text);
593         }
594 }
595
596 static void viewer_ungrab(GtkWidget *vnc, GtkWidget *window)
597 {
598         int i;
599
600         viewer_set_title(VNC_DISPLAY(vnc), window, FALSE);
601
602         for (i = 0 ; i < LAST_MENU; i++) {
603                 gtk_label_set_text_with_mnemonic(GTK_LABEL(menuItems[i].label), menuItems[i].ungrabbed_text);
604         }
605 }
606 */
607
608 static void
609 viewer_shutdown (GtkWidget *src, void *dummy, GtkWidget *vnc)
610 {
611   vnc_display_close (VNC_DISPLAY(vnc));
612
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),
616                                                    vnc));
617 }
618
619 #if 0
620 static void
621 viewer_quit (GtkWidget *src, GtkWidget *vnc)
622 {
623   viewer_shutdown (src, NULL, vnc);
624 }
625 #endif
626
627 static void
628 viewer_connected (GtkWidget *vnc)
629 {
630   DEBUG ("Connected to server");
631 }
632
633 static void
634 viewer_initialized (GtkWidget *vnc, GtkWidget *data)
635 {
636   DEBUG ("Connection initialized");
637 }
638
639 static void
640 viewer_disconnected (GtkWidget *vnc)
641 {
642   DEBUG ("Disconnected from server");
643 }
644
645 static void
646 viewer_credential (GtkWidget *vnc, GValueArray *credList)
647 {
648         GtkWidget *dialog = NULL;
649         int response;
650         unsigned int i, prompt = 0;
651         const char **data;
652
653         DEBUG ("Got credential request for %d credential(s)",
654                credList->n_values);
655
656         data = g_new0(const char *, credList->n_values);
657
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:
663                         prompt++;
664                         break;
665                 case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
666                         data[i] = "libvirt";
667                 default:
668                         break;
669                 }
670         }
671
672         if (prompt) {
673                 GtkWidget **label, **entry, *box, *vbox;
674                 int row;
675                 dialog = gtk_dialog_new_with_buttons("Authentication required",
676                                                      NULL,
677                                                      0,
678                                                      GTK_STOCK_CANCEL,
679                                                      GTK_RESPONSE_CANCEL,
680                                                      GTK_STOCK_OK,
681                                                      GTK_RESPONSE_OK,
682                                                      NULL);
683                 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
684
685                 box = gtk_table_new(credList->n_values, 2, FALSE);
686                 label = g_new(GtkWidget *, prompt);
687                 entry = g_new(GtkWidget *, prompt);
688
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:");
694                                 break;
695                         case VNC_DISPLAY_CREDENTIAL_PASSWORD:
696                                 label[row] = gtk_label_new("Password:");
697                                 break;
698                         default:
699                                 continue;
700                         }
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);
704
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);
707                         row++;
708                 }
709
710                 vbox = gtk_bin_get_child(GTK_BIN(dialog));
711                 gtk_container_add(GTK_CONTAINER(vbox), box);
712
713                 gtk_widget_show_all(dialog);
714                 response = gtk_dialog_run(GTK_DIALOG(dialog));
715                 gtk_widget_hide(GTK_WIDGET(dialog));
716
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]));
724                                         break;
725                                 }
726                         }
727                 }
728         }
729
730         for (i = 0 ; i < credList->n_values ; i++) {
731                 GValue *cred = g_value_array_get_nth(credList, i);
732                 if (data[i]) {
733                         if (vnc_display_set_credential(VNC_DISPLAY(vnc),
734                                                        g_value_get_enum(cred),
735                                                        data[i])) {
736                                 DEBUG("Failed to set credential type %d",
737                                       g_value_get_enum(cred));
738                                 vnc_display_close(VNC_DISPLAY(vnc));
739                         }
740                 } else {
741                         DEBUG("Unsupported credential type %d",
742                               g_value_get_enum(cred));
743                         vnc_display_close(VNC_DISPLAY(vnc));
744                 }
745         }
746
747         g_free(data);
748         if (dialog)
749                 gtk_widget_destroy(GTK_WIDGET(dialog));
750 }
751
752 #if defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME)
753
754 static int 
755 viewer_open_vnc_socket(const char* vnchost, int vncport)
756 {
757   int socketfd;
758   struct hostent *serv;
759   struct sockaddr_in serv_addr;
760
761   socketfd = socket(PF_INET, SOCK_STREAM, 0);
762   if(socketfd < 0){
763       return -1;
764   }
765
766   serv = gethostbyname(vnchost);
767   if(serv == NULL){
768       return -1;
769   }
770
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; 
774
775   if (connect(socketfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
776       return -1;
777   }
778
779   return socketfd;
780 }
781
782 #endif /* defined(HAVE_SOCKET) && defined(HAVE_CONNECT) && defined(HAVE_HTONS) && defined(HAVE_GETHOSTBYNAME) */
783
784 /* Remove all menu items from the Connect menu. */
785 static void
786 remove_menu_item (GtkWidget *menu_item, gpointer data)
787 {
788   gtk_container_remove (GTK_CONTAINER (connectmenu), menu_item);
789 }
790
791 static void
792 clear_connectmenu (void)
793 {
794   DEBUG ("clear Connect menu");
795   gtk_container_foreach (GTK_CONTAINER (connectmenu), remove_menu_item, NULL);
796 }
797
798 /* The WUI thread has changed its state to connected. */
799 gboolean
800 main_connected (gpointer data)
801 {
802   DEBUG ("connected");
803   ASSERT_IS_MAIN_THREAD ();
804
805   gtk_label_set_text (GTK_LABEL (ca_error), NULL);
806
807   gtk_widget_hide (connection_area);
808   if (!wui_thread_is_logged_in ())
809     gtk_widget_show (login_area);
810   return FALSE;
811 }
812
813 /* The WUI thread has changed its state to disconnected. */
814 gboolean
815 main_disconnected (gpointer data)
816 {
817   DEBUG ("disconnected");
818   ASSERT_IS_MAIN_THREAD ();
819
820   gtk_widget_show (connection_area);
821   gtk_widget_hide (login_area);
822
823   clear_connectmenu ();
824   gtk_menu_append (GTK_MENU (connectmenu), no_connections);
825   gtk_widget_show_all (connectmenu);
826
827   return FALSE;
828 }
829
830 /* The WUI thread has changed its state to logged in. */
831 gboolean
832 main_logged_in (gpointer data)
833 {
834   DEBUG ("logged in");
835   ASSERT_IS_MAIN_THREAD ();
836
837   gtk_widget_hide (login_area);
838   return FALSE;
839 }
840
841 /* The WUI thread has changed its state to logged out. */
842 gboolean
843 main_logged_out (gpointer data)
844 {
845   DEBUG ("logged out");
846   ASSERT_IS_MAIN_THREAD ();
847
848   if (wui_thread_is_connected ())
849     gtk_widget_show (login_area);
850   return FALSE;
851 }
852
853 /* The WUI thread has changed its state to busy. */
854 gboolean
855 main_busy (gpointer data)
856 {
857   GdkWindow *gdk_window;
858
859   DEBUG ("busy");
860   ASSERT_IS_MAIN_THREAD ();
861
862   gdk_window = gtk_widget_get_window (window);
863   if (gdk_window) {
864     gdk_window_set_cursor (gdk_window, busy_cursor);
865     gdk_flush ();
866   }
867
868   return FALSE;
869 }
870
871 /* The WUI thread has changed its state to idle. */
872 gboolean
873 main_idle (gpointer data)
874 {
875   GdkWindow *gdk_window;
876
877   DEBUG ("idle");
878   ASSERT_IS_MAIN_THREAD ();
879
880   gdk_window = gtk_widget_get_window (window);
881   if (gdk_window) {
882     gdk_window_set_cursor (gdk_window, NULL);
883     gdk_flush ();
884   }
885
886   return FALSE;
887 }
888
889 /* The WUI thread had a connection error.  This function must
890  * free the string.
891  */
892 gboolean
893 main_connection_error (gpointer _str)
894 {
895   char *str = (char *) _str;
896
897   DEBUG ("connection error: %s", str);
898   ASSERT_IS_MAIN_THREAD ();
899
900   gtk_label_set_text (GTK_LABEL (ca_error), str);
901   g_free (str);
902
903   return FALSE;
904 }
905
906 /* The WUI thread had a login error.  This function must
907  * free the string.
908  */
909 gboolean
910 main_login_error (gpointer _str)
911 {
912   char *str = (char *) _str;
913
914   DEBUG ("login error: %s", str);
915   ASSERT_IS_MAIN_THREAD ();
916
917   /*
918   gtk_label_set_text (GTK_LABEL (la_error), str);
919   */
920   g_free (str);
921
922   return FALSE;
923 }
924
925 /* The WUI thread reports a general status error.  This function
926  * must free the string.
927  */
928 gboolean
929 main_status_error (gpointer _str)
930 {
931   char *str = (char *) _str;
932
933   DEBUG ("status error: %s", str);
934   ASSERT_IS_MAIN_THREAD ();
935
936   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), statusbar_ctx);
937   gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_ctx, str);
938   g_free (str);
939
940   return FALSE;
941 }
942
943 /* The WUI thread has updated the vm list.  Here in the main thread
944  * we keep our own copy of the vmlist.
945  */
946 static GSList *vmlist = NULL;
947
948 static void add_vm_to_connectmenu (gpointer _vm, gpointer data);
949
950 gboolean
951 main_vmlist_updated (gpointer data)
952 {
953   GSList *new_vmlist;
954
955   DEBUG ("vmlist updated");
956   ASSERT_IS_MAIN_THREAD ();
957
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 ...
963      */
964     free_vmlist (vmlist);
965
966     vmlist = new_vmlist;
967
968     clear_connectmenu ();
969
970     gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist);
971
972     if (vmlist != NULL) {
973       gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist_separator);
974       g_slist_foreach (vmlist, add_vm_to_connectmenu, NULL);
975     }
976
977     /* Grrrr Gtk is stupid. */
978     gtk_widget_show_all (connectmenu);
979   }
980
981   return FALSE;
982 }
983
984 static void
985 add_vm_to_connectmenu (gpointer _vm, gpointer data)
986 {
987   struct vm *vm = (struct vm *) _vm;
988   GtkWidget *item;
989
990   DEBUG ("adding %s to Connect menu", vm->description);
991
992   item = gtk_menu_item_new_with_label (vm->description);
993   gtk_menu_append (GTK_MENU (connectmenu), item);
994
995   g_signal_connect (G_OBJECT (item), "activate",
996                     G_CALLBACK (connect_to_vm), vm);
997 }