[devhelp: 22/36] book: Setup titlecasing of languages



commit 9c1056756eb9653d32ca69bac0d79719d8cba845
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Thu Dec 9 22:16:22 2010 +0100

    book: Setup titlecasing of languages

 src/dh-book.c        |   16 ++++++++++++++--
 src/dh-preferences.c |    4 ++--
 src/dh-util.c        |   25 +++++++++++++++++++++++++
 src/dh-util.h        |    4 ++++
 4 files changed, 45 insertions(+), 4 deletions(-)
---
diff --git a/src/dh-book.c b/src/dh-book.c
index 0ac849e..0c52a06 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -30,6 +30,7 @@
 #include "dh-parser.h"
 #include "dh-book.h"
 #include "dh-marshal.h"
+#include "dh-util.h"
 
 /* Timeout to wait for new events in the book so that
  * they are merged and we don't spam unneeded signals */
@@ -206,6 +207,7 @@ dh_book_new (const gchar *book_path)
         DhBook     *book;
         GError     *error = NULL;
         GFile      *book_path_file;
+        gchar      *language;
 
         g_return_val_if_fail (book_path, NULL);
 
@@ -216,7 +218,7 @@ dh_book_new (const gchar *book_path)
         if (!dh_parser_read_file  (book_path,
                                    &priv->title,
                                    &priv->name,
-                                   &priv->language,
+                                   &language,
                                    &priv->tree,
                                    &priv->keywords,
                                    &error)) {
@@ -233,6 +235,16 @@ dh_book_new (const gchar *book_path)
         /* Store path */
         priv->path = g_strdup (book_path);
 
+        /* Rewrite language, if any, including the prefix we want
+         * to use when seeing it. It is pretty ugly to do it here,
+         * but it's the only way of making sure we standarize how
+         * the language group is shown */
+        dh_util_ascii_strtitle (language);
+        priv->language = (language ?
+                          g_strdup_printf (_("Language: %s"), language) :
+                          g_strdup (_("Language: Undefined")));
+        g_free (language);
+
         /* Setup monitor for changes */
         book_path_file = g_file_new_for_path (book_path);
         priv->monitor = g_file_monitor_file (book_path_file,
@@ -388,7 +400,7 @@ dh_book_get_language (DhBook *book)
 
         priv = GET_PRIVATE (book);
 
-        return priv->language ? priv->language : _("Undefined language");
+        return priv->language;
 }
 
 const gchar *
diff --git a/src/dh-preferences.c b/src/dh-preferences.c
index 0ecef11..f3a287b 100644
--- a/src/dh-preferences.c
+++ b/src/dh-preferences.c
@@ -655,10 +655,10 @@ preferences_bookshelf_add_book_to_store (DhBook   *book,
                 gboolean     language_iter_found;
                 GtkTreeIter  next_language_iter;
                 gboolean     next_language_iter_found;
-                gchar       *language_title;
+                const gchar *language_title;
                 gboolean     first_in_language = FALSE;
 
-                language_title = g_ascii_strup (dh_book_get_language (book), -1);
+                language_title = dh_book_get_language (book);
 
                 /* Look for the proper language group */
                 preferences_bookshelf_find_language_group (language_title,
diff --git a/src/dh-util.c b/src/dh-util.c
index bf5d4c7..38c221b 100644
--- a/src/dh-util.c
+++ b/src/dh-util.c
@@ -833,3 +833,28 @@ dh_util_cmp_book (DhLink *a, DhLink *b)
         return rc;
 }
 
+/* We're only going to expect ASCII strings here, so there's no point in
+ * playing with g_unichar_totitle() and such.
+ * Note that we modify the string in place.
+ */
+void
+dh_util_ascii_strtitle (gchar *str)
+{
+        gboolean word_start;
+
+        if (!str)
+                return;
+
+        word_start = TRUE;
+        while (*str != '\0') {
+                if (g_ascii_isalpha (*str)) {
+                        *str = (word_start ?
+                                g_ascii_toupper (*str) :
+                                g_ascii_tolower (*str));
+                        word_start = FALSE;
+                } else {
+                        word_start = TRUE;
+                }
+                str++;
+        }
+}
diff --git a/src/dh-util.h b/src/dh-util.h
index d595e01..24835bb 100644
--- a/src/dh-util.h
+++ b/src/dh-util.h
@@ -49,6 +49,7 @@ void         dh_util_state_manage_notebook        (GtkNotebook *notebook,
 void         dh_util_state_set_notebook_page_name (GtkWidget   *page,
                                                    const gchar *page_name);
 const gchar *dh_util_state_get_notebook_page_name (GtkWidget   *page);
+
 GSList *     dh_util_state_load_books_disabled    (void);
 void         dh_util_state_store_books_disabled   (GSList *books_disabled);
 
@@ -62,9 +63,12 @@ void         dh_util_font_get_fixed               (gchar        **name,
                                                    gdouble       *size,
                                                    gboolean       use_system_font);
 void         dh_util_font_add_web_view            (WebKitWebView *view);
+
 gint         dh_util_cmp_book                     (DhLink *a,
                                                    DhLink *b);
 
+void         dh_util_ascii_strtitle               (gchar *str);
+
 G_END_DECLS
 
 #endif /* __DH_UTIL_H__ */



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