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