Add 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
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 let clear_statusbar ws = set_statusbar ws ""
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   (match uri with
51    | None -> set_statusbar ws "Connecting to default libvirt ..."
52    | Some uri -> set_statusbar ws (sprintf "Connecting to %s ..." uri)
53   );
54   clear_view ws;
55   Slave.discard_command_queue ();
56   Slave.connect uri (when_connected ws uri)
57
58 (* Called back when connected to a new hypervisor. *)
59 and when_connected ws uri doms =
60   (match uri with
61    | None -> set_statusbar ws "Connected to default libvirt"
62    | Some uri -> set_statusbar ws (sprintf "Connected to %s" uri)
63   );
64   (* Populate the VM combo box. *)
65   let combo, (model, column) = ws.vmcombo in
66   model#clear ();
67   List.iter (
68     fun { Slave.dom_name = name } ->
69       let row = model#append () in
70       model#set ~row ~column name
71   ) doms
72
73 (* When a new domain is selected by the user, eg through vmcombo. *)
74 let rec open_domain ws name =
75   set_statusbar ws (sprintf "Opening %s ..." name);
76   clear_view ws;
77   Slave.discard_command_queue ();
78   Slave.open_domain name (when_opened_domain ws name)
79
80 (* Called back when domain was opened successfully. *)
81 and when_opened_domain ws name data =
82   debug "when_opened_domain callback";
83   set_statusbar ws (sprintf "Opened %s" name);
84   when_opened_common ws name data
85
86 (* When a set of disk images is selected by the user. *)
87 and open_disk_images ws images =
88   match images with
89   | [] -> ()
90   | images ->
91       set_statusbar ws "Opening disks ...";
92       clear_view ws;
93       Slave.discard_command_queue ();
94       Slave.open_images images (when_opened_disk_images ws images)
95
96 (* Called back when disk image(s) were opened successfully. *)
97 and when_opened_disk_images ws images data =
98   match images with
99   | [] -> ()
100   | (image, _) :: _ ->
101       debug "when_opened_disk_images callback";
102       set_statusbar ws "Opened disk";
103       when_opened_common ws image data
104
105 (* Common code for when_opened_domain/when_opened_disk_images. *)
106 and when_opened_common ws name data =
107   (* Dump some of the inspection data in debug messages. *)
108   List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
109     data.Slave.insp_all_filesystems;
110   List.iter (
111     fun { Slave.insp_root = root; insp_type = typ; insp_distro = distro;
112           insp_major_version = major; insp_minor_version = minor } ->
113       debug "root device %s contains %s %s %d.%d" root typ distro major minor;
114   ) data.Slave.insp_oses;
115
116   Filetree.add ws.view name data
117
118 let throbber_busy ws () =
119   (*throbber#set_pixbuf animation*)
120   (* XXX Workaround because no binding for GdkPixbufAnimation: *)
121   let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
122   ws.throbber#set_file file
123
124 let throbber_idle ws () =
125   ws.throbber#set_pixbuf ws.throbber_static
126
127 let progress ws (position, total) =
128   ws.progress_bar#set_fraction
129     (Int64.to_float position /. Int64.to_float total)
130
131 (* This is called in the main thread whenever a command fails in the
132  * slave thread.  The command queue has been cleared before this is
133  * called, so our job here is to reset the main window, and if
134  * necessary to turn the exception into an error message.
135  *)
136 let failure ws exn =
137   let title = "Error" in
138   let msg = Printexc.to_string exn in
139   debug "failure hook: %s" msg;
140   let icon = GMisc.image () in
141   icon#set_stock `DIALOG_ERROR;
142   icon#set_icon_size `DIALOG;
143   GToolbox.message_box ~title ~icon msg
144
145 let rec open_main_window () =
146   (* I prototyped the basic window layout using Glade, but have
147    * implemented it by hand to give us more flexibility.
148    *)
149   let title = "Guest Filesystem Browser" in
150   let window = GWindow.window ~width:700 ~height:700 ~title () in
151   let vbox = GPack.vbox ~packing:window#add () in
152
153   (* Menus. *)
154   let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
155     make_menubar window vbox ~packing:vbox#pack () in
156
157   (* Top toolbar. *)
158   let vmcombo, throbber, throbber_static =
159     make_toolbar ~packing:vbox#pack () in
160
161   (* Main part of display is the file tree. *)
162   let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
163
164   (* Status bar and progress bar. *)
165   let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
166   let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
167   let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
168   let statusbar_context = statusbar#new_context ~name:"Standard" in
169   ignore (statusbar_context#push title);
170
171   window#show ();
172
173   (* Construct the window_state struct. *)
174   let ws = {
175     window = window;
176     view = view;
177     vmcombo = vmcombo;
178     throbber = throbber; throbber_static = throbber_static;
179     statusbar = statusbar; statusbar_context = statusbar_context;
180     progress_bar = progress_bar
181   } in
182
183   (* Connect up the callback for menu entries etc.  These require the
184    * window_state struct in callbacks.
185    *)
186
187   (* Connect to different hypervisors. *)
188   ignore (connect_kvm_item#connect#activate
189             ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
190   ignore (connect_xen_item#connect#activate
191             ~callback:(fun () -> connect_to ws (Some "xen:///")));
192   ignore (connect_none_item#connect#activate
193             ~callback:(fun () -> connect_to ws None));
194
195   (* VM combo box when changed by the user. *)
196   let combo, (model, column) = ws.vmcombo in
197   ignore (
198     combo#connect#changed
199       ~callback:(
200         fun () ->
201           match combo#active_iter with
202           | None -> () (* nothing selected *)
203           | Some row -> open_domain ws (model#get ~row ~column)
204       )
205   );
206
207   ws
208
209 and make_menubar window vbox ~packing () =
210   let menubar = GMenu.menu_bar ~packing:vbox#pack () in
211   let factory = new GMenu.factory menubar in
212   let accel_group = factory#accel_group in
213   let connect_menu = factory#add_submenu "_Connect" in
214
215   let factory = new GMenu.factory connect_menu ~accel_group in
216   let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
217   let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
218   let connect_none_item = factory#add_item "_Connect to default hypervisor" in
219   let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
220   ignore (factory#add_separator ());
221   let open_image_item =
222     factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
223   ignore (factory#add_separator ());
224   let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
225
226   (* Quit. *)
227   let quit _ = GMain.quit (); false in
228   ignore (window#connect#destroy ~callback:GMain.quit);
229   ignore (window#event#connect#delete ~callback:quit);
230   ignore (quit_item#connect#activate
231             ~callback:(fun () -> ignore (quit ()); ()));
232
233   window#add_accel_group accel_group;
234
235   connect_kvm_item, connect_xen_item, connect_none_item,
236   connect_uri_item, open_image_item
237
238 (* Top toolbar.  In fact, not a toolbar because you don't seem to be
239  * able to put a combo box into a toolbar, so it's just an hbox for now.
240  *)
241 and make_toolbar ~packing () =
242   let hbox = GPack.hbox ~border_width:4 ~packing () in
243
244   (* Combo box for displaying virtual machine names. *)
245   hbox#pack (mklabel "Guest: ");
246   let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
247
248   (* Throbber. *)
249   let static = Throbber.static () in
250   (*let animation = Throbber.animation () in*)
251   let throbber =
252     GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
253
254   vmcombo, throbber, static
255
256 and make_filetree ~packing () =
257   let sw =
258     GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
259   Filetree.create ~packing:sw#add ()