[seahorse/refactor: 16/33] Use the new importer infrastructure in libgcr



commit fa4224188a4b0e9f33b2d1e984833ba894a22151
Author: Stef Walter <stefw collabora co uk>
Date:   Thu Sep 22 18:40:37 2011 +0200

    Use the new importer infrastructure in libgcr
    
     * Show a preview of what we're going to import before importing.

 libseahorse/seahorse-viewer.c    |   67 ----------------
 pgp/seahorse-hkp-source.c        |    4 +-
 pgp/seahorse-keyserver-results.c |   83 +++++++++++++++++++--
 pgp/seahorse-ldap-source.c       |    1 +
 pgp/seahorse-transfer.c          |    2 +-
 src/Makefile.am                  |    1 +
 src/seahorse-import-dialog.c     |  157 ++++++++++++++++++++++++++++++++++++++
 src/seahorse-import-dialog.h     |   52 +++++++++++++
 src/seahorse-key-manager.c       |  156 ++++++++------------------------------
 9 files changed, 323 insertions(+), 200 deletions(-)
---
diff --git a/libseahorse/seahorse-viewer.c b/libseahorse/seahorse-viewer.c
index 2753c8b..62bc38a 100644
--- a/libseahorse/seahorse-viewer.c
+++ b/libseahorse/seahorse-viewer.c
@@ -32,7 +32,6 @@
 #include "seahorse-view.h"
 #include "seahorse-viewer.h"
 
-
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
@@ -55,7 +54,6 @@ struct _SeahorseViewerPrivate {
 	GtkUIManager *ui_manager;
 	GtkActionGroup *object_actions;
 	GtkActionGroup *export_actions;
-	GtkActionGroup *import_actions;
 	GtkAction *delete_action;
 	GArray *predicates;
 	GList *all_commands;
@@ -71,7 +69,6 @@ G_DEFINE_TYPE_EXTENDED (SeahorseViewer, seahorse_viewer, SEAHORSE_TYPE_WIDGET, 0
 /* Predicates which control export and delete commands, inited in class_init */
 static SeahorsePredicate exportable_predicate = { 0, };
 static SeahorsePredicate deletable_predicate = { 0, };
-static SeahorsePredicate importable_predicate = { 0, };
 
 /* -----------------------------------------------------------------------------
  * INTERNAL
@@ -444,54 +441,6 @@ on_key_delete (GtkAction* action, SeahorseViewer* self)
 	g_list_free (objects);
 }
 
-#if REFACTOR_IMPORT
-static void
-on_import_complete (GObject *source,
-                    GAsyncResult *result,
-                    gpointer user_data)
-{
-	SeahorseViewer *self = SEAHORSE_VIEWER (user_data);
-	GError *error = NULL;
-
-	if (!seahorse_context_transfer_objects_finish (SEAHORSE_CONTEXT (source),
-	                                               result, &error))
-		seahorse_util_handle_error (&error, seahorse_viewer_get_window (self),
-		                            _("Couldn't import keys"));
-	else
-		seahorse_viewer_set_status (self, _ ("Imported keys"));
-
-	g_object_unref (self);
-}
-#endif
-
-static void
-on_key_import_keyring (GtkAction* action, SeahorseViewer* self)
-{
-#ifdef REFACTOR_IMPORT
-	GCancellable *cancellable;
-	GList* objects;
-
-	g_return_if_fail (SEAHORSE_IS_VIEWER (self));
-	g_return_if_fail (GTK_IS_ACTION (action));
-
-	objects = seahorse_viewer_get_selected_objects (self);
-	objects = objects_prune_non_exportable (objects);
-
-	/* No objects, nothing to do */
-	if (objects == NULL)
-		return;
-
-	cancellable = g_cancellable_new ();
-	seahorse_context_transfer_objects_async (seahorse_context_instance (),
-	                                         objects, NULL, cancellable,
-	                                         on_import_complete, g_object_ref (self));
-	seahorse_progress_show (cancellable, _ ("Importing keys from key servers"), TRUE);
-	g_object_unref (cancellable);
-
-	g_list_free (objects);
-#endif
-}
-
 static gboolean
 show_properties_for_selected (SeahorseViewer *self,
                               SeahorseCommands *commands,
@@ -524,11 +473,6 @@ static const GtkActionEntry EXPORT_ENTRIES[] = {
 	  N_("Copy to the clipboard"), G_CALLBACK (on_key_export_clipboard) }
 };
 
-static const GtkActionEntry IMPORT_ENTRIES[] = {
-	{ "key-import-keyring", GTK_STOCK_ADD, N_("_Import"), "",
-	  N_("Import selected keys to local key ring"), G_CALLBACK (on_key_import_keyring) }
-};
-
 static void
 include_basic_actions (SeahorseViewer* self)
 {
@@ -557,12 +501,6 @@ include_basic_actions (SeahorseViewer* self)
 	gtk_action_group_set_translation_domain (pv->export_actions, GETTEXT_PACKAGE);
 	gtk_action_group_add_actions (pv->export_actions, EXPORT_ENTRIES, G_N_ELEMENTS (EXPORT_ENTRIES), self);
 	seahorse_viewer_include_actions (self, pv->export_actions);
-
-	pv->import_actions = gtk_action_group_new ("import");
-	gtk_action_group_set_translation_domain (pv->import_actions, GETTEXT_PACKAGE);
-	gtk_action_group_add_actions (pv->import_actions, IMPORT_ENTRIES, G_N_ELEMENTS (IMPORT_ENTRIES), self);
-	g_object_set (gtk_action_group_get_action (pv->import_actions, "key-import-keyring"), "is-important", TRUE, NULL);
-	seahorse_viewer_include_actions (self, pv->import_actions);
 }
 
 static void
@@ -585,10 +523,6 @@ on_selection_changed (SeahorseView* view, SeahorseViewer* self)
 	gtk_action_group_set_sensitive (pv->export_actions,
 	                                has_matching_objects (&exportable_predicate, objects));
 
-    /* Enable if any importable objects are selected */
-    gtk_action_group_set_sensitive (pv->import_actions,
-	                                has_matching_objects (&importable_predicate, objects));
-
 	/* Enable if any deletable objects are selected */
 	gtk_action_set_sensitive (pv->delete_action,
 	                          has_matching_objects (&deletable_predicate, objects));
@@ -823,7 +757,6 @@ seahorse_viewer_class_init (SeahorseViewerClass *klass)
 
 	exportable_predicate.flags = SEAHORSE_FLAG_EXPORTABLE;
 	deletable_predicate.flags = SEAHORSE_FLAG_DELETABLE;
-	importable_predicate.flags = SEAHORSE_FLAG_EXPORTABLE;
 }
 
 static void
diff --git a/pgp/seahorse-hkp-source.c b/pgp/seahorse-hkp-source.c
index b27de48..68f1002 100644
--- a/pgp/seahorse-hkp-source.c
+++ b/pgp/seahorse-hkp-source.c
@@ -657,8 +657,10 @@ on_search_message_complete (SoupSession *session,
 
 	} else {
 		keys = parse_hkp_index (message->response_body->data);
-		for (l = keys; l; l = g_list_next (l))
+		for (l = keys; l; l = g_list_next (l)) {
+			g_object_set (l->data, "source", closure->source, NULL);
 			gcr_simple_collection_add (closure->results, l->data);
+		}
 		g_list_free_full (keys, g_object_unref);
 	}
 
diff --git a/pgp/seahorse-keyserver-results.c b/pgp/seahorse-keyserver-results.c
index b672b85..cd82d70 100644
--- a/pgp/seahorse-keyserver-results.c
+++ b/pgp/seahorse-keyserver-results.c
@@ -23,11 +23,13 @@
 #include "config.h"
 
 #include "seahorse-pgp-backend.h"
+#include "seahorse-gpgme-keyring.h"
 #include "seahorse-keyserver-search.h"
 #include "seahorse-keyserver-results.h"
 
 #include "seahorse-key-manager-store.h"
 #include "seahorse-progress.h"
+#include "seahorse-util.h"
 
 #include <glib/gi18n.h>
 
@@ -50,7 +52,7 @@ struct _SeahorseKeyserverResultsPrivate {
 	char *search_string;
 	GtkTreeView *view;
 	GcrSimpleCollection *collection;
-	GtkActionGroup *object_actions;
+	GtkActionGroup *import_actions;
 	SeahorseKeyManagerStore *store;
 	GSettings *settings;
 };
@@ -72,8 +74,8 @@ fire_selection_changed (SeahorseKeyserverResults* self)
 	selection = gtk_tree_view_get_selection (self->pv->view);
 	rows = gtk_tree_selection_count_selected_rows (selection);
 	seahorse_viewer_set_numbered_status (SEAHORSE_VIEWER (self), ngettext ("Selected %d key", "Selected %d keys", rows), rows);
-	if (self->pv->object_actions)
-		gtk_action_group_set_sensitive (self->pv->object_actions, rows > 0);
+	if (self->pv->import_actions)
+		gtk_action_group_set_sensitive (self->pv->import_actions, rows > 0);
 	g_signal_emit_by_name (G_OBJECT (SEAHORSE_VIEW (self)), "selection-changed");
 	return FALSE;
 }
@@ -188,7 +190,67 @@ on_remote_find (GtkAction* action, SeahorseKeyserverResults* self)
 	seahorse_keyserver_search_show (seahorse_viewer_get_window (SEAHORSE_VIEWER (self)));
 }
 
+static void
+on_import_complete (GObject *source,
+                    GAsyncResult *result,
+                    gpointer user_data)
+{
+	SeahorseViewer *self = SEAHORSE_VIEWER (user_data);
+	GError *error = NULL;
+
+	if (!seahorse_pgp_backend_transfer_finish (SEAHORSE_PGP_BACKEND (source),
+	                                           result, &error))
+		seahorse_util_handle_error (&error, seahorse_viewer_get_window (self),
+		                            _("Couldn't import keys"));
+	else
+		seahorse_viewer_set_status (self, _ ("Imported keys"));
 
+	g_object_unref (self);
+}
+
+static GList*
+objects_prune_non_exportable (GList *objects)
+{
+	GList *exportable = NULL;
+	GList *l;
+
+	for (l = objects; l; l = g_list_next (l)) {
+		if (seahorse_object_get_flags (l->data) & SEAHORSE_FLAG_EXPORTABLE)
+			exportable = g_list_append (exportable, l->data);
+	}
+
+	g_list_free (objects);
+	return exportable;
+}
+
+static void
+on_key_import_keyring (GtkAction* action, SeahorseViewer* self)
+{
+	GCancellable *cancellable;
+	SeahorsePgpBackend *backend;
+	SeahorseGpgmeKeyring *keyring;
+	GList* objects;
+
+	g_return_if_fail (SEAHORSE_IS_VIEWER (self));
+	g_return_if_fail (GTK_IS_ACTION (action));
+
+	objects = seahorse_viewer_get_selected_objects (self);
+	objects = objects_prune_non_exportable (objects);
+
+	/* No objects, nothing to do */
+	if (objects == NULL)
+		return;
+
+	cancellable = g_cancellable_new ();
+	backend = seahorse_pgp_backend_get ();
+	keyring = seahorse_pgp_backend_get_default_keyring (NULL);
+	seahorse_pgp_backend_transfer_async (backend, objects, SEAHORSE_SOURCE (keyring),
+	                                     cancellable, on_import_complete, g_object_ref (self));
+	seahorse_progress_show (cancellable, _ ("Importing keys from key servers"), TRUE);
+	g_object_unref (cancellable);
+
+	g_list_free (objects);
+}
 
 /**
 * widget: sending widget
@@ -223,6 +285,11 @@ static const GtkActionEntry SERVER_ENTRIES[] = {
 	  N_("Search for keys on a key server"), G_CALLBACK (on_remote_find) }
 };
 
+static const GtkActionEntry IMPORT_ENTRIES[] = {
+	{ "key-import-keyring", GTK_STOCK_ADD, N_("_Import"), "",
+	  N_("Import selected keys to local key ring"), G_CALLBACK (on_key_import_keyring) }
+};
+
 /* -----------------------------------------------------------------------------
  * OBJECT
  */
@@ -307,6 +374,12 @@ seahorse_keyserver_results_constructor (GType type, guint n_props, GObjectConstr
 	gtk_action_group_add_actions (actions, SERVER_ENTRIES, G_N_ELEMENTS (SERVER_ENTRIES), self);
 	seahorse_viewer_include_actions (SEAHORSE_VIEWER (self), actions);
 
+	self->pv->import_actions = gtk_action_group_new ("import");
+	gtk_action_group_set_translation_domain (self->pv->import_actions, GETTEXT_PACKAGE);
+	gtk_action_group_add_actions (self->pv->import_actions, IMPORT_ENTRIES, G_N_ELEMENTS (IMPORT_ENTRIES), self);
+	g_object_set (gtk_action_group_get_action (self->pv->import_actions, "key-import-keyring"), "is-important", TRUE, NULL);
+	seahorse_viewer_include_actions (SEAHORSE_VIEWER (self), self->pv->import_actions);
+
 	/* init key list & selection settings */
 	self->pv->view = GTK_TREE_VIEW (seahorse_widget_get_widget (SEAHORSE_WIDGET (self), "key_list"));
 	selection = gtk_tree_view_get_selection (self->pv->view);
@@ -360,9 +433,7 @@ seahorse_keyserver_results_finalize (GObject *obj)
 	g_free (self->pv->search_string);
 	self->pv->search_string = NULL;
 
-	if (self->pv->object_actions)
-		g_object_unref (self->pv->object_actions);
-	self->pv->object_actions = NULL;
+	g_clear_object (&self->pv->import_actions);
 
 	g_clear_object (&self->pv->collection);
 
diff --git a/pgp/seahorse-ldap-source.c b/pgp/seahorse-ldap-source.c
index a672bbc..03d8cb9 100644
--- a/pgp/seahorse-ldap-source.c
+++ b/pgp/seahorse-ldap-source.c
@@ -844,6 +844,7 @@ search_parse_key_from_ldap_entry (SeahorseLDAPSource *self,
 		seahorse_pgp_key_set_subkeys (key, list);
 		seahorse_object_list_free (list);
 		g_object_set (key, "flags", flags, NULL);
+		g_object_set (key, "source", self, NULL);
 
 		seahorse_pgp_key_realize (key);
 		gcr_simple_collection_add (results, G_OBJECT (key));
diff --git a/pgp/seahorse-transfer.c b/pgp/seahorse-transfer.c
index 93e17d3..7209a13 100644
--- a/pgp/seahorse-transfer.c
+++ b/pgp/seahorse-transfer.c
@@ -200,7 +200,7 @@ seahorse_transfer_async (SeahorseSource *from,
 	                        _("Exporting data") : _("Retrieving data"));
 	seahorse_progress_prep (cancellable, &closure->to,
 	                        SEAHORSE_IS_GPGME_KEYRING (closure->to) ?
-	                        _("Sending data") : _("Importing data"));
+	                        _("Importing data") : _("Sending data"));
 
 	seahorse_debug ("starting export");
 
diff --git a/src/Makefile.am b/src/Makefile.am
index b319c38..2639ea4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,6 +32,7 @@ bin_PROGRAMS = seahorse
 
 seahorse_SOURCES = \
 	seahorse-generate-select.c seahorse-generate-select.h \
+	seahorse-import-dialog.c seahorse-import-dialog.h \
 	seahorse-key-manager.c seahorse-key-manager.h \
 	seahorse-main.c \
 	seahorse-sidebar.c seahorse-sidebar.h \
diff --git a/src/seahorse-import-dialog.c b/src/seahorse-import-dialog.c
new file mode 100644
index 0000000..fb3fd2a
--- /dev/null
+++ b/src/seahorse-import-dialog.c
@@ -0,0 +1,157 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * 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.,
+ * 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Stef Walter <stefw collabora co uk>
+ */
+
+#include "config.h"
+
+#include "seahorse-import-dialog.h"
+
+#include <gcr/gcr.h>
+
+#include <glib/gi18n.h>
+
+struct _SeahorseImportDialog {
+	GtkDialog parent;
+	GcrViewerWidget *viewer;
+	GcrImportButton *import;
+};
+
+struct _SeahorseImportDialogClass {
+	GtkDialogClass parent_class;
+};
+
+G_DEFINE_TYPE (SeahorseImportDialog, seahorse_import_dialog, GTK_TYPE_DIALOG);
+
+static void
+on_viewer_renderer_added (GcrViewerWidget *viewer,
+                          GcrRenderer *renderer,
+                          GcrParser *parser,
+                          gpointer user_data)
+{
+	SeahorseImportDialog *self = SEAHORSE_IMPORT_DIALOG (user_data);
+	gcr_import_button_add_parsed (self->import, parser);
+}
+
+static void
+seahorse_import_dialog_init (SeahorseImportDialog *self)
+{
+
+}
+
+static void
+on_import_button_imported (GcrImportButton *button,
+                           GcrImporter *importer,
+                           GError *error,
+                           gpointer user_data)
+{
+	SeahorseImportDialog *self = SEAHORSE_IMPORT_DIALOG (user_data);
+	GcrRenderer *renderer;
+
+	if (error == NULL) {
+		gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
+
+	} else {
+		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+			renderer = gcr_failure_renderer_new (_("Import failed"), error);
+			gcr_viewer_add_renderer (GCR_VIEWER (self->viewer), renderer);
+			g_object_unref (renderer);
+		}
+	}
+}
+
+static void
+seahorse_import_dialog_constructed (GObject *obj)
+{
+	SeahorseImportDialog *self = SEAHORSE_IMPORT_DIALOG (obj);
+	GtkWidget *button;
+
+	G_OBJECT_CLASS (seahorse_import_dialog_parent_class)->constructed (obj);
+
+	button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
+	gtk_widget_show (button);
+	gtk_dialog_add_action_widget (GTK_DIALOG (self), GTK_WIDGET (button), GTK_RESPONSE_CANCEL);
+
+	self->import = gcr_import_button_new (_("Import"));
+	g_signal_connect_object (self->import, "imported",
+	                         G_CALLBACK (on_import_button_imported),
+	                         self, 0);
+	gtk_widget_show (GTK_WIDGET (self->import));
+	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_action_area (GTK_DIALOG (self))),
+	                    GTK_WIDGET (self->import), FALSE, TRUE, 0);
+
+	self->viewer = gcr_viewer_widget_new ();
+	g_signal_connect_object (self->viewer, "added",
+	                         G_CALLBACK (on_viewer_renderer_added),
+	                         self, 0);
+	gtk_widget_show (GTK_WIDGET (self->viewer));
+
+	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))),
+	                    GTK_WIDGET (self->viewer), TRUE, TRUE, 0);
+}
+
+static void
+seahorse_import_dialog_class_init (SeahorseImportDialogClass *klass)
+{
+	GObjectClass *gobject_class= G_OBJECT_CLASS (klass);
+
+	gobject_class->constructed = seahorse_import_dialog_constructed;
+
+	g_type_class_add_private (klass, sizeof (SeahorseImportDialog));
+}
+
+GtkDialog *
+seahorse_import_dialog_new (GtkWindow *parent)
+{
+	g_return_val_if_fail (GTK_WINDOW (parent), NULL);
+
+	return g_object_new (SEAHORSE_TYPE_IMPORT_DIALOG,
+	                     "transient-for", parent,
+	                     NULL);
+}
+
+void
+seahorse_import_dialog_add_uris (SeahorseImportDialog *self,
+                                 const gchar **uris)
+{
+	GFile *file;
+	guint i;
+
+	g_return_if_fail (SEAHORSE_IS_IMPORT_DIALOG (self));
+	g_return_if_fail (uris != NULL);
+
+	for (i = 0; uris[i] != NULL; i++) {
+		file = g_file_new_for_uri (uris[i]);
+		gcr_viewer_widget_load_file (self->viewer, file);
+		g_object_unref (file);
+	}
+}
+
+void
+seahorse_import_dialog_add_text (SeahorseImportDialog *self,
+                                 const gchar *display_name,
+                                 const gchar *text)
+{
+	g_return_if_fail (SEAHORSE_IS_IMPORT_DIALOG (self));
+	g_return_if_fail (text != NULL);
+	gcr_viewer_widget_load_data (self->viewer, display_name,
+	                             text, strlen (text));
+}
diff --git a/src/seahorse-import-dialog.h b/src/seahorse-import-dialog.h
new file mode 100644
index 0000000..f1feb62
--- /dev/null
+++ b/src/seahorse-import-dialog.h
@@ -0,0 +1,52 @@
+/*
+ * Seahorse
+ *
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * 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.,
+ * 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Stef Walter <stefw collabora co uk>
+ */
+
+#ifndef __SEAHORSE_IMPORT_DIALOG_H__
+#define __SEAHORSE_IMPORT_DIALOG_H__
+
+#include <gcr/gcr.h>
+
+#include <gtk/gtk.h>
+
+#define SEAHORSE_TYPE_IMPORT_DIALOG             (seahorse_import_dialog_get_type ())
+#define SEAHORSE_IMPORT_DIALOG(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEAHORSE_TYPE_IMPORT_DIALOG, SeahorseImportDialog))
+#define SEAHORSE_IMPORT_DIALOG_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), SEAHORSE_TYPE_IMPORT_DIALOG, SeahorseImportDialogClass))
+#define SEAHORSE_IS_IMPORT_DIALOG(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SEAHORSE_TYPE_IMPORT_DIALOG))
+#define SEAHORSE_IS_IMPORT_DIALOG_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), SEAHORSE_TYPE_IMPORT_DIALOG))
+#define SEAHORSE_IMPORT_DIALOG_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), SEAHORSE_TYPE_IMPORT_DIALOG_KEY, SeahorseImportDialogClass))
+
+typedef struct _SeahorseImportDialog SeahorseImportDialog;
+typedef struct _SeahorseImportDialogClass SeahorseImportDialogClass;
+
+GType               seahorse_import_dialog_get_type           (void);
+
+GtkDialog *         seahorse_import_dialog_new                (GtkWindow *parent);
+
+void                seahorse_import_dialog_add_uris           (SeahorseImportDialog *self,
+                                                               const gchar **uris);
+
+void                seahorse_import_dialog_add_text           (SeahorseImportDialog *self,
+                                                               const gchar *display_name,
+                                                               const gchar *text);
+
+#endif /* __SEAHORSE_IMPORT_DIALOG_H__ */
diff --git a/src/seahorse-key-manager.c b/src/seahorse-key-manager.c
index 5f057f3..442e7a6 100644
--- a/src/seahorse-key-manager.c
+++ b/src/seahorse-key-manager.c
@@ -23,6 +23,7 @@
 #include "config.h"
 
 #include "seahorse-generate-select.h"
+#include "seahorse-import-dialog.h"
 #include "seahorse-key-manager.h"
 #include "seahorse-key-manager-store.h"
 #include "seahorse-preferences.h"
@@ -202,82 +203,21 @@ on_filter_changed (GtkEntry* entry, SeahorseKeyManager* self)
 	g_object_set (self->pv->store, "filter", text, NULL);
 }
 
-#ifdef REFACTOR_IMPORT
-
-static void 
-on_import_complete (GObject *source,
-                    GAsyncResult *result,
-                    gpointer user_data)
-{
-	SeahorseKeyManager* self = SEAHORSE_KEY_MANAGER (user_data);
-	GError *error = NULL;
-
-	if (!seahorse_source_import_finish (SEAHORSE_SOURCE (source), result, &error)) {
-		seahorse_util_handle_error (&error, seahorse_viewer_get_window (SEAHORSE_VIEWER (self)),
-		                            "%s", _("Couldn't import keys"));
-	} else {
-		seahorse_viewer_set_status (SEAHORSE_VIEWER (self), _("Imported keys"));
-	}
-
-	g_object_unref (self);
-}
-
 static void 
-import_files (SeahorseKeyManager* self, const gchar** uris) 
+import_files (SeahorseKeyManager* self,
+              const gchar** uris)
 {
-	GError *error = NULL;
-	GFileInputStream* input;
-	GCancellable *cancellable;
-	const gchar *uri;
-	GString *errmsg;
-	GFile* file;
-
-	g_return_if_fail (SEAHORSE_IS_KEY_MANAGER (self));
-	errmsg = g_string_new ("");
-	cancellable = g_cancellable_new ();
-
-	for (uri = *uris; uri; uris++, uri = *uris) {
-		GQuark ktype;
-		SeahorseSource* sksrc;
-		
-		if(!uri[0])
-			continue;
-			
-		/* Figure out where to import to */
-		ktype = seahorse_util_detect_file_type (uri);
-		if (ktype == 0) {
-			g_string_append_printf (errmsg, "%s: Invalid file format\n", uri);
-			continue;
-		}
-		
-		/* All our supported key types have a local source */
-		sksrc = seahorse_context_find_source (NULL, ktype, SEAHORSE_LOCATION_LOCAL);
-		g_return_if_fail (sksrc != NULL);
-
-		file = g_file_new_for_uri (uri);
-		input = g_file_read (file, NULL, &error);
-		if (error) {
-			g_string_append_printf (errmsg, "%s: %s\n", uri, error->message);
-			g_clear_error (&error);
-			continue;
-		}
-
-		seahorse_source_import_async (sksrc, G_INPUT_STREAM (input),
-		                              cancellable, on_import_complete,
-		                              g_object_ref (self));
-	}
+	GtkDialog *dialog;
+	GtkWindow *parent;
 
-	seahorse_progress_show (cancellable, _("Importing keys"), TRUE);
-	g_object_unref (cancellable);
+	parent = seahorse_viewer_get_window (SEAHORSE_VIEWER (self));
+	dialog = seahorse_import_dialog_new (parent);
+	seahorse_import_dialog_add_uris (SEAHORSE_IMPORT_DIALOG (dialog), uris);
 
-	if (errmsg->len > 0)
-		seahorse_util_show_error (GTK_WIDGET (seahorse_viewer_get_window (SEAHORSE_VIEWER (self))), 
-		                          _("Couldn't import keys"), errmsg->str);
-
-	g_string_free (errmsg, TRUE);
+	gtk_dialog_run (dialog);
+	gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
-
 static void 
 import_prompt (SeahorseKeyManager* self) 
 {
@@ -297,69 +237,41 @@ import_prompt (SeahorseKeyManager* self)
 		uris[1] = NULL;
 		import_files (self, (const gchar**)uris);
 	}
-	
+
 	g_free (uri);
 }
 
-#endif /* REFACTOR_IMPORT */
-
 static void 
 on_key_import_file (GtkAction* action, SeahorseKeyManager* self) 
 {
-#ifdef REFACTOR_IMPORT
 	g_return_if_fail (SEAHORSE_IS_KEY_MANAGER (self));
 	g_return_if_fail (GTK_IS_ACTION (action));
 	import_prompt (self);
-#endif
 }
 
 G_MODULE_EXPORT void 
 on_keymanager_import_button (GtkButton* button, SeahorseKeyManager* self) 
 {
-#ifdef REFACTOR_IMPORT
 	g_return_if_fail (SEAHORSE_IS_KEY_MANAGER (self));
 	g_return_if_fail (GTK_IS_BUTTON (button));
 	import_prompt (self);
-#endif
 }
 
-#ifdef REFACTOR_IMPORT
-
 static void 
-import_text (SeahorseKeyManager* self, const char* text) 
+import_text (SeahorseKeyManager* self,
+             const gchar *display_name,
+             const char* text)
 {
-	glong len;
-	GQuark ktype;
-	SeahorseSource* sksrc;
-	GMemoryInputStream* input;
-	GCancellable *cancellable;
-
-	g_return_if_fail (SEAHORSE_IS_KEY_MANAGER (self));
-	g_return_if_fail (text != NULL);
-	
-	len = g_utf8_strlen (text, -1);
-	ktype = seahorse_util_detect_data_type (text, len);
-	if (ktype == 0) {
-		seahorse_util_show_error (GTK_WIDGET (seahorse_viewer_get_window (SEAHORSE_VIEWER (self))), 
-		                          _("Couldn't import keys"), _("Unrecognized key type, or invalid data format"));
-		return;
-	}
-	
-	/* All our supported key types have a local key source */
-	sksrc = seahorse_context_find_source (seahorse_context_instance (), ktype, SEAHORSE_LOCATION_LOCAL);
-	g_return_if_fail (sksrc != NULL);
+	GtkDialog *dialog;
+	GtkWindow *parent;
 
-	input = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (g_strndup (text, len),
-	                                                                    len, g_free));
+	parent = seahorse_viewer_get_window (SEAHORSE_VIEWER (self));
+	dialog = seahorse_import_dialog_new (parent);
+	seahorse_import_dialog_add_text (SEAHORSE_IMPORT_DIALOG (dialog),
+	                                 display_name, text);
 
-	cancellable = g_cancellable_new ();
-	seahorse_source_import_async (sksrc, G_INPUT_STREAM (input), cancellable,
-	                              on_import_complete, g_object_ref (self));
-
-	seahorse_progress_show (cancellable, _("Importing Keys"), TRUE);
-	g_object_unref (cancellable);
-
-	g_object_unref (input);
+	gtk_dialog_run (dialog);
+	gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 static void 
@@ -378,7 +290,7 @@ on_target_drag_data_received (GtkWindow* window, GdkDragContext* context, gint x
 	
 	if (info == TARGETS_PLAIN) {
 		text = gtk_selection_data_get_text (selection_data);
-		import_text (self, (gchar*)text);
+		import_text (self, _("Dropped text"), (gchar*)text);
 		g_free (text);
 	} else if (info == TARGETS_URIS) {
 		uris = gtk_selection_data_get_uris (selection_data);
@@ -396,30 +308,26 @@ on_clipboard_received (GtkClipboard* board, const char* text, SeahorseKeyManager
 	g_return_if_fail (GTK_IS_CLIPBOARD (board));
 	g_return_if_fail (text != NULL);
 
-    g_assert(self->pv->filter_entry);
-    if (gtk_widget_is_focus (GTK_WIDGET (self->pv->filter_entry)) == TRUE)
-	    gtk_editable_paste_clipboard (GTK_EDITABLE (self->pv->filter_entry));
-    else	
-    	if (text != NULL && g_utf8_strlen (text, -1) > 0)
-    		import_text (self, text);
+	g_assert(self->pv->filter_entry);
+	if (gtk_widget_is_focus (GTK_WIDGET (self->pv->filter_entry)) == TRUE)
+		gtk_editable_paste_clipboard (GTK_EDITABLE (self->pv->filter_entry));
+	else
+		if (text != NULL && g_utf8_strlen (text, -1) > 0)
+			import_text (self, _("Clipboard text"), text);
 }
 
-#endif /* REFACTOR_IMPORT */
-
 static void 
 on_key_import_clipboard (GtkAction* action, SeahorseKeyManager* self) 
 {
-#ifdef REFACTOR_IMPORT
 	GdkAtom atom;
 	GtkClipboard* board;
-	
+
 	g_return_if_fail (SEAHORSE_IS_KEY_MANAGER (self));
 	g_return_if_fail (GTK_IS_ACTION (action));
-	
+
 	atom = gdk_atom_intern ("CLIPBOARD", FALSE);
 	board = gtk_clipboard_get (atom);
 	gtk_clipboard_request_text (board, (GtkClipboardTextReceivedFunc)on_clipboard_received, self);
-#endif
 }
 
 static gboolean
@@ -759,10 +667,8 @@ seahorse_key_manager_constructed (GObject *object)
 	gtk_target_list_add_text_targets (targets, TARGETS_PLAIN);
 	gtk_drag_dest_set_target_list (GTK_WIDGET (seahorse_viewer_get_window (SEAHORSE_VIEWER (self))), targets);
 
-#ifdef REFACTOR_IMPORT
 	g_signal_connect_object (seahorse_viewer_get_window (SEAHORSE_VIEWER (self)), "drag-data-received", 
 	                         G_CALLBACK (on_target_drag_data_received), self, 0);
-#endif
 
 #ifdef REFACTOR_FIRST
 	/* To show first time dialog */



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