Prepare for first binary release.
[guestfs-browser.git] / window.ml
1 (* Guestfs Browser.
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Printf
20
21 open Utils
22
23 module G = Guestfs
24
25 (* Main window state. *)
26 type window_state = {
27   window : GWindow.window;
28   view : Filetree.t;
29   vmcombo : GEdit.combo_box GEdit.text_combo;
30   throbber : GMisc.image;
31   throbber_static : GdkPixbuf.pixbuf;
32   statusbar : GMisc.statusbar;
33   statusbar_context : GMisc.statusbar_context;
34   progress_bar : GRange.progress_bar;
35 }
36
37 (* Set the statusbar text. *)
38 let set_statusbar ws msg =
39   ws.statusbar_context#pop ();
40   ignore (ws.statusbar_context#push msg)
41
42 (* Clear the filetree. *)
43 let clear_view ws =
44   Filetree.clear ws.view
45
46 (* Callback from Connect -> ... menu items. *)
47 let rec connect_to ws uri =
48   clear_view ws;
49   Slave.discard_command_queue ();
50   Slave.connect uri (when_connected ws uri)
51
52 (* Called back when connected to a new hypervisor. *)
53 and when_connected ws uri doms =
54   populate_vmcombo ws doms
55
56 and populate_vmcombo ws doms =
57   (* Populate the VM combo box. *)
58   let combo, (model, column) = ws.vmcombo in
59   model#clear ();
60   List.iter (
61     fun { Slave.dom_name = name } ->
62       let row = model#append () in
63       model#set ~row ~column name
64   ) doms
65
66 (* When a new domain is selected by the user, eg through vmcombo. *)
67 let rec open_domain ws name =
68   clear_view ws;
69   Slave.discard_command_queue ();
70   Slave.open_domain name (when_opened_domain ws name)
71
72 (* Called back when domain was opened successfully. *)
73 and when_opened_domain ws name data =
74   debug "when_opened_domain callback";
75   when_opened_common ws name data
76
77 (* When a set of disk images is selected by the user. *)
78 and open_disk_images ws images =
79   match images with
80   | [] -> ()
81   | images ->
82       clear_view ws;
83       Slave.discard_command_queue ();
84       Slave.open_images images (when_opened_disk_images ws images)
85
86 (* Called back when disk image(s) were opened successfully. *)
87 and when_opened_disk_images ws images data =
88   match images with
89   | [] -> ()
90   | (image, _) :: _ ->
91       debug "when_opened_disk_images callback";
92       when_opened_common ws image data
93
94 (* Common code for when_opened_domain/when_opened_disk_images. *)
95 and when_opened_common ws name data =
96   (* Dump some of the inspection data in debug messages. *)
97   List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
98     data.Slave.insp_all_filesystems;
99   List.iter (
100     fun { Slave.insp_root = root; insp_type = typ; insp_distro = distro;
101           insp_major_version = major; insp_minor_version = minor } ->
102       debug "root device %s contains %s %s %d.%d" root typ distro major minor;
103   ) data.Slave.insp_oses;
104
105   Filetree.add ws.view name data
106
107 let throbber_busy ws () =
108   (*throbber#set_pixbuf animation*)
109   (* XXX Workaround because no binding for GdkPixbufAnimation: *)
110   let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
111   ws.throbber#set_file file
112
113 let throbber_idle ws () =
114   ws.throbber#set_pixbuf ws.throbber_static
115
116 let progress ws (position, total) =
117   ws.progress_bar#set_fraction
118     (Int64.to_float position /. Int64.to_float total)
119
120 (* This is called in the main thread whenever a command fails in the
121  * slave thread.  The command queue has been cleared before this is
122  * called, so our job here is to reset the main window, and if
123  * necessary to turn the exception into an error message.
124  *)
125 let failure ws exn =
126   let title = "Error" in
127   let msg = Printexc.to_string exn in
128   debug "failure hook: %s" msg;
129   let icon = GMisc.image () in
130   icon#set_stock `DIALOG_ERROR;
131   icon#set_icon_size `DIALOG;
132   GToolbox.message_box ~title ~icon msg
133
134 let rec open_main_window () =
135   (* I prototyped the basic window layout using Glade, but have
136    * implemented it by hand to give us more flexibility.
137    *)
138   let title = "Guest Filesystem Browser" in
139   let window = GWindow.window ~width:700 ~height:700 ~title () in
140   let vbox = GPack.vbox ~packing:window#add () in
141
142   (* Menus. *)
143   let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
144     make_menubar window vbox ~packing:vbox#pack () in
145
146   (* Top toolbar. *)
147   let vmcombo, throbber, throbber_static =
148     make_toolbar ~packing:vbox#pack () in
149
150   (* Main part of display is the file tree. *)
151   let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
152
153   (* Status bar and progress bar. *)
154   let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
155   let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
156   let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
157   let statusbar_context = statusbar#new_context ~name:"Standard" in
158   ignore (statusbar_context#push title);
159
160   window#show ();
161
162   (* Construct the window_state struct. *)
163   let ws = {
164     window = window;
165     view = view;
166     vmcombo = vmcombo;
167     throbber = throbber; throbber_static = throbber_static;
168     statusbar = statusbar; statusbar_context = statusbar_context;
169     progress_bar = progress_bar
170   } in
171
172   (* Connect up the callback for menu entries etc.  These require the
173    * window_state struct in callbacks.
174    *)
175
176   (* Connect to different hypervisors. *)
177   ignore (connect_kvm_item#connect#activate
178             ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
179   ignore (connect_xen_item#connect#activate
180             ~callback:(fun () -> connect_to ws (Some "xen:///")));
181   ignore (connect_none_item#connect#activate
182             ~callback:(fun () -> connect_to ws None));
183
184   (* VM combo box when changed by the user. *)
185   let combo, (model, column) = ws.vmcombo in
186   ignore (
187     combo#connect#changed
188       ~callback:(
189         fun () ->
190           match combo#active_iter with
191           | None -> () (* nothing selected *)
192           | Some row -> open_domain ws (model#get ~row ~column)
193       )
194   );
195
196   (* Return the window_state struct. *)
197   ws
198
199 and make_menubar window vbox ~packing () =
200   let menubar = GMenu.menu_bar ~packing:vbox#pack () in
201   let factory = new GMenu.factory menubar in
202   let accel_group = factory#accel_group in
203   let connect_menu = factory#add_submenu "_Connect" in
204
205   let factory = new GMenu.factory connect_menu ~accel_group in
206   let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
207   let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
208   let connect_none_item = factory#add_item "_Connect to default hypervisor" in
209   let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
210   ignore (factory#add_separator ());
211   let open_image_item =
212     factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
213   ignore (factory#add_separator ());
214   let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
215
216   (* Quit. *)
217   let quit _ = GMain.quit (); false in
218   ignore (window#connect#destroy ~callback:GMain.quit);
219   ignore (window#event#connect#delete ~callback:quit);
220   ignore (quit_item#connect#activate
221             ~callback:(fun () -> ignore (quit ()); ()));
222
223   window#add_accel_group accel_group;
224
225   connect_kvm_item, connect_xen_item, connect_none_item,
226   connect_uri_item, open_image_item
227
228 (* Top toolbar.  In fact, not a toolbar because you don't seem to be
229  * able to put a combo box into a toolbar, so it's just an hbox for now.
230  *)
231 and make_toolbar ~packing () =
232   let hbox = GPack.hbox ~border_width:4 ~packing () in
233
234   (* Combo box for displaying virtual machine names. *)
235   hbox#pack (mklabel "Guest: ");
236   let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
237
238   (* Throbber. *)
239   let static = Throbber.static () in
240   (*let animation = Throbber.animation () in*)
241   let throbber =
242     GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
243
244   vmcombo, throbber, static
245
246 and make_filetree ~packing () =
247   let sw =
248     GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
249   Filetree.create ~packing:sw#add ()
250
251 (* Do what the user asked on the command line. *)
252 let rec run_cli_request ws = function
253   | Cmdline.Empty_window -> ()
254   | Cmdline.Open_images images ->
255       open_disk_images ws images
256   | Cmdline.Open_guest guest ->
257       (* Open libvirt connection, and in the callback open the guest. *)
258       let uri = connect_uri () in
259       Slave.connect uri (when_connected_cli_request ws guest)
260 and when_connected_cli_request ws guest doms =
261   populate_vmcombo ws doms;
262
263   (* "guest" should match a domain in "doms".  Check this and
264    * get the index of it.
265    *)
266   let rec loop i = function
267     | [] ->
268         failwith "guest %s not found (do you need to use --connect?)" guest
269     | d::ds when d = guest -> i
270     | _::ds -> loop (i+1) ds
271   in
272   let i = loop 0 (List.map (fun { Slave.dom_name = name } -> name) doms) in
273
274   let combo, _ = ws.vmcombo in
275   combo#set_active i