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 the VM combo box. *)
65 let combo, (model, column) = ws.vmcombo in
68 fun { Slave.dom_name = name } ->
69 let row = model#append () in
70 model#set ~row ~column name
73 (* When a new domain is selected by the user, eg through vmcombo. *)
74 let rec open_domain ws name =
75 set_statusbar ws (sprintf "Opening %s ..." name);
77 Slave.discard_command_queue ();
78 Slave.open_domain name (when_opened_domain ws name)
80 (* Called back when domain was opened successfully. *)
81 and when_opened_domain ws name data =
82 debug "when_opened_domain callback";
83 set_statusbar ws (sprintf "Opened %s" name);
84 when_opened_common ws name data
86 (* When a set of disk images is selected by the user. *)
87 and open_disk_images ws images =
91 set_statusbar ws "Opening disks ...";
93 Slave.discard_command_queue ();
94 Slave.open_images images (when_opened_disk_images ws images)
96 (* Called back when disk image(s) were opened successfully. *)
97 and when_opened_disk_images ws images data =
101 debug "when_opened_disk_images callback";
102 set_statusbar ws "Opened disk";
103 when_opened_common ws image data
105 (* Common code for when_opened_domain/when_opened_disk_images. *)
106 and when_opened_common ws name data =
107 (* Dump some of the inspection data in debug messages. *)
108 List.iter (fun (dev, t) -> debug "filesystem: %s: %s" dev t)
109 data.Slave.insp_all_filesystems;
111 fun { Slave.insp_root = root; insp_type = typ; insp_distro = distro;
112 insp_major_version = major; insp_minor_version = minor } ->
113 debug "root device %s contains %s %s %d.%d" root typ distro major minor;
114 ) data.Slave.insp_oses;
116 Filetree.add ws.view name data
118 let throbber_busy ws () =
119 (*throbber#set_pixbuf animation*)
120 (* XXX Workaround because no binding for GdkPixbufAnimation: *)
121 let file = Filename.dirname Sys.argv.(0) // "Throbber.gif" in
122 ws.throbber#set_file file
124 let throbber_idle ws () =
125 ws.throbber#set_pixbuf ws.throbber_static
127 let progress ws (position, total) =
128 ws.progress_bar#set_fraction
129 (Int64.to_float position /. Int64.to_float total)
131 (* This is called in the main thread whenever a command fails in the
132 * slave thread. The command queue has been cleared before this is
133 * called, so our job here is to reset the main window, and if
134 * necessary to turn the exception into an error message.
137 let title = "Error" in
138 let msg = Printexc.to_string exn in
139 debug "failure hook: %s" msg;
140 let icon = GMisc.image () in
141 icon#set_stock `DIALOG_ERROR;
142 icon#set_icon_size `DIALOG;
143 GToolbox.message_box ~title ~icon msg
145 let rec open_main_window () =
146 (* I prototyped the basic window layout using Glade, but have
147 * implemented it by hand to give us more flexibility.
149 let title = "Guest Filesystem Browser" in
150 let window = GWindow.window ~width:700 ~height:700 ~title () in
151 let vbox = GPack.vbox ~packing:window#add () in
154 let connect_kvm_item, connect_xen_item, connect_none_item, _, _ =
155 make_menubar window vbox ~packing:vbox#pack () in
158 let vmcombo, throbber, throbber_static =
159 make_toolbar ~packing:vbox#pack () in
161 (* Main part of display is the file tree. *)
162 let view = make_filetree ~packing:(vbox#pack ~expand:true ~fill:true) () in
164 (* Status bar and progress bar. *)
165 let hbox = GPack.hbox ~spacing:4 ~packing:vbox#pack () in
166 let progress_bar = GRange.progress_bar ~packing:hbox#pack () in
167 let statusbar = GMisc.statusbar ~packing:(hbox#pack ~expand:true) () in
168 let statusbar_context = statusbar#new_context ~name:"Standard" in
169 ignore (statusbar_context#push title);
173 (* Construct the window_state struct. *)
178 throbber = throbber; throbber_static = throbber_static;
179 statusbar = statusbar; statusbar_context = statusbar_context;
180 progress_bar = progress_bar
183 (* Connect up the callback for menu entries etc. These require the
184 * window_state struct in callbacks.
187 (* Connect to different hypervisors. *)
188 ignore (connect_kvm_item#connect#activate
189 ~callback:(fun () -> connect_to ws (Some "qemu:///system")));
190 ignore (connect_xen_item#connect#activate
191 ~callback:(fun () -> connect_to ws (Some "xen:///")));
192 ignore (connect_none_item#connect#activate
193 ~callback:(fun () -> connect_to ws None));
195 (* VM combo box when changed by the user. *)
196 let combo, (model, column) = ws.vmcombo in
198 combo#connect#changed
201 match combo#active_iter with
202 | None -> () (* nothing selected *)
203 | Some row -> open_domain ws (model#get ~row ~column)
209 and make_menubar window vbox ~packing () =
210 let menubar = GMenu.menu_bar ~packing:vbox#pack () in
211 let factory = new GMenu.factory menubar in
212 let accel_group = factory#accel_group in
213 let connect_menu = factory#add_submenu "_Connect" in
215 let factory = new GMenu.factory connect_menu ~accel_group in
216 let connect_kvm_item = factory#add_item "Connect to local _KVM hypervisor" in
217 let connect_xen_item = factory#add_item "Connect to local _Xen hypervisor" in
218 let connect_none_item = factory#add_item "_Connect to default hypervisor" in
219 let connect_uri_item = factory#add_item "Connect to a _libvirt URI ..." in
220 ignore (factory#add_separator ());
221 let open_image_item =
222 factory#add_item "_Open disk image ..." ~key:GdkKeysyms._O in
223 ignore (factory#add_separator ());
224 let quit_item = factory#add_item "E_xit" ~key:GdkKeysyms._Q in
227 let quit _ = GMain.quit (); false in
228 ignore (window#connect#destroy ~callback:GMain.quit);
229 ignore (window#event#connect#delete ~callback:quit);
230 ignore (quit_item#connect#activate
231 ~callback:(fun () -> ignore (quit ()); ()));
233 window#add_accel_group accel_group;
235 connect_kvm_item, connect_xen_item, connect_none_item,
236 connect_uri_item, open_image_item
238 (* Top toolbar. In fact, not a toolbar because you don't seem to be
239 * able to put a combo box into a toolbar, so it's just an hbox for now.
241 and make_toolbar ~packing () =
242 let hbox = GPack.hbox ~border_width:4 ~packing () in
244 (* Combo box for displaying virtual machine names. *)
245 hbox#pack (mklabel "Guest: ");
246 let vmcombo = GEdit.combo_box_text ~packing:hbox#pack () in
249 let static = Throbber.static () in
250 (*let animation = Throbber.animation () in*)
252 GMisc.image ~pixbuf:static ~packing:(hbox#pack ~from:`END) () in
254 vmcombo, throbber, static
256 and make_filetree ~packing () =
258 GBin.scrolled_window ~packing ~hpolicy:`AUTOMATIC ~vpolicy:`ALWAYS () in
259 Filetree.create ~packing:sw#add ()