gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r19583 - gnunet-gtk/src/fs
Date: Tue, 31 Jan 2012 23:54:08 +0100

Author: grothoff
Date: 2012-01-31 23:54:08 +0100 (Tue, 31 Jan 2012)
New Revision: 19583

Added:
   gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_meta_data_context_menu.c
Modified:
   gnunet-gtk/src/fs/Makefile.am
   gnunet-gtk/src/fs/gnunet-fs-gtk-download.c
   gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c
Log:
-cleaning up meta data context menu code and moving to its own file

Modified: gnunet-gtk/src/fs/Makefile.am
===================================================================
--- gnunet-gtk/src/fs/Makefile.am       2012-01-31 21:59:01 UTC (rev 19582)
+++ gnunet-gtk/src/fs/Makefile.am       2012-01-31 22:54:08 UTC (rev 19583)
@@ -16,6 +16,7 @@
   gnunet-fs-gtk-edit_publish_dialog.c gnunet-fs-gtk-edit_publish_dialog.h \
   gnunet-fs-gtk-event_handler.c gnunet-fs-gtk-event_handler.h \
   gnunet-fs-gtk-anonymity_spin_buttons.c \
+  gnunet-fs-gtk-main_window_meta_data_context_menu.c \
   gnunet-fs-gtk.c gnunet-fs-gtk.h \
   gnunet-fs-gtk-main_window_create_pseudonym.c \
   gnunet-fs-gtk-main_window_file_download.c \

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-download.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-download.c  2012-01-31 21:59:01 UTC (rev 
19582)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-download.c  2012-01-31 22:54:08 UTC (rev 
19583)
@@ -232,11 +232,6 @@
   GtkTreeIter iter;
   GtkTreePath *path;
 
-  fs = GNUNET_FS_GTK_get_fs_handle ();
-  opt = GNUNET_FS_DOWNLOAD_OPTION_NONE;
-  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;
@@ -268,6 +263,11 @@
     if (NULL != path)
       gtk_tree_path_free (path);
   }
+  fs = GNUNET_FS_GTK_get_fs_handle ();
+  opt = GNUNET_FS_DOWNLOAD_OPTION_NONE;
+  if (dc->is_recursive)
+    opt |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
+  len = GNUNET_FS_uri_chk_get_file_size (dc->uri);
   if (dc->sr != NULL)
   {
     GNUNET_break (NULL !=

Modified: gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c     2012-01-31 21:59:01 UTC 
(rev 19582)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-event_handler.c     2012-01-31 22:54:08 UTC 
(rev 19583)
@@ -28,12 +28,11 @@
 #include "gnunet-fs-gtk-event_handler.h"
 #include <string.h>
 
-static struct SearchTab *search_tab_head;
 
-static struct SearchTab *search_tab_tail;
-
-static struct SearchTab *uri_tab;
-
+/**
+ * We have a single tab where we display publishing operations.
+ * So there is only one instance of this struct.
+ */ 
 struct PublishTab
 {
 
@@ -54,6 +53,11 @@
 };
 
 
+/**
+ * Information we keep for each file or directory being published.
+ * Used to quickly identify the tab and row of the operation; stored
+ * in the user-context of the FS library for the publish operation.
+ */
 struct PublishEntry
 {
   /**
@@ -76,10 +80,19 @@
    */
   struct GNUNET_FS_Uri *uri;
 
+  /**
+   * Is this the top-level entry for the publish operation
+   * or sub-operation?
+   */
   int is_top;
 };
 
 
+/**
+ * Information we keep for each search result.  Used to quickly
+ * identify the tab and row of the result; stored in the user-context
+ * of the FS library for the search result.
+ */
 struct SearchResult
 {
   /**
@@ -105,48 +118,450 @@
 };
 
 
-struct StartDownloadContext
-{
-  struct SearchTab *tab;
-  gboolean recursive;
-};
 
+/**
+ * Head of linked list of tabs for searches.
+ */
+static struct SearchTab *search_tab_head;
+
+/**
+ * Tail of linked list of tabs for searches.
+ */
+static struct SearchTab *search_tab_tail;
+
+/**
+ * Special tab we use to for downloads-by-URIs and downloads
+ * where the search tab has been closed ("parent lost").
+ */
+static struct SearchTab *uri_tab;
+
+/**
+ * Special tab we use to store publishing operations.
+ */
 static struct PublishTab *publish_tab;
 
 /**
  * Row reference for the current search context menu.
+ * FIXME: de-globalize?
  */
 static GtkTreeRowReference *current_context_row_reference;
 
 /**
  * Search tab used for the current search context menu.
+ * FIXME: de-globalize?
  */
 static struct SearchTab *current_context_search_tab;
 
-static void
-start_download_ctx_menu (GtkMenuItem *item, gpointer user_data);
 
+
+/**
+ * Closure for the 'start_download' callback.
+ */
+struct StartDownloadContext
+{
+  /**
+   * Search tab to search for the request.
+   */
+  struct SearchTab *tab;
+
+  /**
+   * Was the 'recursive' option requested?
+   */
+  gboolean recursive;
+
+};
+
+
+/**
+ * This should get the default download directory (so that GNUnet
+ * won't offer the user to download files to the 'bin' subdirectory,
+ * or whatever is the cwd).  Returns NULL on failure (such as
+ * non-existend directory).  Should also preserve the last setting (so
+ * if the user saves files somewhere else, next time we default to
+ * somewhere else, at least until application restart, or maybe even
+ * between application restarts).
+ *
+ * Fills the @buffer up to @size bytes, returns a pointer to it.
+ */
+static char *
+get_default_download_directory (char *buffer, size_t size)
+{
+  return NULL;
+}
+
+
+/**
+ * Called recursively to build a suggested filename by prepending
+ * suggested names for its parent directories (if any).
+ *
+ * @param tm FIXME
+ * @param iter FIXME
+ * @param top FIXME
+ * @param local_parents set to  GNUNET_YES if all parents are directories, and 
are downloaded.
+ * @param anonymity FIXME
+ * @param filename_is_absolute FIXME
+ * @return suggested filename, possibly NULL
+ */
+static char *
+get_suggested_filename_anonymity (GtkTreeModel *tm, 
+                                 GtkTreeIter *iter, 
+                                 int top,
+                                 int *local_parents, 
+                                 int *anonymity, 
+                                 int *filename_is_absolute)
+{
+  char *result;
+  char *dirname;
+  char *local_filename, *filename;
+  int downloaded_anonymity;
+  int have_a_parent;
+  struct GNUNET_CONTAINER_MetaData *meta;
+  GtkTreeIter parent;
+  gtk_tree_model_get (tm, iter, 0, &meta, 15, &local_filename, 16, 
&downloaded_anonymity, -1);
+  if (local_filename == NULL && !top)
+    *local_parents = GNUNET_NO;
+  if (downloaded_anonymity != -1 && *anonymity == -1 && !top)
+    *anonymity = downloaded_anonymity;
+  if (gtk_tree_model_iter_parent (tm, &parent, iter))
+  {
+    have_a_parent = GNUNET_YES;
+    dirname = get_suggested_filename_anonymity (tm, &parent, GNUNET_NO, 
local_parents, anonymity, filename_is_absolute);
+  }
+  else
+  {
+    have_a_parent = GNUNET_NO;
+    dirname = NULL;
+    if (top)
+      *local_parents = GNUNET_NO;
+  }
+  if (local_filename == NULL)
+    filename = GNUNET_FS_meta_data_suggest_filename (meta);
+  else
+  {
+    /* This directory was downloaded as /foo/bar/baz/somedirname
+     * Hopefully, "somedirname" is actually "somedir.gnd"
+     * We need to strip the ".gnd" part to get "somedir", which is
+     * what we're going to use instead of suggested original filename
+     * Without the .gnd extension we're going to just use a copy
+     * of the directory file name - and that would fail. Sad.
+     */
+    const char *basename; 
+    if (dirname == NULL && !have_a_parent)
+    {
+      /* This is the ealderlest parent directory. Use absolute path. */
+      basename = (const char *) local_filename;
+      *filename_is_absolute = GNUNET_YES;
+    }
+    else
+      basename = GNUNET_STRINGS_get_short_name (local_filename);
+    if (basename != NULL && strlen (basename) > 0)
+    {
+      char *dot;
+      filename = GNUNET_strdup (basename);
+      dot = strrchr (filename, '.');
+      if (dot)
+        *dot = '\0';
+    }
+    else
+      filename = GNUNET_FS_meta_data_suggest_filename (meta);
+  }
+  if (dirname && filename)
+  {
+    GNUNET_asprintf (&result, "%s%s%s", dirname, DIR_SEPARATOR_STR, filename);
+    GNUNET_free (filename);
+    GNUNET_free (dirname);
+    return result;
+  }
+  else if (filename)
+    return filename;
+  else
+    return NULL;
+}
+
+
+/**
+ * This function is called when the user double-clicks on a search
+ * result.  Begins the download, if necessary by opening the "save as"
+ * window.
+ *
+ * @param tree_view tree view with the details
+ * @param path path selecting which entry we want to download
+ * @param column unused entry specifying which column the mouse was in
+ * @param user_data a 'struct StartDownloadContext' with additional details
+ */
 static void
-start_download_recursively_ctx_menu (GtkMenuItem *item, gpointer user_data);
+start_download (GtkTreeView * tree_view, GtkTreePath * path,
+                GtkTreeViewColumn * column, gpointer user_data)
+{
+  struct StartDownloadContext *sdc = user_data;
+  struct SearchTab *tab = sdc->tab;
+  GtkTreeModel *tm;
+  GtkTreeIter iter;
+  struct GNUNET_FS_Uri *uri;
+  struct GNUNET_CONTAINER_MetaData *meta;
+  struct SearchResult *sr;
+  gchar *mime;
+  struct DownloadContext *dc;
+  char *buf = NULL;
+  char *tmp;
+  size_t tmplen;
+  char cwd[FILENAME_MAX];
+  char *download_directory;
+  char *filename;
+  int local_parents;
+  int have_a_suggestion;
+  int anonymity;
+  int filename_is_absolute;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting a %sdownload\n", 
+             sdc->recursive ? "recursive " : "");
+
+  GNUNET_assert (tab != NULL);
+  tm = gtk_tree_view_get_model (tree_view);
+  if (TRUE != gtk_tree_model_get_iter (tm, &iter, path))
+  {
+    GNUNET_break (0);
+    // FIXME: this is bad...
+    GNUNET_free (sdc);
+    return;
+  }
+  gtk_tree_model_get (tm, &iter, 0, &meta, 1, &uri, 9, &sr, 10, &mime, -1);
+  if (NULL == uri)
+  {
+    /* user clicked on directory that was opened (not downloaded!), so we
+       have no URI and downloading makes no sense. Ignore! */
+    if (NULL != mime)
+      g_free (mime);
+    return;
+  }
+  if (!(GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri)))
+  {
+    /* can only download chk/loc URIs, ignore */
+    g_free (mime);
+    return;
+  }
+
+  download_directory = get_default_download_directory (cwd, sizeof (cwd));
+  /* If no download directory is known, try working directory */
+  if (download_directory == NULL)
+    download_directory = getcwd (cwd, sizeof (cwd));
+  /* Calculate suggested filename */
+  local_parents = GNUNET_YES;
+  anonymity = -1;
+  filename_is_absolute = GNUNET_NO;
+  filename = get_suggested_filename_anonymity (tm, &iter, GNUNET_YES, 
&local_parents, &anonymity, &filename_is_absolute);
+  have_a_suggestion = GNUNET_NO;
+  if (NULL != download_directory)
+  {
+    if (NULL == filename)
+    {
+      buf = GNUNET_strdup (download_directory);
+    }
+    else
+    {
+      have_a_suggestion = GNUNET_YES;
+      if (filename_is_absolute)
+        GNUNET_asprintf (&tmp, "%s", filename);
+      else
+        GNUNET_asprintf (&tmp, "%s%s%s",
+                        download_directory,
+                        DIR_SEPARATOR_STR,
+                        filename);
+      tmplen = strlen (tmp);
+      /* now, if we have a directory, replace trailing '/' with ".gnd" */
+      if (GNUNET_YES ==
+         GNUNET_FS_meta_data_test_for_directory (meta))
+      {
+       if ( (tmp[tmplen-1] == '/') ||
+            (tmp[tmplen-1] == '\\') )
+         tmp[tmplen-1] = '\0';
+       GNUNET_asprintf (&buf,
+                        "%s%s",
+                        tmp,
+                        GNUNET_FS_DIRECTORY_EXT);
+       GNUNET_free (tmp);           
+      }
+      else
+      {
+       buf = tmp;
+      }
+    }
+  }
+  GNUNET_free_non_null (filename);
+
+  /* now setup everything for the save-as dialog */
+  dc = GNUNET_malloc (sizeof (struct DownloadContext));
+  dc->uri = GNUNET_FS_uri_dup (uri);
+  dc->mime = mime;
+  dc->filename = buf;
+  dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
+  dc->rr = gtk_tree_row_reference_new (tm, path);
+  dc->sr = sr->result;
+  dc->anonymity = anonymity;
+  dc->is_recursive = sdc->recursive;
+  dc->tab = tab;
+  if (local_parents && have_a_suggestion)
+    /* Skip the dialog, call directly */
+    GNUNET_FS_GTK_download_context_start_download (dc);
+  else
+    GNUNET_FS_GTK_open_download_as_dialog (dc);
+}
+
+
+/**
+ * An item was selected from the context menu; destroy 
+ * the menu shell.
+ *
+ * @param menushell menu to destroy
+ * @parma user_data the 'struct DownloadEntry' for the menu (unused)
+ */
 static void
-abort_download_ctx_menu (GtkMenuItem *item, gpointer user_data);
+search_list_popup_selection_done (GtkMenuShell *menushell,
+                                 gpointer user_data)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Item selected in menu shell %p, destroying it.\n",
+             menushell);
+  gtk_widget_destroy (GTK_WIDGET (menushell));
+}
 
+
+/**
+ * "Download" was selected in the current search context menu.
+ * 
+ * @param item the 'download' menu item
+ * @parma user_data the 'struct DownloadEntry' to download.
+ */
 static void
-copy_uri_to_clipboard_ctx_menu (GtkMenuItem *item, gpointer user_data);
+start_download_ctx_menu (GtkMenuItem *item, gpointer user_data)
+{
+  GtkTreePath *path;
+  GtkTreeView *tv;
+  struct StartDownloadContext sdc;
 
+  if (current_context_row_reference == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  path = gtk_tree_row_reference_get_path (current_context_row_reference);
+  gtk_tree_row_reference_free (current_context_row_reference);
+  current_context_row_reference = NULL;
+  tv = GTK_TREE_VIEW (gtk_builder_get_object
+                      (current_context_search_tab->builder,
+                       "_search_result_frame"));
+  sdc.tab = current_context_search_tab;
+  sdc.recursive = FALSE;
+  start_download (tv, path, NULL, &sdc);
+  gtk_tree_path_free (path);
+  current_context_search_tab = NULL;
+}
+
+
+/**
+ * "Download recursively" was selected in the current search context menu.
+ * 
+ * @param item the 'download recursively' menu item
+ * @parma user_data the 'struct DownloadEntry' to download.
+ */
 static void
-free_search_result (struct SearchResult *sr);
+start_download_recursively_ctx_menu (GtkMenuItem *item, gpointer user_data)
+{
+  GtkTreePath *path;
+  GtkTreeView *tv;
+  struct StartDownloadContext sdc;
 
-void
-search_list_popup_selection_done (GtkMenuShell *menushell,
-    gpointer user_data)
+  if (current_context_row_reference == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  path = gtk_tree_row_reference_get_path (current_context_row_reference);
+  gtk_tree_row_reference_free (current_context_row_reference);
+  current_context_row_reference = NULL;
+  tv = GTK_TREE_VIEW (gtk_builder_get_object
+                      (current_context_search_tab->builder,
+                       "_search_result_frame"));
+  sdc.tab = current_context_search_tab;
+  sdc.recursive = TRUE;
+  start_download (tv, path, NULL, &sdc);
+  gtk_tree_path_free (path);
+  current_context_search_tab = NULL;
+}
+
+
+/**
+ * Download "abort" was selected in the current search context menu.
+ * 
+ * @param item the 'abort' menu item
+ * @parma user_data the 'struct DownloadEntry' to abort.
+ */
+static void
+abort_download_ctx_menu (GtkMenuItem *item, gpointer user_data)
 {
+  struct DownloadEntry *de = user_data;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-      "Item selected in menu shell %x\n", menushell);
-  gtk_widget_destroy (GTK_WIDGET (menushell));
+             "Aborting download DE=%p\n", 
+             de);
+  GNUNET_assert (de->dc != NULL);
+  GNUNET_FS_download_stop (de->dc, GNUNET_YES);
+  current_context_search_tab = NULL;
 }
 
+
+/**
+ * Copy current URI to clipboard.
+ * 
+ * @param item the 'copy-to-clipboard' menu item
+ * @parma user_data the 'struct DownloadEntry' to copy from
+ */
+static void
+copy_uri_to_clipboard_ctx_menu (GtkMenuItem *item, gpointer user_data)
+{
+  GtkTreePath *path;
+  GtkTreeView *tv;
+  GtkTreeModel *tm;
+  GtkTreeIter iter;
+  struct GNUNET_FS_Uri *uri;
+  char *uris;
+  GtkClipboard *cb;
+
+  if (current_context_row_reference == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  path = gtk_tree_row_reference_get_path (current_context_row_reference);
+  gtk_tree_row_reference_free (current_context_row_reference);
+  current_context_row_reference = NULL;
+  tv = GTK_TREE_VIEW (gtk_builder_get_object
+                      (current_context_search_tab->builder,
+                       "_search_result_frame"));
+  tm = gtk_tree_view_get_model (tv);
+  if (TRUE != gtk_tree_model_get_iter (tm, &iter, path))
+  {
+    GNUNET_break (0);
+    gtk_tree_path_free (path);
+    return;
+  }
+  gtk_tree_model_get (tm, &iter, 1, &uri, -1);
+  gtk_tree_path_free (path);
+  current_context_search_tab = NULL;
+  if (uri == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  uris = GNUNET_FS_uri_to_string (uri);
+  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+  gtk_clipboard_set_text (cb, uris, -1);
+  gtk_clipboard_store (cb);
+  GNUNET_free (uris);
+}
+
+
 static gboolean
 search_list_popup (GtkTreeView *tv, struct SearchTab *tab, GdkEventButton 
*event_button)
 {
@@ -813,346 +1228,10 @@
   return de;
 }
 
-/**
- * This should get the default download directory
- * (so that GNUnet won't offer the user to download files
- * to the 'bin' subdirectory, or whatever is the cwd).
- * Returns NULL on failure (such as non-existend directory).
- * Should also preserve the last setting (so if the user
- * saves files somewhere else, next time we default to
- * somewhere else, at least until application restart, or maybe even
- * between application restarts).
- * Fills the @buffer up to @size bytes, returns a pointer to it.
- */
-static char *
-get_default_download_directory (char *buffer, size_t size)
-{
-  return NULL;
-}
 
-/**
- * Called recursively to build a suggested filename by
- * prepending suggested names for its parent directories (if any).
- * Might return NULL. Returned name might be absolute.
- * local_parents is set to GNUNET_YES if all parents are directories,
- * and are downloaded.
- */
-static char *
-get_suggested_filename_anonymity (GtkTreeModel *tm, GtkTreeIter *iter, int top,
-    int *local_parents, int *anonymity, int *filename_is_absolute)
-{
-  char *result;
-  char *dirname;
-  char *local_filename, *filename;
-  int downloaded_anonymity;
-  int have_a_parent;
-  struct GNUNET_CONTAINER_MetaData *meta;
-  GtkTreeIter parent;
-  gtk_tree_model_get (tm, iter, 0, &meta, 15, &local_filename, 16, 
&downloaded_anonymity, -1);
-  if (local_filename == NULL && !top)
-    *local_parents = GNUNET_NO;
-  if (downloaded_anonymity != -1 && *anonymity == -1 && !top)
-    *anonymity = downloaded_anonymity;
-  if (gtk_tree_model_iter_parent (tm, &parent, iter))
-  {
-    have_a_parent = GNUNET_YES;
-    dirname = get_suggested_filename_anonymity (tm, &parent, GNUNET_NO, 
local_parents, anonymity, filename_is_absolute);
-  }
-  else
-  {
-    have_a_parent = GNUNET_NO;
-    dirname = NULL;
-    if (top)
-      *local_parents = GNUNET_NO;
-  }
-  if (local_filename == NULL)
-    filename = GNUNET_FS_meta_data_suggest_filename (meta);
-  else
-  {
-    /* This directory was downloaded as /foo/bar/baz/somedirname
-     * Hopefully, "somedirname" is actually "somedir.gnd"
-     * We need to strip the ".gnd" part to get "somedir", which is
-     * what we're going to use instead of suggested original filename
-     * Without the .gnd extension we're going to just use a copy
-     * of the directory file name - and that would fail. Sad.
-     */
-    const char *basename; 
-    if (dirname == NULL && !have_a_parent)
-    {
-      /* This is the ealderlest parent directory. Use absolute path. */
-      basename = (const char *) local_filename;
-      *filename_is_absolute = GNUNET_YES;
-    }
-    else
-      basename = GNUNET_STRINGS_get_short_name (local_filename);
-    if (basename != NULL && strlen (basename) > 0)
-    {
-      char *dot;
-      filename = GNUNET_strdup (basename);
-      dot = strrchr (filename, '.');
-      if (dot)
-        *dot = '\0';
-    }
-    else
-      filename = GNUNET_FS_meta_data_suggest_filename (meta);
-  }
-  if (dirname && filename)
-  {
-    GNUNET_asprintf (&result, "%s%s%s", dirname, DIR_SEPARATOR_STR, filename);
-    GNUNET_free (filename);
-    GNUNET_free (dirname);
-    return result;
-  }
-  else if (filename)
-    return filename;
-  else
-    return NULL;
-}
 
-/**
- * Tell FS to start a download.  Begins by opening the
- * "save as" window.
- */
-static void
-start_download (GtkTreeView * tree_view, GtkTreePath * path,
-                GtkTreeViewColumn * column, gpointer user_data)
-{
-  struct StartDownloadContext *sdc = user_data;
-  struct SearchTab *tab = sdc->tab;
-  GtkTreeModel *tm;
-  GtkTreeIter iter;
-  struct GNUNET_FS_Uri *uri;
-  struct GNUNET_CONTAINER_MetaData *meta;
-  struct SearchResult *sr;
-  gchar *mime;
-  struct DownloadContext *dc;
-  char *buf = NULL;
-  char *tmp;
-  size_t tmplen;
-  char cwd[FILENAME_MAX];
-  char *download_directory;
-  char *filename;
-  int local_parents;
-  int have_a_suggestion;
-  int anonymity;
-  int filename_is_absolute;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-      "Starting a %sdownload\n", sdc->recursive ? "recursive " : "");
 
-  GNUNET_assert (tab != NULL);
-  tm = gtk_tree_view_get_model (tree_view);
-  if (TRUE != gtk_tree_model_get_iter (tm, &iter, path))
-  {
-    GNUNET_break (0);
-    GNUNET_free (sdc);
-    return;
-  }
-  gtk_tree_model_get (tm, &iter, 0, &meta, 1, &uri, 9, &sr, 10, &mime, -1);
-  if (NULL == uri)
-  {
-    /* user clicked on directory that was opened (not downloaded!), so we
-       have no URI and downloading makes no sense. Ignore! */
-    if (NULL != mime)
-      g_free (mime);
-    return;
-  }
-  if (!(GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri)))
-  {
-    /* can only download chk/loc URIs, ignore */
-    g_free (mime);
-    return;
-  }
-
-  download_directory = get_default_download_directory (cwd, sizeof (cwd));
-  /* If no download directory is known, try working directory */
-  if (download_directory == NULL)
-    download_directory = getcwd (cwd, sizeof (cwd));
-  /* Calculate suggested filename */
-  local_parents = GNUNET_YES;
-  anonymity = -1;
-  filename_is_absolute = GNUNET_NO;
-  filename = get_suggested_filename_anonymity (tm, &iter, GNUNET_YES, 
&local_parents, &anonymity, &filename_is_absolute);
-  have_a_suggestion = GNUNET_NO;
-  if (NULL != download_directory)
-  {
-    if (NULL == filename)
-    {
-      buf = GNUNET_strdup (download_directory);
-    }
-    else
-    {
-      have_a_suggestion = GNUNET_YES;
-      if (filename_is_absolute)
-        GNUNET_asprintf (&tmp, "%s", filename);
-      else
-        GNUNET_asprintf (&tmp, "%s%s%s",
-                        download_directory,
-                        DIR_SEPARATOR_STR,
-                        filename);
-      tmplen = strlen (tmp);
-      /* now, if we have a directory, replace trailing '/' with ".gnd" */
-      if (GNUNET_YES ==
-         GNUNET_FS_meta_data_test_for_directory (meta))
-      {
-       if ( (tmp[tmplen-1] == '/') ||
-            (tmp[tmplen-1] == '\\') )
-         tmp[tmplen-1] = '\0';
-       GNUNET_asprintf (&buf,
-                        "%s%s",
-                        tmp,
-                        GNUNET_FS_DIRECTORY_EXT);
-       GNUNET_free (tmp);           
-      }
-      else
-      {
-       buf = tmp;
-      }
-    }
-  }
-  GNUNET_free_non_null (filename);
-
-  /* now setup everything for the save-as dialog */
-  dc = GNUNET_malloc (sizeof (struct DownloadContext));
-  dc->uri = GNUNET_FS_uri_dup (uri);
-  dc->mime = mime;
-  dc->filename = buf;
-  dc->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
-  dc->rr = gtk_tree_row_reference_new (tm, path);
-  dc->sr = sr->result;
-  dc->anonymity = anonymity;
-  dc->is_recursive = sdc->recursive;
-  dc->tab = tab;
-  if (local_parents && have_a_suggestion)
-    /* Skip the dialog, call directly */
-    GNUNET_FS_GTK_download_context_start_download (dc);
-  else
-    GNUNET_FS_GTK_open_download_as_dialog (dc);
-}
-
-
-
-/**
- * "Download" was selected in the current search context menu.
- */
-static void
-start_download_ctx_menu (GtkMenuItem *item, gpointer user_data)
-{
-  GtkTreePath *path;
-  GtkTreeView *tv;
-  struct StartDownloadContext sdc;
-
-  if (current_context_row_reference == NULL)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  path = gtk_tree_row_reference_get_path (current_context_row_reference);
-  gtk_tree_row_reference_free (current_context_row_reference);
-  current_context_row_reference = NULL;
-  tv = GTK_TREE_VIEW (gtk_builder_get_object
-                      (current_context_search_tab->builder,
-                       "_search_result_frame"));
-  sdc.tab = current_context_search_tab;
-  sdc.recursive = FALSE;
-  start_download (tv, path, NULL, &sdc);
-  gtk_tree_path_free (path);
-  current_context_search_tab = NULL;
-}
-
-/**
- * "Download recursively" was selected in the current search context menu.
- */
-static void
-start_download_recursively_ctx_menu (GtkMenuItem *item, gpointer user_data)
-{
-  GtkTreePath *path;
-  GtkTreeView *tv;
-  struct StartDownloadContext sdc;
-
-  if (current_context_row_reference == NULL)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  path = gtk_tree_row_reference_get_path (current_context_row_reference);
-  gtk_tree_row_reference_free (current_context_row_reference);
-  current_context_row_reference = NULL;
-  tv = GTK_TREE_VIEW (gtk_builder_get_object
-                      (current_context_search_tab->builder,
-                       "_search_result_frame"));
-  sdc.tab = current_context_search_tab;
-  sdc.recursive = TRUE;
-  start_download (tv, path, NULL, &sdc);
-  gtk_tree_path_free (path);
-  current_context_search_tab = NULL;
-}
-
-
-/**
- * Download was selected in the current search context menu.
- */
-static void
-abort_download_ctx_menu (GtkMenuItem *item, gpointer user_data)
-{
-  struct DownloadEntry *de = user_data;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-      "Aborting a download DE=%p\n", de);
-
-  GNUNET_assert (de->dc != NULL);
-  GNUNET_FS_download_stop (de->dc, GNUNET_YES);
-  current_context_search_tab = NULL;
-}
-
-
-/**
- * Copy current URI to clipboard.
- */
-static void
-copy_uri_to_clipboard_ctx_menu (GtkMenuItem *item, gpointer user_data)
-{
-  GtkTreePath *path;
-  GtkTreeView *tv;
-  GtkTreeModel *tm;
-  GtkTreeIter iter;
-  struct GNUNET_FS_Uri *uri;
-  char *uris;
-  GtkClipboard *cb;
-
-  if (current_context_row_reference == NULL)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  path = gtk_tree_row_reference_get_path (current_context_row_reference);
-  gtk_tree_row_reference_free (current_context_row_reference);
-  current_context_row_reference = NULL;
-  tv = GTK_TREE_VIEW (gtk_builder_get_object
-                      (current_context_search_tab->builder,
-                       "_search_result_frame"));
-  tm = gtk_tree_view_get_model (tv);
-  if (TRUE != gtk_tree_model_get_iter (tm, &iter, path))
-  {
-    GNUNET_break (0);
-    gtk_tree_path_free (path);
-    return;
-  }
-  gtk_tree_model_get (tm, &iter, 1, &uri, -1);
-  gtk_tree_path_free (path);
-  current_context_search_tab = NULL;
-  if (uri == NULL)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  uris = GNUNET_FS_uri_to_string (uri);
-  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
-  gtk_clipboard_set_text (cb, uris, -1);
-  gtk_clipboard_store (cb);
-  GNUNET_free (uris);
-}
-
 gboolean
 search_list_on_popup (GtkWidget *widget, gpointer user_data)
 {
@@ -1375,6 +1454,46 @@
 }
 
 
+static void
+free_search_result (struct SearchResult *sr)
+{
+  GtkTreePath *tp;
+  GtkTreeModel *tm;
+  GtkTreeIter iter;
+  struct GNUNET_FS_Uri *uri;
+  struct GNUNET_CONTAINER_MetaData *meta;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "Freeing a search result SR=%p\n", sr);
+
+  if (sr == NULL)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_assert (sr->rr != NULL);
+  tp = gtk_tree_row_reference_get_path (sr->rr);
+  GNUNET_assert (tp != NULL);
+  tm = gtk_tree_row_reference_get_model (sr->rr);
+  GNUNET_assert (tm != NULL);
+  if (TRUE != gtk_tree_model_get_iter (tm, &iter, tp))
+  {
+    GNUNET_break (0);
+    gtk_tree_path_free (tp);
+    return;
+  }
+  gtk_tree_path_free (tp);
+  gtk_tree_model_get (tm, &iter, 0, &meta, 1, &uri, -1);
+  if (uri != NULL)
+    GNUNET_FS_uri_destroy (uri);
+  if (meta != NULL)
+    GNUNET_CONTAINER_meta_data_destroy (meta);
+  gtk_tree_row_reference_free (sr->rr);
+  (void) gtk_tree_store_remove (GTK_TREE_STORE (tm), &iter);
+  GNUNET_free (sr);
+}
+
+
 /**
  * Stop completed downloads (or those that failed).  Should
  * iterate over the underlying tree store and stop all
@@ -1924,46 +2043,8 @@
 }
 
 
-static void
-free_search_result (struct SearchResult *sr)
-{
-  GtkTreePath *tp;
-  GtkTreeModel *tm;
-  GtkTreeIter iter;
-  struct GNUNET_FS_Uri *uri;
-  struct GNUNET_CONTAINER_MetaData *meta;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-      "Freeing a search result SR=%p\n", sr);
 
-  if (sr == NULL)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  GNUNET_assert (sr->rr != NULL);
-  tp = gtk_tree_row_reference_get_path (sr->rr);
-  GNUNET_assert (tp != NULL);
-  tm = gtk_tree_row_reference_get_model (sr->rr);
-  GNUNET_assert (tm != NULL);
-  if (TRUE != gtk_tree_model_get_iter (tm, &iter, tp))
-  {
-    GNUNET_break (0);
-    gtk_tree_path_free (tp);
-    return;
-  }
-  gtk_tree_path_free (tp);
-  gtk_tree_model_get (tm, &iter, 0, &meta, 1, &uri, -1);
-  if (uri != NULL)
-    GNUNET_FS_uri_destroy (uri);
-  if (meta != NULL)
-    GNUNET_CONTAINER_meta_data_destroy (meta);
-  gtk_tree_row_reference_free (sr->rr);
-  (void) gtk_tree_store_remove (GTK_TREE_STORE (tm), &iter);
-  GNUNET_free (sr);
-}
-
-
 /**
  * Tell FS to stop all active publish operations.  Then close the tab.
  */
@@ -2364,188 +2445,6 @@
   gtk_list_store_clear (ms);
 }
 
-static void
-copy_metadata_to_clipboard (GtkTreeModel * model, GtkTreePath * path,
-                            GtkTreeIter * iter, gpointer user_data)
-{
-  gchar *type, *value;
-  GList **l = (GList **) user_data;
 
-  gtk_tree_model_get (model, iter, 2, &type, 3, &value, -1);
 
-  *l = g_list_prepend (*l, type);
-  *l = g_list_prepend (*l, value);
-}
-
-void
-metadata_copy_selection_activated (GtkMenuItem * menuitem, gpointer user_data)
-{
-  GtkBuilder *builder;
-  GtkTreeView *tree;
-  GtkClipboard *cb;
-  GList *pairs = NULL, *l, *next, *value, *type;
-  guint total_len;
-  gchar *s, *p;
-
-  builder = GTK_BUILDER (user_data);
-  tree =
-      GTK_TREE_VIEW (gtk_builder_get_object
-                     (builder, "GNUNET_GTK_main_window_metadata_treeview"));
-
-  gtk_tree_selection_selected_foreach (gtk_tree_view_get_selection (tree),
-                                       copy_metadata_to_clipboard, &pairs);
-
-  total_len = 0;
-  pairs = g_list_reverse (pairs);
-  for (l = pairs; l; l = next)
-  {
-    type = l;
-    value = l->next;
-    if (!value)
-      break;
-    next = value->next;
-    total_len +=
-        strlen ((gchar *) type->data) + strlen ((gchar *) value->data) +
-        2 /* ": " */  + (next ? 1 : 0) /* "\n" */ ;
-  }
-  if (total_len > 0)
-  {
-    total_len += 1;             /* "\0" */
-    s = g_new0 (gchar, total_len);
-    p = s;
-    for (l = pairs; l; l = next)
-    {
-      type = l;
-      value = l->next;
-      if (value)
-      {
-        next = value->next;
-        p = g_stpcpy (p, (gchar *) type->data);
-        p = g_stpcpy (p, ": ");
-        p = g_stpcpy (p, (gchar *) value->data);
-        if (next)
-          p = g_stpcpy (p, "\n");
-      }
-      else
-        next = NULL;
-    }
-  }
-  g_list_foreach (pairs, (GFunc) g_free, NULL);
-  g_list_free (pairs);
-  pairs = NULL;
-
-  if (total_len > 0)
-  {
-    cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
-    gtk_clipboard_set_text (cb, s, -1);
-    gtk_clipboard_store (cb);
-    g_free (s);
-  }
-}
-
-void
-metadata_menu_popup_position (GtkMenu * menu, gint * x, gint * y,
-                              gboolean * push_in, gpointer user_data)
-{
-  GtkBuilder *builder;
-  GtkTreeView *tree;
-  GtkTreeSelection *sel;
-  GList *rows;
-  GtkTreePath *p;
-  GtkAllocation tree_allocation;
-  GdkWindow *main_window_gdk;
-  gint mwg_x, mwg_y, t_x, t_y, popup_x, popup_y;
-
-  builder = GTK_BUILDER (user_data);
-
-  tree =
-      GTK_TREE_VIEW (gtk_builder_get_object
-                     (builder, "GNUNET_GTK_main_window_metadata_treeview"));
-
-  gtk_widget_get_allocation (GTK_WIDGET (tree), &tree_allocation);
-
-  main_window_gdk = gtk_widget_get_window (GTK_WIDGET (tree));
-
-  gdk_window_get_origin (main_window_gdk, &mwg_x, &mwg_y);
-
-  t_x = mwg_x + tree_allocation.x;
-  t_y = mwg_y + tree_allocation.y;
-  popup_x = t_x;
-  popup_y = t_y;
-
-  sel = gtk_tree_view_get_selection (tree);
-
-  rows = gtk_tree_selection_get_selected_rows (sel, NULL);
-
-  if (rows->data)
-  {
-    GdkRectangle r;
-
-    p = (GtkTreePath *) rows->data;
-    gtk_tree_view_get_cell_area (tree, p, NULL, &r);
-    popup_x += r.x;
-    popup_y += r.y;
-  }
-
-  g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
-  g_list_free (rows);
-  *x = popup_x;
-  *y = popup_y;
-  *push_in = FALSE;
-}
-
-static void
-do_metadata_popup_menu (GtkWidget * widget, GdkEventButton * event,
-                        gpointer user_data)
-{
-  GtkMenu *menu;
-  GtkBuilder *builder;
-  int button, event_time;
-  GtkMenuPositionFunc mpf = NULL;
-
-  builder = GTK_BUILDER (user_data);
-
-  menu = GTK_MENU (gtk_builder_get_object (builder, "metadata_popup_menu"));
-
-  if (event)
-  {
-    button = event->button;
-    event_time = event->time;
-  }
-  else
-  {
-    button = 0;
-    event_time = gtk_get_current_event_time ();
-  }
-
-  gtk_menu_popup (menu, NULL, NULL, mpf, user_data, button, event_time);
-}
-
-gboolean
-GNUNET_GTK_main_window_metadata_treeview_button_press_event_cb (GtkWidget *
-                                                                widget,
-                                                                GdkEventButton 
*
-                                                                event,
-                                                                gpointer
-                                                                user_data)
-{
-  /* Ignore double-clicks and triple-clicks */
-  if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
-  {
-    do_metadata_popup_menu (widget, event, user_data);
-    return TRUE;
-  }
-
-  return FALSE;
-}
-
-gboolean
-GNUNET_GTK_main_window_metadata_treeview_popup_menu_cb (GtkWidget * widget,
-                                                        gpointer user_data)
-{
-  do_metadata_popup_menu (widget, NULL, user_data);
-  return TRUE;
-}
-
-
 /* end of gnunet-fs-gtk-event_handler.c */

Added: gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_meta_data_context_menu.c
===================================================================
--- gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_meta_data_context_menu.c        
                        (rev 0)
+++ gnunet-gtk/src/fs/gnunet-fs-gtk-main_window_meta_data_context_menu.c        
2012-01-31 22:54:08 UTC (rev 19583)
@@ -0,0 +1,192 @@
+/*
+     This file is part of GNUnet.
+     (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+
+     GNUnet 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, or (at your
+     option) any later version.
+
+     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file src/fs/gnunet-fs-gtk-main_window_meta_data_context_menu.c
+ * @brief context menu for the 'meta data' tree view in the main window
+ * @author Christian Grothoff
+ */
+#include "gnunet-fs-gtk.h"
+#include "gnunet-fs-gtk-download.h"
+#include "gnunet-fs-gtk-event_handler.h"
+#include <string.h>
+
+
+
+/**
+ * Helper function of GNUNET_GTK_FS_metadata_copy_selection_activated
+ * which copies the (selected) entries from the tree view to the
+ * GList.
+ *
+ * @param model the tree model with the data
+ * @param path unused
+ * @param iter position in the model to access
+ * @param user_data 'GList**' where we should store the types and values found
+ */ 
+static void
+copy_metadata_to_clipboard (GtkTreeModel * model, GtkTreePath * path,
+                            GtkTreeIter * iter, gpointer user_data)
+{
+  GList **l = user_data;
+  gchar *type;
+  gchar *value;
+
+  gtk_tree_model_get (model, iter, 2, &type, 3, &value, -1);
+  *l = g_list_prepend (*l, type);
+  *l = g_list_prepend (*l, value);
+}
+
+
+/**
+ * User activated metadata pop up menu "Copy selection" entry.
+ *
+ * @param menuitem the 'copy selection' menu item
+ * @param user_data the GtkBuilder of the main window
+ */
+void
+GNUNET_GTK_FS_metadata_copy_selection_activated (GtkMenuItem * menuitem, 
+                                                gpointer user_data)
+{
+  GtkBuilder *builder = GTK_BUILDER (user_data);
+  GtkTreeView *tree;
+  GtkClipboard *cb;
+  GList *pairs;
+  GList *pos;
+  GList *value;
+  GList *type;
+  guint total_len;
+  gchar *s;
+  gchar *p;
+
+  tree =
+      GTK_TREE_VIEW (gtk_builder_get_object
+                     (builder, "GNUNET_GTK_main_window_metadata_treeview"));
+  pairs = NULL;
+  gtk_tree_selection_selected_foreach (gtk_tree_view_get_selection (tree),
+                                       &copy_metadata_to_clipboard, &pairs);
+  if (NULL == pairs)
+    return; /* nothing selected */
+  total_len = 0;
+  pairs = g_list_reverse (pairs);
+  for (pos = pairs; NULL != pos; pos = value->next)
+  {
+    type = pos;
+    value = pos->next;
+    GNUNET_assert (NULL != value);
+    total_len +=
+      strlen ((gchar *) type->data) + strlen ((gchar *) value->data) +
+      2 /* ": " */  + ((NULL != value->next) ? 1 : 0) /* "\n" */ ;
+  }
+  GNUNET_assert (total_len > 0);
+  total_len++;             /* "\0" */
+  s = g_new0 (gchar, total_len);
+  if (NULL == s)
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
+    return;
+  }
+  p = s;
+  for (pos = pairs; NULL != pos; pos = value->next)
+  {
+    type = pos;
+    value = pos->next;
+    GNUNET_assert (NULL != value);
+    p = g_stpcpy (p, (gchar *) type->data);
+    p = g_stpcpy (p, ": ");
+    p = g_stpcpy (p, (gchar *) value->data);
+    if (NULL != value->next)
+      p = g_stpcpy (p, "\n");        
+  }
+  g_list_foreach (pairs, (GFunc) &g_free, NULL);
+  g_list_free (pairs);
+  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
+  gtk_clipboard_set_text (cb, s, -1);
+  gtk_clipboard_store (cb);
+  g_free (s);  
+}
+
+
+/**
+ * Got asked to pop up the context menu in the metadata treeview in
+ * the main window.  Do it.
+ *
+ * @param button which button caused the event (0 for none)
+ * @param event_time time of the event (current time or 'event->time')
+ * @param user_data the gtk builder of the main window
+ */
+static void
+do_metadata_popup_menu (int button,
+                       int event_time,
+                        GtkBuilder *builder)
+{
+  GtkMenu *menu;
+
+  menu = GTK_MENU (gtk_builder_get_object (builder, "metadata_popup_menu"));
+  gtk_menu_popup (menu, NULL, NULL, NULL, builder, button, event_time);
+}
+
+
+/**
+ * Got a button press event on the metadata treeview in the main window.
+ * If it was a right click, pop up the context menu.
+ *
+ * @param widget the tree view widget
+ * @param user_data the gtk builder of the main window
+ */
+gboolean
+GNUNET_GTK_main_window_metadata_treeview_button_press_event_cb (GtkWidget *
+                                                                widget,
+                                                                GdkEventButton 
*
+                                                                event,
+                                                                gpointer
+                                                                user_data)
+{
+  GtkBuilder *builder = GTK_BUILDER (user_data);
+
+  /* Ignore double-clicks and triple-clicks */
+  if ( (event->button != 3) || (event->type != GDK_BUTTON_PRESS) )
+    return FALSE;
+  do_metadata_popup_menu (event->button,
+                         event->time,
+                         builder);
+  return TRUE;
+}
+
+
+/**
+ * Metadata treeview in the main window got the 'popup-menu' signal.
+ * Pop up the menu.
+ *
+ * @param widget the tree view widget
+ * @param user_data the gtk builder of the main window
+ */
+gboolean
+GNUNET_GTK_main_window_metadata_treeview_popup_menu_cb (GtkWidget * widget,
+                                                        gpointer user_data)
+{
+  GtkBuilder *builder = GTK_BUILDER (user_data);
+
+  do_metadata_popup_menu (0 /* no button */,
+                         gtk_get_current_event_time (),
+                         builder);
+  return TRUE;
+}
+
+/* end of gnunet-fs-gtk-meta_data_context_menu.c */




reply via email to

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