2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
26 (* Main window state. *)
28 window : GWindow.window;
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;
39 (* Set the statusbar text. *)
40 let set_statusbar ws msg =
41 ws.statusbar_context#pop ();
42 ignore (ws.statusbar_context#push msg)
44 (* Clear the filetree. *)
48 (* Callback from Connect -> ... menu items. *)
49 let rec connect_to ws uri =
51 Slave.discard_command_queue ();
52 Slave.connect uri (when_connected ws uri)
54 (* Called back when connected to a new hypervisor. *)
55 and when_connected ws uri doms =
56 populate_vmcombo ws doms
58 and populate_vmcombo ws doms =
59 (* Populate the VM combo box. *)
60 let combo, (model, column) = ws.vmcombo in
63 fun { dom_name = name } ->
64 let row = model#append () in
65 model#set ~row ~column name
68 (* When a new domain is selected by the user, eg through vmcombo. *)
69 let rec open_domain ws name =
71 Slave.discard_command_queue ();
72 Slave.open_domain name (when_opened_domain ws name)
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
79 (* When a set of disk images is selected by the user. *)
80 and open_disk_images ws images =
85 Slave.discard_command_queue ();
86 Slave.open_images images (when_opened_disk_images ws images)
88 (* Called back when disk image(s) were opened successfully. *)
89 and when_opened_disk_images ws images data =
93 debug "when_opened_disk_images callback";
94 when_opened_common ws image data
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;
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;
107 ws.view#add_os name data
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
115 let throbber_idle ws () =
116 ws.throbber#set_pixbuf ws.throbber_static
118 let progress ws (position, total) =
119 if position = 0L && total = 1L then
120 ws.progress_bar#pulse ()
122 let frac = Int64.to_float position /. Int64.to_float total in
123 if frac < 0. || frac > 1. then
124 eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
126 let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
127 ws.progress_bar#set_fraction frac
130 (* This is called in the main thread whenever a command fails in the
131 * slave thread. The command queue has been cleared before this is
132 * called, so our job here is to reset the main window, and if
133 * necessary to turn the exception into an error message.
136 let raw_msg = Printexc.to_string exn in
137 debug "failure hook: %s" raw_msg;
139 let title, msg = pretty_string_of_exn exn in
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
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.
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
154 let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
155 make_menubar window vbox ~packing:vbox#pack () in
158 let vmcombo, refresh_button, throbber, throbber_static =
159 make_toolbar ~packing:vbox#pack () in
161 (* Main part of display is the file tree. *)
162 let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
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);
173 (* Construct the window_state struct. *)
178 refresh_button = refresh_button;
179 throbber = throbber; throbber_static = throbber_static;
180 statusbar = statusbar; statusbar_context = statusbar_context;
181 progress_bar = progress_bar
184 (* Connect up the callback for menu entries etc. These require the
185 * window_state struct in callbacks.
188 (* Connect to different hypervisors. *)
189 ignore (connect_kvm_item#connect#activate
190 ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
191 ignore (connect_xen_item#connect#activate
192 ~callback:(fun () -> connect_to ws (Some "xen:///")));
193 ignore (connect_none_item#connect#activate
194 ~callback:(fun () -> connect_to ws None));
196 (* VM combo box when changed by the user.
197 * The refresh button acts like changing the VM combo too.
199 let combo, (model, column) = ws.vmcombo in
201 combo#connect#changed
204 match combo#active_iter with
205 | None -> () (* nothing selected *)
206 | Some row -> open_domain ws (model#get ~row ~column)
210 refresh_button#connect#clicked
213 match combo#active_iter with
214 | None -> () (* nothing selected *)
215 | Some row -> open_domain ws (model#get ~row ~column)
219 (* Return the window_state struct. *)
222 and make_menubar window vbox ~packing () =
223 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
224 let factory = new GMenu.factory menubar in
225 let accel_group = factory#accel_group in
226 let connect_menu = factory#add_submenu "_Connect" in
228 let factory = new GMenu.factory connect_menu ~accel_group in
229 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
230 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
231 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
232 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
233 ignore (factory#add_separator ());
234 let open_image_item =
235 factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
236 ignore (factory#add_separator ());
237 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
240 let quit _ = GMain.quit (); false in
241 ignore (window#connect#destroy ~callback:GMain.quit);
242 ignore (window#event#connect#delete ~callback:quit);
243 ignore (quit_item#connect#activate
244 ~callback:(fun () -> ignore (quit ()); ()));
246 window#add_accel_group accel_group;
248 connect_kvm_item, connect_xen_item, connect_none_item,
249 connect_uri_item, open_image_item
251 (* Top toolbar. In fact, not a toolbar because you don't seem to be
252 * able to put a combo box into a toolbar, so it's just an hbox for now.
254 and make_toolbar ~packing () =
255 let hbox = GPack.hbox ~border_width:4 ~packing () in
257 (* Combo box for displaying virtual machine names. *)
258 hbox#pack (mklabel "Guest: ");
259 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
262 * http://stackoverflow.com/questions/2188659/stock-icons-not-shown-on-buttons
265 let image = GMisc.image ~stock:`REFRESH () in
266 let b = GButton.button ~packing:hbox#pack () in
267 b#set_image (image :> GObj.widget);
271 let static = Throbber.static () in
272 (*let animation = Throbber.animation () in*)
274 (* Workaround for http://caml.inria.fr/mantis/view.php?id=4732 *)
275 let from = Obj.magic 3448763 (* `END *) in
276 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from) () in
278 vmcombo, refresh_button, throbber, static
280 and make_filetree ~packing () =
281 (* Create the filetree inside a scrolled window. *)
283 GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
284 let tree = new Filetree.tree ~packing:sw#add () in
286 (* Wire up the loosely-coupled external components of the filetree.
287 * See the note about signals in {!Filetree.tree} documentation.
289 ignore (tree#op_checksum_file
290 ~callback:(Op_checksum_file.checksum_file tree));
291 ignore (tree#op_copy_regvalue
292 ~callback:(Op_copy_regvalue.copy_regvalue tree));
293 ignore (tree#op_disk_usage
294 ~callback:(Op_disk_usage.disk_usage tree));
295 ignore (tree#op_download_as_reg
296 ~callback:(Op_download_as_reg.download_as_reg tree));
297 ignore (tree#op_download_dir_find0
298 ~callback:(Op_download_dir_find0.download_dir_find0 tree));
299 ignore (tree#op_download_dir_tarball
300 ~callback:(Op_download_dir_tarball.download_dir_tarball tree));
301 ignore (tree#op_download_file
302 ~callback:(Op_download_file.download_file tree));
303 ignore (tree#op_file_information
304 ~callback:(Op_file_information.file_information tree));
305 ignore (tree#op_inspection_dialog
306 ~callback:(Op_inspection_dialog.inspection_dialog tree));
307 ignore (tree#op_view_file
308 ~callback:(Op_view_file.view_file tree));
312 (* Do what the user asked on the command line. *)
313 let rec run_cli_request ws = function
314 | Cmdline.Empty_window -> ()
315 | Cmdline.Open_images images ->
316 open_disk_images ws images
317 | Cmdline.Open_guest guest ->
318 (* Open libvirt connection, and in the callback open the guest. *)
319 let uri = connect_uri () in
320 Slave.connect uri (when_connected_cli_request ws guest)
321 and when_connected_cli_request ws guest doms =
322 populate_vmcombo ws doms;
324 (* "guest" should match a domain in "doms". Check this and
325 * get the index of it.
327 let rec loop i = function
329 failwith "guest %s not found (do you need to use --connect?)" guest
330 | d::ds when d = guest -> i
331 | _::ds -> loop (i+1) ds
333 let i = loop 0 (List.map (fun { dom_name = name } -> name) doms) in
335 let combo, _ = ws.vmcombo in