[gtksourceview] Better default candidate encodings, with translations
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview] Better default candidate encodings, with translations
- Date: Wed, 30 Jul 2014 15:44:59 +0000 (UTC)
commit fc67d8c2c815674e9fa67cf278e8aed2b7fc6d46
Author: Sébastien Wilmet <swilmet gnome org>
Date: Tue Jul 29 14:33:33 2014 +0200
Better default candidate encodings, with translations
The translations are available in gedit. It is something difficult to
translate, so it's better to move that in GtkSourceView.
There is a new public function in GtkSourceEncoding to get the default
candidates. The FileLoader uses by default that list, and adds the
GtkSourceFile's encoding if it has been set by a previous file loading
or saving operation. The file's encoding is added to the beginning of
the list.
gtksourceview/gtksourceencoding.c | 42 ++++++++++++++++++++++++++++-
gtksourceview/gtksourceencoding.h | 7 +++-
gtksourceview/gtksourcefile.c | 15 +++++++++-
gtksourceview/gtksourcefile.h | 3 ++
gtksourceview/gtksourcefileloader.c | 51 ++++++++++++++++++++++++++++++----
5 files changed, 108 insertions(+), 10 deletions(-)
---
diff --git a/gtksourceview/gtksourceencoding.c b/gtksourceview/gtksourceencoding.c
index 211c28b..870a4b6 100644
--- a/gtksourceview/gtksourceencoding.c
+++ b/gtksourceview/gtksourceencoding.c
@@ -488,6 +488,46 @@ gtk_source_encoding_get_name (const GtkSourceEncoding* enc)
}
/**
+ * gtk_source_encoding_get_default_candidates:
+ *
+ * Returns: (transfer container) (element-type GtkSource.Encoding): the list of
+ * default candidates encodings. Free with g_slist_free().
+ */
+GSList *
+gtk_source_encoding_get_default_candidates (void)
+{
+ const gchar *encodings_str;
+ GVariant *encodings_variant;
+ const gchar **encodings_strv;
+ GSList *encodings_list;
+
+ /* Translators: This is the sorted list of encodings used by
+ * GtkSourceView for automatic detection of the file encoding. You may
+ * want to customize it adding encodings that are common in your
+ * country, for instance the GB18030 encoding for the Chinese
+ * translation. You may also want to remove the ISO-8859-15 encoding
+ * (covering English and most Western European languages) if you think
+ * people in your country will rarely use it. "CURRENT" is a magic
+ * value used by gedit and it represents the encoding for the current
+ * locale, so please don't translate the "CURRENT" term. Only
+ * recognized encodings are used. See
+ * https://git.gnome.org/browse/gtksourceview/tree/gtksourceview/gtksourceencoding.c#n147
+ * for a list of supported encodings.
+ */
+ encodings_str = _("['UTF-8', 'CURRENT', 'ISO-8859-15', 'UTF-16']");
+
+ encodings_variant = g_variant_new_parsed (encodings_str);
+ g_variant_ref_sink (encodings_variant);
+
+ encodings_strv = g_variant_get_strv (encodings_variant, NULL);
+
+ encodings_list = _gtk_source_encoding_strv_to_list (encodings_strv);
+
+ g_variant_unref (encodings_variant);
+ return encodings_list;
+}
+
+/**
* gtk_source_encoding_copy:
* @enc: a #GtkSourceEncoding.
*
@@ -518,7 +558,6 @@ gtk_source_encoding_free (GtkSourceEncoding *enc)
g_return_if_fail (enc != NULL);
}
-#if 0
/* Will probably be used in the future. */
static gboolean
data_exists (GSList *list,
@@ -564,6 +603,7 @@ _gtk_source_encoding_strv_to_list (const gchar * const *enc_str)
return g_slist_reverse (res);
}
+#if 0
gchar **
_gtk_source_encoding_list_to_strv (const GSList *enc_list)
{
diff --git a/gtksourceview/gtksourceencoding.h b/gtksourceview/gtksourceencoding.h
index 260dfbb..2567e3e 100644
--- a/gtksourceview/gtksourceencoding.h
+++ b/gtksourceview/gtksourceencoding.h
@@ -60,15 +60,18 @@ const GtkSourceEncoding *gtk_source_encoding_get_current (void);
void gtk_source_encoding_foreach (GtkSourceEncodingForeachFunc func,
gpointer user_data);
+GSList *gtk_source_encoding_get_default_candidates
+ (void);
+
/* These should not be used, they are just to make python bindings happy */
GtkSourceEncoding *gtk_source_encoding_copy (const GtkSourceEncoding *enc);
void gtk_source_encoding_free (GtkSourceEncoding *enc);
-#if 0
-/* Will probably be used in the future. */
G_GNUC_INTERNAL
GSList *_gtk_source_encoding_strv_to_list (const gchar * const *enc_str);
+#if 0
+/* Will probably be used in the future. */
G_GNUC_INTERNAL
gchar **_gtk_source_encoding_list_to_strv (const GSList *enc);
#endif
diff --git a/gtksourceview/gtksourcefile.c b/gtksourceview/gtksourcefile.c
index 98b0415..d889676 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -65,6 +65,9 @@ struct _GtkSourceFilePrivate
GTimeVal modification_time;
guint modification_time_set : 1;
+
+ /* TRUE if the encoding has been set by a FileLoader or a FileSaver. */
+ guint encoding_set : 1;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceFile, gtk_source_file, G_TYPE_OBJECT)
@@ -173,7 +176,7 @@ gtk_source_file_class_init (GtkSourceFileClass *klass)
/**
* GtkSourceFile:encoding:
*
- * The character encoding.
+ * The character encoding. UTF-8 by default.
*
* Since: 3.14
*/
@@ -227,6 +230,7 @@ gtk_source_file_init (GtkSourceFile *file)
file->priv = gtk_source_file_get_instance_private (file);
file->priv->encoding = gtk_source_encoding_get_utf8 ();
+ file->priv->encoding_set = FALSE;
}
/**
@@ -295,6 +299,8 @@ _gtk_source_file_set_encoding (GtkSourceFile *file,
{
g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+ file->priv->encoding_set = TRUE;
+
if (encoding == NULL)
{
encoding = gtk_source_encoding_get_utf8 ();
@@ -307,6 +313,13 @@ _gtk_source_file_set_encoding (GtkSourceFile *file,
}
}
+/* Returns TRUE if the encoding has been set by a FileLoader or a FileSaver. */
+gboolean
+_gtk_source_file_is_encoding_set (GtkSourceFile *file)
+{
+ return file->priv->encoding_set;
+}
+
/**
* gtk_source_file_get_encoding:
* @file: a #GtkSourceFile.
diff --git a/gtksourceview/gtksourcefile.h b/gtksourceview/gtksourcefile.h
index e4859f1..922eb6b 100644
--- a/gtksourceview/gtksourcefile.h
+++ b/gtksourceview/gtksourcefile.h
@@ -92,6 +92,9 @@ void _gtk_source_file_set_encoding (GtkSourceFile
*file,
const GtkSourceEncoding *encoding);
G_GNUC_INTERNAL
+gboolean _gtk_source_file_is_encoding_set (GtkSourceFile *file);
+
+G_GNUC_INTERNAL
void _gtk_source_file_set_newline_type (GtkSourceFile *file,
GtkSourceNewlineType newline_type);
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index be2c24f..f297a9e 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -259,18 +259,57 @@ gtk_source_file_loader_dispose (GObject *object)
}
static void
+set_default_candidate_encodings (GtkSourceFileLoader *loader)
+{
+ GSList *list;
+ GSList *l;
+ const GtkSourceEncoding *file_encoding;
+
+ /* Get first the default candidates from GtkSourceEncoding. If the
+ * GtkSourceFile's encoding has been set by a FileLoader or FileSaver,
+ * put it at the beginning of the list.
+ */
+ list = gtk_source_encoding_get_default_candidates ();
+
+ if (loader->priv->file == NULL ||
+ !_gtk_source_file_is_encoding_set (loader->priv->file))
+ {
+ goto end;
+ }
+
+ file_encoding = gtk_source_file_get_encoding (loader->priv->file);
+
+ /* Remove file_encoding from the list, if already present, and prepend
+ * it to the list.
+ */
+ for (l = list; l != NULL; l = l->next)
+ {
+ const GtkSourceEncoding *cur_encoding = l->data;
+
+ if (cur_encoding == file_encoding)
+ {
+ list = g_slist_delete_link (list, l);
+
+ /* The list doesn't contain duplicates, normally. */
+ break;
+ }
+ }
+
+ list = g_slist_prepend (list, (gpointer) file_encoding);
+
+end:
+ g_slist_free (loader->priv->candidate_encodings);
+ loader->priv->candidate_encodings = list;
+}
+
+static void
gtk_source_file_loader_constructed (GObject *object)
{
GtkSourceFileLoader *loader = GTK_SOURCE_FILE_LOADER (object);
if (loader->priv->file != NULL)
{
- const GtkSourceEncoding *encoding;
-
- encoding = gtk_source_file_get_encoding (loader->priv->file);
-
- g_slist_free (loader->priv->candidate_encodings);
- loader->priv->candidate_encodings = g_slist_prepend (NULL, (gpointer) encoding);
+ set_default_candidate_encodings (loader);
if (loader->priv->location == NULL &&
loader->priv->input_stream_property == NULL)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]