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 let make_menubar window (vbox : GPack.box) ~packing () =
27 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
28 let factory = new GMenu.factory menubar in
29 let accel_group = factory#accel_group in
30 let connect_menu = factory#add_submenu "_Connect" in
32 let factory = new GMenu.factory connect_menu ~accel_group in
33 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
34 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
35 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
36 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
37 ignore (factory#add_separator ());
39 factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
40 ignore (factory#add_separator ());
41 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
44 let quit _ = GMain.quit (); false in
45 ignore (window#connect#destroy ~callback:GMain.quit);
46 ignore (window#event#connect#delete ~callback:quit);
47 ignore (quit_item#connect#activate
48 ~callback:(fun () -> ignore (quit ()); ()));
50 window#add_accel_group accel_group;
52 connect_kvm_item, connect_xen_item, connect_none_item,
53 connect_uri_item, open_image_item
55 (* Top toolbar. In fact, not a toolbar because you don't seem to be
56 * able to put a combo box into a toolbar, so it's just an hbox for now.
58 and make_toolbar ~packing () =
59 let hbox = GPack.hbox ~border_width:4 ~packing () in
61 (* Combo box for displaying virtual machine names. *)
62 hbox#pack (mklabel "Guest: ");
63 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
66 * http://stackoverflow.com/questions/2188659/stock-icons-not-shown-on-buttons
69 let image = GMisc.image ~stock:`REFRESH () in
70 let b = GButton.button ~packing:hbox#pack () in
71 b#set_image (image :> GObj.widget);
75 let static = Throbber.static () in
76 let animation = Throbber.animation () in
78 (* Workaround for http://caml.inria.fr/mantis/view.php?id=4732 *)
79 let from = Obj.magic 3448763 (* `END *) in
80 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from) () in
82 vmcombo, refresh_button, throbber, static, animation
84 and make_filetree ~packing () =
85 (* Create the filetree inside a scrolled window. *)
87 GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
88 let tree = new Filetree.tree ~packing:sw#add () in
90 (* Wire up the loosely-coupled external components of the filetree.
91 * See the note about signals in {!Filetree.tree} documentation.
93 ignore (tree#op_checksum_file
94 ~callback:(Op_checksum_file.checksum_file tree));
95 ignore (tree#op_copy_regvalue
96 ~callback:(Op_copy_regvalue.copy_regvalue tree));
97 ignore (tree#op_disk_usage
98 ~callback:(Op_disk_usage.disk_usage tree));
99 ignore (tree#op_download_as_reg
100 ~callback:(Op_download_as_reg.download_as_reg tree));
101 ignore (tree#op_download_dir_find0
102 ~callback:(Op_download_dir_find0.download_dir_find0 tree));
103 ignore (tree#op_download_dir_tarball
104 ~callback:(Op_download_dir_tarball.download_dir_tarball tree));
105 ignore (tree#op_download_file
106 ~callback:(Op_download_file.download_file tree));
107 ignore (tree#op_file_information
108 ~callback:(Op_file_information.file_information tree));
109 ignore (tree#op_inspection_dialog
110 ~callback:(Op_inspection_dialog.inspection_dialog tree));
111 ignore (tree#op_view_file
112 ~callback:(Op_view_file.view_file tree));
117 (* I prototyped the basic window layout using Glade, but have
118 * implemented it by hand to give us more flexibility.
120 let title = "Guest Filesystem Browser" in
121 let window = GWindow.window ~width:700 ~height:700 ~title () in
122 let vbox = GPack.vbox ~packing:window#add () in
125 let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
126 make_menubar window vbox ~packing:vbox#pack () in
129 let vmcombo, refresh_button, throbber, throbber_static, throbber_animation =
130 make_toolbar ~packing:vbox#pack () in
132 (* Main part of display is the file tree. *)
133 let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
135 (* Status bar and progress bar. *)
136 let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
137 let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
138 let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
139 let statusbar_context = statusbar#new_context ~name:"Standard" in
143 ignore (statusbar_context#push title);
146 (* Connect up the callback for menu entries etc. These require the
147 * window_state struct in callbacks.
150 (* Connect to different hypervisors. *)
151 ignore (connect_kvm_item#connect#activate
152 ~callback:(fun () -> self#connect_to (Some "qemu:///system")));
153 ignore (connect_xen_item#connect#activate
154 ~callback:(fun () -> self#connect_to (Some "xen:///")));
155 ignore (connect_none_item#connect#activate
156 ~callback:(fun () -> self#connect_to None));
158 (* VM combo box when changed by the user.
159 * The refresh button acts like changing the VM combo too.
161 let combo, (model, column) = vmcombo in
163 combo#connect#changed
166 match combo#active_iter with
167 | None -> () (* nothing selected *)
168 | Some row -> self#open_domain (model#get ~row ~column)
172 refresh_button#connect#clicked
175 match combo#active_iter with
176 | None -> () (* nothing selected *)
177 | Some row -> self#open_domain (model#get ~row ~column)
181 (* Set the statusbar text. *)
182 method set_statusbar msg =
183 statusbar_context#pop ();
184 ignore (statusbar_context#push msg)
186 (* Clear the filetree. *)
187 method private clear_view () =
190 (* Callback from Connect -> ... menu items. *)
191 method private connect_to uri =
193 Slave.discard_command_queue ();
194 Slave.connect uri (self#when_connected uri)
196 (* Called back when connected to a new hypervisor. *)
197 method private when_connected uri doms =
198 self#populate_vmcombo doms
200 (* Populate the VM combo box. *)
201 method private populate_vmcombo doms =
202 let combo, (model, column) = vmcombo in
205 fun { dom_name = name } ->
206 let row = model#append () in
207 model#set ~row ~column name
210 (* When a new domain is selected by the user, eg through vmcombo. *)
211 method private open_domain name =
213 Slave.discard_command_queue ();
214 Slave.open_domain name (self#when_opened_domain name)
216 (* Called back when domain was opened successfully. *)
217 method private when_opened_domain name data =
218 debug "when_opened_domain callback";
219 self#when_opened_common name data
221 (* When a set of disk images is selected by the user. *)
222 method private open_disk_images images =
227 Slave.discard_command_queue ();
228 Slave.open_images images (self#when_opened_disk_images images)
230 (* Called back when disk image(s) were opened successfully. *)
231 method private when_opened_disk_images images data =
235 debug "when_opened_disk_images callback";
236 self#when_opened_common image data
238 (* Common code for when_opened_domain/when_opened_disk_images. *)
239 method private when_opened_common name data =
240 (* Dump some of the inspection data in debug messages. *)
241 List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
242 data.insp_all_filesystems;
244 fun { insp_root = root; insp_type = typ; insp_distro = distro;
245 insp_major_version = major; insp_minor_version = minor } ->
246 debug "root device %s contains %s %s %d.%d" root typ distro major minor;
249 view#add_os name data
251 (* Public callbacks. *)
252 method throbber_busy () =
253 throbber#set_pixbuf throbber_animation
255 method throbber_idle () =
256 throbber#set_pixbuf throbber_static
258 method progress (position, total) =
259 if position = 0L && total = 1L then
260 progress_bar#pulse ()
262 let frac = Int64.to_float position /. Int64.to_float total in
263 if frac < 0. || frac > 1. then
264 eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
266 let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
267 progress_bar#set_fraction frac
270 (* This is called in the main thread whenever a command fails in the
271 * slave thread. The command queue has been cleared before this is
272 * called, so our job here is to reset the main window, and if
273 * necessary to turn the exception into an error message.
276 let raw_msg = Printexc.to_string exn in
277 debug "failure hook: %s" raw_msg;
279 let title, msg = pretty_string_of_exn exn in
280 let icon = GMisc.image () in
281 icon#set_stock `DIALOG_ERROR;
282 icon#set_icon_size `DIALOG;
283 GToolbox.message_box ~title ~icon msg
285 (* Do what the user asked on the command line. *)
286 method run_cli_request = function
287 | Cmdline.Empty_window -> ()
288 | Cmdline.Open_images images ->
289 self#open_disk_images images
290 | Cmdline.Open_guest guest ->
291 (* Open libvirt connection, and in the callback open the guest. *)
292 let uri = connect_uri () in
293 Slave.connect uri (self#when_connected_cli_request guest)
295 method private when_connected_cli_request guest doms =
296 self#populate_vmcombo doms;
298 (* "guest" should match a domain in "doms". Check this and
299 * get the index of it.
301 let rec loop i = function
303 failwith "guest %s not found (do you need to use --connect?)" guest
304 | d::ds when d = guest -> i
305 | _::ds -> loop (i+1) ds
307 let i = loop 0 (List.map (fun { dom_name = name } -> name) doms) in
309 let combo, _ = vmcombo in