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.
28 let title = "Guest Filesystem Browser" in
29 let window = GWindow.window ~width:700 ~height:700 ~title () in
30 let vbox = GPack.vbox ~packing:window#add () in
33 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
34 let factory = new GMenu.factory menubar in
35 let accel_group = factory#accel_group in
36 let connect_menu = factory#add_submenu "_Connect" in
38 let factory = new GMenu.factory connect_menu ~accel_group in
39 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
40 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
41 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
42 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
43 let () = ignore (factory#add_separator ()) in
44 let _ = factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
45 let () = ignore (factory#add_separator ()) in
46 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
49 let quit _ = GMain.quit (); false in
51 ignore (window#connect#destroy ~callback:GMain.quit);
52 ignore (window#event#connect#delete ~callback:quit);
53 ignore (quit_item#connect#activate
54 ~callback:(fun () -> ignore (quit ()); ()));
56 window#add_accel_group accel_group in
59 let hbox = GPack.hbox ~border_width:4 ~packing:vbox#pack () in
60 let () = hbox#pack (mklabel "Guest: ") in
62 (* Combo box for displaying virtual machine names. *)
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 throbber_static = Throbber.static () in
76 let throbber_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:throbber_static ~packing:(hbox#pack ~from) () in
82 (* Main part of display is the file tree. *)
83 (* Create the filetree inside a scrolled window. *)
84 let sw = GBin.scrolled_window
85 ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS
86 ~packing:(vbox#pack ~expand:true ~fill:true) () in
87 let tree = new Filetree.tree ~packing:sw#add () in
89 (* Status bar and progress bar. *)
90 let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
91 let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
92 let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
93 let statusbar_context = statusbar#new_context ~name:"Standard" in
96 let connect_kvm_signal = new GUtil.signal () in
97 let connect_xen_signal = new GUtil.signal () in
98 let connect_none_signal = new GUtil.signal () in
99 let connect_uri_signal = new GUtil.signal () in
102 inherit GUtil.ml_signals [connect_kvm_signal#disconnect;
103 connect_xen_signal#disconnect;
104 connect_none_signal#disconnect;
105 connect_uri_signal#disconnect]
107 method connect_kvm_signal = connect_kvm_signal#connect ~after
108 method connect_xen_signal = connect_xen_signal#connect ~after
109 method connect_none_signal = connect_none_signal#connect ~after
110 method connect_uri_signal = connect_uri_signal#connect ~after
113 ignore (statusbar_context#push title);
116 (* Menu entries emit signals. *)
117 ignore (connect_kvm_item#connect#activate
118 ~callback:connect_kvm_signal#call);
119 ignore (connect_xen_item#connect#activate
120 ~callback:connect_xen_signal#call);
121 ignore (connect_none_item#connect#activate
122 ~callback:connect_none_signal#call);
123 ignore (connect_uri_item#connect#activate
124 ~callback:connect_uri_signal#call);
126 (* VM combo box when changed by the user.
127 * The refresh button acts like changing the VM combo too.
129 let combo, (model, column) = vmcombo in
131 combo#connect#changed
134 match combo#active_iter with
135 | None -> () (* nothing selected *)
136 | Some row -> self#open_domain (model#get ~row ~column)
140 refresh_button#connect#clicked
143 match combo#active_iter with
144 | None -> () (* nothing selected *)
145 | Some row -> self#open_domain (model#get ~row ~column)
149 (* Set the statusbar text. *)
150 method set_statusbar msg =
151 statusbar_context#pop ();
152 ignore (statusbar_context#push msg)
154 (* Return the filetree. *)
157 (* Connect to the given URI. *)
158 method connect_to uri () =
160 Slave.discard_command_queue ();
161 Slave.connect uri (self#when_connected uri)
163 (* Called back when connected to a new hypervisor. *)
164 method private when_connected uri doms =
165 self#populate_vmcombo doms
167 (* Populate the VM combo box. *)
168 method private populate_vmcombo doms =
169 let combo, (model, column) = vmcombo in
172 fun { dom_name = name } ->
173 let row = model#append () in
174 model#set ~row ~column name
177 (* When a new domain is selected by the user, eg through vmcombo. *)
178 method private open_domain name =
180 Slave.discard_command_queue ();
181 Slave.open_domain name (self#when_opened_domain name)
183 (* Called back when domain was opened successfully. *)
184 method private when_opened_domain name data =
185 debug "when_opened_domain callback";
186 self#when_opened_common name data
188 (* When a set of disk images is selected by the user. *)
189 method private open_disk_images images =
194 Slave.discard_command_queue ();
195 Slave.open_images images (self#when_opened_disk_images images)
197 (* Called back when disk image(s) were opened successfully. *)
198 method private when_opened_disk_images images data =
202 debug "when_opened_disk_images callback";
203 self#when_opened_common image data
205 (* Common code for when_opened_domain/when_opened_disk_images. *)
206 method private when_opened_common name data =
207 (* Dump some of the inspection data in debug messages. *)
208 List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
209 data.insp_all_filesystems;
211 fun { insp_root = root; insp_type = typ; insp_distro = distro;
212 insp_major_version = major; insp_minor_version = minor } ->
213 debug "root device %s contains %s %s %d.%d" root typ distro major minor;
216 tree#add_os name data
218 (* Public callbacks. *)
219 method throbber_busy () =
220 throbber#set_pixbuf throbber_animation
222 method throbber_idle () =
223 throbber#set_pixbuf throbber_static
225 method progress (position, total) =
226 if position = 0L && total = 1L then
227 progress_bar#pulse ()
229 let frac = Int64.to_float position /. Int64.to_float total in
230 if frac < 0. || frac > 1. then
231 eprintf "warning: progress bar out of range: %Ld / %Ld (%g)\n"
233 let frac = if frac < 0. then 0. else if frac > 1. then 1. else frac in
234 progress_bar#set_fraction frac
237 (* This is called in the main thread whenever a command fails in the
238 * slave thread. The command queue has been cleared before this is
239 * called, so our job here is to reset the main window, and if
240 * necessary to turn the exception into an error message.
243 let raw_msg = Printexc.to_string exn in
244 debug "failure hook: %s" raw_msg;
246 let title, msg = pretty_string_of_exn exn in
247 let icon = GMisc.image () in
248 icon#set_stock `DIALOG_ERROR;
249 icon#set_icon_size `DIALOG;
250 GToolbox.message_box ~title ~icon msg
252 (* Do what the user asked on the command line. *)
253 method run_cli_request = function
254 | Cmdline.Empty_window -> ()
255 | Cmdline.Open_images images ->
256 self#open_disk_images images
257 | Cmdline.Open_guest guest ->
258 (* Open libvirt connection, and in the callback open the guest. *)
259 let uri = connect_uri () in
260 Slave.connect uri (self#when_connected_cli_request guest)
262 method private when_connected_cli_request guest doms =
263 self#populate_vmcombo doms;
265 (* "guest" should match a domain in "doms". Check this and
266 * get the index of it.
268 let rec loop i = function
270 failwith "guest %s not found (do you need to use --connect?)" guest
271 | d::ds when d = guest -> i
272 | _::ds -> loop (i+1) ds
274 let i = loop 0 (List.map (fun { dom_name = name } -> name) doms) in
276 let combo, _ = vmcombo in