[gcr] Parser: Store imported key filename in Parsed & Parser



commit 3a5c4663bc7de0708d7e645dd89d78efed5e58d2
Author: Hashem Nasarat <hashem riseup net>
Date:   Tue Sep 3 06:53:29 2013 -0400

    Parser: Store imported key filename in Parsed & Parser
    
    1. Access to the filename is useful in many ways. Importers, e.g.
    GcrOpenSshImporter, have access to the GcrParsed, and can read the
    filename to preserve filenames of imported keys.
    
    2. When the GcrViewerWindow reads a key to create a parser, store
    the filename as an instance variable of the parser
    
    3. In the parse_...() functions where the GcrParsed is created, store
    the filename as an instance variable via push_parsed (read it from
    the parser).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=705225

 docs/reference/gcr/gcr-sections.txt |    3 +
 gcr/gcr-base.symbols                |    3 +
 gcr/gcr-parser.c                    |   80 ++++++++++++++++++++++++++++-------
 gcr/gcr-parser.h                    |    7 +++
 gcr/tests/test-parser.c             |   41 ++++++++++++++++++
 ui/gcr-viewer-widget.c              |   25 +++++++----
 6 files changed, 134 insertions(+), 25 deletions(-)
---
diff --git a/docs/reference/gcr/gcr-sections.txt b/docs/reference/gcr/gcr-sections.txt
index 3144d00..547a1dd 100644
--- a/docs/reference/gcr/gcr-sections.txt
+++ b/docs/reference/gcr/gcr-sections.txt
@@ -22,6 +22,8 @@ gcr_parser_get_parsed_attributes
 gcr_parser_get_parsed_block
 gcr_parser_get_parsed_bytes
 gcr_parser_get_parsed_format
+gcr_parser_get_filename
+gcr_parser_set_filename
 GcrParsed
 gcr_parsed_ref
 gcr_parsed_unref
@@ -32,6 +34,7 @@ gcr_parsed_get_description
 gcr_parsed_get_format
 gcr_parsed_get_label
 gcr_parser_get_parsed
+gcr_parsed_get_filename
 <SUBSECTION Standard>
 GCR_TYPE_PARSED
 GcrParserPrivate
diff --git a/gcr/gcr-base.symbols b/gcr/gcr-base.symbols
index 935bf84..030e057 100644
--- a/gcr/gcr-base.symbols
+++ b/gcr/gcr-base.symbols
@@ -117,6 +117,7 @@ gcr_parsed_get_attributes
 gcr_parsed_get_bytes
 gcr_parsed_get_data
 gcr_parsed_get_description
+gcr_parsed_get_filename
 gcr_parsed_get_format
 gcr_parsed_get_label
 gcr_parsed_get_type
@@ -126,6 +127,7 @@ gcr_parser_add_password
 gcr_parser_format_disable
 gcr_parser_format_enable
 gcr_parser_format_supported
+gcr_parser_get_filename
 gcr_parser_get_parsed
 gcr_parser_get_parsed_attributes
 gcr_parser_get_parsed_bytes
@@ -140,6 +142,7 @@ gcr_parser_parse_data
 gcr_parser_parse_stream
 gcr_parser_parse_stream_async
 gcr_parser_parse_stream_finish
+gcr_parser_set_filename
 gcr_pkcs11_add_module
 gcr_pkcs11_add_module_from_file
 gcr_pkcs11_certificate_get_attributes
diff --git a/gcr/gcr-parser.c b/gcr/gcr-parser.c
index 23ae80e..109d2d7 100644
--- a/gcr/gcr-parser.c
+++ b/gcr/gcr-parser.c
@@ -127,6 +127,7 @@ struct _GcrParsed {
        GBytes *data;
        gboolean sensitive;
        GcrDataFormat format;
+       gchar *filename;
        struct _GcrParsed *next;
 };
 
@@ -135,6 +136,7 @@ struct _GcrParserPrivate {
        gboolean normal_formats;
        GPtrArray *passwords;
        GcrParsed *parsed;
+       gchar *filename;
 };
 
 G_DEFINE_TYPE (GcrParser, gcr_parser, G_TYPE_OBJECT);
@@ -380,26 +382,33 @@ push_parsed (GcrParser *self,
        parsed->refs = 0;
        parsed->sensitive = sensitive;
        parsed->next = self->pv->parsed;
+       parsed->filename = g_strdup (gcr_parser_get_filename (self));
        self->pv->parsed = parsed;
        return parsed;
 }
 
 static void
-pop_parsed (GcrParser *self,
-            GcrParsed *parsed)
+_gcr_parsed_free (GcrParsed *parsed)
 {
-       g_assert (parsed == self->pv->parsed);
-
-       self->pv->parsed = parsed->next;
-
        gck_builder_clear (&parsed->builder);
-       gck_attributes_unref (parsed->attrs);
+       if (parsed->attrs)
+               gck_attributes_unref (parsed->attrs);
        if (parsed->data)
                g_bytes_unref (parsed->data);
        g_free (parsed->label);
+       g_free (parsed->filename);
        g_free (parsed);
 }
 
+static void
+pop_parsed (GcrParser *self,
+            GcrParsed *parsed)
+{
+       g_assert (parsed == self->pv->parsed);
+       self->pv->parsed = parsed->next;
+       _gcr_parsed_free (parsed);
+}
+
 static gint
 enum_next_password (GcrParser *self, PasswordState *state, const gchar **password)
 {
@@ -1620,7 +1629,6 @@ parse_base64_spkac (GcrParser *self,
        GBytes *bytes;
        gsize n_data;
        gint ret;
-
        data = g_bytes_get_data (dat, &n_data);
 
        if (n_data > PREFIX_LEN && memcmp (PREFIX, data, PREFIX_LEN))
@@ -2583,6 +2591,37 @@ gcr_parsed_get_type (void)
 }
 
 /**
+ * gcr_parser_get_filename:
+ * @self: a parser item
+ *
+ * Get the filename of the parser item.
+ *
+ * Returns: the filename set on the parser, or %NULL
+ */
+const gchar *
+gcr_parser_get_filename (GcrParser *self)
+{
+       g_return_val_if_fail (GCR_IS_PARSER (self), NULL);
+       return self->pv->filename;
+}
+
+/**
+ * gcr_parser_set_filename:
+ * @self: a parser item
+ * @filename: (allow-none): a string of the filename of the parser item
+ *
+ * Sets the filename of the parser item.
+ */
+void
+gcr_parser_set_filename (GcrParser *self,
+                         const gchar *filename)
+{
+       g_return_if_fail (GCR_IS_PARSER (self));
+       g_free (self->pv->filename);
+       self->pv->filename = g_strdup (filename);
+}
+
+/**
  * gcr_parsed_ref:
  * @parsed: a parsed item
  *
@@ -2606,6 +2645,7 @@ gcr_parsed_ref (GcrParsed *parsed)
        copy = g_new0 (GcrParsed, 1);
        copy->refs = 1;
        copy->label = g_strdup (gcr_parsed_get_label (parsed));
+       copy->filename = g_strdup (gcr_parsed_get_filename (parsed));
        copy->attrs = gcr_parsed_get_attributes (parsed);
        copy->format = gcr_parsed_get_format (parsed);
        if (copy->attrs)
@@ -2640,13 +2680,7 @@ gcr_parsed_unref (gpointer parsed)
        g_return_if_fail (parsed != NULL);
 
        if (g_atomic_int_dec_and_test (&par->refs)) {
-               gck_builder_clear (&par->builder);
-               if (par->attrs)
-                       gck_attributes_unref (par->attrs);
-               g_free (par->label);
-               if (par->data)
-                       g_bytes_unref (par->data);
-               g_free (par);
+               _gcr_parsed_free (parsed);
        }
 }
 
@@ -2893,6 +2927,22 @@ gcr_parsed_get_format (GcrParsed *parsed)
 
        return 0;
 }
+
+/**
+ * gcr_parsed_get_filename:
+ * @parsed: a parsed item
+ *
+ * Get the filename of the parsed item.
+ *
+ * Returns: (transfer none): the filename of
+ *          the parsed item, or %NULL
+ */
+const gchar *
+gcr_parsed_get_filename (GcrParsed *parsed)
+{
+       g_return_val_if_fail (parsed != NULL, NULL);
+       return parsed->filename;
+}
 /* ---------------------------------------------------------------------------------
  * STREAM PARSING
  */
diff --git a/gcr/gcr-parser.h b/gcr/gcr-parser.h
index db4bf18..caffdb4 100644
--- a/gcr/gcr-parser.h
+++ b/gcr/gcr-parser.h
@@ -118,6 +118,11 @@ GBytes *                 gcr_parser_get_parsed_bytes       (GcrParser *self);
 
 GcrDataFormat            gcr_parser_get_parsed_format      (GcrParser *self);
 
+const gchar*             gcr_parser_get_filename           (GcrParser *self);
+
+void                     gcr_parser_set_filename           (GcrParser *self,
+                                                           const gchar *filename);
+
 #define                  GCR_TYPE_PARSED                   (gcr_parsed_get_type ())
 
 GType                    gcr_parsed_get_type               (void) G_GNUC_CONST;
@@ -139,6 +144,8 @@ GBytes *                 gcr_parsed_get_bytes              (GcrParsed *parsed);
 
 GcrDataFormat            gcr_parsed_get_format             (GcrParsed *parsed);
 
+const gchar*             gcr_parsed_get_filename           (GcrParsed *parsed);
+
 G_END_DECLS
 
 #endif /* __GCR_PARSER_H__ */
diff --git a/gcr/tests/test-parser.c b/gcr/tests/test-parser.c
index d274d71..c387b85 100644
--- a/gcr/tests/test-parser.c
+++ b/gcr/tests/test-parser.c
@@ -284,6 +284,46 @@ test_parse_stream (void)
        g_object_unref (parser);
 }
 
+static void
+on_parsed_ref (GcrParser *parser,
+               gpointer user_data)
+{
+       GcrParsed **parsed = user_data;
+       g_assert (parsed != NULL);
+       g_assert (*parsed == NULL);
+       *parsed = gcr_parsed_ref (gcr_parser_get_parsed (parser));
+}
+
+static void
+test_parse_filename (void)
+{
+       GcrParser *parser = gcr_parser_new ();
+       GcrParsed *parsed = NULL;
+       GError *error = NULL;
+       gboolean result;
+       gchar *contents;
+       gsize len;
+       GBytes *bytes;
+
+       if (!g_file_get_contents (SRCDIR "/files/cacert.org.cer", &contents, &len, NULL))
+               g_assert_not_reached ();
+
+       bytes = g_bytes_new_take (contents, len);
+       gcr_parser_set_filename (parser, "cacert.org.cer");
+       g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_ref), &parsed);
+
+       result = gcr_parser_parse_bytes (parser, bytes, &error);
+       g_assert_cmpstr (gcr_parser_get_filename (parser), ==, "cacert.org.cer");
+       g_assert_no_error (error);
+       g_assert (result);
+
+       g_bytes_unref (bytes);
+       g_object_unref (parser);
+
+       g_assert (parsed != NULL);
+       g_assert_cmpstr (gcr_parsed_get_filename (parsed), ==, "cacert.org.cer");
+       gcr_parsed_unref (parsed);
+}
 
 int
 main (int argc, char **argv)
@@ -337,6 +377,7 @@ main (int argc, char **argv)
        g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);
        g_test_add_func ("/gcr/parser/parse_stream", test_parse_stream);
        g_test_add_func ("/gcr/parser/parsed_bytes", test_parsed_bytes);
+       g_test_add_func ("/gcr/parser/filename", test_parse_filename);
 
        ret = g_test_run ();
        g_ptr_array_free (strings, TRUE);
diff --git a/ui/gcr-viewer-widget.c b/ui/gcr-viewer-widget.c
index e81d636..d83afce 100644
--- a/ui/gcr-viewer-widget.c
+++ b/ui/gcr-viewer-widget.c
@@ -421,16 +421,11 @@ on_parser_parse_stream_returned (GObject *source,
 
 static void
 update_display_name (GcrViewerWidget *self,
-                     GFile *file)
+                     gchar *display_name)
 {
-       gchar *basename;
-
        if (!self->pv->display_name_explicit) {
-               basename = g_file_get_basename (file);
                g_free (self->pv->display_name);
-               self->pv->display_name = g_filename_display_name (basename);
-               g_free (basename);
-
+               self->pv->display_name = g_strdup (display_name);
                g_object_notify (G_OBJECT (self), "display-name");
        }
 }
@@ -445,9 +440,15 @@ on_file_read_returned (GObject *source,
        GError *error = NULL;
        GFileInputStream *fis;
        GcrRenderer *renderer;
+       gchar *basename, *display_name;
 
        fis = g_file_read_finish (file, result, &error);
-       update_display_name (self, file);
+
+       basename = g_file_get_basename (file);
+       display_name = g_filename_display_name (basename);
+       g_free (basename);
+
+       update_display_name (self, display_name);
 
        if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
                viewer_stop_loading_files (self);
@@ -461,11 +462,15 @@ on_file_read_returned (GObject *source,
                viewer_load_next_file (self);
 
        } else {
-               gcr_parser_parse_stream_async (self->pv->parser, G_INPUT_STREAM (fis),
-                                              self->pv->cancellable, on_parser_parse_stream_returned,
+               gcr_parser_set_filename (self->pv->parser, display_name);
+               gcr_parser_parse_stream_async (self->pv->parser,
+                                              G_INPUT_STREAM (fis),
+                                              self->pv->cancellable,
+                                              on_parser_parse_stream_returned,
                                               self);
                g_object_unref (fis);
        }
+       g_free (display_name);
 }
 
 static void


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