Removed $Id$ everywhere.
[virt-top.git] / mlvirtmanager / mlvirtmanager_helpers.ml
1 (* virt-manager-like graphical management tool.
2    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
3    http://libvirt.org/
4 *)
5
6 module C = Libvirt.Connect
7 module D = Libvirt.Domain
8 module N = Libvirt.Network
9
10 (* Given two lists, xs and ys, return a list of items which have been
11  * added to ys, items which are the same, and items which have been
12  * removed from ys.
13  * Returns a triplet (list of added, list of same, list of removed).
14  *)
15 let differences xs ys =
16   let rec d = function
17     | [], [] -> (* Base case. *)
18         ([], [], [])
19     | [], ys -> (* All ys have been added. *)
20         (ys, [], [])
21     | xs, [] -> (* All xs have been removed. *)
22         ([], [], xs)
23     | (x :: xs), (y :: ys) when x = y -> (* Not added or removed. *)
24         let added, unchanged, removed = d (xs, ys) in
25         added, x :: unchanged, removed
26     | (x :: xs), ((y :: _) as ys) when x < y -> (* x removed. *)
27         let added, unchanged, removed = d (xs, ys) in
28         added, unchanged, x :: removed
29     | ((x :: _) as xs), (y :: ys) (* when x > y *) -> (* y added. *)
30         let added, unchanged, removed = d (xs, ys) in
31         y :: added, unchanged, removed
32   in
33   d (List.sort compare xs, List.sort compare ys)
34
35 let string_of_domain_state = function
36   | D.InfoNoState -> "unknown"
37   | D.InfoRunning -> "running"
38   | D.InfoBlocked -> "blocked"
39   | D.InfoPaused -> "paused"
40   | D.InfoShutdown -> "shutdown"
41   | D.InfoShutoff -> "shutoff"
42   | D.InfoCrashed -> "crashed"
43
44 (* Filter top level rows (only) in a tree_store.  If function f returns
45  * true then the row remains, but if it returns false then the row is
46  * removed.
47  *)
48 let rec filter_top_level_rows (model : GTree.tree_store) f =
49   match model#get_iter_first with
50   | None -> ()
51   | Some iter -> filter_rows model f iter
52
53 (* Filter rows in a tree_store at a particular level. *)
54 and filter_rows model f row =
55   let keep = f row in
56   let iter_still_valid =
57     if not keep then model#remove row else model#iter_next row in
58   if iter_still_valid then filter_rows model f row
59
60 (* Find the first top level row matching predicate f and return it. *)
61 let rec find_top_level_row (model : GTree.tree_store) f =
62   match model#get_iter_first with
63   | None -> raise Not_found (* no rows *)
64   | Some row -> find_row model f row
65
66 (* Find the first row matching predicate f at a particular level. *)
67 and find_row model f row =
68   if f row then row
69   else if model#iter_next row then find_row model f row
70   else raise Not_found
71
72 (* Iterate over top level rows (only) in a tree_store. *)
73 let rec iter_top_level_rows (model : GTree.tree_store) f =
74   match model#get_iter_first with
75   | None -> ()
76   | Some iter -> iter_rows model f iter
77
78 (* Iterate over rows in a tree_store at a particular level. *)
79 and iter_rows model f row =
80   f row;
81   if model#iter_next row then iter_rows model f row