+ (* Called when the top level registry node has been opened and the
+ * hive file was downloaded to the cache file successfully.
+ *)
+ method private when_downloaded_registry path _ =
+ debug "when_downloaded_registry";
+ let row = model#get_iter path in
+ let hdata = self#get_hdata row in
+ let h = Option.get hdata.hiveh in
+
+ (* Continue as if expanding any other hive node. *)
+ let root = Hivex.root h in
+ self#expand_hive_node row root
+
+ (* Called instead of {!when_downloaded_registry} if the download failed. *)
+ method private when_downloaded_registry_fail path exn =
+ debug "when_downloaded_registry_fail: %s" (Printexc.to_string exn);
+
+ match exn with
+ | G.Error msg
+ | Hivex.Error (_, _, msg) ->
+ let row = model#get_iter path in
+ let row = model#iter_children ~nth:0 (Some row) in
+
+ let hdata =
+ { state=IsLeaf; content=ErrorMessage msg;
+ visited=false; hiveh=None } in
+ self#store_hdata row hdata;
+
+ model#set ~row ~column:name_col (markup_escape msg)
+
+ | exn ->
+ (* unexpected exception: re-raise it *)
+ raise exn
+
+ (* Expand a hive node. *)
+ method private expand_hive_node row node =
+ debug "expand_hive_node";
+ let hdata = self#get_hdata row in
+ let h = Option.get hdata.hiveh in
+
+ (* Read the hive entries (values, subkeys) at this node and add them
+ * to the tree.
+ *)
+ let values = Hivex.node_values h node in
+ let cmp v1 v2 =
+ UTF8.compare (Hivex.value_key h v1) (Hivex.value_key h v2)
+ in
+ Array.sort cmp values;
+ Array.iter (
+ fun value ->
+ let row = model#append ~parent:row () in
+ self#make_leaf row (RegValue value) (Some h);
+ model#set ~row ~column:name_col (markup_of_regvalue h value);
+ model#set ~row ~column:size_col (markup_of_regvaluesize h value);
+ model#set ~row ~column:date_col (markup_of_regvaluetype h value);
+ ) values;
+
+ let children = Hivex.node_children h node in
+ let cmp n1 n2 =
+ UTF8.compare (Hivex.node_name h n1) (Hivex.node_name h n2)
+ in
+ Array.sort cmp children;
+ Array.iter (
+ fun node ->
+ let row = model#append ~parent:row () in
+ self#make_node row (RegKey node) (Some h);
+ model#set ~row ~column:name_col (markup_of_regkey h node);
+ ) children;
+
+ (* Remove the placeholder "Loading" entry. NB. Must be done AFTER
+ * adding the other entries, or else Gtk will unexpand the row.
+ *)
+ (try
+ let row = self#find_child_node_by_content row Loading in
+ ignore (model#remove row)
+ with Invalid_argument _ | Not_found -> ()
+ );
+
+ (* The original entry has now been loaded, so update its state. *)
+ hdata.state <- IsNode;
+ self#set_visited row
+
+ (* Signals. *)
+ method clear_tree : callback:(unit -> unit) -> GtkSignal.id =
+ clear_tree#connect ~after
+ method op_checksum_file = op_checksum_file#connect ~after
+ method op_copy_regvalue = op_copy_regvalue#connect ~after
+ method op_disk_usage = op_disk_usage#connect ~after
+ method op_download_as_reg = op_download_as_reg#connect ~after
+ method op_download_dir_find0 = op_download_dir_find0#connect ~after
+ method op_download_dir_tarball = op_download_dir_tarball#connect ~after
+ method op_download_file = op_download_file#connect ~after
+ method op_file_information = op_file_information#connect ~after
+ method op_inspection_dialog = op_inspection_dialog#connect ~after
+ method op_view_file = op_view_file#connect ~after
+
+ (* Handle mouse button press on the selected row. This opens the
+ * pop-up context menu.
+ * http://scentric.net/tutorial/sec-selections-context-menus.html
+ *)
+ method private button_press 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 = self#get_hdata row in
+ match hdata with
+ | { content=(Loading | ErrorMessage _ | Info _) } -> None
+ | { content=(Top _ | Directory _ | File _ |
+ TopWinReg _ | RegKey _ | RegValue _ ) } ->
+ Some (path, hdata)
+ ) paths in
+
+ (* Based on number of selected rows and what is selected, construct
+ * the context menu.
+ *)
+ (match self#make_context_menu paths with
+ | Some menu -> menu#popup ~button ~time
+ | None -> ()
+ );
+
+ (* Return true so no other handler will run. *)
+ true
+ )
+ (* We didn't handle this, defer to other handlers. *)
+ else false
+
+ method private make_context_menu paths =
+ let menu = GMenu.menu () in
+ let factory = new GMenu.factory menu in
+
+ let rec add_file_items path =
+ let item = factory#add_item "View ..." in
+ (match Config.opener with
+ | Some opener ->
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_view_file#call (path, opener)));
+ | None ->
+ item#misc#set_sensitive false
+ );
+ let item = factory#add_item "File information" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_file_information#call path));
+ let item = factory#add_item "MD5 checksum" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_checksum_file#call (path, "md5")));
+ let item = factory#add_item "SHA1 checksum" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_checksum_file#call (path, "sha1")));
+ ignore (factory#add_separator ());
+ let item = factory#add_item "Download ..." in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_download_file#call path));
+
+ and add_directory_items path =
+ let item = factory#add_item "Directory information" in
+ item#misc#set_sensitive false;
+ let item = factory#add_item "Calculate disk usage" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_disk_usage#call path));
+ 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:(fun () -> op_download_dir_tarball#call (Tar, path)));
+ let item = factory#add_item "Download as .tar.gz ..." in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_download_dir_tarball#call (TGZ, path)));
+ let item = factory#add_item "Download as .tar.xz ..." in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_download_dir_tarball#call (TXZ, path)));
+ let item = factory#add_item "Download list of filenames ..." in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_download_dir_find0#call path));
+
+ and add_top_os_items os path =
+ let item = factory#add_item "Operating system information" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_inspection_dialog#call os));
+ ignore (factory#add_separator ());
+ add_top_volume_items path
+
+ and add_top_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
+
+ and add_topwinreg_items path =
+ let item = factory#add_item "Download hive file ..." in
+ item#misc#set_sensitive false;
+ ignore (factory#add_separator ());
+ add_regkey_items path
+
+ and add_regkey_items path =
+ let item = factory#add_item "Download as .reg file ..." in
+ (match Config.hivexregedit with
+ | Some hivexregedit ->
+ ignore (item#connect#activate
+ ~callback:(fun () ->
+ op_download_as_reg#call (path, hivexregedit)));
+ | None ->
+ item#misc#set_sensitive false
+ )
+
+ and add_regvalue_items path =
+ let item = factory#add_item "Copy value to clipboard" in
+ ignore (item#connect#activate
+ ~callback:(fun () -> op_copy_regvalue#call path));
+
+ in
+
+ let has_menu =
+ match paths with
+ | [] -> false
+
+ (* single selection *)
+ | [path, { content=Top (OS os)} ] -> (* top level operating system *)
+ add_top_os_items os path; true
+
+ | [path, { content=Top (Volume dev) }] -> (* top level volume *)
+ add_top_volume_items path; true
+
+ | [path, { content=Directory _ }] -> (* directory *)
+ add_directory_items path; true
+
+ | [path, { content=File _ }] -> (* file *)
+ add_file_items path; true
+
+ | [path, { content=TopWinReg _ }] -> (* top level registry node *)
+ add_topwinreg_items path; true
+
+ | [path, { content=RegKey _ }] -> (* registry node *)
+ add_regkey_items path; true
+
+ | [path, { content=RegValue _ }] -> (* registry key/value pair *)
+ add_regvalue_items path; true
+
+ | [_, { content=(Loading|ErrorMessage _|Info _) }] -> false
+
+ | _::_::_ ->
+ (* At the moment multiple selection is disabled. When/if we
+ * enable it we should do something intelligent here. XXX
+ *)
+ false in
+ if has_menu then Some menu else None
+
+ (* Store hdata into a row. *)
+ method private store_hdata 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. *)
+ method private get_hdata row =
+ let index = model#get ~row ~column:index_col in
+ try Hashtbl.find hash index
+ with Not_found -> assert false
+
+ (* [find_child_node_by_content row content] searches the direct
+ children of [row] looking for one which exactly matches
+ [hdata.content] and returns that child. If no child found,
+ raises [Not_found]. *)
+ method private find_child_node_by_content row c =
+ let rec loop row =
+ if (self#get_hdata row).content = c then
+ row
+ else if model#iter_next row then
+ loop row