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