[gtksourceview/wip/default-encodings] encodings: get default candidates
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/default-encodings] encodings: get default candidates
- Date: Wed, 30 Jul 2014 15:04:50 +0000 (UTC)
commit 12108eb500c725c5e483847d28d1dd34d63698c6
Author: Sébastien Wilmet <swilmet gnome org>
Date: Tue Jul 29 14:33:33 2014 +0200
encodings: get default candidates
Just a prototype.
gtksourceview/gtksourceencoding.c | 36 ++++++++++++++++++++++-
gtksourceview/gtksourceencoding.h | 7 +++-
gtksourceview/gtksourcefile.c | 2 +-
gtksourceview/gtksourcefileloader.c | 54 +++++++++++++++++++++++++++++++----
tests/test-widget.c | 14 +++++++++
5 files changed, 103 insertions(+), 10 deletions(-)
---
diff --git a/gtksourceview/gtksourceencoding.c b/gtksourceview/gtksourceencoding.c
index 211c28b..c7a714e 100644
--- a/gtksourceview/gtksourceencoding.c
+++ b/gtksourceview/gtksourceencoding.c
@@ -487,6 +487,40 @@ gtk_source_encoding_get_name (const GtkSourceEncoding* enc)
return (enc->name == NULL) ? _("Unknown") : _(enc->name);
}
+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 +552,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 +597,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..9e4bc2f 100644
--- a/gtksourceview/gtksourcefile.c
+++ b/gtksourceview/gtksourcefile.c
@@ -173,7 +173,7 @@ gtk_source_file_class_init (GtkSourceFileClass *klass)
/**
* GtkSourceFile:encoding:
*
- * The character encoding.
+ * The character encoding. UTF-8 by default.
*
* Since: 3.14
*/
diff --git a/gtksourceview/gtksourcefileloader.c b/gtksourceview/gtksourcefileloader.c
index be2c24f..7d34568 100644
--- a/gtksourceview/gtksourcefileloader.c
+++ b/gtksourceview/gtksourcefileloader.c
@@ -259,18 +259,60 @@ gtk_source_file_loader_dispose (GObject *object)
}
static void
-gtk_source_file_loader_constructed (GObject *object)
+set_default_candidate_encodings (GtkSourceFileLoader *loader)
{
- GtkSourceFileLoader *loader = GTK_SOURCE_FILE_LOADER (object);
+ GSList *list;
+
+ /* Get first the default candidates from GtkSourceEncoding. Then get the
+ * GtkSourceFile's encoding. If the file's encoding is not already
+ * present in the list, prepend it, since it has a higher priority
+ * (probably set with a previous load operation). On the other hand, if
+ * the file's encoding is present in the list, don't change the order of
+ * the list, because the order has been defined by the translator.
+ *
+ * Another solution is to know if the file's encoding has been set
+ * (which means that a file loading or file saving has already been
+ * done).
+ */
+ list = gtk_source_encoding_get_default_candidates ();
if (loader->priv->file != NULL)
{
- const GtkSourceEncoding *encoding;
+ const GtkSourceEncoding *file_encoding;
+ gboolean file_encoding_present = FALSE;
+ GSList *l;
- encoding = gtk_source_file_get_encoding (loader->priv->file);
+ file_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);
+ for (l = list; l != NULL; l = l->next)
+ {
+ const GtkSourceEncoding *cur_encoding = l->data;
+
+ if (file_encoding == cur_encoding)
+ {
+ file_encoding_present = TRUE;
+ break;
+ }
+ }
+
+ if (!file_encoding_present)
+ {
+ list = g_slist_prepend (list, (gpointer) file_encoding);
+ }
+ }
+
+ 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)
+ {
+ set_default_candidate_encodings (loader);
if (loader->priv->location == NULL &&
loader->priv->input_stream_property == NULL)
diff --git a/tests/test-widget.c b/tests/test-widget.c
index 1667e3e..eb3e8c2 100644
--- a/tests/test-widget.c
+++ b/tests/test-widget.c
@@ -954,6 +954,8 @@ static void
test_widget_init (TestWidget *self)
{
PangoFontDescription *font_desc;
+ GSList *encodings_list;
+ GSList *l;
self->priv = test_widget_get_instance_private (self);
@@ -1004,6 +1006,18 @@ test_widget_init (TestWidget *self)
}
open_file (self, TOP_SRCDIR "/gtksourceview/gtksourcebuffer.c");
+
+ g_print ("\n\n");
+ encodings_list = gtk_source_encoding_get_default_candidates ();
+
+ for (l = encodings_list; l != NULL; l = l->next)
+ {
+ GtkSourceEncoding *encoding = l->data;
+ gchar *encoding_str = gtk_source_encoding_get_charset (encoding);
+
+ g_print ("%s\n", encoding_str);
+ }
+ g_print ("\n");
}
static TestWidget *
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]