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