From b9995d7f7363fcf22ec3f1240105d99bef581e95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?=
 =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= <lrn1986@gmail.com>
Date: Tue, 13 Dec 2011 16:02:39 +0400
Subject: [PATCH 2/3] Various changes

* Make preview sensitive (prevent graying-out)
* Enable fs persistence and probes (crashes, but who said it was going
  to be easy?
* Make Save File dialog non-blocking
---
 contrib/gnunet_fs_gtk_download_as_dialog.glade |  106 +++++++++-------
 contrib/gnunet_fs_gtk_main_window.glade        |   17 ++--
 src/fs/gnunet-fs-gtk-download.c                |  154 +++++++++++++++++------
 src/fs/gnunet-fs-gtk.c                         |    5 +-
 4 files changed, 184 insertions(+), 98 deletions(-)

diff --git a/contrib/gnunet_fs_gtk_download_as_dialog.glade b/contrib/gnunet_fs_gtk_download_as_dialog.glade
index 74aa83f..d15ca72 100644
--- a/contrib/gnunet_fs_gtk_download_as_dialog.glade
+++ b/contrib/gnunet_fs_gtk_download_as_dialog.glade
@@ -9,6 +9,7 @@
     <property name="page_increment">10</property>
   </object>
   <object class="GtkFileChooserDialog" id="GNUNET_GTK_save_as_dialog">
+    <property name="can_focus">False</property>
     <property name="can_default">True</property>
     <property name="has_default">True</property>
     <property name="border_width">5</property>
@@ -17,26 +18,78 @@
     <property name="destroy_with_parent">True</property>
     <property name="icon_name">document-save-as</property>
     <property name="type_hint">dialog</property>
-    <property name="has_separator">False</property>
-    <property name="use_preview_label">False</property>
     <property name="action">save</property>
     <property name="preview_widget_active">False</property>
+    <property name="use_preview_label">False</property>
+    <signal name="response" handler="GNUNET_GTK_save_as_dialog_response_cb" swapped="no"/>
     <child internal-child="vbox">
       <object class="GtkVBox" id="dialog-vbox4">
         <property name="visible">True</property>
+        <property name="can_focus">False</property>
         <property name="spacing">2</property>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area5">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="GNUNET_GTK_save_as_dialog_cancel_button">
+                <property name="label">gtk-cancel</property>
+                <property name="use_action_appearance">False</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_stock">True</property>
+                <signal name="clicked" handler="GNUNET_GTK_save_as_dialog_cancel_button_clicked_cb" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="GNUNET_GTK_save_as_dialog_ok_button">
+                <property name="label">gtk-save</property>
+                <property name="use_action_appearance">False</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="can_default">True</property>
+                <property name="has_default">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+                <signal name="clicked" handler="GNUNET_GTK_save_as_dialog_ok_button_clicked_cb" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkHBox" id="hbox2">
             <property name="visible">True</property>
+            <property name="can_focus">False</property>
             <child>
               <object class="GtkLabel" id="GNUNET_GTK_save_as_dialog_anonymity_label">
                 <property name="visible">True</property>
+                <property name="can_focus">False</property>
                 <property name="label" translatable="yes">_Anonymity:</property>
                 <property name="use_underline">True</property>
                 <property name="selectable">True</property>
                 <property name="mnemonic_widget">GNUNET_GTK_save_as_dialog_anonymity_spin_button</property>
               </object>
               <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
                 <property name="position">0</property>
               </packing>
             </child>
@@ -46,6 +99,10 @@
                 <property name="can_focus">True</property>
                 <property name="invisible_char">●</property>
                 <property name="caps_lock_warning">False</property>
+                <property name="primary_icon_activatable">False</property>
+                <property name="secondary_icon_activatable">False</property>
+                <property name="primary_icon_sensitive">True</property>
+                <property name="secondary_icon_sensitive">True</property>
                 <property name="adjustment">GNUNET_GTK_anonymity_adjustment</property>
                 <property name="numeric">True</property>
                 <signal name="value-changed" handler="GNUNET_GTK_anonymity_spin_button_value_changed_cb" after="yes" swapped="no"/>
@@ -59,11 +116,11 @@
             <child>
               <object class="GtkCheckButton" id="GNUNET_GTK_save_as_recursive_check_button">
                 <property name="label" translatable="yes">_recursive</property>
+                <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="sensitive">False</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">False</property>
-                <property name="use_action_appearance">False</property>
                 <property name="use_underline">True</property>
                 <property name="draw_indicator">True</property>
               </object>
@@ -81,49 +138,6 @@
             <property name="position">2</property>
           </packing>
         </child>
-        <child internal-child="action_area">
-          <object class="GtkHButtonBox" id="dialog-action_area5">
-            <property name="visible">True</property>
-            <property name="layout_style">end</property>
-            <child>
-              <object class="GtkButton" id="GNUNET_GTK_save_as_dialog_cancel_button">
-                <property name="label">gtk-cancel</property>
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="receives_default">False</property>
-                <property name="use_action_appearance">False</property>
-                <property name="use_stock">True</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">False</property>
-                <property name="position">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkButton" id="GNUNET_GTK_save_as_dialog_ok_button">
-                <property name="label">gtk-save</property>
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="can_default">True</property>
-                <property name="has_default">True</property>
-                <property name="receives_default">True</property>
-                <property name="use_action_appearance">False</property>
-                <property name="use_stock">True</property>
-              </object>
-              <packing>
-                <property name="expand">False</property>
-                <property name="fill">False</property>
-                <property name="position">1</property>
-              </packing>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="pack_type">end</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
       </object>
     </child>
     <action-widgets>
diff --git a/contrib/gnunet_fs_gtk_main_window.glade b/contrib/gnunet_fs_gtk_main_window.glade
index 5183ba9..37dab80 100644
--- a/contrib/gnunet_fs_gtk_main_window.glade
+++ b/contrib/gnunet_fs_gtk_main_window.glade
@@ -575,7 +575,6 @@
                     <property name="width_request">128</property>
                     <property name="height_request">128</property>
                     <property name="visible">True</property>
-                    <property name="sensitive">False</property>
                     <property name="can_focus">False</property>
                     <property name="has_tooltip">True</property>
                     <property name="tooltip_text" translatable="yes">Thumbnail associated with the currently selected content</property>
@@ -759,6 +758,14 @@
     <property name="can_focus">False</property>
     <property name="stock">gtk-index</property>
   </object>
+  <object class="GtkSizeGroup" id="enties_and_comboboxes_sizegroup">
+    <property name="mode">vertical</property>
+    <widgets>
+      <widget name="main_window_search_entry"/>
+      <widget name="main_window_search_anonymity_combobox"/>
+      <widget name="main_window_search_mime_combobox"/>
+    </widgets>
+  </object>
   <object class="GtkListStore" id="main_window_search_anonymity_liststore">
     <columns>
       <!-- column-name Name -->
@@ -786,14 +793,6 @@
       </row>
     </data>
   </object>
-  <object class="GtkSizeGroup" id="enties_and_comboboxes_sizegroup">
-    <property name="mode">vertical</property>
-    <widgets>
-      <widget name="main_window_search_entry"/>
-      <widget name="main_window_search_anonymity_combobox"/>
-      <widget name="main_window_search_mime_combobox"/>
-    </widgets>
-  </object>
   <object class="GtkListStore" id="main_window_search_mime_liststore">
     <columns>
       <!-- column-name MIME -->
diff --git a/src/fs/gnunet-fs-gtk-download.c b/src/fs/gnunet-fs-gtk-download.c
index fe239e5..d9451e1 100644
--- a/src/fs/gnunet-fs-gtk-download.c
+++ b/src/fs/gnunet-fs-gtk-download.c
@@ -27,65 +27,74 @@
 #include "gnunet-fs-gtk.h"
 #include "gnunet-fs-gtk-event_handler.h"
 
+struct dialog_context
+{
+  GtkBuilder *builder;
+  GtkWidget *dialog;
+  gint response;
+  struct DownloadContext *dc;
+};
 
 void
-GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
+GNUNET_GTK_save_as_dialog_ok_button_clicked_cb (GtkButton *button,
+    gpointer user_data)
 {
-  GtkWidget *ad;
-  GtkBuilder *builder;
-  struct GNUNET_FS_Handle *fs;
-  uint64_t len;
+  return;
+  struct dialog_context *dlc = NULL;
+  dlc = g_object_get_data (G_OBJECT (button), "dialog-context");
+  if (dlc != NULL)
+    dlc->response = 1;
+}
+
+void
+GNUNET_GTK_save_as_dialog_cancel_button_clicked_cb (GtkButton *button,
+    gpointer user_data)
+{
+  return;
+  struct dialog_context *dlc = NULL;
+  dlc = g_object_get_data (G_OBJECT (button), "dialog-context");
+  if (dlc != NULL)
+    dlc->response = 2;
+}
+
+gboolean
+GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget *widget, GdkEvent  *event,
+    gpointer user_data)
+{
+  GtkBuilder *builder = NULL;
+  struct dialog_context *dlc = NULL;
+  struct DownloadContext *dc = NULL;
   enum GNUNET_FS_DownloadOptions opt;
-  uint32_t anonymity;
+  struct GNUNET_FS_Handle *fs;
   struct DownloadEntry *de;
   GtkWidget *cb;
+  uint32_t anonymity;
+  uint64_t len;
 
-  builder =
-      GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade");
-  if (builder == NULL)
+  builder = GTK_BUILDER (user_data);
+  dlc = g_object_get_data (G_OBJECT (widget), "dialog-context");
+  if (dlc == NULL)
   {
-    if (dc->rr != NULL)
-      gtk_tree_row_reference_free (dc->rr);
-    GNUNET_free_non_null (dc->mime);
-    GNUNET_free_non_null (dc->filename);
-    GNUNET_FS_uri_destroy (dc->uri);
-    GNUNET_free (dc);
-    return;
+    g_object_unref (G_OBJECT (builder));
+    return FALSE;
   }
+  dc = dlc->dc;
   cb = GTK_WIDGET (gtk_builder_get_object
                    (builder, "GNUNET_GTK_save_as_recursive_check_button"));
-  if (GNUNET_FS_meta_data_test_for_directory (dc->meta))
-    gtk_widget_set_sensitive (cb, TRUE);
-  ad = GTK_WIDGET (gtk_builder_get_object
-                   (builder, "GNUNET_GTK_save_as_dialog"));
-  if (dc->filename != NULL)
-  {
-    char buf[1024];
-
-    if (NULL != getcwd (buf, sizeof (buf)))
-    {
-      if (strlen (buf) + strlen (dc->filename) + 2 < sizeof (buf))
-      {
-        strcat (buf, DIR_SEPARATOR_STR);
-        strcat (buf, dc->filename);
-      }
-      gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (ad), buf);
-    }
-  }
-  if (GTK_RESPONSE_OK != gtk_dialog_run (GTK_DIALOG (ad)))
+  if (GTK_RESPONSE_OK != dlc->response)
   {
-    gtk_widget_destroy (ad);
-    g_object_unref (G_OBJECT (builder));
+    g_object_unref (G_OBJECT (dlc->builder));
     if (dc->rr != NULL)
       gtk_tree_row_reference_free (dc->rr);
     GNUNET_free_non_null (dc->mime);
     GNUNET_free_non_null (dc->filename);
     GNUNET_FS_uri_destroy (dc->uri);
     GNUNET_free (dc);
-    return;
+    g_free (dlc);
+    return FALSE;
   }
   GNUNET_free_non_null (dc->filename);
-  dc->filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (ad));
+  dc->filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlc->dialog));
   dc->is_recursive =
       (TRUE ==
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cb))) ? GNUNET_YES :
@@ -100,7 +109,6 @@ GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
                                   (builder,
                                    "GNUNET_GTK_save_as_dialog_anonymity_spin_button")));
   len = GNUNET_FS_uri_chk_get_file_size (dc->uri);
-  gtk_widget_destroy (ad);
   g_object_unref (G_OBJECT (builder));
   de = GNUNET_malloc (sizeof (struct DownloadEntry));
   de->uri = dc->uri;
@@ -130,6 +138,72 @@ GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
                                             NULL /* parent download ctx */ ));
   }
   GNUNET_free (dc);
+  GNUNET_free (dlc);
+  return FALSE;
+}
+
+void
+GNUNET_GTK_save_as_dialog_response_cb (GtkDialog *dialog,
+    gint response_id, gpointer user_data)
+{
+  struct dialog_context *dlc = NULL;
+  dlc = g_object_get_data (G_OBJECT (dialog), "dialog-context");
+  if (dlc != NULL)
+    dlc->response = response_id;
+  /* dialogs don't get delete-event the way normal windows do,
+   * call the handler manually
+   */
+  GNUNET_GTK_save_as_dialog_delete_event_cb (GTK_WIDGET (dialog), NULL, user_data);
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+void
+GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
+{
+  GtkWidget *ad;
+  GtkBuilder *builder;
+  GtkWidget *cb;
+  struct dialog_context *dlc;
+
+  builder =
+      GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade");
+  if (builder == NULL)
+  {
+    if (dc->rr != NULL)
+      gtk_tree_row_reference_free (dc->rr);
+    GNUNET_free_non_null (dc->mime);
+    GNUNET_free_non_null (dc->filename);
+    GNUNET_FS_uri_destroy (dc->uri);
+    GNUNET_free (dc);
+    return;
+  }
+  cb = GTK_WIDGET (gtk_builder_get_object
+                   (builder, "GNUNET_GTK_save_as_recursive_check_button"));
+  if (GNUNET_FS_meta_data_test_for_directory (dc->meta))
+    gtk_widget_set_sensitive (cb, TRUE);
+  ad = GTK_WIDGET (gtk_builder_get_object
+                   (builder, "GNUNET_GTK_save_as_dialog"));
+  if (dc->filename != NULL)
+  {
+    char buf[1024];
+
+    if (NULL != getcwd (buf, sizeof (buf)))
+    {
+      if (strlen (buf) + strlen (dc->filename) + 2 < sizeof (buf))
+      {
+        strcat (buf, DIR_SEPARATOR_STR);
+        strcat (buf, dc->filename);
+      }
+      gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (ad), buf);
+    }
+  }
+  dlc = g_new0 (struct dialog_context, 1);
+  dlc->builder = builder;
+  dlc->dialog = ad;
+  dlc->response = 0;
+  dlc->dc = dc;
+  g_object_set_data (G_OBJECT (ad), "dialog-context", dlc);
+  gtk_widget_show_all (ad);
 }
 
 /* end of gnunet-fs-gtk-download.c */
diff --git a/src/fs/gnunet-fs-gtk.c b/src/fs/gnunet-fs-gtk.c
index d5a6cb5..aa8d97b 100644
--- a/src/fs/gnunet-fs-gtk.c
+++ b/src/fs/gnunet-fs-gtk.c
@@ -648,9 +648,8 @@ run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
   fs = GNUNET_FS_start (GNUNET_GTK_main_loop_get_configuration (ml),
                         "gnunet-gtk", &GNUNET_GTK_fs_event_handler, NULL,
-                        GNUNET_FS_FLAGS_NONE
-                        /* fixme later for persistence/probes */ ,
-                        /* set other options here later! */
+                        GNUNET_FS_FLAGS_PERSISTENCE |
+                        GNUNET_FS_FLAGS_DO_PROBES,
                         GNUNET_FS_OPTIONS_END);
   if (fs != NULL)
   {
-- 
1.7.4

