[gedit/wip/merge-encoding-settings: 29/33] gedit_settings_get_candidate_encodings()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/merge-encoding-settings: 29/33] gedit_settings_get_candidate_encodings()
- Date: Sat, 28 Mar 2015 19:12:18 +0000 (UTC)
commit 899ccc45d14cb94cb64732856f6d8db8583b787a
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Mar 28 15:56:30 2015 +0100
gedit_settings_get_candidate_encodings()
So it is implemented in only one place.
gedit/gedit-encoding-items.c | 53 +++---------------------------
gedit/gedit-encodings-dialog.c | 10 ++----
gedit/gedit-settings.c | 69 ++++++++++++++++++++++++++++++++++++++++
gedit/gedit-settings.h | 2 +
gedit/gedit-tab.c | 25 ++-------------
5 files changed, 83 insertions(+), 76 deletions(-)
---
diff --git a/gedit/gedit-encoding-items.c b/gedit/gedit-encoding-items.c
index e580a72..4954325 100644
--- a/gedit/gedit-encoding-items.c
+++ b/gedit/gedit-encoding-items.c
@@ -74,58 +74,20 @@ gedit_encoding_item_get_name (GeditEncodingItem *item)
GSList *
gedit_encoding_items_get (void)
{
- const GtkSourceEncoding *utf8_encoding;
const GtkSourceEncoding *current_encoding;
- GSettings *settings;
- gchar **settings_strv;
GSList *encodings;
- GSList *ret = NULL;
+ GSList *items = NULL;
GSList *l;
- utf8_encoding = gtk_source_encoding_get_utf8 ();
- current_encoding = gtk_source_encoding_get_current ();
-
- settings = g_settings_new ("org.gnome.gedit.preferences.encodings");
-
- settings_strv = g_settings_get_strv (settings, GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
-
- /* First take the candidate encodings from GSettings. If the gsetting is
- * empty, take the default candidates of GtkSourceEncoding.
- */
- if (settings_strv != NULL && settings_strv[0] != NULL)
- {
- encodings = _gedit_utils_encoding_strv_to_list ((const gchar * const *)settings_strv);
+ encodings = gedit_settings_get_candidate_encodings ();
- /* Ensure that UTF-8 is present. */
- if (utf8_encoding != current_encoding &&
- g_slist_find (encodings, utf8_encoding) == NULL)
- {
- encodings = g_slist_prepend (encodings, (gpointer)utf8_encoding);
- }
-
- /* Ensure that the current locale encoding is present (if not
- * present, it must be the first encoding).
- */
- if (g_slist_find (encodings, current_encoding) == NULL)
- {
- encodings = g_slist_prepend (encodings, (gpointer)current_encoding);
- }
- }
- else
- {
- encodings = gtk_source_encoding_get_default_candidates ();
- }
+ current_encoding = gtk_source_encoding_get_current ();
for (l = encodings; l != NULL; l = l->next)
{
const GtkSourceEncoding *enc = l->data;
gchar *name;
- if (enc == NULL)
- {
- continue;
- }
-
if (enc == current_encoding)
{
name = g_strdup_printf (_("Current Locale (%s)"),
@@ -136,15 +98,12 @@ gedit_encoding_items_get (void)
name = gtk_source_encoding_to_string (enc);
}
- ret = g_slist_prepend (ret, gedit_encoding_item_new (enc, name));
+ items = g_slist_prepend (items, gedit_encoding_item_new (enc, name));
}
- ret = g_slist_reverse (ret);
-
- g_object_unref (settings);
- g_strfreev (settings_strv);
g_slist_free (encodings);
- return ret;
+
+ return g_slist_reverse (items);
}
/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-encodings-dialog.c b/gedit/gedit-encodings-dialog.c
index ea476ed..e0cdf84 100644
--- a/gedit/gedit-encodings-dialog.c
+++ b/gedit/gedit-encodings-dialog.c
@@ -93,7 +93,7 @@ get_chosen_encodings_list (GeditEncodingsDialog *dialog)
iter_set = gtk_tree_model_iter_next (model, &iter);
}
- return ret;
+ return g_slist_reverse (ret);
}
static void
@@ -119,6 +119,7 @@ gedit_encodings_dialog_response (GtkDialog *gtk_dialog,
enc_list = get_chosen_encodings_list (dialog);
enc_strv = _gedit_utils_encoding_list_to_strv (enc_list);
+ /* TODO save setting only if list modified. */
g_settings_set_strv (dialog->priv->enc_settings,
GEDIT_SETTINGS_CANDIDATE_ENCODINGS,
(const gchar * const *)enc_strv);
@@ -419,17 +420,13 @@ down_button_clicked_cb (GtkWidget *button,
static void
init_liststores (GeditEncodingsDialog *dialog)
{
- gchar **enc_strv;
GSList *chosen_encodings;
GSList *all_encodings;
GSList *l;
/* Chosen encodings */
- enc_strv = g_settings_get_strv (dialog->priv->enc_settings,
- GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
-
- chosen_encodings = _gedit_utils_encoding_strv_to_list ((const gchar * const *)enc_strv);
+ chosen_encodings = gedit_settings_get_candidate_encodings ();
for (l = chosen_encodings; l != NULL; l = l->next)
{
@@ -453,7 +450,6 @@ init_liststores (GeditEncodingsDialog *dialog)
append_encoding (dialog->priv->liststore_available, cur_encoding);
}
- g_strfreev (enc_strv);
g_slist_free (chosen_encodings);
g_slist_free (all_encodings);
}
diff --git a/gedit/gedit-settings.c b/gedit/gedit-settings.c
index 1d38144..acc41ad 100644
--- a/gedit/gedit-settings.c
+++ b/gedit/gedit-settings.c
@@ -33,6 +33,7 @@
#include "gedit-app.h"
#include "gedit-view.h"
#include "gedit-window.h"
+#include "gedit-utils.h"
#define GEDIT_SETTINGS_LOCKDOWN_COMMAND_LINE "disable-command-line"
#define GEDIT_SETTINGS_LOCKDOWN_PRINTING "disable-printing"
@@ -492,4 +493,72 @@ gedit_settings_set_list (GSettings *settings,
g_free (values);
}
+static gboolean
+strv_is_empty (gchar **strv)
+{
+ if (strv == NULL || strv[0] == NULL)
+ {
+ return TRUE;
+ }
+
+ /* Contains one empty string. */
+ if (strv[1] == NULL && strv[0][0] == '\0')
+ {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/* Take in priority the candidate encodings from GSettings. If the gsetting is
+ * empty, take the default candidates of GtkSourceEncoding.
+ * Also, ensure that UTF-8 and the current locale encoding are present.
+ * TODO remove duplicates.
+ * Returns: a list of GtkSourceEncodings. Free with g_slist_free().
+ */
+GSList *
+gedit_settings_get_candidate_encodings (void)
+{
+ const GtkSourceEncoding *utf8_encoding;
+ const GtkSourceEncoding *current_encoding;
+ GSettings *settings;
+ gchar **settings_strv;
+ GSList *candidates;
+
+ utf8_encoding = gtk_source_encoding_get_utf8 ();
+ current_encoding = gtk_source_encoding_get_current ();
+
+ settings = g_settings_new ("org.gnome.gedit.preferences.encodings");
+
+ settings_strv = g_settings_get_strv (settings, GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
+
+ if (strv_is_empty (settings_strv))
+ {
+ candidates = gtk_source_encoding_get_default_candidates ();
+ }
+ else
+ {
+ candidates = _gedit_utils_encoding_strv_to_list ((const gchar * const *)settings_strv);
+
+ /* Ensure that UTF-8 is present. */
+ if (utf8_encoding != current_encoding &&
+ g_slist_find (candidates, utf8_encoding) == NULL)
+ {
+ candidates = g_slist_prepend (candidates, (gpointer)utf8_encoding);
+ }
+
+ /* Ensure that the current locale encoding is present (if not
+ * present, it must be the first encoding).
+ */
+ if (g_slist_find (candidates, current_encoding) == NULL)
+ {
+ candidates = g_slist_prepend (candidates, (gpointer)current_encoding);
+ }
+ }
+
+ g_object_unref (settings);
+ g_strfreev (settings_strv);
+ return candidates;
+}
+
/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-settings.h b/gedit/gedit-settings.h
index cd8228d..2b54af6 100644
--- a/gedit/gedit-settings.h
+++ b/gedit/gedit-settings.h
@@ -63,6 +63,8 @@ GeditLockdownMask gedit_settings_get_lockdown (GeditSettings *gs);
gchar *gedit_settings_get_system_font (GeditSettings *gs);
+GSList *gedit_settings_get_candidate_encodings (void);
+
/* Utility functions */
GSList *gedit_settings_get_list (GSettings *settings,
const gchar *key);
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 4d345c7..5c1e207 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -1945,31 +1945,15 @@ end:
static GSList *
get_candidate_encodings (GeditTab *tab)
{
+ GSList *candidates = NULL;
GeditDocument *doc;
GtkSourceFile *file;
- GSettings *settings;
- gchar **settings_strv;
gchar *metadata_charset;
const GtkSourceEncoding *file_encoding;
- GSList *candidates = NULL;
-
- settings = g_settings_new ("org.gnome.gedit.preferences.encodings");
- settings_strv = g_settings_get_strv (settings, GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
-
- /* First take the candidate encodings from GSettings. If the gsetting is
- * empty, take the default candidates of GtkSourceEncoding.
- */
- if (settings_strv != NULL && settings_strv[0] != NULL)
- {
- candidates = _gedit_utils_encoding_strv_to_list ((const gchar * const *)settings_strv);
- }
- else
- {
- candidates = gtk_source_encoding_get_default_candidates ();
- }
+ candidates = gedit_settings_get_candidate_encodings ();
- /* Then prepend the encoding stored in the metadata. */
+ /* Prepend the encoding stored in the metadata. */
doc = gedit_tab_get_document (tab);
metadata_charset = gedit_document_get_metadata (doc, GEDIT_METADATA_ATTRIBUTE_ENCODING);
@@ -1996,10 +1980,7 @@ get_candidate_encodings (GeditTab *tab)
candidates = g_slist_prepend (candidates, (gpointer)file_encoding);
}
- g_object_unref (settings);
- g_strfreev (settings_strv);
g_free (metadata_charset);
-
return candidates;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]