Update the Connect menu with vmlist.
[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_SYS_SOCKET_H
31 #include <sys/socket.h>
32 #endif
33
34 #ifdef HAVE_SYS_UN_H
35 #include <sys/un.h>
36 #endif
37
38 #ifdef HAVE_WINDOWS_H
39 #include <windows.h>
40 #endif
41
42 #include "internal.h"
43
44 /*#define HTTPS "https"*/
45 #define HTTPS "http"
46
47 gboolean debug = 0;
48
49 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
50  * configure or on the command line.
51  */
52 const char *cainfo = CAINFO;
53 gboolean check_cert = TRUE;
54
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
64 /* For any widgets accessed from multiple functions. */
65 static GtkWidget *window;
66 static GtkWidget *connectitem;
67 static GtkWidget *connectmenu;
68 static GtkWidget *no_connections;
69 static GtkWidget *refresh_vmlist;
70 static GtkWidget *refresh_vmlist_separator;
71 static GtkWidget *connection_area;
72 static GtkWidget *ca_hostname;
73 static GtkWidget *ca_button;
74 static GtkWidget *ca_error;
75 static GtkWidget *login_area;
76 static GtkWidget *la_username;
77 static GtkWidget *la_password;
78 static GtkWidget *la_button;
79 static GdkCursor *busy_cursor;
80
81 /* Menus. */
82 enum menuNums {
83   CONNECT_MENU,
84   VIEW_MENU,
85   SEND_KEY_MENU,
86   WINDOW_MENU,
87   HELP_MENU,
88 };
89
90 struct menuItem {
91   guint menu;
92   GtkWidget *label;
93   const char *ungrabbed_text;
94   const char *grabbed_text;
95 };
96
97 static struct menuItem menuItems[] = {
98   { CONNECT_MENU, NULL, "_Connect", "Connect" },
99   { VIEW_MENU, NULL, "_View", "View" },
100   { SEND_KEY_MENU, NULL, "_Send Key", "Send Key" },
101   { WINDOW_MENU, NULL, "_Window", "Window" },
102   { HELP_MENU, NULL, "_Help", "Help" }
103 };
104
105 /* Window title. */
106 static const char *title = "oVirt Viewer";
107
108 /* Gtk widget styles.  Avoid installation hassles by keeping this
109  * inside the binary.  It can still be overridden by the user (who
110  * will do that?)
111  */
112 static const char *styles =
113   "style \"ovirt-viewer-yellow-box\"\n"
114   "{\n"
115   "  bg[NORMAL] = shade (1.5, \"yellow\")\n"
116   "}\n"
117   "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
118   ;
119
120 /* Command-line arguments. */
121 static int print_version = 0;
122
123 static const char *help_msg =
124   "Use '" PACKAGE " --help' to see a list of available command line options";
125
126 static const GOptionEntry options[] = {
127   { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
128     "set the path of the CA certificate bundle", NULL },
129   { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
130     "if --no-check-certificate is passed we don't check the SSL certificate of the server", NULL },
131   { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
132     "turn on debugging messages", NULL },
133   { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
134     "display version and exit", NULL },
135   { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
136 };
137
138 int
139 main (int argc, char *argv[])
140 {
141   GOptionContext *context;
142   GError *error = NULL;
143
144   /* Initialize GLib threads before anything else.
145    *
146    * There is one main thread, which is used for all Gtk interactions
147    * and to keep the UI responsive, and one WUI thread.  The WUI
148    * thread is used to connect to the WUI, log in, and maintain the list
149    * of virtual machines.  The WUI thread is the only thread allowed
150    * to use the CURL library.
151    *
152    * The main thread sends instructions to the WUI thread (like "connect",
153    * "disconnect", etc.) using a simple message-passing protocol and
154    * a GAsyncQueue.
155    *
156    * The WUI thread keeps the UI updated by adding idle events which are
157    * processed in the main thread - see:
158    * http://mail.gnome.org/archives/gtk-app-devel-list/2007-March/msg00232.html
159    *
160    * Note that under Win32 you must confine all Gtk/Gdk interactions
161    * to a single thread - see:
162    * http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html
163    */
164   if (!g_thread_supported ()) {
165     g_thread_init (NULL);
166 #ifndef WIN32
167     gdk_threads_init ();
168 #endif
169   } else {
170     fprintf (stderr, "GLib threads not supported or not working.");
171     exit (1);
172   }
173
174   gtk_init (&argc, &argv);
175
176   /* Parse command-line options. */
177   context = g_option_context_new ("oVirt viewer");
178   g_option_context_add_main_entries (context, options, NULL);
179   g_option_context_add_group (context, gtk_get_option_group (TRUE));
180   g_option_context_add_group (context, vnc_display_get_option_group ());
181   g_option_context_parse (context, &argc, &argv, &error);
182
183   if (error) {
184     g_print ("%s\n%s\n", error->message, help_msg);
185     g_error_free (error);
186     exit (1);
187   }
188
189   if (print_version) {
190     printf ("%s %s\n", PACKAGE, VERSION);
191     exit (0);
192   }
193
194   start_wui_thread ();
195   start_ui ();
196
197   DEBUG ("entering the Gtk main loop");
198
199   gtk_main ();
200
201   stop_wui_thread ();
202
203   exit (0);
204 }
205
206 /* Create the viewer window, menus. */
207 static void
208 start_ui (void)
209 {
210   GtkWidget *vbox;
211   GtkWidget *menubar;
212   GtkWidget *view;
213   GtkWidget *viewmenu;
214   GtkWidget *sendkey;
215   GtkWidget *sendkeymenu;
216   GtkWidget *wind;
217   GtkWidget *windmenu;
218   GtkWidget *help;
219   GtkWidget *helpmenu;
220   GtkWidget *ca_vbox;
221   GtkWidget *ca_hbox;
222   GtkWidget *ca_label;
223   GtkWidget *la_hbox;
224   GtkWidget *notebook;
225
226   DEBUG ("creating viewer windows and menus");
227
228   /* Parse styles. */
229   gtk_rc_parse_string (styles);
230
231   /* Busy cursor, used by main_busy() function.
232    * XXX This cursor is crap - how can we use the Bluecurve/theme cursor?
233    */
234   busy_cursor = gdk_cursor_new (GDK_WATCH);
235
236   /* Window. */
237   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
238   gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
239   gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
240   gtk_window_set_title (GTK_WINDOW (window), title);
241
242   g_signal_connect (G_OBJECT (window), "delete_event",
243                     G_CALLBACK (delete_event), NULL);
244   g_signal_connect (G_OBJECT (window), "destroy",
245                     G_CALLBACK (destroy), NULL);
246
247   /* VBox for layout within the window. */
248   vbox = gtk_vbox_new (FALSE, 0);
249
250   /* Menubar. */
251   menubar = gtk_menu_bar_new ();
252   connectitem = menu_item_new (CONNECT_MENU);
253   connectmenu = gtk_menu_new ();
254   gtk_menu_item_set_submenu (GTK_MENU_ITEM (connectitem), connectmenu);
255
256   no_connections = gtk_menu_item_new_with_label ("Not connected");
257   gtk_menu_append (GTK_MENU (connectmenu), no_connections);
258   gtk_widget_set_sensitive (no_connections, FALSE);
259
260   /* This is not added to the menu yet, but will be when we are
261    * connected.
262    */
263   refresh_vmlist =
264     gtk_menu_item_new_with_label ("Refresh list of virtual machines");
265   g_object_ref (refresh_vmlist);
266   refresh_vmlist_separator = gtk_separator_menu_item_new ();
267   g_object_ref (refresh_vmlist_separator);
268
269 #if 0
270   screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
271   gtk_menu_append (GTK_MENU (filemenu), screenshot);
272   g_signal_connect (screenshot, "activate",
273                     GTK_SIGNAL_FUNC (take_screenshot), NULL);
274 #endif
275
276   view = menu_item_new (VIEW_MENU);
277   viewmenu = gtk_menu_new ();
278   gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
279
280   sendkey = menu_item_new (SEND_KEY_MENU);
281   sendkeymenu = gtk_menu_new ();
282   gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
283
284   wind = menu_item_new (WINDOW_MENU);
285   windmenu = gtk_menu_new ();
286   gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
287
288   help = menu_item_new (HELP_MENU);
289   helpmenu = gtk_menu_new ();
290   gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
291
292   gtk_menu_bar_append (GTK_MENU_BAR (menubar), connectitem);
293   gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
294   gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
295   gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
296   gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
297
298   /* For login dialogs, etc., usually invisible. */
299   connection_area = gtk_event_box_new ();
300   ca_vbox = gtk_vbox_new (FALSE, 0);
301   ca_label = gtk_label_new ("Give the name of the oVirt management server:");
302   ca_hbox = gtk_hbox_new (FALSE, 0);
303   ca_hostname = gtk_entry_new ();
304   gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
305   ca_button = gtk_button_new_with_label ("Connect");
306   ca_error = gtk_label_new (NULL);
307   gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
308   gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button,   FALSE, FALSE, 0);
309   gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label,    FALSE, FALSE, 4);
310   gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox,     TRUE,  FALSE, 4);
311   gtk_box_pack_start (GTK_BOX (ca_vbox), ca_error,    TRUE,  FALSE, 4);
312   gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
313
314   gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
315
316   g_signal_connect (G_OBJECT (ca_button), "clicked",
317                     G_CALLBACK (connect_to_wui), NULL);
318
319   login_area = gtk_event_box_new ();
320   la_hbox = gtk_hbox_new (FALSE, 0);
321   la_username = gtk_entry_new ();
322   gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
323   la_password = gtk_entry_new ();
324   gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
325   gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
326   la_button = gtk_button_new_with_label ("Login");
327   gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
328   gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
329   gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
330   gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
331
332   gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
333
334   g_signal_connect (G_OBJECT (la_button), "clicked",
335                     G_CALLBACK (login_to_wui), NULL);
336
337   /* Tabbed notebook. */
338   notebook = gtk_notebook_new ();
339   gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
340   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
341   gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
342
343   /* Packing. */
344   gtk_container_add (GTK_CONTAINER (window), vbox);
345   gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
346                                      "expand", FALSE, NULL);
347   gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
348                                      "expand", FALSE, "fill", TRUE, NULL);
349   gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
350                                      "expand", FALSE, "fill", TRUE, NULL);
351   gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
352                                      "expand", TRUE, NULL);
353
354   /* Show widgets. */
355   gtk_widget_show_all (window);
356
357   if (wui_thread_is_connected ())
358     gtk_widget_hide (connection_area);
359   if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
360     gtk_widget_hide (login_area);
361 }
362
363 static GtkWidget *
364 menu_item_new (int which_menu)
365 {
366   GtkWidget *widget;
367   GtkWidget *label;
368   const char *text;
369
370   text = menuItems[which_menu].ungrabbed_text;
371
372   widget = gtk_menu_item_new ();
373   label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
374   gtk_label_set_text_with_mnemonic (GTK_LABEL (label), text);
375   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
376
377   gtk_container_add (GTK_CONTAINER (widget), label);
378   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), widget);
379   gtk_widget_show (label);
380
381   menuItems[which_menu].label = label;
382
383   return widget;
384 }
385
386 static gboolean
387 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
388 {
389   DEBUG ("delete_event");
390   return FALSE;
391 }
392
393 static void
394 destroy (GtkWidget *widget, gpointer data)
395 {
396   DEBUG ("destroy");
397   gtk_main_quit ();
398 }
399
400 static void
401 connect_to_wui (GtkWidget *widget, gpointer data)
402 {
403   const char *hostname;
404   char *uri;
405   int len;
406
407   hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
408   if (STREQ (hostname, "")) return;
409
410   /* https:// + hostname + /ovirt + \0 */
411   len = 8 + strlen (hostname) + 6 + 1;
412   uri = g_alloca (len);
413   snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
414
415   wui_thread_send_connect (uri);
416 }
417
418 static void
419 login_to_wui (GtkWidget *widget, gpointer data)
420 {
421   const char *username, *password;
422
423   username = gtk_entry_get_text (GTK_ENTRY (la_username));
424   if (STREQ (username, "")) return;
425   password = gtk_entry_get_text (GTK_ENTRY (la_password));
426
427   wui_thread_send_login (username, password);
428 }
429
430 /* Remove all menu items from the Connect menu. */
431 static void
432 remove_menu_item (GtkWidget *menu_item, gpointer data)
433 {
434   gtk_container_remove (GTK_CONTAINER (connectmenu), menu_item);
435 }
436
437 static void
438 clear_connectmenu (void)
439 {
440   DEBUG ("clear Connect menu");
441   gtk_container_foreach (GTK_CONTAINER (connectmenu), remove_menu_item, NULL);
442 }
443
444 /* The WUI thread has changed its state to connected. */
445 gboolean
446 main_connected (gpointer data)
447 {
448   DEBUG ("connected");
449   ASSERT_IS_MAIN_THREAD ();
450
451   gtk_label_set_text (GTK_LABEL (ca_error), NULL);
452
453   gtk_widget_hide (connection_area);
454   if (!wui_thread_is_logged_in ())
455     gtk_widget_show (login_area);
456   return FALSE;
457 }
458
459 /* The WUI thread has changed its state to disconnected. */
460 gboolean
461 main_disconnected (gpointer data)
462 {
463   DEBUG ("disconnected");
464   ASSERT_IS_MAIN_THREAD ();
465
466   gtk_widget_show (connection_area);
467   gtk_widget_hide (login_area);
468
469   clear_connectmenu ();
470   gtk_menu_append (GTK_MENU (connectmenu), no_connections);
471   gtk_widget_show_all (connectmenu);
472
473   return FALSE;
474 }
475
476 /* The WUI thread has changed its state to logged in. */
477 gboolean
478 main_logged_in (gpointer data)
479 {
480   DEBUG ("logged in");
481   ASSERT_IS_MAIN_THREAD ();
482
483   gtk_widget_hide (login_area);
484   return FALSE;
485 }
486
487 /* The WUI thread has changed its state to logged out. */
488 gboolean
489 main_logged_out (gpointer data)
490 {
491   DEBUG ("logged out");
492   ASSERT_IS_MAIN_THREAD ();
493
494   if (wui_thread_is_connected ())
495     gtk_widget_show (login_area);
496   return FALSE;
497 }
498
499 /* The WUI thread has changed its state to busy. */
500 gboolean
501 main_busy (gpointer data)
502 {
503   GdkWindow *gdk_window;
504
505   DEBUG ("busy");
506   ASSERT_IS_MAIN_THREAD ();
507
508   gdk_window = gtk_widget_get_window (window);
509   if (gdk_window) {
510     gdk_window_set_cursor (gdk_window, busy_cursor);
511     gdk_flush ();
512   }
513
514   return FALSE;
515 }
516
517 /* The WUI thread has changed its state to idle. */
518 gboolean
519 main_idle (gpointer data)
520 {
521   GdkWindow *gdk_window;
522
523   DEBUG ("idle");
524   ASSERT_IS_MAIN_THREAD ();
525
526   gdk_window = gtk_widget_get_window (window);
527   if (gdk_window) {
528     gdk_window_set_cursor (gdk_window, NULL);
529     gdk_flush ();
530   }
531
532   return FALSE;
533 }
534
535 /* The WUI thread had a connection error.  This function must
536  * free the string.
537  */
538 gboolean
539 main_connection_error (gpointer _str)
540 {
541   char *str = (char *) _str;
542
543   DEBUG ("connection error: %s", str);
544   ASSERT_IS_MAIN_THREAD ();
545
546   gtk_label_set_text (GTK_LABEL (ca_error), str);
547   g_free (str);
548
549   return FALSE;
550 }
551
552 /* The WUI thread had a login error.  This function must
553  * free the string.
554  */
555 gboolean
556 main_login_error (gpointer _str)
557 {
558   char *str = (char *) _str;
559
560   DEBUG ("login error: %s", str);
561   ASSERT_IS_MAIN_THREAD ();
562
563   /*
564   gtk_label_set_text (GTK_LABEL (la_error), str);
565   */
566   g_free (str);
567
568   return FALSE;
569 }
570
571 /* The WUI thread reports a general status error.  This function
572  * must free the string.
573  */
574 gboolean
575 main_status_error (gpointer _str)
576 {
577   char *str = (char *) _str;
578
579   DEBUG ("status error: %s", str);
580   ASSERT_IS_MAIN_THREAD ();
581
582   /*
583   gtk_label_set_text (GTK_LABEL (...), str);
584   */
585   g_free (str);
586
587   return FALSE;
588 }
589
590 /* The WUI thread has updated the vm list. */
591 static void
592 add_vm_to_connectmenu (gpointer _vm, gpointer data)
593 {
594   struct vm *vm = (struct vm *) _vm;
595   GtkWidget *item;
596
597   DEBUG ("adding %s to Connect menu", vm->description);
598
599   item = gtk_menu_item_new_with_label (vm->description);
600   gtk_menu_append (GTK_MENU (connectmenu), item);
601 }
602
603 gboolean
604 main_vmlist_updated (gpointer data)
605 {
606   GSList *vmlist;
607
608   DEBUG ("vmlist updated");
609   ASSERT_IS_MAIN_THREAD ();
610
611   /* Get the new vmlist. */
612   if (wui_thread_get_vmlist (&vmlist)) {
613     clear_connectmenu ();
614
615     gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist);
616
617     if (vmlist != NULL) {
618       gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist_separator);
619       g_slist_foreach (vmlist, add_vm_to_connectmenu, NULL);
620     }
621     free_vmlist (vmlist);
622
623     /* Grrrr Gtk is stupid. */
624     gtk_widget_show_all (connectmenu);
625   }
626
627   return FALSE;
628 }