-(* Main window state. *)
-type window_state = {
- window : GWindow.window;
- view : Filetree.tree;
- vmcombo : GEdit.combo_box GEdit.text_combo;
- refresh_button : GButton.button;
- throbber : GMisc.image;
- throbber_static : GdkPixbuf.pixbuf;
- statusbar : GMisc.statusbar;
- statusbar_context : GMisc.statusbar_context;
- progress_bar : GRange.progress_bar;
-}
-
-(* Set the statusbar text. *)
-let set_statusbar ws msg =
- ws.statusbar_context#pop ();
- ignore (ws.statusbar_context#push msg)
-
-(* Clear the filetree. *)
-let clear_view ws =
- ws.view#clear ()
-
-(* Callback from Connect -> ... menu items. *)
-let rec connect_to ws uri =
- clear_view ws;
- Slave.discard_command_queue ();
- Slave.connect uri (when_connected ws uri)
-
-(* Called back when connected to a new hypervisor. *)
-and when_connected ws uri doms =
- populate_vmcombo ws doms
-
-and populate_vmcombo ws doms =
- (* Populate the VM combo box. *)
- let combo, (model, column) = ws.vmcombo in
- model#clear ();
- List.iter (
- fun { dom_name = name } ->
- let row = model#append () in
- model#set ~row ~column name
- ) doms
-
-(* When a new domain is selected by the user, eg through vmcombo. *)
-let rec open_domain ws name =
- clear_view ws;
- Slave.discard_command_queue ();
- Slave.open_domain name (when_opened_domain ws name)
-
-(* Called back when domain was opened successfully. *)
-and when_opened_domain ws name data =
- debug "when_opened_domain callback";
- when_opened_common ws name data
-
-(* When a set of disk images is selected by the user. *)
-and open_disk_images ws images =
- match images with
- | [] -> ()
- | images ->
- clear_view ws;
- Slave.discard_command_queue ();
- Slave.open_images images (when_opened_disk_images ws images)
-
-(* Called back when disk image(s) were opened successfully. *)
-and when_opened_disk_images ws images data =
- match images with
- | [] -> ()
- | (image, _) :: _ ->
- debug "when_opened_disk_images callback";
- when_opened_common ws image data
-
-(* Common code for when_opened_domain/when_opened_disk_images. *)
-and when_opened_common ws name data =
- (* Dump some of the inspection data in debug messages. *)
- List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
- data.insp_all_filesystems;
- List.iter (
- fun { insp_root = root; insp_type = typ; insp_distro = distro;
- insp_major_version = major; insp_minor_version = minor } ->
- debug "root device %s contains %s %s %d.%d" root typ distro major minor;
- ) data.insp_oses;
-
- ws.view#add_os name data
-
-let throbber_busy ws () =
- (*throbber#set_pixbuf animation*)
- (* XXX Workaround because no binding for GdkPixbufAnimation: *)
- let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
- ws.throbber#set_file file
-
-let throbber_idle ws () =
- ws.throbber#set_pixbuf ws.throbber_static
-
-let progress ws (position, total) =
- if position = 0L && total = 1L then
- ws.progress_bar#pulse ()
- else (
- let frac = Int64.to_float position /. Int64.to_float total in
- if frac < 0. || frac > 1. then
- eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
- position total frac;
- let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
- ws.progress_bar#set_fraction frac
- )
-
-(* This is called in the main thread whenever a command fails in the
- * slave thread. The command queue has been cleared before this is
- * called, so our job here is to reset the main window, and if
- * necessary to turn the exception into an error message.
- *)
-let failure ws exn =
- let raw_msg = Printexc.to_string exn in
- debug "failure hook: %s" raw_msg;
-
- let title, msg = pretty_string_of_exn exn in
- let icon = GMisc.image () in
- icon#set_stock `DIALOG_ERROR;
- icon#set_icon_size `DIALOG;
- GToolbox.message_box ~title ~icon msg
-
-let rec open_main_window () =
- (* I prototyped the basic window layout using Glade, but have
- * implemented it by hand to give us more flexibility.
- *)
- let title = "Guest Filesystem Browser" in
- let window = GWindow.window ~width:700 ~height:700 ~title () in
- let vbox = GPack.vbox ~packing:window#add () in
-
- (* Menus. *)
- let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
- make_menubar window vbox ~packing:vbox#pack () in
-
- (* Top toolbar. *)
- let vmcombo, refresh_button, throbber, throbber_static =
- make_toolbar ~packing:vbox#pack () in
-
- (* Main part of display is the file tree. *)
- let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
-
- (* Status bar and progress bar. *)
- let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
- let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
- let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
- let statusbar_context = statusbar#new_context ~name:"Standard" in
- ignore (statusbar_context#push title);
-
- window#show ();
-
- (* Construct the window_state struct. *)
- let ws = {
- window = window;
- view = view;
- vmcombo = vmcombo;
- refresh_button = refresh_button;
- throbber = throbber; throbber_static = throbber_static;
- statusbar = statusbar; statusbar_context = statusbar_context;
- progress_bar = progress_bar
- } in
-
- (* Connect up the callback for menu entries etc. These require the
- * window_state struct in callbacks.
- *)
-
- (* Connect to different hypervisors. *)
- ignore (connect_kvm_item#connect#activate
- ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
- ignore (connect_xen_item#connect#activate
- ~callback:(fun () -> connect_to ws (Some "xen:///")));
- ignore (connect_none_item#connect#activate
- ~callback:(fun () -> connect_to ws None));
-
- (* VM combo box when changed by the user.
- * The refresh button acts like changing the VM combo too.
- *)
- let combo, (model, column) = ws.vmcombo in
- ignore (
- combo#connect#changed
- ~callback:(
- fun () ->
- match combo#active_iter with
- | None -> () (* nothing selected *)
- | Some row -> open_domain ws (model#get ~row ~column)
- )
- );
- ignore (
- refresh_button#connect#clicked
- ~callback:(
- fun () ->
- match combo#active_iter with
- | None -> () (* nothing selected *)
- | Some row -> open_domain ws (model#get ~row ~column)
- )
- );
-
- (* Return the window_state struct. *)
- ws
-
-and make_menubar window vbox ~packing () =