aa33b79f9ef1645173c67ce50ad57faf65505e69
[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 open Slave_types
23
24 module G = Guestfs
25
26 let make_menubar window (vbox : GPack.box) ~packing () =
27   let menubar = GMenu.menu_bar ~packing:vbox#pack () in
28   let factory = new GMenu.factory menubar in
29   let accel_group = factory#accel_group in
30   let connect_menu = factory#add_submenu "_Connect" in
31
32   let factory = new GMenu.factory connect_menu ~accel_group in
33   let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
34   let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
35   let connect_none_item = factory#add_item "_Connect to default hypervisor" in
36   let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
37   ignore (factory#add_separator ());
38   let open_image_item =
39     factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
40   ignore (factory#add_separator ());
41   let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
42
43   (* Quit. *)
44   let quit _ = GMain.quit (); false in
45   ignore (window#connect#destroy ~callback:GMain.quit);
46   ignore (window#event#connect#delete ~callback:quit);
47   ignore (quit_item#connect#activate
48             ~callback:(fun () -> ignore (quit ()); ()));
49
50   window#add_accel_group accel_group;
51
52   connect_kvm_item, connect_xen_item, connect_none_item,
53   connect_uri_item, open_image_item
54
55 (* Top toolbar.  In fact, not a toolbar because you don't seem to be
56  * able to put a combo box into a toolbar, so it's just an hbox for now.
57  *)
58 and make_toolbar ~packing () =
59   let hbox = GPack.hbox ~border_width:4 ~packing () in
60
61   (* Combo box for displaying virtual machine names. *)
62   hbox#pack (mklabel "Guest: ");
63   let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
64
65   (* Refresh button.
66    * http://stackoverflow.com/questions/2188659/stock-icons-not-shown-on-buttons
67    *)
68   let refresh_button =
69     let image = GMisc.image ~stock:`REFRESH () in
70     let b = GButton.button ~packing:hbox#pack () in
71     b#set_image (image :> GObj.widget);
72     b in
73
74   (* Throbber. *)
75   let static = Throbber.static () in
76   let animation = Throbber.animation () in
77   let throbber =
78     (* Workaround for http://caml.inria.fr/mantis/view.php?id=4732 *)
79     let from = Obj.magic 3448763 (* `END *) in
80     GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from) () in
81
82   vmcombo, refresh_button, throbber, static, animation
83
84 and make_filetree ~packing () =
85   (* Create the filetree inside a scrolled window. *)
86   let sw =
87     GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
88   let tree = new Filetree.tree ~packing:sw#add () in
89
90   (* Wire up the loosely-coupled external components of the filetree.
91    * See the note about signals in {!Filetree.tree} documentation.
92    *)
93   ignore (tree#op_checksum_file
94             ~callback:(Op_checksum_file.checksum_file tree));
95   ignore (tree#op_copy_regvalue
96             ~callback:(Op_copy_regvalue.copy_regvalue tree));
97   ignore (tree#op_disk_usage
98             ~callback:(Op_disk_usage.disk_usage tree));
99   ignore (tree#op_download_as_reg
100             ~callback:(Op_download_as_reg.download_as_reg tree));
101   ignore (tree#op_download_dir_find0
102             ~callback:(Op_download_dir_find0.download_dir_find0 tree));
103   ignore (tree#op_download_dir_tarball
104             ~callback:(Op_download_dir_tarball.download_dir_tarball tree));
105   ignore (tree#op_download_file
106             ~callback:(Op_download_file.download_file tree));
107   ignore (tree#op_file_information
108             ~callback:(Op_file_information.file_information tree));
109   ignore (tree#op_inspection_dialog
110             ~callback:(Op_inspection_dialog.inspection_dialog tree));
111   ignore (tree#op_view_file
112             ~callback:(Op_view_file.view_file tree));
113
114   tree
115
116 class window =
117   (* I prototyped the basic window layout using Glade, but have
118    * implemented it by hand to give us more flexibility.
119    *)
120   let title = "Guest Filesystem Browser" in
121   let window = GWindow.window ~width:700 ~height:700 ~title () in
122   let vbox = GPack.vbox ~packing:window#add () in
123
124   (* Menus. *)
125   let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
126     make_menubar window vbox ~packing:vbox#pack () in
127
128   (* Top toolbar. *)
129   let vmcombo, refresh_button, throbber, throbber_static, throbber_animation =
130     make_toolbar ~packing:vbox#pack () in
131
132   (* Main part of display is the file tree. *)
133   let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
134
135   (* Status bar and progress bar. *)
136   let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
137   let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
138   let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
139   let statusbar_context = statusbar#new_context ~name:"Standard" in
140
141 object (self)
142   initializer
143     ignore (statusbar_context#push title);
144     window#show ();
145
146     (* Connect up the callback for menu entries etc.  These require the
147      * window_state struct in callbacks.
148      *)
149
150     (* Connect to different hypervisors. *)
151     ignore (connect_kvm_item#connect#activate
152               ~callback:(fun () -> self#connect_to (Some "qemu:///system")));
153     ignore (connect_xen_item#connect#activate
154               ~callback:(fun () -> self#connect_to (Some "xen:///")));
155     ignore (connect_none_item#connect#activate
156               ~callback:(fun () -> self#connect_to None));
157
158     (* VM combo box when changed by the user.
159      * The refresh button acts like changing the VM combo too.
160      *)
161     let combo, (model, column) = vmcombo in
162     ignore (
163       combo#connect#changed
164         ~callback:(
165           fun () ->
166             match combo#active_iter with
167             | None -> () (* nothing selected *)
168             | Some row -> self#open_domain (model#get ~row ~column)
169         )
170     );
171     ignore (
172       refresh_button#connect#clicked
173         ~callback:(
174           fun () ->
175             match combo#active_iter with
176             | None -> () (* nothing selected *)
177             | Some row -> self#open_domain (model#get ~row ~column)
178         )
179     )
180
181   (* Set the statusbar text. *)
182   method set_statusbar msg =
183     statusbar_context#pop ();
184     ignore (statusbar_context#push msg)
185
186   (* Clear the filetree. *)
187   method private clear_view () =
188     view#clear ()
189
190   (* Callback from Connect -> ... menu items. *)
191   method private connect_to uri =
192     self#clear_view ();
193     Slave.discard_command_queue ();
194     Slave.connect uri (self#when_connected uri)
195
196   (* Called back when connected to a new hypervisor. *)
197   method private when_connected uri doms =
198     self#populate_vmcombo doms
199
200   (* Populate the VM combo box. *)
201   method private populate_vmcombo doms =
202     let combo, (model, column) = vmcombo in
203     model#clear ();
204     List.iter (
205       fun { dom_name = name } ->
206         let row = model#append () in
207         model#set ~row ~column name
208     ) doms
209
210   (* When a new domain is selected by the user, eg through vmcombo. *)
211   method private open_domain name =
212     self#clear_view ();
213     Slave.discard_command_queue ();
214     Slave.open_domain name (self#when_opened_domain name)
215
216   (* Called back when domain was opened successfully. *)
217   method private when_opened_domain name data =
218     debug "when_opened_domain callback";
219     self#when_opened_common name data
220
221   (* When a set of disk images is selected by the user. *)
222   method private open_disk_images images =
223     match images with
224     | [] -> ()
225     | images ->
226         self#clear_view ();
227         Slave.discard_command_queue ();
228         Slave.open_images images (self#when_opened_disk_images images)
229
230   (* Called back when disk image(s) were opened successfully. *)
231   method private when_opened_disk_images images data =
232     match images with
233     | [] -> ()
234     | (image, _) :: _ ->
235         debug "when_opened_disk_images callback";
236         self#when_opened_common image data
237
238   (* Common code for when_opened_domain/when_opened_disk_images. *)
239   method private when_opened_common name data =
240     (* Dump some of the inspection data in debug messages. *)
241     List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
242       data.insp_all_filesystems;
243     List.iter (
244       fun { insp_root = root; insp_type = typ; insp_distro = distro;
245             insp_major_version = major; insp_minor_version = minor } ->
246         debug "root device %s contains %s %s %d.%d" root typ distro major minor;
247     ) data.insp_oses;
248
249     view#add_os name data
250
251   (* Public callbacks. *)
252   method throbber_busy () =
253     throbber#set_pixbuf throbber_animation
254
255   method throbber_idle () =
256     throbber#set_pixbuf throbber_static
257
258   method progress (position, total) =
259     if position = 0L && total = 1L then
260       progress_bar#pulse ()
261     else (
262       let frac = Int64.to_float position /. Int64.to_float total in
263       if frac < 0. || frac > 1. then
264         eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
265           position total frac;
266       let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
267       progress_bar#set_fraction frac
268     )
269
270   (* This is called in the main thread whenever a command fails in the
271    * slave thread.  The command queue has been cleared before this is
272    * called, so our job here is to reset the main window, and if
273    * necessary to turn the exception into an error message.
274    *)
275   method failure exn =
276     let raw_msg = Printexc.to_string exn in
277     debug "failure hook: %s" raw_msg;
278
279     let title, msg = pretty_string_of_exn exn in
280     let icon = GMisc.image () in
281     icon#set_stock `DIALOG_ERROR;
282     icon#set_icon_size `DIALOG;
283     GToolbox.message_box ~title ~icon msg
284
285   (* Do what the user asked on the command line. *)
286   method run_cli_request = function
287   | Cmdline.Empty_window -> ()
288   | Cmdline.Open_images images ->
289       self#open_disk_images images
290   | Cmdline.Open_guest guest ->
291       (* Open libvirt connection, and in the callback open the guest. *)
292       let uri = connect_uri () in
293       Slave.connect uri (self#when_connected_cli_request guest)
294
295   method private when_connected_cli_request guest doms =
296     self#populate_vmcombo doms;
297
298     (* "guest" should match a domain in "doms".  Check this and
299      * get the index of it.
300      *)
301     let rec loop i = function
302       | [] ->
303           failwith "guest %s not found (do you need to use --connect?)" guest
304       | d::ds when d = guest -> i
305       | _::ds -> loop (i+1) ds
306     in
307     let i = loop 0 (List.map (fun { dom_name = name } -> name) doms) in
308
309     let combo, _ = vmcombo in
310     combo#set_active i
311
312 end