"Finish off" this program, add manpage.
[virt-top.git] / virt-ctrl / vc_mainwindow.ml
1 (* virt-ctrl: A graphical management tool.
2    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
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 open Printf
21
22 let title = "Virtual Control"
23
24 let utf8_copyright = "\194\169"
25
26 let help_about () =
27   let gtk_version =
28     let gtk_major, gtk_minor, gtk_micro = GMain.Main.version in
29     sprintf "%d.%d.%d" gtk_major gtk_minor gtk_micro in
30   let virt_version = string_of_int (fst (Libvirt.get_version ())) in
31   let title = "About " ^ title in
32   let icon = GMisc.image () in
33   icon#set_stock `DIALOG_INFO;
34   icon#set_icon_size `DIALOG;
35   GToolbox.message_box
36     ~title
37     ~icon
38     ("Virtualization control tool (virt-ctrl) by\n" ^
39      "Richard W.M. Jones (rjones@redhat.com).\n\n" ^
40      "Copyright " ^ utf8_copyright ^ " 2007-2008 Red Hat Inc.\n\n" ^
41      "Libvirt version: " ^ virt_version ^ "\n" ^
42      "Gtk toolkit version: " ^ gtk_version)
43
44 (* Catch any exception and throw up a dialog. *)
45 let () =
46   (* A nicer exception printing function. *)
47   let string_of_exn = function
48     | Libvirt.Virterror err ->
49         "Virtualisation error: " ^ (Libvirt.Virterror.to_string err)
50     | Failure msg -> msg
51     | exn -> Printexc.to_string exn
52   in
53   GtkSignal.user_handler :=
54     fun exn ->
55       let label = string_of_exn exn in
56       prerr_endline label;
57       let title = "Error" in
58       let icon = GMisc.image () in
59       icon#set_stock `DIALOG_ERROR;
60       icon#set_icon_size `DIALOG;
61       GToolbox.message_box ~title ~icon label
62
63 let make
64     ~start_domain ~pause_domain ~resume_domain ~shutdown_domain
65     ~open_domain_details =
66   (* Create the main window. *)
67   let window = GWindow.window ~width:800 ~height:600 ~title () in
68   let vbox = GPack.vbox ~packing:window#add () in
69
70   (* Menu bar. *)
71   let menubar = GMenu.menu_bar ~packing:vbox#pack () in
72   let factory = new GMenu.factory menubar in
73   let accel_group = factory#accel_group in
74   let file_menu = factory#add_submenu "File" in
75   let help_menu = factory#add_submenu "Help" in
76
77   (* File menu. *)
78   let factory = new GMenu.factory file_menu ~accel_group in
79   let open_item = factory#add_item "Open connection ..."
80     ~key:GdkKeysyms._O in
81   ignore (factory#add_separator ());
82   let quit_item = factory#add_item "Quit" ~key:GdkKeysyms._Q in
83
84   ignore (open_item#connect#activate
85             ~callback:Vc_connection_dlg.open_connection);
86
87   (* Help menu. *)
88   let factory = new GMenu.factory help_menu ~accel_group in
89   let help_item = factory#add_item "Help" in
90   let help_about_item = factory#add_item "About ..." in
91
92   ignore (help_about_item#connect#activate ~callback:help_about);
93
94   (* The toolbar. *)
95   let toolbar = GButton.toolbar ~packing:vbox#pack () in
96   let connect_button =
97     GButton.tool_button ~label:"Connect ..." ~stock:`CONNECT
98       ~packing:toolbar#insert () in
99   ignore (GButton.separator_tool_item ~packing:toolbar#insert ());
100   let open_button =
101     GButton.tool_button ~label:"Details" ~stock:`OPEN
102       ~packing:toolbar#insert () in
103   ignore (GButton.separator_tool_item ~packing:toolbar#insert ());
104   let start_button =
105     GButton.tool_button ~label:"Start" ~stock:`ADD
106       ~packing:toolbar#insert () in
107   let pause_button =
108     GButton.tool_button ~label:"Pause" ~stock:`MEDIA_PAUSE
109       ~packing:toolbar#insert () in
110   let resume_button =
111     GButton.tool_button ~label:"Resume" ~stock:`MEDIA_PLAY
112       ~packing:toolbar#insert () in
113   let shutdown_button =
114     GButton.tool_button ~label:"Shutdown" ~stock:`STOP
115       ~packing:toolbar#insert () in
116
117   (* The treeview. *)
118   let (tree, model, columns, initial_state) =
119     Vc_connections.make_treeview
120       ~packing:(vbox#pack ~expand:true ~fill:true) () in
121
122   (* Set callbacks for the buttons. *)
123   ignore (connect_button#connect#clicked
124             ~callback:Vc_connection_dlg.open_connection);
125   ignore (open_button#connect#clicked
126             ~callback:(open_domain_details tree model columns));
127   ignore (start_button#connect#clicked
128             ~callback:(start_domain tree model columns));
129   ignore (pause_button#connect#clicked
130             ~callback:(pause_domain tree model columns));
131   ignore (resume_button#connect#clicked
132             ~callback:(resume_domain tree model columns));
133   ignore (shutdown_button#connect#clicked
134             ~callback:(shutdown_domain tree model columns));
135
136   (* Make a timeout function which is called once per second. *)
137   let state = ref initial_state in
138   let callback () =
139     (* Gc.compact is generally not safe in lablgtk programs, but
140      * is explicitly allowed in timeouts (see lablgtk README).
141      * This ensures memory is compacted regularly, but is also an
142      * excellent way to catch memory bugs in the ocaml libvirt bindings.
143      *)
144     Gc.compact ();
145
146     (* Ugh: Bug in lablgtk causes a segfault if a timeout raises an
147      * exception.  Catch and print exceptions instead.
148      *)
149     (try state := Vc_connections.repopulate tree model columns !state
150      with exn -> prerr_endline (Printexc.to_string exn));
151
152     true
153   in
154   let timeout_id = GMain.Timeout.add ~ms:1000 ~callback in
155
156   (* Quit. *)
157   let quit _ =
158     GMain.Timeout.remove timeout_id;
159     GMain.Main.quit ();
160     false
161   in
162
163   ignore (window#connect#destroy ~callback:GMain.quit);
164   ignore (window#event#connect#delete ~callback:quit);
165   ignore (quit_item#connect#activate
166             ~callback:(fun () -> ignore (quit ()); ()));
167
168   window#add_accel_group accel_group;
169
170   (* Display the window. *)
171   window#show ()