1 /* ovirt viewer console application
2 * Copyright (C) 2008 Red Hat Inc.
3 * Written by Richard W.M. Jones <rjones@redhat.com>
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.
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.
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.
28 #include <vncdisplay.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h>
42 #ifndef G_THREADS_ENABLED
43 #error "This program requires GLib threads, and cannot be compiled without."
48 /*#define HTTPS "https"*/
53 /* Usually /etc/pki/tls/certs/ca-bundle.crt unless overridden during
54 * configure or on the command line.
56 const char *cainfo = CAINFO;
57 gboolean check_cert = TRUE;
59 /* Private functions. */
60 static void start_ui (void);
61 static GtkWidget *menu_item_new (int which_menu);
62 static void connect_to_wui (GtkWidget *, gpointer);
63 static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
64 static void destroy (GtkWidget *widget, gpointer data);
66 /* For any widgets accessed from multiple functions. */
67 static GtkWidget *connection_area;
68 static GtkWidget *ca_hostname;
69 static GtkWidget *ca_button;
70 static GtkWidget *login_area;
71 static GtkWidget *la_username;
72 static GtkWidget *la_password;
73 static GtkWidget *la_button;
87 const char *ungrabbed_text;
88 const char *grabbed_text;
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" }
100 static const char *title = "oVirt Viewer";
102 /* Gtk widget styles. Avoid installation hassles by keeping this
103 * inside the binary. It can still be overridden by the user (who
106 static const char *styles =
107 "style \"ovirt-viewer-yellow-box\"\n"
109 " bg[NORMAL] = shade (1.5, \"yellow\")\n"
111 "widget \"*.ovirt-viewer-connection-area\" style \"ovirt-viewer-yellow-box\"\n"
114 /* Command-line arguments. */
115 static int print_version = 0;
117 static const char *help_msg =
118 "Use '" PACKAGE " --help' to see a list of available command line options";
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 }
133 main (int argc, char *argv[])
135 GOptionContext *context;
136 GError *error = NULL;
138 /* Initialize GLib threads before anything else.
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.
146 * The main thread sends instructions to the WUI thread (like "connect",
147 * "disconnect", etc.) using a simple message-passing protocol and
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
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
158 if (!g_thread_supported ()) {
159 g_thread_init (NULL);
164 fprintf (stderr, "GLib threads not supported or not working.");
168 gtk_init (&argc, &argv);
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);
178 g_print ("%s\n%s\n", error->message, help_msg);
179 g_error_free (error);
184 printf ("%s %s\n", PACKAGE, VERSION);
191 DEBUG ("entering the Gtk main loop");
200 /* Create the viewer window, menus. */
212 GtkWidget *sendkeymenu;
223 DEBUG ("creating viewer windows and menus");
226 gtk_rc_parse_string (styles);
229 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
230 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
231 gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
232 gtk_window_set_title (GTK_WINDOW (window), title);
234 g_signal_connect (G_OBJECT (window), "delete_event",
235 G_CALLBACK (delete_event), NULL);
236 g_signal_connect (G_OBJECT (window), "destroy",
237 G_CALLBACK (destroy), NULL);
239 /* VBox for layout within the window. */
240 vbox = gtk_vbox_new (FALSE, 0);
243 menubar = gtk_menu_bar_new ();
244 file = menu_item_new (FILE_MENU);
245 filemenu = gtk_menu_new ();
246 gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), filemenu);
249 screenshot = gtk_menu_item_new_with_mnemonic ("_Screenshot");
250 gtk_menu_append (GTK_MENU (filemenu), screenshot);
251 g_signal_connect (screenshot, "activate",
252 GTK_SIGNAL_FUNC (take_screenshot), NULL);
255 view = menu_item_new (VIEW_MENU);
256 viewmenu = gtk_menu_new ();
257 gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu);
259 sendkey = menu_item_new (SEND_KEY_MENU);
260 sendkeymenu = gtk_menu_new ();
261 gtk_menu_item_set_submenu (GTK_MENU_ITEM (sendkey), sendkeymenu);
263 wind = menu_item_new (WINDOW_MENU);
264 windmenu = gtk_menu_new ();
265 gtk_menu_item_set_submenu (GTK_MENU_ITEM (wind), windmenu);
267 help = menu_item_new (HELP_MENU);
268 helpmenu = gtk_menu_new ();
269 gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);
271 gtk_menu_bar_append (GTK_MENU_BAR (menubar), file);
272 gtk_menu_bar_append (GTK_MENU_BAR (menubar), view);
273 gtk_menu_bar_append (GTK_MENU_BAR (menubar), sendkey);
274 gtk_menu_bar_append (GTK_MENU_BAR (menubar), wind);
275 gtk_menu_bar_append (GTK_MENU_BAR (menubar), help);
277 /* For login dialogs, etc., usually invisible. */
278 connection_area = gtk_event_box_new ();
279 ca_vbox = gtk_vbox_new (FALSE, 0);
280 ca_label = gtk_label_new ("Give the name of the oVirt management server:");
281 ca_hbox = gtk_hbox_new (FALSE, 0);
282 ca_hostname = gtk_entry_new ();
283 gtk_entry_set_width_chars (GTK_ENTRY (ca_hostname), 24);
284 ca_button = gtk_button_new_with_label ("Connect");
285 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_hostname, FALSE, FALSE, 0);
286 gtk_box_pack_start (GTK_BOX (ca_hbox), ca_button, FALSE, FALSE, 0);
287 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_label, FALSE, FALSE, 4);
288 gtk_box_pack_start (GTK_BOX (ca_vbox), ca_hbox, TRUE, FALSE, 4);
289 gtk_container_add (GTK_CONTAINER (connection_area), ca_vbox);
291 gtk_widget_set_name (connection_area, "ovirt-viewer-connection-area");
293 g_signal_connect (G_OBJECT (ca_button), "clicked",
294 G_CALLBACK (connect_to_wui), NULL);
296 login_area = gtk_event_box_new ();
297 la_hbox = gtk_hbox_new (FALSE, 0);
298 la_username = gtk_entry_new ();
299 gtk_entry_set_width_chars (GTK_ENTRY (la_username), 12);
300 la_password = gtk_entry_new ();
301 gtk_entry_set_width_chars (GTK_ENTRY (la_password), 12);
302 gtk_entry_set_visibility (GTK_ENTRY (la_password), FALSE);
303 la_button = gtk_button_new_with_label ("Login");
304 gtk_container_add (GTK_CONTAINER (la_hbox), la_username);
305 gtk_container_add (GTK_CONTAINER (la_hbox), la_password);
306 gtk_container_add (GTK_CONTAINER (la_hbox), la_button);
307 gtk_container_add (GTK_CONTAINER (login_area), la_hbox);
309 gtk_widget_set_name (login_area, "ovirt-viewer-login-area");
311 /* Tabbed notebook. */
312 notebook = gtk_notebook_new ();
313 gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_LEFT);
314 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
315 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
318 gtk_container_add (GTK_CONTAINER (window), vbox);
319 gtk_container_add_with_properties (GTK_CONTAINER (vbox), menubar,
320 "expand", FALSE, NULL);
321 gtk_container_add_with_properties (GTK_CONTAINER (vbox), connection_area,
322 "expand", FALSE, "fill", TRUE, NULL);
323 gtk_container_add_with_properties (GTK_CONTAINER (vbox), login_area,
324 "expand", FALSE, "fill", TRUE, NULL);
325 gtk_container_add_with_properties (GTK_CONTAINER (vbox), notebook,
326 "expand", TRUE, NULL);
329 gtk_widget_show_all (window);
331 if (wui_thread_is_connected ())
332 gtk_widget_hide (connection_area);
333 if (!wui_thread_is_connected () || wui_thread_is_logged_in ())
334 gtk_widget_hide (login_area);
338 menu_item_new(int which_menu)
344 text = menuItems[which_menu].ungrabbed_text;
346 widget = gtk_menu_item_new();
347 label = g_object_new(GTK_TYPE_ACCEL_LABEL, NULL);
348 gtk_label_set_text_with_mnemonic(GTK_LABEL(label), text);
349 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
351 gtk_container_add(GTK_CONTAINER(widget), label);
352 gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(label), widget);
353 gtk_widget_show(label);
355 menuItems[which_menu].label = label;
361 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
363 DEBUG ("delete_event");
368 destroy (GtkWidget *widget, gpointer data)
375 connect_to_wui (GtkWidget *widget, gpointer data)
377 const char *hostname;
381 hostname = gtk_entry_get_text (GTK_ENTRY (ca_hostname));
382 if (STREQ (hostname, "")) return;
384 /* https:// + hostname + /ovirt + \0 */
385 len = 8 + strlen (hostname) + 6 + 1;
386 uri = g_alloca (len);
387 snprintf (uri, len, HTTPS "://%s/ovirt", hostname);
389 wui_thread_send_connect (uri);