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.
25 (* Main window state. *)
27 window : GWindow.window;
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;
37 (* Set the statusbar text. *)
38 let set_statusbar ws msg =
39 ws.statusbar_context#pop ();
40 ignore (ws.statusbar_context#push msg)
42 let clear_statusbar ws = set_statusbar ws ""
44 (* Clear the filetree. *)
46 Filetree.clear ws.view
48 (* Callback from Connect -> ... menu items. *)
49 let rec connect_to ws uri =
51 | None -> set_statusbar ws "Connecting to default libvirt ..."
52 | Some uri -> set_statusbar ws (sprintf "Connecting to %s ..." uri)
55 Slave.discard_command_queue ();
56 Slave.connect uri (when_connected ws uri)
58 (* Called back when connected to a new hypervisor. *)
59 and when_connected ws uri doms =
61 | None -> set_statusbar ws "Connected to default libvirt"
62 | Some uri -> set_statusbar ws (sprintf "Connected to %s" uri)
64 populate_vmcombo ws doms
66 and populate_vmcombo ws doms =
67 (* Populate the VM combo box. *)
68 let combo, (model, column) = ws.vmcombo in
71 fun { Slave.dom_name = name } ->
72 let row = model#append () in
73 model#set ~row ~column name
76 (* When a new domain is selected by the user, eg through vmcombo. *)
77 let rec open_domain ws name =
78 set_statusbar ws (sprintf "Opening %s ..." name);
80 Slave.discard_command_queue ();
81 Slave.open_domain name (when_opened_domain ws name)
83 (* Called back when domain was opened successfully. *)
84 and when_opened_domain ws name data =
85 debug "when_opened_domain callback";
86 set_statusbar ws (sprintf "Opened %s" name);
87 when_opened_common ws name data
89 (* When a set of disk images is selected by the user. *)
90 and open_disk_images ws images =
94 set_statusbar ws "Opening disks ...";
96 Slave.discard_command_queue ();
97 Slave.open_images images (when_opened_disk_images ws images)
99 (* Called back when disk image(s) were opened successfully. *)
100 and when_opened_disk_images ws images data =
104 debug "when_opened_disk_images callback";
105 set_statusbar ws "Opened disk";
106 when_opened_common ws image data
108 (* Common code for when_opened_domain/when_opened_disk_images. *)
109 and when_opened_common ws name data =
110 (* Dump some of the inspection data in debug messages. *)
111 List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
112 data.Slave.insp_all_filesystems;
114 fun { Slave.insp_root = root; insp_type = typ; insp_distro = distro;
115 insp_major_version = major; insp_minor_version = minor } ->
116 debug "root device %s contains %s %s %d.%d" root typ distro major minor;
117 ) data.Slave.insp_oses;
119 Filetree.add ws.view name data
121 let throbber_busy ws () =
122 (*throbber#set_pixbuf animation*)
123 (* XXX Workaround because no binding for GdkPixbufAnimation: *)
124 let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
125 ws.throbber#set_file file
127 let throbber_idle ws () =
128 ws.throbber#set_pixbuf ws.throbber_static
130 let progress ws (position, total) =
131 ws.progress_bar#set_fraction
132 (Int64.to_float position /. Int64.to_float total)
134 (* This is called in the main thread whenever a command fails in the
135 * slave thread. The command queue has been cleared before this is
136 * called, so our job here is to reset the main window, and if
137 * necessary to turn the exception into an error message.
140 let title = "Error" in
141 let msg = Printexc.to_string exn in
142 debug "failure hook: %s" msg;
143 let icon = GMisc.image () in
144 icon#set_stock `DIALOG_ERROR;
145 icon#set_icon_size `DIALOG;
146 GToolbox.message_box ~title ~icon msg
148 let rec open_main_window () =
149 (* I prototyped the basic window layout using Glade, but have
150 * implemented it by hand to give us more flexibility.
152 let title = "Guest Filesystem Browser" in
153 let window = GWindow.window ~width:700 ~height:700 ~title () in
154 let vbox = GPack.vbox ~packing:window#add () in
157 let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
158 make_menubar window vbox ~packing:vbox#pack () in
161 let vmcombo, throbber, throbber_static =
162 make_toolbar ~packing:vbox#pack () in
164 (* Main part of display is the file tree. *)
165 let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
167 (* Status bar and progress bar. *)
168 let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
169 let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
170 let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
171 let statusbar_context = statusbar#new_context ~name:"Standard" in
172 ignore (statusbar_context#push title);
176 (* Construct the window_state struct. *)
181 throbber = throbber; throbber_static = throbber_static;
182 statusbar = statusbar; statusbar_context = statusbar_context;
183 progress_bar = progress_bar
186 (* Connect up the callback for menu entries etc. These require the
187 * window_state struct in callbacks.
190 (* Connect to different hypervisors. *)
191 ignore (connect_kvm_item#connect#activate
192 ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
193 ignore (connect_xen_item#connect#activate
194 ~callback:(fun () -> connect_to ws (Some "xen:///")));
195 ignore (connect_none_item#connect#activate
196 ~callback:(fun () -> connect_to ws None));
198 (* VM combo box when changed by the user. *)
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 Filetree.set_status_fn view (set_statusbar ws);
212 (* Return the window_state struct. *)
215 and make_menubar window vbox ~packing () =
216 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
217 let factory = new GMenu.factory menubar in
218 let accel_group = factory#accel_group in
219 let connect_menu = factory#add_submenu "_Connect" in
221 let factory = new GMenu.factory connect_menu ~accel_group in
222 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
223 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
224 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
225 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
226 ignore (factory#add_separator ());
227 let open_image_item =
228 factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
229 ignore (factory#add_separator ());
230 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
233 let quit _ = GMain.quit (); false in
234 ignore (window#connect#destroy ~callback:GMain.quit);
235 ignore (window#event#connect#delete ~callback:quit);
236 ignore (quit_item#connect#activate
237 ~callback:(fun () -> ignore (quit ()); ()));
239 window#add_accel_group accel_group;
241 connect_kvm_item, connect_xen_item, connect_none_item,
242 connect_uri_item, open_image_item
244 (* Top toolbar. In fact, not a toolbar because you don't seem to be
245 * able to put a combo box into a toolbar, so it's just an hbox for now.
247 and make_toolbar ~packing () =
248 let hbox = GPack.hbox ~border_width:4 ~packing () in
250 (* Combo box for displaying virtual machine names. *)
251 hbox#pack (mklabel "Guest: ");
252 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
255 let static = Throbber.static () in
256 (*let animation = Throbber.animation () in*)
258 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
260 vmcombo, throbber, static
262 and make_filetree ~packing () =
264 GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
265 Filetree.create ~packing:sw#add ()
267 (* Do what the user asked on the command line. *)
268 let rec run_cli_request ws = function
269 | Cmdline.Empty_window -> ()
270 | Cmdline.Open_images images ->
271 open_disk_images ws images
272 | Cmdline.Open_guest guest ->
273 (* Open libvirt connection, and in the callback open the guest. *)
274 let uri = connect_uri () in
275 Slave.connect uri (when_connected_cli_request ws guest)
276 and when_connected_cli_request ws guest doms =
277 populate_vmcombo ws doms;
279 (* "guest" should match a domain in "doms". Check this and
280 * get the index of it.
282 let rec loop i = function
284 failwith "guest %s not found (do you need to use --connect?)" guest
285 | d::ds when d = guest -> i
286 | _::ds -> loop (i+1) ds
288 let i = loop 0 (List.map (fun { Slave.dom_name = name } -> name) doms) in
290 let combo, _ = ws.vmcombo in