039cf3903c215acabf63ecabd690d5d0d42ef47b
[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 #include <alloca.h>
26
27 #include <glib.h>
28 #include <gtk/gtk.h>
29 #include <vncdisplay.h>
30
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34
35 #ifdef HAVE_SYS_UN_H
36 #include <sys/un.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #include <windows.h>
41 #endif
42
43 #ifndef G_THREADS_ENABLED
44 #error "This program requires GLib threads, and cannot be compiled without."
45 #endif
46
47 #include "internal.h"
48
49 /*#define HTTPS "https"*/
50 #define HTTPS "http"
51
52 gboolean debug = 0;
53
54 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
55  * configure or on the command line.
56  */
57 const char *cainfo = CAINFO;
58 gboolean check_cert = TRUE;
59
60 /* Private functions. */
61 static void start_ui (void);
62 static GtkWidget *menu_item_new (int which_menu);
63 static void connect_to_wui (GtkWidget *, gpointer);
64 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
65 static void destroy (GtkWidget *widget, gpointer data);
66
67 /* For any widgets accessed from multiple functions. */
68 static GtkWidget *connection_area;
69 static GtkWidget *ca_hostname;
70 static GtkWidget *ca_button;
71 static GtkWidget *login_area;
72 static GtkWidget *la_username;
73 static GtkWidget *la_password;
74 static GtkWidget *la_button;
75
76 /* Menus. */
77 enum menuNums {
78   FILE_MENU,
79   VIEW_MENU,
80   SEND_KEY_MENU,
81   WINDOW_MENU,
82   HELP_MENU,
83 };
84
85 struct menuItem {
86   guint menu;
87   GtkWidget *label;
88   const char *ungrabbed_text;
89   const char *grabbed_text;
90 };
91
92 static struct menuItem menuItems[] = {
93   { FILE_MENU, NULL, "_File", "File" },
94   { VIEW_MENU, NULL, "_View", "View" },
95   { SEND_KEY_MENU, NULL, "_Send Key", "Send Key" },
96   { WINDOW_MENU, NULL, "_Window", "Window" },
97   { HELP_MENU, NULL, "_Help", "Help" }
98 };
99
100 /* Window title. */
101 static const char *title = "oVirt Viewer";
102
103 /* Gtk widget styles.  Avoid installation hassles by keeping this
104  * inside the binary.  It can still be overridden by the user (who
105  * will do that?).
106  */
107 static const char *styles =
108   "\
109 style \"ovirt-viewer-yellow-box\"\n\
110 {\n\
111   bg[NORMAL] = shade (1.5, \"yellow\")\n\
112 }\n\
113 widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n\
114 ";
115
116 /* Command-line arguments. */
117 static int print_version = 0;
118
119 static const char *help_msg =
120   "Use '" PACKAGE " --help' to see a list of available command line options";
121
122 static const GOptionEntry options[] = {
123   { "cainfo", 0, 0, G_OPTION_ARG_STRING, &cainfo,
124     "set the path of the CA certificate bundle", NULL },
125   { "check-certificate", 0, 0, G_OPTION_ARG_NONE, &check_cert,
126     "if --no-check-certificate is passed we don't check the SSL certificate of the server", NULL },
127   { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
128     "turn on debugging messages", NULL },
129   { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
130     "display version and exit", NULL },
131   { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
132 };
133
134 int
135 main (int argc, char *argv[])
136 {
137   GOptionContext *context;
138   GError *error = NULL;
139
140   /* Initialize GLib threads before anything else.
141    *
142    * There is one main thread, which is used for all Gtk interactions
143    * and to keep the UI responsive, and one WUI thread.  The WUI
144    * thread is used to connect to the WUI, log in, and maintain the list
145    * of virtual machines.  The WUI thread is the only thread allowed
146    * to use the CURL library.
147    *
148    * The main thread sends instructions to the WUI thread (like "connect",
149    * "disconnect", etc.) using a simple message-passing protocol and
150    * a GAsyncQueue.
151    *
152    * The WUI thread keeps the UI updated by adding idle events which are
153    * processed in the main thread - see:
154    * http://mail.gnome.org/archives/gtk-app-devel-list/2007-March/msg00232.html
155    */
156   if (!g_thread_supported ()) {
157      g_thread_init (NULL);
158      gdk_threads_init ();
159   } else {
160      fprintf (stderr, "GLib threads not supported or not working.");
161      exit (1);
162   }
163
164   gtk_init (&argc, &argv);
165
166   /* Parse command-line options. */
167   context = g_option_context_new ("oVirt viewer");
168   g_option_context_add_main_entries (context, options, NULL);
169   g_option_context_add_group (context, gtk_get_option_group (TRUE));
170   g_option_context_add_group (context, vnc_display_get_option_group ());
171   g_option_context_parse (context, &argc, &argv, &error);
172
173   if (error) {
174     g_print ("%s\n%s\n", error->message, help_msg);
175     g_error_free (error);
176     exit (1);
177   }
178
179   if (print_version) {
180     printf ("%s %s\n", PACKAGE, VERSION);
181     exit (0);
182   }
183
184   start_wui_thread ();
185   start_ui ();
186
187   gtk_main ();
188
189   stop_wui_thread ();
190
191   exit (0);
192 }
193
194 /* Create the viewer window, menus. */
195 static void
196 start_ui (void)
197 {
198   GtkWidget *window;
199   GtkWidget *vbox;
200   GtkWidget *menubar;
201   GtkWidget *file;
202   GtkWidget *filemenu;
203   GtkWidget *view;
204   GtkWidget *viewmenu;
205   GtkWidget *sendkey;
206   GtkWidget *sendkeymenu;
207   GtkWidget *wind;
208   GtkWidget *windmenu;
209   GtkWidget *help;
210   GtkWidget *helpmenu;
211   GtkWidget *ca_vbox;
212   GtkWidget *ca_hbox;
213   GtkWidget *ca_label;
214   GtkWidget *la_hbox;
215   GtkWidget *notebook;
216
217   /* Parse styles. */
218   gtk_rc_parse_string (styles);
219
220   /* Window. */
221   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
222   gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
223   gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
224   gtk_window_set_title (GTK_WINDOW (window), title);
225
226   g_signal_connect (G_OBJECT (window), "delete_event",
227                     G_CALLBACK (delete_event), NULL);
228   g_signal_connect (G_OBJECT (window), "destroy",
229                     G_CALLBACK (destroy), NULL);
230
231   /* VBox for layout within the window. */
232   vbox = gtk_vbox_new (FALSE, 0);
233
234   /* Menubar. */
235   menubar = gtk_menu_bar_new ();
236   file = menu_item_new (FILE_MENU);
237   filemenu = gtk_menu_new ();
238   gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), filemenu);
239
240 #if 0
241   screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
242   gtk_menu_append (GTK_MENU (filemenu), screenshot);
243   g_signal_connect (screenshot, "activate",
244                     GTK_SIGNAL_FUNC (take_screenshot), NULL);
245 #endif
246
247   view = menu_item_new (VIEW_MENU);
248   viewmenu = gtk_menu_new ();
249   gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
250
251   sendkey = menu_item_new (SEND_KEY_MENU);
252   sendkeymenu = gtk_menu_new ();
253   gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
254
255   wind = menu_item_new (WINDOW_MENU);
256   windmenu = gtk_menu_new ();
257   gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
258
259   help = menu_item_new (HELP_MENU);
260   helpmenu = gtk_menu_new ();
261   gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
262
263   gtk_menu_bar_append (GTK_MENU_BAR (menubar), file);
264   gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
265   gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
266   gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
267   gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
268
269   /* For login dialogs, etc., usually invisible. */
270   connection_area = gtk_event_box_new ();
271   ca_vbox = gtk_vbox_new (FALSE, 0);
272   ca_label = gtk_label_new ("Give the name of the oVirt management server:");
273   ca_hbox = gtk_hbox_new (FALSE, 0);
274   ca_hostname = gtk_entry_new ();
275   gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
276   ca_button = gtk_button_new_with_label ("Connect");
277   gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
278   gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button,   FALSE, FALSE, 0);
279   gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label,    FALSE, FALSE, 4);
280   gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox,     TRUE,  FALSE, 4);
281   gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
282
283   gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
284
285   g_signal_connect (G_OBJECT (ca_button), "clicked",
286                     G_CALLBACK (connect_to_wui), NULL);
287
288   login_area = gtk_event_box_new ();
289   la_hbox = gtk_hbox_new (FALSE, 0);
290   la_username = gtk_entry_new ();
291   gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
292   la_password = gtk_entry_new ();
293   gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
294   gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
295   la_button = gtk_button_new_with_label ("Login");
296   gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
297   gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
298   gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
299   gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
300
301   gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
302
303   /* Tabbed notebook. */
304   notebook = gtk_notebook_new ();
305   gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
306   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
307   gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
308
309   /* Packing. */
310   gtk_container_add (GTK_CONTAINER (window), vbox);
311   gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
312                                      "expand", FALSE, NULL);
313   gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
314                                      "expand", FALSE, "fill", TRUE, NULL);
315   gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
316                                      "expand", FALSE, "fill", TRUE, NULL);
317   gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
318                                      "expand", TRUE, NULL);
319
320   /* Show widgets. */
321   gtk_widget_show_all (window);
322
323   if (wui_thread_is_connected ())
324     gtk_widget_hide (connection_area);
325   if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
326     gtk_widget_hide (login_area);
327 }
328
329 static GtkWidget *
330 menu_item_new(int which_menu)
331 {
332   GtkWidget *widget;
333   GtkWidget *label;
334   const char *text;
335
336   text = menuItems[which_menu].ungrabbed_text;
337
338   widget = gtk_menu_item_new();
339   label = g_object_new(GTK_TYPE_ACCEL_LABEL, NULL);
340   gtk_label_set_text_with_mnemonic(GTK_LABEL(label), text);
341   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
342
343   gtk_container_add(GTK_CONTAINER(widget), label);
344   gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(label), widget);
345   gtk_widget_show(label);
346
347   menuItems[which_menu].label = label;
348
349   return widget;
350 }
351
352 static gboolean
353 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
354 {
355   DEBUG ("delete_event");
356   return FALSE;
357 }
358
359 static void
360 destroy (GtkWidget *widget, gpointer data)
361 {
362   DEBUG ("destroy");
363   gtk_main_quit ();
364 }
365
366 static void
367 connect_to_wui (GtkWidget *widget, gpointer data)
368 {
369   const char *hostname;
370   char *uri;
371   int len;
372
373   hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
374   if (STREQ (hostname, "")) return;
375
376   /* https:// + hostname + /ovirt + \0 */
377   len = 8 + strlen (hostname) + 6 + 1;
378   uri = alloca (len);
379   snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
380
381   wui_thread_send_connect (uri);
382 }