Daily check-in.
[guestfs-browser.git] / filetree_type.ml
1 (* Guestfs Browser.
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  *)
18
19 open Utils
20
21 type t = {
22   view : GTree.view;
23   model : GTree.tree_store;
24   hash : (int, hdata) Hashtbl.t;    (* hash from index_col -> hdata *)
25   index_col : int GTree.column;
26   mode_col : string GTree.column;
27   name_col : string GTree.column;
28   size_col : int64 GTree.column;
29   date_col : string GTree.column;
30   link_col : string GTree.column;
31   mutable status : (string -> unit) option;
32 }
33
34 and hdata = state_t * content_t
35
36 (* The type of the hidden column used to implement on-demand loading.
37  * All rows are classified as either nodes or leafs (eg. a "node" might
38  * be a directory, or a top-level operating system, or anything else
39  * which the user could open and look inside).
40  *)
41 and state_t =
42   | IsLeaf           (* there are no children *)
43   | NodeNotStarted   (* user has not tried to open this *)
44   | NodeLoading      (* user tried to open it, still loading *)
45   | IsNode           (* we've loaded the children of this directory *)
46
47 (* The actual content of a row. *)
48 and content_t =
49   | Loading                          (* special "loading ..." node *)
50   | ErrorMessage of string           (* error message node *)
51   | Top of Slave.source              (* top level OS or volume node *)
52   | Directory of Slave.direntry      (* a directory *)
53   | File of Slave.direntry           (* a file inc. special files *)
54
55 (* Store hdata into a row. *)
56 let store_hdata {model = model; hash = hash; index_col = index_col} row hdata =
57   let index = unique () in
58   Hashtbl.add hash index hdata;
59   model#set ~row ~column:index_col index
60
61 (* Retrieve previously stored hdata from a row. *)
62 let get_hdata { model = model; hash = hash; index_col = index_col } row =
63   let index = model#get ~row ~column:index_col in
64   try Hashtbl.find hash index
65   with Not_found -> assert false
66
67 (* Search up to the top of the tree so we know if this directory
68  * comes from an OS or a volume, and the full path to here.
69  *
70  * The path up the tree will always look something like:
71  *     Top
72  *       \_ Directory
73  *            \_ Directory
74  *                 \_ Loading    <--- you are here
75  *)
76 let rec get_pathname ({ model = model } as t) row =
77   let hdata = get_hdata t row in
78   let parent = model#iter_parent row in
79
80   match hdata, parent with
81   | (IsLeaf, Loading), Some parent ->
82       get_pathname t parent
83   | (IsLeaf, Loading), None ->
84       assert false
85   | (_, Directory { Slave.dent_name = name }), Some parent
86   | (_, File { Slave.dent_name = name }), Some parent ->
87       let src, parent_name = get_pathname t parent in
88       let path =
89         if parent_name = "/" then "/" ^ name
90         else parent_name ^ "/" ^ name in
91       src, path
92   | (_, Top src), _ -> src, "/"
93   | (_, Directory _), None -> assert false
94   | (_, File _), None -> assert false
95   | (_, Loading), _ -> assert false
96   | (_, ErrorMessage _), _ -> assert false
97
98 (* Update the status bar. *)
99 let update_status { status = f } msg =
100   match f with
101   | None -> () (* user didn't give us a [status] function to call *)
102   | Some f -> f msg