]> Pileus Git - ~andy/gtk/commitdiff
API: cssprovider: Change parsing-error signal
authorBenjamin Otte <otte@redhat.com>
Fri, 17 Jun 2011 05:39:11 +0000 (07:39 +0200)
committerBenjamin Otte <otte@redhat.com>
Fri, 26 Aug 2011 14:26:28 +0000 (16:26 +0200)
Instead of path, line and position, emit the section the error happened
in. This has a lot more information to consume.

gtk/gtkcssprovider.c
gtk/gtkcssprovider.h
gtk/gtkcsssection.c
gtk/gtkcsssectionprivate.h
gtk/gtkmarshalers.list
tests/css/parser/test-css-parser.c
tests/testboxcss.c

index 996fe9d98f4b4d1d613e1037341b915b49728ce4..dd73322ec5bdc0146a1776d89d991092e533e2f4 100644 (file)
@@ -1036,10 +1036,8 @@ G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
 
 static void
 gtk_css_provider_parsing_error (GtkCssProvider  *provider,
-                                const gchar     *path,
-                                guint            line,
-                                guint            position,
-                                const GError *   error)
+                                GtkCssSection   *section,
+                                const GError    *error)
 {
   /* Only emit a warning when we have no error handlers. This is our
    * default handlers. And in this case erroneous CSS files are a bug
@@ -1052,7 +1050,34 @@ gtk_css_provider_parsing_error (GtkCssProvider  *provider,
                                      0,
                                      TRUE))
     {
-      g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
+      GFileInfo *info;
+      GFile *file;
+      const char *path;
+
+      file = gtk_css_section_get_file (section);
+      if (file)
+        {
+          GFileInfo *info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
+
+          if (info)
+            path = g_file_info_get_display_name (info);
+          else
+            path = "<broken file>";
+        }
+      else
+        {
+          info = NULL;
+          path = "<data>";
+        }
+
+      g_warning ("Theme parsing error: %s:%u:%u: %s",
+                 path,
+                 gtk_css_section_get_end_line (section) + 1,
+                 gtk_css_section_get_end_position (section),
+                 error->message);
+
+      if (info)
+        g_object_unref (info);
     }
 }
 
@@ -1064,11 +1089,7 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
   /**
    * GtkCssProvider::parsing-error:
    * @provider: the provider that had a parsing error
-   * @path: path to the parsed file or %NULL if the file cannot be
-   *   identified or the data was not loaded from a file
-   * @line: line in the file or data or 0 if unknown
-   * @position: offset into the current line or 0 if unknown or the
-   *   whole line is affected
+   * @section: section the error happened in
    * @error: The parsing error
    *
    * Signals that a parsing error occured. the @path, @line and @position
@@ -1089,9 +1110,8 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
                   NULL, NULL,
-                  _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
-                  G_TYPE_NONE, 4,
-                  G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
+                  _gtk_marshal_VOID__BOXED_BOXED,
+                  G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
 
   object_class->finalize = gtk_css_provider_finalize;
 
@@ -1100,27 +1120,6 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
   g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
 }
 
-static void
-gtk_css_provider_take_error_full (GtkCssProvider *provider,
-                                  GFile          *file,
-                                  guint           line,
-                                  guint           position,
-                                  GError         *error)
-{
-  char *filename;
-
-  if (file)
-    filename = g_file_get_path (file);
-  else
-    filename = NULL;
-
-  g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
-                 filename, line, position, error);
-
-  g_free (filename);
-  g_error_free (error);
-}
-
 static void
 gtk_css_ruleset_init_copy (GtkCssRuleset       *new,
                            const GtkCssRuleset *ruleset,
@@ -1248,6 +1247,15 @@ gtk_css_scanner_destroy (GtkCssScanner *scanner)
   g_slice_free (GtkCssScanner, scanner);
 }
 
+static void
+gtk_css_provider_emit_error (GtkCssProvider *provider,
+                             GtkCssScanner  *scanner,
+                             const GError   *error)
+{
+  g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
+                 scanner->section, error);
+}
+
 static void
 gtk_css_scanner_parser_error (GtkCssParser *parser,
                               const GError *error,
@@ -1255,11 +1263,9 @@ gtk_css_scanner_parser_error (GtkCssParser *parser,
 {
   GtkCssScanner *scanner = user_data;
 
-  gtk_css_provider_take_error_full (scanner->provider,
-                                    scanner->file,
-                                    _gtk_css_parser_get_line (scanner->parser),
-                                    _gtk_css_parser_get_position (scanner->parser),
-                                    g_error_copy (error));
+  gtk_css_provider_emit_error (scanner->provider,
+                               scanner,
+                               error);
 }
 
 static GtkCssScanner *
@@ -1559,11 +1565,11 @@ gtk_css_provider_take_error (GtkCssProvider *provider,
                              GtkCssScanner  *scanner,
                              GError         *error)
 {
-  gtk_css_provider_take_error_full (provider,
-                                    scanner->file,
-                                    _gtk_css_parser_get_line (scanner->parser),
-                                    _gtk_css_parser_get_position (scanner->parser),
-                                    error);
+  gtk_css_provider_emit_error (scanner->provider,
+                               scanner,
+                               error);
+
+  g_error_free (error);
 }
 
 static void
@@ -2512,23 +2518,21 @@ gtk_css_provider_load_internal (GtkCssProvider *css_provider,
         }
       else
         {
+          GtkCssSection *section;
+          
           if (parent)
-            {
-              gtk_css_provider_error (css_provider,
-                                      parent,
-                                      GTK_CSS_PROVIDER_ERROR,
-                                      GTK_CSS_PROVIDER_ERROR_IMPORT,
-                                      "Failed to import: %s",
-                                      load_error->message);
-              g_error_free (load_error);
-            }
+            section = gtk_css_section_ref (parent->section);
           else
-            {
-              gtk_css_provider_take_error_full (css_provider,
-                                                file,
-                                                0, 0,
-                                                load_error);
-            }
+            section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
+
+          gtk_css_provider_error (css_provider,
+                                  parent,
+                                  GTK_CSS_PROVIDER_ERROR,
+                                  GTK_CSS_PROVIDER_ERROR_IMPORT,
+                                  "Failed to import: %s",
+                                  load_error->message);
+
+          gtk_css_section_unref (section);
         }
     }
 
index a9c7fdf41be06e7fb9792815edc6d25d4f91fbcf..e1d0cfa84a856a2cb1edf7f03ffc2aec3d872abb 100644 (file)
@@ -21,6 +21,7 @@
 #define __GTK_CSS_PROVIDER_H__
 
 #include <gio/gio.h>
+#include <gtk/gtkcsssection.h>
 
 G_BEGIN_DECLS
 
@@ -60,9 +61,7 @@ struct _GtkCssProviderClass
   GObjectClass parent_class;
 
   void (* parsing_error)                        (GtkCssProvider  *provider,
-                                                 const gchar     *path,
-                                                 guint            line,
-                                                 guint            position,
+                                                 GtkCssSection   *section,
                                                  const GError *   error);
 
   /* Padding for future expansion */
index 4b1d0e513f0c24d9ae0e421692426807fc2d0eac..a33e3b454562bf37993e1c7cddf46e62c08a4f6b 100644 (file)
@@ -74,6 +74,23 @@ _gtk_css_section_new (GtkCssSection     *parent,
   return section;
 }
 
+GtkCssSection *
+_gtk_css_section_new_for_file (GtkCssSectionType  type,
+                               GFile             *file)
+{
+  GtkCssSection *section;
+
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+  section = g_slice_new0 (GtkCssSection);
+
+  section->ref_count = 1;
+  section->section_type = type;
+  section->file = g_object_ref (file);
+
+  return section;
+}
+
 void
 _gtk_css_section_end (GtkCssSection *section)
 {
index 776fd5d9c58d511aeab3902c490a9442934e6459..ba704147216178cbd3aa9696d1da8f1d668d14c8 100644 (file)
@@ -30,6 +30,8 @@ GtkCssSection *    _gtk_css_section_new                (GtkCssSection        *pa
                                                         GtkCssSectionType     type,
                                                         GtkCssParser         *parser,
                                                         GFile                *file);
+GtkCssSection *    _gtk_css_section_new_for_file       (GtkCssSectionType     type,
+                                                        GFile                *file);
 
 void               _gtk_css_section_end                (GtkCssSection        *section);
 
index 7961149f11c93193f6d8fd5c2f3d8cbec757f350..fa60a7c91d96c179628a7fc503f36c343c0228e3 100644 (file)
@@ -108,7 +108,6 @@ VOID:STRING,STRING,STRING
 VOID:STRING,INT,POINTER
 VOID:STRING,UINT,FLAGS
 VOID:STRING,UINT,FLAGS,UINT
-VOID:STRING,UINT,UINT,BOXED
 VOID:UINT,FLAGS,BOXED
 VOID:UINT,UINT
 VOID:UINT,STRING
index 8d4fcd078c7bce577803f534962f71fec6b18616..60f77ded27eadf974de431b010961ab4e159a910 100644 (file)
@@ -134,21 +134,16 @@ append_error_value (GString *string,
 
 static void
 parsing_error_cb (GtkCssProvider *provider,
-                  const gchar     *path,
-                  guint            line,
-                  guint            position,
-                  const GError *   error,
-                  GString *        errors)
+                  GtkCssSection  *section,
+                  const GError   *error,
+                  GString        *errors)
 {
   char *basename;
 
-  g_assert (path);
-  g_assert (line > 0);
-
-  basename = g_path_get_basename (path);
+  basename = g_file_get_basename (gtk_css_section_get_file (section));
   g_string_append_printf (errors,
                           "%s:%u: error: ",
-                          basename, line + 1);
+                          basename, gtk_css_section_get_end_line (section) + 1);
   g_free (basename);
                           
   if (error->domain == GTK_CSS_PROVIDER_ERROR)
index 2ae081b577bef885a25951e9aca13480e6f65767..17d576e10cea67e3323653b9ca6727a5a09a20fe 100644 (file)
 
 static void
 show_parsing_error (GtkCssProvider *provider,
-                    const gchar    *path,
-                    guint           line,
-                    guint           position,
+                    GtkCssSection  *section,
                     const GError   *error,
                     GtkTextBuffer  *buffer)
 {
   GtkTextIter start, end;
   const char *tag_name;
 
-  gtk_text_buffer_get_iter_at_line (buffer, &start, line - 1);
-  if (gtk_text_buffer_get_line_count (buffer) <= line)
-    gtk_text_buffer_get_end_iter (buffer, &end);
-  else
-    gtk_text_buffer_get_iter_at_line (buffer, &end, line);
+  gtk_text_buffer_get_iter_at_line_index (buffer,
+                                          &start,
+                                          gtk_css_section_get_start_line (section),
+                                          gtk_css_section_get_start_position (section));
+  gtk_text_buffer_get_iter_at_line_index (buffer,
+                                          &end,
+                                          gtk_css_section_get_end_line (section),
+                                          gtk_css_section_get_end_position (section));
 
   if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
     tag_name = "warning";