Daily checkin of rewritten code.
[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 (* This is called in the main thread whenever a command fails in the
128  * slave thread.  The command queue has been cleared before this is
129  * called, so our job here is to reset the main window, and if
130  * necessary to turn the exception into an error message.
131  *)
132 let failure ws exn =
133   let title = "Error" in
134   let msg = Printexc.to_string exn in
135   debug "failure hook: %s" msg;
136   let icon = GMisc.image () in
137   icon#set_stock `DIALOG_ERROR;
138   icon#set_icon_size `DIALOG;
139   GToolbox.message_box ~title ~icon msg
140
141 let rec open_main_window () =
142   (* I prototyped the basic window layout using Glade, but have
143    * implemented it by hand to give us more flexibility.
144    *)
145   let title = "Guest Filesystem Browser" in
146   let window = GWindow.window ~width:700 ~height:700 ~title () in
147   let vbox = GPack.vbox ~packing:window#add () in
148
149   (* Menus. *)
150   let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
151     make_menubar window vbox ~packing:vbox#pack () in
152
153   (* Top toolbar. *)
154   let vmcombo, throbber, throbber_static =
155     make_toolbar ~packing:vbox#pack () in
156
157   (* Main part of display is the file tree. *)
158   let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
159
160   (* Status bar and progress bar. *)
161   let hbox = GPack.hbox ~packing:vbox#pack () in
162   let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
163   let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
164   let statusbar_context = statusbar#new_context ~name:"Standard" in
165   ignore (statusbar_context#push title);
166
167   window#show ();
168
169   (* Construct the window_state struct. *)
170   let ws = {
171     window = window;
172     view = view;
173     vmcombo = vmcombo;
174     throbber = throbber; throbber_static = throbber_static;
175     statusbar = statusbar; statusbar_context = statusbar_context;
176     progress_bar = progress_bar
177   } in
178
179   (* Connect up the callback for menu entries etc.  These require the
180    * window_state struct in callbacks.
181    *)
182
183   (* Connect to different hypervisors. *)
184   ignore (connect_kvm_item#connect#activate
185             ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
186   ignore (connect_xen_item#connect#activate
187             ~callback:(fun () -> connect_to ws (Some "xen:///")));
188   ignore (connect_none_item#connect#activate
189             ~callback:(fun () -> connect_to ws None));
190
191   (* VM combo box when changed by the user. *)
192   let combo, (model, column) = ws.vmcombo in
193   ignore (
194     combo#connect#changed
195       ~callback:(
196         fun () ->
197           match combo#active_iter with
198           | None -> () (* nothing selected *)
199           | Some row -> open_domain ws (model#get ~row ~column)
200       )
201   );
202
203   ws
204
205 and make_menubar window vbox ~packing () =
206   let menubar = GMenu.menu_bar ~packing:vbox#pack () in
207   let factory = new GMenu.factory menubar in
208   let accel_group = factory#accel_group in
209   let connect_menu = factory#add_submenu "_Connect" in
210
211   let factory = new GMenu.factory connect_menu ~accel_group in
212   let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
213   let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
214   let connect_none_item = factory#add_item "_Connect to default hypervisor" in
215   let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
216   ignore (factory#add_separator ());
217   let open_image_item =
218     factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
219   ignore (factory#add_separator ());
220   let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
221
222   (* Quit. *)
223   let quit _ = GMain.quit (); false in
224   ignore (window#connect#destroy ~callback:GMain.quit);
225   ignore (window#event#connect#delete ~callback:quit);
226   ignore (quit_item#connect#activate
227             ~callback:(fun () -> ignore (quit ()); ()));
228
229   window#add_accel_group accel_group;
230
231   connect_kvm_item, connect_xen_item, connect_none_item,
232   connect_uri_item, open_image_item
233
234 (* Top toolbar.  In fact, not a toolbar because you don't seem to be
235  * able to put a combo box into a toolbar, so it's just an hbox for now.
236  *)
237 and make_toolbar ~packing () =
238   let hbox = GPack.hbox ~border_width:4 ~packing () in
239
240   (* Combo box for displaying virtual machine names. *)
241   hbox#pack (mklabel "Guest: ");
242   let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
243
244   (* Throbber. *)
245   let static = Throbber.static () in
246   (*let animation = Throbber.animation () in*)
247   let throbber =
248     GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
249
250   vmcombo, throbber, static
251
252 and make_filetree ~packing () =
253   let sw =
254     GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
255   Filetree.create ~packing:sw#add ()