1 (* virt-manager-like graphical management tool.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
6 module C = Libvirt.Connect
7 module D = Libvirt.Domain
8 module N = Libvirt.Network
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
13 * Returns a triplet (list of added, list of same, list of removed).
15 let differences xs ys =
17 | [], [] -> (* Base case. *)
19 | [], ys -> (* All ys have been added. *)
21 | xs, [] -> (* All xs have been removed. *)
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
33 d (List.sort compare xs, List.sort compare ys)
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"
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
48 let rec filter_top_level_rows (model : GTree.tree_store) f =
49 match model#get_iter_first with
51 | Some iter -> filter_rows model f iter
53 (* Filter rows in a tree_store at a particular level. *)
54 and filter_rows model f row =
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
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
66 (* Find the first row matching predicate f at a particular level. *)
67 and find_row model f row =
69 else if model#iter_next row then find_row model f row
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
76 | Some iter -> iter_rows model f iter
78 (* Iterate over rows in a tree_store at a particular level. *)
79 and iter_rows model f row =
81 if model#iter_next row then iter_rows model f row