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