*)
open ExtString
+open ExtList
open Printf
open Utils
open DeviceSet
+open Filetree_type
+open Filetree_ops
+
module G = Guestfs
-type t = {
- view : GTree.view;
- model : GTree.tree_store;
- hash : (int, hdata) Hashtbl.t; (* hash from index_col -> hdata *)
- index_col : int GTree.column;
- mode_col : string GTree.column;
- name_col : string GTree.column;
- size_col : int64 GTree.column;
- date_col : string GTree.column;
- link_col : string GTree.column;
-}
-
-and hdata = state_t * content_t
-
-(* The type of the hidden column used to implement on-demand loading.
- * All rows are classified as either nodes or leafs (eg. a "node" might
- * be a directory, or a top-level operating system, or anything else
- * which the user could open and look inside).
- *)
-and state_t =
- | IsLeaf (* there are no children *)
- | NodeNotStarted (* user has not tried to open this *)
- | NodeLoading (* user tried to open it, still loading *)
- | IsNode (* we've loaded the children of this directory *)
-
-(* The actual content of a row. *)
-and content_t =
- | Loading (* special "loading ..." node *)
- | ErrorMessage of string (* error message node *)
- | Top of Slave.source (* top level OS or volume node *)
- | Directory of Slave.direntry (* a directory *)
- | File of Slave.direntry (* a file inc. special files *)
-
-let loading_msg = "<i>Loading ...</i>"
-
-let create ~packing () =
+type t = Filetree_type.t
+
+let rec create ?status ~packing () =
let view = GTree.view ~packing () in
(*view#set_rules_hint true;*)
- view#selection#set_mode `MULTIPLE;
+ (*view#selection#set_mode `MULTIPLE; -- add this later *)
(* Hash of index numbers -> hdata. We do this because it's more
* efficient for the GC compared to storing OCaml objects directly in
let link_view = GTree.view_column ~title:"Link" ~renderer () in
ignore (view#append_column link_view);
- { view = view; model = model; hash = hash;
+ let t = {
+ view = view; model = model; hash = hash;
index_col = index_col;
mode_col = mode_col; name_col = name_col; size_col = size_col;
- date_col = date_col; link_col = link_col }
+ date_col = date_col; link_col = link_col;
+ status = status
+ } in
-let clear { model = model; hash = hash } =
- model#clear ();
- Hashtbl.clear hash
+ (* Open a context menu when a button is pressed. *)
+ ignore (view#event#connect#button_press ~callback:(button_press t));
+
+ t
+
+(* Handle mouse button press on the selected row. This opens the
+ * pop-up context menu.
+ * http://scentric.net/tutorial/sec-selections-context-menus.html
+ *)
+and button_press ({ model = model; view = view } as t) ev =
+ let button = GdkEvent.Button.button ev in
+ let x = int_of_float (GdkEvent.Button.x ev) in
+ let y = int_of_float (GdkEvent.Button.y ev) in
+ let time = GdkEvent.Button.time ev in
+
+ (* Right button for opening the context menu. *)
+ if button = 3 then (
+(*
+ (* If no row is selected, select the row under the mouse. *)
+ let paths =
+ let sel = view#selection in
+ if sel#count_selected_rows < 1 then (
+ match view#get_path_at_pos ~x ~y with
+ | None -> []
+ | Some (path, _, _, _) ->
+ sel#unselect_all ();
+ sel#select_path path;
+ [path]
+ ) else
+ sel#get_selected_rows (* actually returns paths *) in
+*)
+ (* Select the row under the mouse. *)
+ let paths =
+ let sel = view#selection in
+ match view#get_path_at_pos ~x ~y with
+ | None -> []
+ | Some (path, _, _, _) ->
+ sel#unselect_all ();
+ sel#select_path path;
+ [path] in
+
+ (* Get the hdata for all the paths. Filter out rows that it doesn't
+ * make sense to select.
+ *)
+ let paths =
+ List.filter_map (
+ fun path ->
+ let row = model#get_iter path in
+ let hdata = get_hdata t row in
+ match hdata with
+ | _, (Loading | ErrorMessage _) -> None
+ | _, (Top _ | Directory _ | File _) -> Some (path, hdata)
+ ) paths in
+
+ (* Based on number of selected rows and what is selected, construct
+ * the context menu.
+ *)
+ if paths <> [] then (
+ let menu = make_context_menu t paths in
+ menu#popup ~button ~time
+ );
+
+ (* Return true so no other handler will run. *)
+ true
+ )
+ (* We didn't handle this, defer to other handlers. *)
+ else false
+
+and make_context_menu t paths =
+ let menu = GMenu.menu () in
+ let factory = new GMenu.factory menu in
+
+ let item = factory#add_item "Open" in
+ item#misc#set_sensitive false;
+
+ let rec add_file_items path =
+ let item = factory#add_item "File information" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Checksum" in
+ item#misc#set_sensitive false;
+ ignore (factory#add_separator ());
+ let item = factory#add_item "Download ..." in
+ ignore (item#connect#activate ~callback:(download_file t path));
+
+ and add_directory_items path =
+ let item = factory#add_item "Directory information" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Space used by directory" in
+ item#misc#set_sensitive false;
+ ignore (factory#add_separator ());
+ let item = factory#add_item "Download ..." in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Download as .tar ..." in
+ ignore (item#connect#activate
+ ~callback:(download_dir_tarball t Slave.Tar path));
+ let item = factory#add_item "Download as .tar.gz ..." in
+ ignore (item#connect#activate
+ ~callback:(download_dir_tarball t Slave.TGZ path));
+ let item = factory#add_item "Download as .tar.xz ..." in
+ ignore (item#connect#activate
+ ~callback:(download_dir_tarball t Slave.TXZ path));
+ let item = factory#add_item "Download list of filenames ..." in
+ ignore (item#connect#activate ~callback:(download_dir_find0 t path));
+
+ and add_os_items path =
+ let item = factory#add_item "Operating system information" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Block device information" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Filesystem used & free" in
+ item#misc#set_sensitive false;
+ ignore (factory#add_separator ());
+ add_directory_items path
+
+ and add_volume_items path =
+ let item = factory#add_item "Filesystem used & free" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Block device information" in
+ item#misc#set_sensitive false;
+ ignore (factory#add_separator ());
+ add_directory_items path
+ in
+
+ (match paths with
+ (* single selection *)
+ | [path, (_, Top (Slave.OS os))] -> (* top level operating system *)
+ add_os_items path
+
+ | [path, (_, Top (Slave.Volume dev))] -> (* top level volume *)
+ add_volume_items path
+
+ | [path, (_, Directory direntry)] -> (* directory *)
+ add_directory_items path
+
+ | [path, (_, File direntry)] -> (* file *)
+ add_file_items path
+
+ | [_, (_, Loading)]
+ | [_, (_, ErrorMessage _)] -> ()
+
+ | _ ->
+ (* At the moment multiple selection is disabled. When/if we
+ * enable it we should do something intelligent here. XXX
+ *)
+ ()
+ );
+
+ menu
(* XXX No binding for g_markup_escape in lablgtk2. *)
let markup_escape name =
(tm.Unix.tm_year + 1900) (tm.Unix.tm_mon + 1) tm.Unix.tm_mday
tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec
-(* Store hdata into a row. *)
-let store_hdata {model = model; hash = hash; index_col = index_col} row hdata =
- let index = unique () in
- Hashtbl.add hash index hdata;
- model#set ~row ~column:index_col index
-
-(* Retrieve previously stored hdata from a row. *)
-let get_hdata { model = model; hash = hash; index_col = index_col } row =
- let index = model#get ~row ~column:index_col in
- try Hashtbl.find hash index
- with Not_found -> assert false
+let clear { model = model; hash = hash } =
+ model#clear ();
+ Hashtbl.clear hash
let rec add ({ model = model; hash = hash } as t) name data =
clear t;
let placeholder = model#append ~parent:row () in
let hdata = IsLeaf, Loading in
store_hdata t placeholder hdata;
- model#set ~row:placeholder ~column:t.name_col loading_msg;
+ model#set ~row:placeholder ~column:t.name_col "<i>Loading ...</i>";
ignore (t.view#connect#row_expanded ~callback:(expand_row t))
and make_leaf ({ model = model; hash = hash } as t) row content =
(* Node should not exist in the tree. *)
| NodeNotStarted, (Loading | ErrorMessage _) -> assert false
-(* Search up to the top of the tree so we know if this directory
- * comes from an OS or a volume, and the full path to here.
- *
- * The path up the tree will always look something like:
- * Top
- * \_ Directory
- * \_ Directory
- * \_ Loading <--- you are here
- *)
-and get_pathname ({ model = model } as t) row =
- let hdata = get_hdata t row in
- let parent = model#iter_parent row in
-
- match hdata, parent with
- | (IsLeaf, Loading), Some parent ->
- get_pathname t parent
- | (IsLeaf, Loading), None ->
- assert false
- | (_, Directory { Slave.dent_name = name }), Some parent
- | (_, File { Slave.dent_name = name }), Some parent ->
- let src, parent_name = get_pathname t parent in
- let path =
- if parent_name = "/" then "/" ^ name
- else parent_name ^ "/" ^ name in
- src, path
- | (_, Top src), _ -> src, "/"
- | (_, Directory _), None -> assert false
- | (_, File _), None -> assert false
- | (_, Loading), _ -> assert false
- | (_, ErrorMessage _), _ -> assert false
-
(* This is the callback when the slave has read the directory for us. *)
and when_read_directory ({ model = model } as t) path entries =
debug "when_read_directory";
| exn ->
(* unexpected exception: re-raise it *)
raise exn
+
+let set_status_fn t status =
+ t.status <- Some status