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 refresh_button : GButton.button;
31 throbber : GMisc.image;
32 throbber_static : GdkPixbuf.pixbuf;
33 statusbar : GMisc.statusbar;
34 statusbar_context : GMisc.statusbar_context;
35 progress_bar : GRange.progress_bar;
38 (* Set the statusbar text. *)
39 let set_statusbar ws msg =
40 ws.statusbar_context#pop ();
41 ignore (ws.statusbar_context#push msg)
43 (* Clear the filetree. *)
45 Filetree.clear ws.view
47 (* Callback from Connect -> ... menu items. *)
48 let rec connect_to ws uri =
50 Slave.discard_command_queue ();
51 Slave.connect uri (when_connected ws uri)
53 (* Called back when connected to a new hypervisor. *)
54 and when_connected ws uri doms =
55 populate_vmcombo ws doms
57 and populate_vmcombo ws doms =
58 (* Populate the VM combo box. *)
59 let combo, (model, column) = ws.vmcombo in
62 fun { Slave.dom_name = name } ->
63 let row = model#append () in
64 model#set ~row ~column name
67 (* When a new domain is selected by the user, eg through vmcombo. *)
68 let rec open_domain ws name =
70 Slave.discard_command_queue ();
71 Slave.open_domain name (when_opened_domain ws name)
73 (* Called back when domain was opened successfully. *)
74 and when_opened_domain ws name data =
75 debug "when_opened_domain callback";
76 when_opened_common ws name data
78 (* When a set of disk images is selected by the user. *)
79 and open_disk_images ws images =
84 Slave.discard_command_queue ();
85 Slave.open_images images (when_opened_disk_images ws images)
87 (* Called back when disk image(s) were opened successfully. *)
88 and when_opened_disk_images ws images data =
92 debug "when_opened_disk_images callback";
93 when_opened_common ws image data
95 (* Common code for when_opened_domain/when_opened_disk_images. *)
96 and when_opened_common ws name data =
97 (* Dump some of the inspection data in debug messages. *)
98 List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
99 data.Slave.insp_all_filesystems;
101 fun { Slave.insp_root = root; insp_type = typ; insp_distro = distro;
102 insp_major_version = major; insp_minor_version = minor } ->
103 debug "root device %s contains %s %s %d.%d" root typ distro major minor;
104 ) data.Slave.insp_oses;
106 Filetree.add ws.view name data
108 let throbber_busy ws () =
109 (*throbber#set_pixbuf animation*)
110 (* XXX Workaround because no binding for GdkPixbufAnimation: *)
111 let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
112 ws.throbber#set_file file
114 let throbber_idle ws () =
115 ws.throbber#set_pixbuf ws.throbber_static
117 let progress ws (position, total) =
118 ws.progress_bar#set_fraction
119 (Int64.to_float position /. Int64.to_float total)
121 (* This is called in the main thread whenever a command fails in the
122 * slave thread. The command queue has been cleared before this is
123 * called, so our job here is to reset the main window, and if
124 * necessary to turn the exception into an error message.
127 let title = "Error" in
128 let msg = Printexc.to_string exn in
129 debug "failure hook: %s" msg;
130 let icon = GMisc.image () in
131 icon#set_stock `DIALOG_ERROR;
132 icon#set_icon_size `DIALOG;
133 GToolbox.message_box ~title ~icon msg
135 let rec open_main_window () =
136 (* I prototyped the basic window layout using Glade, but have
137 * implemented it by hand to give us more flexibility.
139 let title = "Guest Filesystem Browser" in
140 let window = GWindow.window ~width:700 ~height:700 ~title () in
141 let vbox = GPack.vbox ~packing:window#add () in
144 let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
145 make_menubar window vbox ~packing:vbox#pack () in
148 let vmcombo, refresh_button, throbber, throbber_static =
149 make_toolbar ~packing:vbox#pack () in
151 (* Main part of display is the file tree. *)
152 let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
154 (* Status bar and progress bar. *)
155 let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
156 let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
157 let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
158 let statusbar_context = statusbar#new_context ~name:"Standard" in
159 ignore (statusbar_context#push title);
163 (* Construct the window_state struct. *)
168 refresh_button = refresh_button;
169 throbber = throbber; throbber_static = throbber_static;
170 statusbar = statusbar; statusbar_context = statusbar_context;
171 progress_bar = progress_bar
174 (* Connect up the callback for menu entries etc. These require the
175 * window_state struct in callbacks.
178 (* Connect to different hypervisors. *)
179 ignore (connect_kvm_item#connect#activate
180 ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
181 ignore (connect_xen_item#connect#activate
182 ~callback:(fun () -> connect_to ws (Some "xen:///")));
183 ignore (connect_none_item#connect#activate
184 ~callback:(fun () -> connect_to ws None));
186 (* VM combo box when changed by the user.
187 * The refresh button acts like changing the VM combo too.
189 let combo, (model, column) = ws.vmcombo in
191 combo#connect#changed
194 match combo#active_iter with
195 | None -> () (* nothing selected *)
196 | Some row -> open_domain ws (model#get ~row ~column)
200 refresh_button#connect#clicked
203 match combo#active_iter with
204 | None -> () (* nothing selected *)
205 | Some row -> open_domain ws (model#get ~row ~column)
209 (* Return the window_state struct. *)
212 and make_menubar window vbox ~packing () =
213 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
214 let factory = new GMenu.factory menubar in
215 let accel_group = factory#accel_group in
216 let connect_menu = factory#add_submenu "_Connect" in
218 let factory = new GMenu.factory connect_menu ~accel_group in
219 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
220 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
221 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
222 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
223 ignore (factory#add_separator ());
224 let open_image_item =
225 factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
226 ignore (factory#add_separator ());
227 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
230 let quit _ = GMain.quit (); false in
231 ignore (window#connect#destroy ~callback:GMain.quit);
232 ignore (window#event#connect#delete ~callback:quit);
233 ignore (quit_item#connect#activate
234 ~callback:(fun () -> ignore (quit ()); ()));
236 window#add_accel_group accel_group;
238 connect_kvm_item, connect_xen_item, connect_none_item,
239 connect_uri_item, open_image_item
241 (* Top toolbar. In fact, not a toolbar because you don't seem to be
242 * able to put a combo box into a toolbar, so it's just an hbox for now.
244 and make_toolbar ~packing () =
245 let hbox = GPack.hbox ~border_width:4 ~packing () in
247 (* Combo box for displaying virtual machine names. *)
248 hbox#pack (mklabel "Guest: ");
249 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
252 * http://stackoverflow.com/questions/2188659/stock-icons-not-shown-on-buttons
255 let image = GMisc.image ~stock:`REFRESH () in
256 let b = GButton.button ~packing:hbox#pack () in
257 b#set_image (image :> GObj.widget);
261 let static = Throbber.static () in
262 (*let animation = Throbber.animation () in*)
264 (* Workaround for http://caml.inria.fr/mantis/view.php?id=4732 *)
265 let from = Obj.magic 3448763 (* `END *) in
266 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from) () in
268 vmcombo, refresh_button, throbber, static
270 and make_filetree ~packing () =
272 GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
273 Filetree.create ~packing:sw#add ()
275 (* Do what the user asked on the command line. *)
276 let rec run_cli_request ws = function
277 | Cmdline.Empty_window -> ()
278 | Cmdline.Open_images images ->
279 open_disk_images ws images
280 | Cmdline.Open_guest guest ->
281 (* Open libvirt connection, and in the callback open the guest. *)
282 let uri = connect_uri () in
283 Slave.connect uri (when_connected_cli_request ws guest)
284 and when_connected_cli_request ws guest doms =
285 populate_vmcombo ws doms;
287 (* "guest" should match a domain in "doms". Check this and
288 * get the index of it.
290 let rec loop i = function
292 failwith "guest %s not found (do you need to use --connect?)" guest
293 | d::ds when d = guest -> i
294 | _::ds -> loop (i+1) ds
296 let i = loop 0 (List.map (fun { Slave.dom_name = name } -> name) doms) in
298 let combo, _ = ws.vmcombo in