[gtranslator] Update the comments correctly.



commit aa577df78b36ff2b5495a1f569576737a527b78b
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Jan 28 17:42:19 2010 +0100

    Update the comments correctly.

 src/header.c |  186 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 src/po.c     |  163 +--------------------------------------------------
 2 files changed, 162 insertions(+), 187 deletions(-)
---
diff --git a/src/header.c b/src/header.c
index 45b959d..96a0f00 100644
--- a/src/header.c
+++ b/src/header.c
@@ -26,6 +26,7 @@
 #include "header.h"
 #include "prefs-manager.h"
 #include "profile.h"
+#include "utils.h"
 
 #include <glib.h>
 #include <glib-object.h>
@@ -40,7 +41,6 @@ G_DEFINE_TYPE (GtranslatorHeader, gtranslator_header, GTR_TYPE_MSG)
 
 struct _GtranslatorHeaderPrivate
 {
-  gchar *prev_translator;
   gint   nplurals;
 };
 
@@ -110,17 +110,12 @@ gtranslator_header_init (GtranslatorHeader * header)
 {
   header->priv = GTR_HEADER_GET_PRIVATE (header);
 
-  header->priv->prev_translator = NULL;
   header->priv->nplurals = -1;
 }
 
 static void
 gtranslator_header_finalize (GObject * object)
 {
-  GtranslatorHeader *header = GTR_HEADER (object);
-
-  g_free (header->priv->prev_translator);
-
   G_OBJECT_CLASS (gtranslator_header_parent_class)->finalize (object);
 }
 
@@ -318,25 +313,6 @@ gtranslator_header_set_translator (GtranslatorHeader *header,
   g_free (translator);
 }
 
-const gchar *
-gtranslator_header_get_prev_translator (GtranslatorHeader *header)
-{
-  g_return_val_if_fail (GTR_IS_HEADER (header), NULL);
-
-  return header->priv->prev_translator;
-}
-
-void
-gtranslator_header_set_prev_translator (GtranslatorHeader *header,
-                                        const gchar *prev_translator)
-{
-  g_return_if_fail (GTR_IS_HEADER (header));
-  g_return_if_fail (prev_translator != NULL);
-
-  g_free (header->priv->prev_translator);
-  header->priv->prev_translator = g_strdup (prev_translator);
-}
-
 gchar *
 gtranslator_header_get_language (GtranslatorHeader *header)
 {
@@ -445,7 +421,7 @@ gtranslator_header_set_charset (GtranslatorHeader *header,
 
   g_return_if_fail (GTR_IS_HEADER (header));
 
-  set = g_strconcat(" text/plain;", " charset=",
+  set = g_strconcat("text/plain;", " charset=",
                     charset, NULL);
 
   gtranslator_header_set_field (header, "Content-Type",
@@ -529,3 +505,161 @@ gtranslator_header_get_nplurals (GtranslatorHeader * header)
   else
     return 1;
 }
+
+static void
+set_profile_values (GtranslatorHeader *header)
+{
+  if (gtranslator_prefs_manager_get_use_profile_values ())
+    {
+      GtranslatorProfile *active_profile;
+
+      active_profile = gtranslator_application_get_active_profile (GTR_APP);
+
+      gtranslator_header_set_translator (header, gtranslator_profile_get_author_name (active_profile),
+                                         gtranslator_profile_get_author_email (active_profile));
+      gtranslator_header_set_language (header, gtranslator_profile_get_language_name (active_profile),
+                                       gtranslator_profile_get_group_email (active_profile));
+      gtranslator_header_set_charset (header, gtranslator_profile_get_charset (active_profile));
+      gtranslator_header_set_encoding (header, gtranslator_profile_get_encoding (active_profile));
+      gtranslator_header_set_plural_forms (header, gtranslator_profile_get_plurals (active_profile));
+    }
+}
+
+static void
+update_po_date (GtranslatorHeader *header)
+{
+  gchar *current_date;
+  gchar *current_time;
+  gchar *new_date;
+
+  current_date = gtranslator_utils_get_current_date ();
+  current_time = gtranslator_utils_get_current_time ();
+
+  new_date = g_strconcat (current_date, " ", current_time, NULL);
+
+  g_free (current_date);
+  g_free (current_time);
+
+  gtranslator_header_set_po_date (header, new_date);
+
+  g_free (new_date);
+}
+
+static void
+update_comments (GtranslatorHeader *header,
+                 const gchar       *comments)
+{
+  GString *new_comments;
+  GString *years;
+  gchar **comment_lines;
+  gchar *translator;
+  gchar *email;
+  gchar *current_year;
+  gint i;
+
+  current_year = gtranslator_utils_get_current_year ();
+
+  /* Save the previous translator to update the header's comment */
+  if (gtranslator_prefs_manager_get_use_profile_values ())
+    {
+      GtranslatorProfile *active_profile;
+
+      active_profile = gtranslator_application_get_active_profile (GTR_APP);
+
+      translator = g_strdup (gtranslator_profile_get_author_name (active_profile));
+      email = g_strdup (gtranslator_profile_get_author_email (active_profile));
+    }
+  else
+    {
+      translator = gtranslator_header_get_translator (header);
+      email = gtranslator_header_get_tr_email (header);
+    }
+
+  comment_lines = g_strsplit (comments, "\n", -1);
+  new_comments = g_string_new ("");
+  years = g_string_new ("");
+
+  for (i = 0; comment_lines != NULL && comment_lines[i] != NULL; i++)
+    {
+      if (g_str_has_prefix (comment_lines[i], translator))
+        {
+          gchar **year_array;
+          gint j;
+
+          year_array = g_strsplit (comment_lines[i], ",", -1);
+
+          for (j = 1; year_array != NULL && year_array[j] != NULL; j++)
+            {
+              gchar *search;
+
+              if (g_str_has_suffix (year_array[j], "."))
+                {
+                  gint len;
+
+                  len = g_utf8_strlen (year_array[j], -1);
+                  search = g_strndup (year_array[j], len - 1);
+                }
+              else
+                search = g_strdup (year_array[j]);
+
+              if (g_strrstr (years->str, search) == NULL)
+                {
+                  years = g_string_append (years, search);
+                  years = g_string_append_c (years, ',');
+                }
+
+              g_free (search);
+            }
+
+          g_strfreev (year_array);
+        }
+      else
+        {
+          new_comments = g_string_append (new_comments, comment_lines[i]);
+          new_comments = g_string_append_c (new_comments, '\n');
+        }
+    }
+
+  g_strfreev (comment_lines);
+
+  g_string_append_printf (years, " %s.", current_year);
+
+  /* If the last line is a \n just remove it */
+  if (new_comments->str[new_comments->len - 1] == '\n')
+    new_comments = g_string_truncate (new_comments, new_comments->len - 1);
+
+  g_string_append_printf (new_comments, "%s <%s>,%s",
+                          translator, email, years->str);
+
+  g_string_free (years, TRUE);
+
+  gtranslator_header_set_comments (header, new_comments->str);
+
+  g_string_free (new_comments, TRUE);
+}
+
+/* FIXME: complete this */
+static void
+add_default_comments (GtranslatorHeader *header)
+{
+}
+
+void
+gtranslator_header_update_header (GtranslatorHeader *header)
+{
+  const gchar *comments;
+
+  /* If needed update the header with the profile values */
+  set_profile_values (header);
+
+  /* Update the po date */
+  update_po_date (header);
+
+  /* Update the header's comment */
+  comments = gtranslator_header_get_comments (header);
+
+  if (comments != NULL)
+    update_comments (header, comments);
+  else
+    add_default_comments (header);
+}
diff --git a/src/po.c b/src/po.c
index b266d25..2dbfe08 100644
--- a/src/po.c
+++ b/src/po.c
@@ -501,163 +501,6 @@ gtranslator_po_parse (GtranslatorPo * po, GFile * location, GError ** error)
 }
 
 /**
- * gtranslator_po_save_header_in_msg:
- * @po: a #GtranslatorPo
- *
- * It saves the header's values into the msgstr
- **/
-void
-gtranslator_po_save_header_in_msg (GtranslatorPo     *po,
-                                   GtranslatorHeader *header)
-{
-
-  const gchar *msgstr, *header_comment;
-  const gchar *prev_translator;
-
-  gchar *current_date;
-  gchar *current_time;
-  gchar *year;
-  gchar *new_date;
-  gchar *aux;
-  gchar *aux2;
-  const gchar *comments;
-  gchar **comments_lines;
-  gchar **comments_translator_values;
-
-  gboolean use_profile_values;
-
-  gint i = 0;
-  gint j;
-  gint k = 0;
-  gint l;
-
-  gchar *new_comments = "";
-  gchar *old_line;
-  gchar *line = "";
-  gchar *comp_year;
-  gchar *line_without_dot;
-
-  gboolean current_translator_in_comments = FALSE;
-
-  use_profile_values = gtranslator_prefs_manager_get_use_profile_values ();
-
-  /*
-   * Save the previous translator to update the header's comment
-   */
-  prev_translator = gtranslator_header_get_prev_translator (header);
-
-  /*
-   * If button use_profile_values is pushed, then header's values
-   * should be taking from default profile
-   */
-  if (use_profile_values)
-    {
-
-      GtranslatorProfile *active_profile;
-
-      active_profile = gtranslator_application_get_active_profile (GTR_APP);
-
-      gtranslator_header_set_translator (header, gtranslator_profile_get_author_name (active_profile),
-                                         gtranslator_profile_get_author_email (active_profile));
-      gtranslator_header_set_language (header, gtranslator_profile_get_language_name (active_profile),
-                                       gtranslator_profile_get_group_email (active_profile));
-      gtranslator_header_set_charset (header, gtranslator_profile_get_charset (active_profile));
-      gtranslator_header_set_encoding (header, gtranslator_profile_get_encoding (active_profile));
-      gtranslator_header_set_plural_forms (header, gtranslator_profile_get_plurals (active_profile));
-    }
-
-  /*
-   * Update the po date
-   */
-  current_date = gtranslator_utils_get_current_date ();
-  current_time = gtranslator_utils_get_current_time ();
-  year = gtranslator_utils_get_current_year ();
-
-  new_date = g_strconcat (current_date, " ", current_time, NULL);
-
-  gtranslator_header_set_po_date (header, new_date);
-
-  g_free (new_date);
-
-  /* Update the header's comment */
-  comments = gtranslator_header_get_comments (header);
-
-  if (comments != NULL)
-    {
-      comments_lines = g_strsplit (comments, "\n", -1);
-
-      /* Searching if the current translator is in comments. */
-      while (comments_lines[i] != NULL)
-        {
-          if (g_str_has_prefix (comments_lines[i], prev_translator))
-            {
-              current_translator_in_comments = TRUE;
-              comments_translator_values =
-              g_strsplit (comments_lines[i], ",", -1);
-              j = i;
-            }
-          i++;
-        }
-      aux = g_strconcat (gtranslator_header_get_translator (header), " ", "<",
-                         gtranslator_header_get_tr_email (header), ">", NULL);
-
-      comp_year = g_strconcat (" ", year, ".", NULL);
-
-      /* Current translator is already in comments but its last year < current year. */
-      if (current_translator_in_comments)
-        {
-          if ((g_utf8_collate (prev_translator, aux) == 0) &&
-              (g_utf8_collate (comments_translator_values [g_strv_length (comments_translator_values) - 1],
-               comp_year) != 0))
-            {
-              if (g_str_has_suffix (comments_lines[j], "."))
-                {
-                  line_without_dot = g_strndup (comments_lines[j],
-                  g_utf8_strlen (comments_lines[j], -1) - 1);
-                  line = g_strconcat (line_without_dot, ", ", year, ".", NULL);
-                  g_free (line_without_dot);
-                }
-              else
-                {
-                  line = g_strconcat (comments_lines[j], ", ", year, ".", NULL);
-                }
-
-              for (l = j; l < (g_strv_length (comments_lines)); l++)
-              {
-                comments_lines[l] = comments_lines[l + 1];
-              }
-
-              comments_lines[g_strv_length (comments_lines) - 1] = line;
-              while (comments_lines[k] != NULL)
-                {
-                  new_comments = g_strconcat (new_comments, comments_lines[k], "\n", NULL);
-                  k++;
-                }
-              gtranslator_header_set_comments (header, (const gchar *) new_comments);
-              g_free (line);
-              g_free (new_comments);
-            }
-        }
-
-      /* Current translator is not in the comments. */
-      if ((g_utf8_collate (prev_translator, aux) != 0) || 
-          (prev_translator == NULL))
-        {
-          header_comment =	(const gchar *) g_strchomp ((gchar *) gtranslator_header_get_comments (header));
-          aux2 = g_strconcat (header_comment, "\n",
-                              gtranslator_header_get_translator (header), " ", "<",
-                              gtranslator_header_get_tr_email (header), ">", ",", " ",
-                              year, ".", NULL);
-
-          gtranslator_header_set_comments (header, (const gchar *) aux2);
-          gtranslator_header_set_prev_translator (header, aux);
-          g_free (aux2);
-        }
-      g_free (aux);
-    }
-}
-
-/**
  * gtranslator_po_save_file:
  * @po: a #GtranslatorPo
  * @error: a GError to manage the exceptions
@@ -723,11 +566,9 @@ gtranslator_po_save_file (GtranslatorPo * po, GError ** error)
      } */
 
 
-  /*
-   * Save header fields into msg
-   */
+  /* Save header fields into msg */
   header = gtranslator_po_get_header (po);
-  gtranslator_po_save_header_in_msg (po, header);
+  gtranslator_header_update_header (header);
 
   if (!po_file_write (gtranslator_po_get_po_file (po), filename, &handler))
     {



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