+
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+
+#ifdef HAVE_WINDOWS_H
+#include <windows.h>
+#endif
+
+#include "internal.h"
+
+/*#define HTTPS "https"*/
+#define HTTPS "http"
+
+gboolean debug = 0;
+
+/* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
+ * configure or on the command line.
+ */
+const char *cainfo = CAINFO;
+gboolean check_cert = TRUE;
+
+/* Private functions. */
+static void start_ui (void);
+static GtkWidget *menu_item_new (int which_menu);
+static void connect_to_wui (GtkWidget *, gpointer);
+static void login_to_wui (GtkWidget *, gpointer);
+static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
+static void destroy (GtkWidget *widget, gpointer data);
+static void clear_connectmenu (void);
+
+/* For any widgets accessed from multiple functions. */
+static GtkWidget *window;
+static GtkWidget *connectitem;
+static GtkWidget *connectmenu;
+static GtkWidget *no_connections;
+static GtkWidget *refresh_vmlist;
+static GtkWidget *refresh_vmlist_separator;
+static GtkWidget *connection_area;
+static GtkWidget *ca_hostname;
+static GtkWidget *ca_button;
+static GtkWidget *ca_error;
+static GtkWidget *login_area;
+static GtkWidget *la_username;
+static GtkWidget *la_password;
+static GtkWidget *la_button;
+static GdkCursor *busy_cursor;
+
+/* Menus. */
+enum menuNums {
+ CONNECT_MENU,
+ VIEW_MENU,
+ SEND_KEY_MENU,
+ WINDOW_MENU,
+ HELP_MENU,
+};
+
+struct menuItem {
+ guint menu;
+ GtkWidget *label;
+ const char *ungrabbed_text;
+ const char *grabbed_text;
+};
+
+static struct menuItem menuItems[] = {
+ { CONNECT_MENU, NULL, "_Connect", "Connect" },
+ { VIEW_MENU, NULL, "_View", "View" },
+ { SEND_KEY_MENU, NULL, "_Send Key", "Send Key" },
+ { WINDOW_MENU, NULL, "_Window", "Window" },
+ { HELP_MENU, NULL, "_Help", "Help" }
+};
+
+/* Window title. */
+static const char *title = "oVirt Viewer";
+
+/* Gtk widget styles. Avoid installation hassles by keeping this
+ * inside the binary. It can still be overridden by the user (who
+ * will do that?)
+ */
+static const char *styles =
+ "style \"ovirt-viewer-yellow-box\"\n"
+ "{\n"
+ " bg[NORMAL] = shade (1.5, \"yellow\")\n"
+ "}\n"
+ "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
+ ;
+
+/* Command-line arguments. */
+static int print_version = 0;
+
+static const char *help_msg =
+ "Use '" PACKAGE " --help' to see a list of available command line options";
+
+static const GOptionEntry options[] = {
+ { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
+ "set the path of the CA certificate bundle", NULL },
+ { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
+ "if --no-check-certificate is passed we don't check the SSL certificate of the server", NULL },
+ { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
+ "turn on debugging messages", NULL },
+ { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
+ "display version and exit", NULL },
+ { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
+};
+
+int
+main (int argc, char *argv[])
+{
+ GOptionContext *context;
+ GError *error = NULL;
+
+ /* Initialize GLib threads before anything else.
+ *
+ * There is one main thread, which is used for all Gtk interactions
+ * and to keep the UI responsive, and one WUI thread. The WUI
+ * thread is used to connect to the WUI, log in, and maintain the list
+ * of virtual machines. The WUI thread is the only thread allowed
+ * to use the CURL library.
+ *
+ * The main thread sends instructions to the WUI thread (like "connect",
+ * "disconnect", etc.) using a simple message-passing protocol and
+ * a GAsyncQueue.
+ *
+ * The WUI thread keeps the UI updated by adding idle events which are
+ * processed in the main thread - see:
+ * http://mail.gnome.org/archives/gtk-app-devel-list/2007-March/msg00232.html
+ *
+ * Note that under Win32 you must confine all Gtk/Gdk interactions
+ * to a single thread - see:
+ * http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html
+ */
+ if (!g_thread_supported ()) {
+ g_thread_init (NULL);
+#ifndef WIN32
+ gdk_threads_init ();
+#endif
+ } else {
+ fprintf (stderr, "GLib threads not supported or not working.");
+ exit (1);
+ }
+
+ gtk_init (&argc, &argv);
+
+ /* Parse command-line options. */
+ context = g_option_context_new ("oVirt viewer");
+ g_option_context_add_main_entries (context, options, NULL);
+ g_option_context_add_group (context, gtk_get_option_group (TRUE));
+ g_option_context_add_group (context, vnc_display_get_option_group ());
+ g_option_context_parse (context, &argc, &argv, &error);
+
+ if (error) {
+ g_print ("%s\n%s\n", error->message, help_msg);
+ g_error_free (error);
+ exit (1);
+ }
+
+ if (print_version) {
+ printf ("%s %s\n", PACKAGE, VERSION);
+ exit (0);
+ }
+
+ start_wui_thread ();
+ start_ui ();
+
+ DEBUG ("entering the Gtk main loop");
+
+ gtk_main ();
+
+ stop_wui_thread ();
+
+ exit (0);
+}
+
+/* Create the viewer window, menus. */
+static void
+start_ui (void)
+{
+ GtkWidget *vbox;
+ GtkWidget *menubar;
+ GtkWidget *view;
+ GtkWidget *viewmenu;
+ GtkWidget *sendkey;
+ GtkWidget *sendkeymenu;
+ GtkWidget *wind;
+ GtkWidget *windmenu;
+ GtkWidget *help;
+ GtkWidget *helpmenu;
+ GtkWidget *ca_vbox;
+ GtkWidget *ca_hbox;
+ GtkWidget *ca_label;
+ GtkWidget *la_hbox;
+ GtkWidget *notebook;
+
+ DEBUG ("creating viewer windows and menus");
+
+ /* Parse styles. */
+ gtk_rc_parse_string (styles);
+
+ /* Busy cursor, used by main_busy() function.
+ * XXX This cursor is crap - how can we use the Bluecurve/theme cursor?
+ */
+ busy_cursor = gdk_cursor_new (GDK_WATCH);
+
+ /* Window. */
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
+ gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
+ gtk_window_set_title (GTK_WINDOW (window), title);
+
+ g_signal_connect (G_OBJECT (window), "delete_event",
+ G_CALLBACK (delete_event), NULL);
+ g_signal_connect (G_OBJECT (window), "destroy",
+ G_CALLBACK (destroy), NULL);
+
+ /* VBox for layout within the window. */
+ vbox = gtk_vbox_new (FALSE, 0);
+
+ /* Menubar. */
+ menubar = gtk_menu_bar_new ();
+ connectitem = menu_item_new (CONNECT_MENU);
+ connectmenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (connectitem), connectmenu);
+
+ no_connections = gtk_menu_item_new_with_label ("Not connected");
+ gtk_menu_append (GTK_MENU (connectmenu), no_connections);
+ gtk_widget_set_sensitive (no_connections, FALSE);
+
+ /* This is not added to the menu yet, but will be when we are
+ * connected.
+ */
+ refresh_vmlist =
+ gtk_menu_item_new_with_label ("Refresh list of virtual machines");
+ g_object_ref (refresh_vmlist);
+ refresh_vmlist_separator = gtk_separator_menu_item_new ();
+ g_object_ref (refresh_vmlist_separator);
+
+#if 0
+ screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
+ gtk_menu_append (GTK_MENU (filemenu), screenshot);
+ g_signal_connect (screenshot, "activate",
+ GTK_SIGNAL_FUNC (take_screenshot), NULL);
+#endif
+
+ view = menu_item_new (VIEW_MENU);
+ viewmenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
+
+ sendkey = menu_item_new (SEND_KEY_MENU);
+ sendkeymenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
+
+ wind = menu_item_new (WINDOW_MENU);
+ windmenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
+
+ help = menu_item_new (HELP_MENU);
+ helpmenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
+
+ gtk_menu_bar_append (GTK_MENU_BAR (menubar), connectitem);
+ gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
+ gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
+ gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
+ gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
+
+ /* For login dialogs, etc., usually invisible. */
+ connection_area = gtk_event_box_new ();
+ ca_vbox = gtk_vbox_new (FALSE, 0);
+ ca_label = gtk_label_new ("Give the name of the oVirt management server:");
+ ca_hbox = gtk_hbox_new (FALSE, 0);
+ ca_hostname = gtk_entry_new ();
+ gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
+ ca_button = gtk_button_new_with_label ("Connect");
+ ca_error = gtk_label_new (NULL);
+ gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button, FALSE, FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label, FALSE, FALSE, 4);
+ gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox, TRUE, FALSE, 4);
+ gtk_box_pack_start (GTK_BOX (ca_vbox), ca_error, TRUE, FALSE, 4);
+ gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
+
+ gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
+
+ g_signal_connect (G_OBJECT (ca_button), "clicked",
+ G_CALLBACK (connect_to_wui), NULL);
+
+ login_area = gtk_event_box_new ();
+ la_hbox = gtk_hbox_new (FALSE, 0);
+ la_username = gtk_entry_new ();
+ gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
+ la_password = gtk_entry_new ();
+ gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
+ gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
+ la_button = gtk_button_new_with_label ("Login");
+ gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
+ gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
+ gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
+ gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
+
+ gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
+
+ g_signal_connect (G_OBJECT (la_button), "clicked",
+ G_CALLBACK (login_to_wui), NULL);
+
+ /* Tabbed notebook. */
+ notebook = gtk_notebook_new ();
+ gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
+ gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
+ gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
+
+ /* Packing. */
+ gtk_container_add (GTK_CONTAINER (window), vbox);
+ gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
+ "expand", FALSE, NULL);
+ gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
+ "expand", FALSE, "fill", TRUE, NULL);
+ gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
+ "expand", FALSE, "fill", TRUE, NULL);
+ gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
+ "expand", TRUE, NULL);
+
+ /* Show widgets. */
+ gtk_widget_show_all (window);
+
+ if (wui_thread_is_connected ())
+ gtk_widget_hide (connection_area);
+ if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
+ gtk_widget_hide (login_area);
+}
+
+static GtkWidget *
+menu_item_new (int which_menu)
+{
+ GtkWidget *widget;
+ GtkWidget *label;
+ const char *text;
+
+ text = menuItems[which_menu].ungrabbed_text;
+
+ widget = gtk_menu_item_new ();
+ label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
+ gtk_label_set_text_with_mnemonic (GTK_LABEL (label), text);
+ gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+
+ gtk_container_add (GTK_CONTAINER (widget), label);
+ gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), widget);
+ gtk_widget_show (label);
+
+ menuItems[which_menu].label = label;
+
+ return widget;
+}
+
+static gboolean
+delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
+{
+ DEBUG ("delete_event");
+ return FALSE;
+}
+
+static void
+destroy (GtkWidget *widget, gpointer data)
+{
+ DEBUG ("destroy");
+ gtk_main_quit ();
+}
+
+static void
+connect_to_wui (GtkWidget *widget, gpointer data)
+{
+ const char *hostname;
+ char *uri;
+ int len;
+
+ hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
+ if (STREQ (hostname, "")) return;
+
+ /* https:// + hostname + /ovirt + \0 */
+ len = 8 + strlen (hostname) + 6 + 1;
+ uri = g_alloca (len);
+ snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
+
+ wui_thread_send_connect (uri);
+}
+
+static void
+login_to_wui (GtkWidget *widget, gpointer data)
+{
+ const char *username, *password;
+
+ username = gtk_entry_get_text (GTK_ENTRY (la_username));
+ if (STREQ (username, "")) return;
+ password = gtk_entry_get_text (GTK_ENTRY (la_password));
+
+ wui_thread_send_login (username, password);
+}
+
+/* Remove all menu items from the Connect menu. */
+static void
+remove_menu_item (GtkWidget *menu_item, gpointer data)
+{
+ gtk_container_remove (GTK_CONTAINER (connectmenu), menu_item);
+}
+
+static void
+clear_connectmenu (void)
+{
+ DEBUG ("clear Connect menu");
+ gtk_container_foreach (GTK_CONTAINER (connectmenu), remove_menu_item, NULL);
+}
+
+/* The WUI thread has changed its state to connected. */
+gboolean
+main_connected (gpointer data)
+{
+ DEBUG ("connected");
+ ASSERT_IS_MAIN_THREAD ();
+
+ gtk_label_set_text (GTK_LABEL (ca_error), NULL);
+
+ gtk_widget_hide (connection_area);
+ if (!wui_thread_is_logged_in ())
+ gtk_widget_show (login_area);
+ return FALSE;
+}
+
+/* The WUI thread has changed its state to disconnected. */
+gboolean
+main_disconnected (gpointer data)
+{
+ DEBUG ("disconnected");
+ ASSERT_IS_MAIN_THREAD ();
+
+ gtk_widget_show (connection_area);
+ gtk_widget_hide (login_area);
+
+ clear_connectmenu ();
+ gtk_menu_append (GTK_MENU (connectmenu), no_connections);
+ gtk_widget_show_all (connectmenu);
+
+ return FALSE;
+}
+
+/* The WUI thread has changed its state to logged in. */
+gboolean
+main_logged_in (gpointer data)
+{
+ DEBUG ("logged in");
+ ASSERT_IS_MAIN_THREAD ();
+
+ gtk_widget_hide (login_area);
+ return FALSE;
+}
+
+/* The WUI thread has changed its state to logged out. */
+gboolean
+main_logged_out (gpointer data)
+{
+ DEBUG ("logged out");
+ ASSERT_IS_MAIN_THREAD ();
+
+ if (wui_thread_is_connected ())
+ gtk_widget_show (login_area);
+ return FALSE;
+}
+
+/* The WUI thread has changed its state to busy. */
+gboolean
+main_busy (gpointer data)
+{
+ GdkWindow *gdk_window;
+
+ DEBUG ("busy");
+ ASSERT_IS_MAIN_THREAD ();
+
+ gdk_window = gtk_widget_get_window (window);
+ if (gdk_window) {
+ gdk_window_set_cursor (gdk_window, busy_cursor);
+ gdk_flush ();
+ }
+
+ return FALSE;
+}
+
+/* The WUI thread has changed its state to idle. */
+gboolean
+main_idle (gpointer data)
+{
+ GdkWindow *gdk_window;
+
+ DEBUG ("idle");
+ ASSERT_IS_MAIN_THREAD ();
+
+ gdk_window = gtk_widget_get_window (window);
+ if (gdk_window) {
+ gdk_window_set_cursor (gdk_window, NULL);
+ gdk_flush ();
+ }
+
+ return FALSE;
+}
+
+/* The WUI thread had a connection error. This function must
+ * free the string.
+ */
+gboolean
+main_connection_error (gpointer _str)
+{
+ char *str = (char *) _str;
+
+ DEBUG ("connection error: %s", str);
+ ASSERT_IS_MAIN_THREAD ();
+
+ gtk_label_set_text (GTK_LABEL (ca_error), str);
+ g_free (str);
+
+ return FALSE;
+}
+
+/* The WUI thread had a login error. This function must
+ * free the string.
+ */
+gboolean
+main_login_error (gpointer _str)
+{
+ char *str = (char *) _str;
+
+ DEBUG ("login error: %s", str);
+ ASSERT_IS_MAIN_THREAD ();
+
+ /*
+ gtk_label_set_text (GTK_LABEL (la_error), str);
+ */
+ g_free (str);
+
+ return FALSE;
+}
+
+/* The WUI thread reports a general status error. This function
+ * must free the string.
+ */
+gboolean
+main_status_error (gpointer _str)
+{
+ char *str = (char *) _str;
+
+ DEBUG ("status error: %s", str);
+ ASSERT_IS_MAIN_THREAD ();
+
+ /*
+ gtk_label_set_text (GTK_LABEL (...), str);
+ */
+ g_free (str);
+
+ return FALSE;
+}
+
+/* The WUI thread has updated the vm list. */
+static void
+add_vm_to_connectmenu (gpointer _vm, gpointer data)
+{
+ struct vm *vm = (struct vm *) _vm;
+ GtkWidget *item;
+
+ DEBUG ("adding %s to Connect menu", vm->description);
+
+ item = gtk_menu_item_new_with_label (vm->description);
+ gtk_menu_append (GTK_MENU (connectmenu), item);
+}
+
+gboolean
+main_vmlist_updated (gpointer data)
+{
+ GSList *vmlist;
+
+ DEBUG ("vmlist updated");
+ ASSERT_IS_MAIN_THREAD ();
+
+ /* Get the new vmlist. */
+ if (wui_thread_get_vmlist (&vmlist)) {
+ clear_connectmenu ();
+
+ gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist);
+
+ if (vmlist != NULL) {
+ gtk_menu_append (GTK_MENU (connectmenu), refresh_vmlist_separator);
+ g_slist_foreach (vmlist, add_vm_to_connectmenu, NULL);
+ }
+ free_vmlist (vmlist);
+
+ /* Grrrr Gtk is stupid. */
+ gtk_widget_show_all (connectmenu);
+ }
+
+ return FALSE;
+}