gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r19582 - gnunet-gtk/src/fs


From: gnunet
Subject: [GNUnet-SVN] r19582 - gnunet-gtk/src/fs
Date: Tue, 31 Jan 2012 22:59:01 +0100

Author: grothoff
Date: 2012-01-31 22:59:01 +0100 (Tue, 31 Jan 2012)
New Revision: 19582

Modified:
   gnunet-gtk/src/fs/gnunet-fs-gtk-download.c
   gnunet-gtk/src/fs/gnunet-fs-gtk-download.h
   gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c
   gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_file_download.c
Log:
-document gnunet-fs-gtk-download.c and simplify logic

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-download.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-download.c  2012-01-31 20:02:12 UTC (rev 
19581)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-download.c  2012-01-31 21:59:01 UTC (rev 
19582)
@@ -20,23 +20,49 @@
 
 /**
  * @file src/fs/gnunet-fs-gtk-download.c
- * @brief functions for downloading
+ * @brief functions for managing the 'save as' dialog 
  * @author Christian Grothoff
  */
 #include "gnunet-fs-gtk-download.h"
 #include "gnunet-fs-gtk.h"
 #include "gnunet-fs-gtk-event_handler.h"
 
+/**
+ * State for a 'save as' dialog of a download.
+ */
 struct DownloadAsDialogContext
 {
+
+  /**
+   * Download context for which this dialog determines the
+   * filename (and other options).
+   */
+  struct DownloadContext *dc;
+
+  /**
+   * Builder for the dialog.
+   */
   GtkBuilder *builder;
+  
+  /**
+   * Main dialog object.
+   */
   GtkWidget *dialog;
+
+  /**
+   * Final response code from the dialog.
+   */
   gint response;
-  struct DownloadContext *dc;
 };
 
-void
-GNUNET_GTK_save_as_dialog_free_download_context (struct DownloadContext *dc)
+
+/**
+ * Free resources associated with the given download context.
+ *
+ * @param dc context to free
+ */
+static void
+save_as_dialog_free_download_context (struct DownloadContext *dc)
 {
   if (NULL != dc->rr)
     gtk_tree_row_reference_free (dc->rr);
@@ -50,6 +76,16 @@
 }
 
 
+/**
+ * The 'save_as' dialog is being deleted.  Check which state we're in
+ * and either simply clean up or start the download with the
+ * respective options (by calling the respective continuation).
+ *
+ * @param widget the dialog object
+ * @param event the deletion event
+ * @param user_data the 'structDownloadAsDialogContext' of the dialog
+ * @return always FALSE
+ */
 gboolean
 GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget * widget, GdkEvent * 
event,
                                            gpointer user_data)
@@ -59,18 +95,13 @@
   struct DownloadContext *dc;
   GtkWidget *cb;
 
-  if (dlc == NULL)
-  {
-    GNUNET_break (0);
-    return FALSE;
-  }
   builder = dlc->builder;
   dc = dlc->dc;
   cb = GTK_WIDGET (gtk_builder_get_object
                    (builder, "GNUNET_GTK_save_as_recursive_check_button"));
   if (GTK_RESPONSE_OK != dlc->response)
   {
-    GNUNET_GTK_save_as_dialog_free_download_context (dc);
+    save_as_dialog_free_download_context (dc);
     g_object_unref (G_OBJECT (dlc->builder));
     GNUNET_free (dlc);
     return FALSE;
@@ -88,86 +119,116 @@
                                   (builder,
                                    
"GNUNET_GTK_save_as_dialog_anonymity_spin_button")));
   g_object_unref (G_OBJECT (builder));
-
   GNUNET_free (dlc);
-
-  dc->cb (dc);
+  GNUNET_FS_GTK_download_context_start_download (dc);
   return FALSE;
 }
 
 
+/**
+ * The user clicked on the 'save as' button of the dialog.
+ * Delete the window and start the download.
+ *
+ * @param dialog the dialog object
+ * @param response_id response_id associated with the button
+ * @param user_data the 'structDownloadAsDialogContext' of the dialog
+ */
 void
-GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, gint response_id,
+GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, 
+                                      gint response_id,
                                        gpointer user_data)
 {
   struct DownloadAsDialogContext *dlc = user_data;
 
-  if (dlc != NULL)
-    dlc->response = response_id;
+  dlc->response = response_id;
   /* dialogs don't get delete-event the way normal windows do,
-   * call the handler manually
-   */
+     call the handler manually */
   GNUNET_GTK_save_as_dialog_delete_event_cb (GTK_WIDGET (dialog), NULL,
                                              user_data);
+  /* FIXME: isn't the dialog going to be destroyed with the builder? 
+     Is this legal and/or required? */
   gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 
+/**
+ * Open the 'save as' dialog for a download.  Calls the 'dc->cb'
+ * continutation when the dialog is complete.  Will release the 'dc'
+ * resources if the dialog is cancelled.
+ *
+ * @param dc download context for the file/directory 
+ */
 void
 GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
 {
-  GtkWidget *ad;
-  GtkBuilder *builder;
+  struct DownloadAsDialogContext *dlc;
   GtkWidget *cb;
-  struct DownloadAsDialogContext *dlc;
 
   dlc = GNUNET_malloc (sizeof (struct DownloadAsDialogContext));
-  builder =
+  dlc->dc = dc;
+  dlc->builder =
     GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade",
                                dlc);
-  if (builder == NULL)
+  if (NULL == dlc->builder)
   {
     GNUNET_break (0);
-    GNUNET_GTK_save_as_dialog_free_download_context (dc);
+    save_as_dialog_free_download_context (dc);
     GNUNET_free (dlc);
     return;
   }
+  dlc->dialog = GTK_WIDGET (gtk_builder_get_object
+                           (dlc->builder, "GNUNET_GTK_save_as_dialog"));
+
+  /* Enable recursive button for directories and
+     set recursive 'default' value based on what the 'dc' tells us */
   cb = GTK_WIDGET (gtk_builder_get_object
-                   (builder, "GNUNET_GTK_save_as_recursive_check_button"));
+                   (dlc->builder, 
"GNUNET_GTK_save_as_recursive_check_button"));
   if (GNUNET_FS_meta_data_test_for_directory (dc->meta))
-    gtk_widget_set_sensitive (cb, TRUE);
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb), dc->is_recursive);
-    
-  ad = GTK_WIDGET (gtk_builder_get_object
-                   (builder, "GNUNET_GTK_save_as_dialog"));
+    gtk_widget_set_sensitive (cb, TRUE);  
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb),
+                               dc->is_recursive);   
+
+  /* initialize filename based on filename from 'dc' */
   if (dc->filename != NULL)
   {
     char *dirname;
     char *basename;
+
     dirname = GNUNET_strdup (dc->filename);
     basename = (char *) GNUNET_STRINGS_get_short_name (dirname);
+    /* basename now points into 'dirname'; cut 'dirname' off at the '/' before 
basename */
     if (basename > dirname)
       basename[-1] = '\0';
-    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (ad), dirname);
-    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (ad), basename);
+
+    fprintf (stderr,
+            "Splitting `%s' into `%s' + `%s' for file chooser dialog.\n",
+            dc->filename,
+            dirname,
+            basename);
+
+    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dlc->dialog), 
dirname);
+    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dlc->dialog), 
basename);
     GNUNET_free (dirname);
   }
-  dlc->builder = builder;
-  dlc->dialog = ad;
-  dlc->response = 0;
-  dlc->dc = dc;
-  gtk_window_present (GTK_WINDOW (ad));
+  gtk_window_present (GTK_WINDOW (dlc->dialog));
 }
 
 
+/**
+ * Actually start the download that is specified by the given download
+ * context and its options.  Will add a download entry to the
+ * respective tree model and trigger a start of the download using the
+ * FS-API.
+ *
+ * @param dc download context of the download to execute
+ */
 void
-GNUNET_FS_GTK_default_open_download_as_callback (struct DownloadContext *dc)
+GNUNET_FS_GTK_download_context_start_download (struct DownloadContext *dc)
 {
   enum GNUNET_FS_DownloadOptions opt;
   struct GNUNET_FS_Handle *fs;
   struct DownloadEntry *de;
   uint64_t len;
-
   GtkTreeIter iter;
   GtkTreePath *path;
 
@@ -176,7 +237,6 @@
   if (dc->is_recursive)
     opt |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
   len = GNUNET_FS_uri_chk_get_file_size (dc->uri);
-
   de = GNUNET_malloc (sizeof (struct DownloadEntry));
   de->uri = dc->uri;
   dc->uri = NULL;
@@ -184,22 +244,29 @@
   dc->meta = NULL;
   if (dc->rr != NULL)
   {
-    /* We're going to free "dc" very soon */
     de->rr = gtk_tree_row_reference_copy (dc->rr);
     de->ts = GTK_TREE_STORE (gtk_tree_row_reference_get_model (dc->rr));
-
     path = gtk_tree_row_reference_get_path (de->rr);
-    GNUNET_assert (NULL != path);
-    if (gtk_tree_model_get_iter (GTK_TREE_MODEL (de->ts), &iter, path))
+    if  ( (NULL != path) &&
+         (gtk_tree_model_get_iter (GTK_TREE_MODEL (de->ts), &iter, path)) )
     {
       /* Store filename and anonymity as specified by the user.
        * These will be re-used when this is a directory, and the user
        * downloads its children.
        */
       gtk_tree_store_set (de->ts, &iter, 15, dc->filename, 16, dc->anonymity, 
-1);
+    } 
+    else
+    {
+      /* We could not find the referenced row in the search tab; this is
+        conceivable as the search tab might have been closed by the 
+        user while the 'save as' dialog was open.  In this case, this
+        is equivalent to having no search, so we drop the (now invalid)
+        'sr' reference */
+      dc->sr = NULL;
     }
-    gtk_tree_path_free (path);
-
+    if (NULL != path)
+      gtk_tree_path_free (path);
   }
   if (dc->sr != NULL)
   {
@@ -220,7 +287,8 @@
                                             len, dc->anonymity, opt, de,
                                             NULL /* parent download ctx */ ));
   }
-  GNUNET_GTK_save_as_dialog_free_download_context (dc);
+  save_as_dialog_free_download_context (dc);
 }
 
+
 /* end of gnunet-fs-gtk-download.c */

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-download.h
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-download.h  2012-01-31 20:02:12 UTC (rev 
19581)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-download.h  2012-01-31 21:59:01 UTC (rev 
19582)
@@ -23,14 +23,17 @@
  * @brief functions for downloading
  * @author Christian Grothoff
  */
-
 #ifndef GNUNET_FS_GTK_DOWNLOAD_H
 #define GNUNET_FS_GTK_DOWNLOAD_H
+
 #include "gnunet-fs-gtk-common.h"
 
+
+/**
+ * Information we keep for a download.
+ */
 struct DownloadContext;
 
-typedef void (*GNUNET_FS_DownloadAsCallback ) (struct DownloadContext *dc);
 
 /**
  * Information we keep for a download.
@@ -84,21 +87,30 @@
    */
   struct SearchTab *tab;
 
-  /**
-   * A function to call once a filename is picked.
-   */
-  GNUNET_FS_DownloadAsCallback cb;
 };
 
 
+/**
+ * Actually start the download that is specified by the given download
+ * context and its options.  Will add a download entry to the
+ * respective tree model and trigger a start of the download using the
+ * FS-API.
+ *
+ * @param dc download context of the download to execute
+ */
 void
-GNUNET_GTK_save_as_dialog_free_download_context (struct DownloadContext *dc);
+GNUNET_FS_GTK_download_context_start_download (struct DownloadContext *dc);
 
 
+/**
+ * Open the 'save as' dialog for a download.  Calls
+ * 'GNUNET_FS_GTK_download_context_start_download' when the dialog is
+ * complete.  Will release the 'dc' resources if the dialog is
+ * cancelled.
+ *
+ * @param dc download context for the file/directory 
+ */
 void
-GNUNET_FS_GTK_default_open_download_as_callback (struct DownloadContext *dc);
-
-void
 GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc);
 
 

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c     2012-01-31 20:02:12 UTC 
(rev 19581)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c     2012-01-31 21:59:01 UTC 
(rev 19582)
@@ -1023,10 +1023,9 @@
   dc->anonymity = anonymity;
   dc->is_recursive = sdc->recursive;
   dc->tab = tab;
-  dc->cb = &GNUNET_FS_GTK_default_open_download_as_callback;
   if (local_parents && have_a_suggestion)
     /* Skip the dialog, call directly */
-    GNUNET_FS_GTK_default_open_download_as_callback (dc);
+    GNUNET_FS_GTK_download_context_start_download (dc);
   else
     GNUNET_FS_GTK_open_download_as_dialog (dc);
 }

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_file_download.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_file_download.c 2012-01-31 
20:02:12 UTC (rev 19581)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_file_download.c 2012-01-31 
21:59:01 UTC (rev 19582)
@@ -129,7 +129,6 @@
     dc = GNUNET_malloc (sizeof (struct DownloadContext));
     dc->uri = uri;
     dc->anonymity = anonymity_level;
-    dc->cb = GNUNET_FS_GTK_default_open_download_as_callback;
     GNUNET_FS_GTK_open_download_as_dialog (dc);
     return;
   }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]