Refactor code to add inspection dialog.
[guestfs-browser.git] / op_download_file.ml
diff --git a/op_download_file.ml b/op_download_file.ml
new file mode 100644 (file)
index 0000000..069b6aa
--- /dev/null
@@ -0,0 +1,50 @@
+(* Guestfs Browser.
+ * Copyright (C) 2011 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Utils
+
+let rec download_file tree path =
+  let model = tree#model in
+  let row = model#get_iter path in
+  let src, pathname = tree#get_pathname row in
+  debug "download_file %s: showing dialog" pathname;
+
+  (* Put up the dialog. *)
+  let title = "Download file" in
+  let dlg = GWindow.file_chooser_dialog ~action:`SAVE ~title ~modal:true () in
+  dlg#add_button_stock `CANCEL `CANCEL;
+  dlg#add_select_button_stock `SAVE `SAVE;
+  dlg#set_current_name (basename pathname);
+
+  match dlg#run () with
+  | `DELETE_EVENT | `CANCEL ->
+      dlg#destroy ()
+  | `SAVE ->
+      match dlg#filename with
+      | None -> ()
+      | Some localfile ->
+          dlg#destroy ();
+
+          (* Download the file. *)
+          Slave.download_file src pathname localfile
+            (when_downloaded_file tree path)
+
+and when_downloaded_file tree path () =
+  let model = tree#model in
+  let row = model#get_iter path in
+  tree#set_visited row