Daily update.
[ovirt-viewer.git] / main.c
diff --git a/main.c b/main.c
index 039cf39..0545ec4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -22,7 +22,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <alloca.h>
 
 #include <glib.h>
 #include <gtk/gtk.h>
 #include <windows.h>
 #endif
 
-#ifndef G_THREADS_ENABLED
-#error "This program requires GLib threads, and cannot be compiled without."
-#endif
-
 #include "internal.h"
 
 /*#define HTTPS "https"*/
@@ -61,17 +56,21 @@ gboolean check_cert = TRUE;
 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);
 
 /* For any widgets accessed from multiple functions. */
+static GtkWidget *window;
 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 {
@@ -102,16 +101,15 @@ 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?).
+ * 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\
-";
+  "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;
@@ -152,13 +150,19 @@ main (int argc, char *argv[])
    * 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);
-     gdk_threads_init ();
+    g_thread_init (NULL);
+#ifndef WIN32
+    gdk_threads_init ();
+#endif
   } else {
-     fprintf (stderr, "GLib threads not supported or not working.");
-     exit (1);
+    fprintf (stderr, "GLib threads not supported or not working.");
+    exit (1);
   }
 
   gtk_init (&argc, &argv);
@@ -184,6 +188,8 @@ main (int argc, char *argv[])
   start_wui_thread ();
   start_ui ();
 
+  DEBUG ("entering the Gtk main loop");
+
   gtk_main ();
 
   stop_wui_thread ();
@@ -195,7 +201,6 @@ main (int argc, char *argv[])
 static void
 start_ui (void)
 {
-  GtkWidget *window;
   GtkWidget *vbox;
   GtkWidget *menubar;
   GtkWidget *file;
@@ -214,9 +219,16 @@ start_ui (void)
   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);
@@ -274,10 +286,12 @@ start_ui (void)
   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");
@@ -300,6 +314,9 @@ start_ui (void)
 
   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);
@@ -375,8 +392,149 @@ connect_to_wui (GtkWidget *widget, gpointer data)
 
   /* https:// + hostname + /ovirt + \0 */
   len = 8 + strlen (hostname) + 6 + 1;
-  uri = alloca (len);
+  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);
+}
+
+/* The WUI thread has changed its state to connected. */
+gboolean
+main_connected (gpointer data)
+{
+  DEBUG ("connected");
+
+  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");
+  gtk_widget_show (connection_area);
+  gtk_widget_hide (login_area);
+  return FALSE;
+}
+
+/* The WUI thread has changed its state to logged in. */
+gboolean
+main_logged_in (gpointer data)
+{
+  DEBUG ("logged in");
+  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");
+  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");
+
+  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");
+
+  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);
+
+  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);
+
+  /*
+  gtk_label_set_text (GTK_LABEL (ca_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);
+
+  /*
+  gtk_label_set_text (GTK_LABEL (ca_error), str);
+  */
+  g_free (str);
+
+  return FALSE;
+}