Commit 86d1d30c authored by Robert Ancell's avatar Robert Ancell Committed by Georges Basile Stavracas Neto

common: Use g_auto for variables

parent eff88ab2
Pipeline #14670 passed with stages
in 8 minutes and 2 seconds
......@@ -43,22 +43,20 @@ iter_for_language (GtkTreeModel *model,
GtkTreeIter *iter,
gboolean region)
{
char *l;
char *name;
char *language;
g_autofree gchar *name = NULL;
g_assert (gtk_tree_model_get_iter_first (model, iter));
do {
g_autofree gchar *l = NULL;
gtk_tree_model_get (model, iter, LOCALE_COL, &l, -1);
if (g_strcmp0 (l, lang) == 0) {
g_free (l);
if (g_strcmp0 (l, lang) == 0)
return TRUE;
}
g_free (l);
} while (gtk_tree_model_iter_next (model, iter));
name = gnome_normalize_locale (lang);
if (name != NULL) {
g_autofree gchar *language = NULL;
if (region) {
language = gnome_get_country_from_locale (name, NULL);
}
......@@ -72,8 +70,6 @@ iter_for_language (GtkTreeModel *model,
LOCALE_COL, name,
DISPLAY_LOCALE_COL, language,
-1);
g_free (name);
g_free (language);
return TRUE;
}
......@@ -91,12 +87,12 @@ cc_common_language_get_iter_for_language (GtkTreeModel *model,
gboolean
cc_common_language_has_font (const gchar *locale)
{
const FcCharSet *charset;
FcPattern *pattern;
FcObjectSet *object_set;
FcFontSet *font_set;
gchar *language_code;
gboolean is_displayable;
const FcCharSet *charset;
FcPattern *pattern;
FcObjectSet *object_set;
FcFontSet *font_set;
g_autofree gchar *language_code = NULL;
gboolean is_displayable;
is_displayable = FALSE;
pattern = NULL;
......@@ -141,8 +137,6 @@ cc_common_language_has_font (const gchar *locale)
if (pattern != NULL)
FcPatternDestroy (pattern);
g_free (language_code);
return is_displayable;
}
......@@ -150,12 +144,11 @@ gchar *
cc_common_language_get_current_language (void)
{
gchar *language;
char *path;
g_autofree gchar *path = NULL;
const gchar *locale;
path = g_strdup_printf ("/org/freedesktop/Accounts/User%d", getuid ());
language = get_lang_for_user_object_path (path);
g_free (path);
if (language != NULL && *language != '\0')
return language;
......@@ -171,9 +164,9 @@ cc_common_language_get_current_language (void)
static char *
get_lang_for_user_object_path (const char *path)
{
GError *error = NULL;
GDBusProxy *user;
GVariant *props;
g_autoptr(GError) error = NULL;
g_autoptr(GDBusProxy) user = NULL;
g_autoptr(GVariant) props = NULL;
char *lang;
user = cc_object_storage_create_dbus_proxy_sync (G_BUS_TYPE_SYSTEM,
......@@ -186,19 +179,14 @@ get_lang_for_user_object_path (const char *path)
if (user == NULL) {
g_warning ("Failed to get proxy for user '%s': %s",
path, error->message);
g_error_free (error);
return NULL;
}
props = g_dbus_proxy_get_cached_property (user, "Language");
if (props == NULL) {
g_object_unref (user);
if (props == NULL)
return NULL;
}
lang = g_variant_dup_string (props, NULL);
g_variant_unref (props);
g_object_unref (user);
return lang;
}
......@@ -210,31 +198,24 @@ static void
insert_language (GHashTable *ht,
const char *lang)
{
char *label_own_lang;
char *label_current_lang;
char *label_untranslated;
char *key;
g_autofree gchar *label_own_lang = NULL;
g_autofree gchar *label_current_lang = NULL;
g_autofree gchar *label_untranslated = NULL;
key = g_strdup (lang);
label_own_lang = gnome_get_language_from_locale (key, key);
label_current_lang = gnome_get_language_from_locale (key, NULL);
label_untranslated = gnome_get_language_from_locale (key, "C");
label_own_lang = gnome_get_language_from_locale (lang, lang);
label_current_lang = gnome_get_language_from_locale (lang, NULL);
label_untranslated = gnome_get_language_from_locale (lang, "C");
/* We don't have a translation for the label in
* its own language? */
if (g_strcmp0 (label_own_lang, label_untranslated) == 0) {
if (g_strcmp0 (label_current_lang, label_untranslated) == 0)
g_hash_table_insert (ht, key, g_strdup (label_untranslated));
g_hash_table_insert (ht, g_strdup (lang), g_strdup (label_untranslated));
else
g_hash_table_insert (ht, key, g_strdup (label_current_lang));
g_hash_table_insert (ht, g_strdup (lang), g_strdup (label_current_lang));
} else {
g_hash_table_insert (ht, key, g_strdup (label_own_lang));
g_hash_table_insert (ht, g_strdup (lang), g_strdup (label_own_lang));
}
g_free (label_own_lang);
g_free (label_current_lang);
g_free (label_untranslated);
}
GHashTable *
......@@ -277,7 +258,7 @@ foreach_user_lang_cb (gpointer key,
void
cc_common_language_add_user_languages (GtkTreeModel *model)
{
char *name;
g_autofree gchar *name = NULL;
GtkTreeIter iter;
GtkListStore *store = GTK_LIST_STORE (model);
GHashTable *user_langs;
......@@ -291,20 +272,17 @@ cc_common_language_add_user_languages (GtkTreeModel *model)
name = cc_common_language_get_current_language ();
display = g_hash_table_lookup (user_langs, name);
if (!display) {
char *language = NULL;
char *country = NULL;
char *codeset = NULL;
g_autofree gchar *language = NULL;
g_autofree gchar *country = NULL;
g_autofree gchar *codeset = NULL;
gnome_parse_locale (name, &language, &country, &codeset, NULL);
g_free (name);
if (!codeset || !g_str_equal (codeset, "UTF-8"))
g_warning ("Current user locale codeset isn't UTF-8");
g_free (name);
name = g_strdup_printf ("%s_%s.UTF-8", language, country);
g_free (language);
g_free (country);
g_free (codeset);
insert_language (user_langs, name);
display = g_hash_table_lookup (user_langs, name);
......@@ -313,7 +291,6 @@ cc_common_language_add_user_languages (GtkTreeModel *model)
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, LOCALE_COL, name, DISPLAY_LOCALE_COL, display, -1);
g_hash_table_remove (user_langs, name);
g_free (name);
/* The rest of the languages */
g_hash_table_foreach (user_langs, (GHFunc) foreach_user_lang_cb, store);
......
......@@ -39,58 +39,43 @@ G_DEFINE_TYPE (CcHostnameEntry, cc_hostname_entry, GTK_TYPE_ENTRY)
static void
cc_hostname_entry_set_hostname (CcHostnameEntry *self)
{
char *hostname;
GVariant *variant;
GError *error = NULL;
g_autofree gchar *hostname = NULL;
g_autoptr(GVariant) pretty_result = NULL;
g_autoptr(GVariant) static_result = NULL;
g_autoptr(GError) pretty_error = NULL;
g_autoptr(GError) static_error = NULL;
const gchar *text;
text = gtk_entry_get_text (GTK_ENTRY (self));
g_debug ("Setting PrettyHostname to '%s'", text);
variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetPrettyHostname",
g_variant_new ("(sb)", text, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &error);
if (variant == NULL)
{
g_warning ("Could not set PrettyHostname: %s", error->message);
g_error_free (error);
error = NULL;
}
else
{
g_variant_unref (variant);
}
pretty_result = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetPrettyHostname",
g_variant_new ("(sb)", text, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &pretty_error);
if (pretty_result == NULL)
g_warning ("Could not set PrettyHostname: %s", pretty_error->message);
/* Set the static hostname */
hostname = pretty_hostname_to_static (text, FALSE);
g_assert (hostname);
g_debug ("Setting StaticHostname to '%s'", hostname);
variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetStaticHostname",
g_variant_new ("(sb)", hostname, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &error);
if (variant == NULL)
{
g_warning ("Could not set StaticHostname: %s", error->message);
g_error_free (error);
}
else
{
g_variant_unref (variant);
}
g_free (hostname);
static_result = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetStaticHostname",
g_variant_new ("(sb)", hostname, FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, &static_error);
if (static_result == NULL)
g_warning ("Could not set StaticHostname: %s", static_error->message);
}
static char *
get_hostname_property (CcHostnameEntry *self,
const char *property)
{
GVariant *variant;
char *str;
g_autoptr(GVariant) variant = NULL;
if (!self->hostnamed_proxy)
return g_strdup ("");
......@@ -99,8 +84,8 @@ get_hostname_property (CcHostnameEntry *self,
property);
if (!variant)
{
GError *error = NULL;
GVariant *inner;
g_autoptr(GError) error = NULL;
g_autoptr(GVariant) inner = NULL;
/* Work around systemd-hostname not sending us back
* the property value when changing values */
......@@ -114,39 +99,31 @@ get_hostname_property (CcHostnameEntry *self,
if (variant == NULL)
{
g_warning ("Failed to get property '%s': %s", property, error->message);
g_error_free (error);
return NULL;
}
g_variant_get (variant, "(v)", &inner);
str = g_variant_dup_string (inner, NULL);
g_variant_unref (variant);
return g_variant_dup_string (inner, NULL);
}
else
{
str = g_variant_dup_string (variant, NULL);
g_variant_unref (variant);
return g_variant_dup_string (variant, NULL);
}
return str;
}
static char *
cc_hostname_entry_get_display_hostname (CcHostnameEntry *self)
{
char *str;
g_autofree gchar *str = NULL;
str = get_hostname_property (self, "PrettyHostname");
/* Empty strings means that we need to fallback */
if (str != NULL &&
*str == '\0')
{
g_free (str);
str = get_hostname_property (self, "Hostname");
}
return get_hostname_property (self, "Hostname");
return str;
return g_steal_pointer (&str);
}
static gboolean
......@@ -205,8 +182,8 @@ cc_hostname_entry_constructed (GObject *object)
{
CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
GPermission *permission;
GError *error = NULL;
char *str;
g_autoptr(GError) error = NULL;
g_autofree gchar *str = NULL;
permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname",
NULL, NULL, NULL);
......@@ -246,7 +223,6 @@ cc_hostname_entry_constructed (GObject *object)
if (self->hostnamed_proxy == NULL)
{
g_debug ("Couldn't get hostnamed to start, bailing: %s", error->message);
g_error_free (error);
return;
}
......@@ -256,7 +232,6 @@ cc_hostname_entry_constructed (GObject *object)
gtk_entry_set_text (GTK_ENTRY (self), str);
else
gtk_entry_set_text (GTK_ENTRY (self), "");
g_free (str);
g_signal_connect (G_OBJECT (self), "changed", G_CALLBACK (text_changed_cb), self);
}
......
......@@ -201,9 +201,9 @@ language_visible (GtkListBoxRow *row,
{
GtkDialog *chooser = user_data;
CcLanguageChooserPrivate *priv = GET_PRIVATE (chooser);
gchar *locale_name = NULL;
gchar *locale_current_name = NULL;
gchar *locale_untranslated_name = NULL;
g_autofree gchar *locale_name = NULL;
g_autofree gchar *locale_current_name = NULL;
g_autofree gchar *locale_untranslated_name = NULL;
gboolean is_extra;
gboolean visible;
......@@ -218,29 +218,21 @@ language_visible (GtkListBoxRow *row,
if (!priv->filter_words)
return TRUE;
visible = FALSE;
locale_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-name"));
visible = match_all (priv->filter_words, locale_name);
if (visible)
goto out;
return TRUE;
locale_current_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-current-name"));
visible = match_all (priv->filter_words, locale_current_name);
if (visible)
goto out;
return TRUE;
locale_untranslated_name =
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-untranslated-name"));
visible = match_all (priv->filter_words, locale_untranslated_name);
out:
g_free (locale_untranslated_name);
g_free (locale_current_name);
g_free (locale_name);
return visible;
return match_all (priv->filter_words, locale_untranslated_name);
}
static gint
......@@ -266,7 +258,7 @@ static void
filter_changed (GtkDialog *chooser)
{
CcLanguageChooserPrivate *priv = GET_PRIVATE (chooser);
gchar *filter_contents = NULL;
g_autofree gchar *filter_contents = NULL;
g_clear_pointer (&priv->filter_words, g_strfreev);
......@@ -278,7 +270,6 @@ filter_changed (GtkDialog *chooser)
return;
}
priv->filter_words = g_strsplit_set (g_strstrip (filter_contents), " ", 0);
g_free (filter_contents);
gtk_list_box_set_placeholder (GTK_LIST_BOX (priv->language_list), priv->no_results);
gtk_list_box_invalidate_filter (GTK_LIST_BOX (priv->language_list));
}
......@@ -305,7 +296,8 @@ set_locale_id (GtkDialog *chooser,
const gchar *locale_id)
{
CcLanguageChooserPrivate *priv = GET_PRIVATE (chooser);
GList *children, *l;
g_autoptr(GList) children = NULL;
GList *l;
children = gtk_container_get_children (GTK_CONTAINER (priv->language_list));
for (l = children; l; l = l->next) {
......@@ -330,7 +322,6 @@ set_locale_id (GtkDialog *chooser,
gtk_widget_set_opacity (check, 0.0);
}
}
g_list_free (children);
g_free (priv->language);
priv->language = g_strdup (locale_id);
......@@ -397,18 +388,16 @@ cc_language_chooser_private_free (gpointer data)
GtkWidget *
cc_language_chooser_new (GtkWidget *parent)
{
GtkBuilder *builder;
g_autoptr(GtkBuilder) builder = NULL;
GtkWidget *chooser;
CcLanguageChooserPrivate *priv;
GError *error = NULL;
g_autoptr(GError) error = NULL;
g_resources_register (cc_common_get_resource ());
builder = gtk_builder_new ();
if (gtk_builder_add_from_resource (builder, "/org/gnome/control-center/common/language-chooser.ui", &error) == 0) {
g_object_unref (builder);
g_warning ("failed to load language chooser: %s", error->message);
g_error_free (error);
return NULL;
}
......
......@@ -46,7 +46,8 @@
char *
cc_util_normalize_casefold_and_unaccent (const char *str)
{
char *normalized, *tmp;
g_autofree gchar *normalized = NULL;
gchar *tmp;
int i = 0, j = 0, ilen;
if (str == NULL)
......@@ -54,7 +55,6 @@ cc_util_normalize_casefold_and_unaccent (const char *str)
normalized = g_utf8_normalize (str, -1, G_NORMALIZE_NFKD);
tmp = g_utf8_casefold (normalized, -1);
g_free (normalized);
ilen = strlen (tmp);
......@@ -110,8 +110,8 @@ cc_util_normalize_casefold_and_unaccent (const char *str)
char *
cc_util_get_smart_date (GDateTime *date)
{
gchar *label;
GDateTime *today, *local;
g_autoptr(GDateTime) today = NULL;
g_autoptr(GDateTime) local = NULL;
GTimeSpan span;
/* Set today date */
......@@ -124,28 +124,23 @@ cc_util_get_smart_date (GDateTime *date)
span = g_date_time_difference (today, date);
if (span <= 0)
{
label = g_strdup (_("Today"));
return g_strdup (_("Today"));
}
else if (span <= G_TIME_SPAN_DAY)
{
label = g_strdup (_("Yesterday"));
return g_strdup (_("Yesterday"));
}
else
{
if (g_date_time_get_year (date) == g_date_time_get_year (today))
{
/* Translators: This is a date format string in the style of "Feb 24". */
label = g_date_time_format (date, _("%b %e"));
return g_date_time_format (date, _("%b %e"));
}
else
{
/* Translators: This is a date format string in the style of "Feb 24, 2013". */
label = g_date_time_format (date, _("%b %e, %Y"));
return g_date_time_format (date, _("%b %e, %Y"));
}
}
g_date_time_unref (local);
g_date_time_unref (today);
return label;
}
......@@ -78,8 +78,7 @@ create_device (GUdevDevice *udev_device)
{
const gchar *vendor, *product, *name;
guint width, height;
GUdevDevice *parent;
GsdDevice *device;
g_autoptr(GUdevDevice) parent = NULL;
parent = g_udev_device_get_parent (udev_device);
g_assert (parent != NULL);
......@@ -96,19 +95,15 @@ create_device (GUdevDevice *udev_device)
width = g_udev_device_get_property_as_int (udev_device, "ID_INPUT_WIDTH_MM");
height = g_udev_device_get_property_as_int (udev_device, "ID_INPUT_HEIGHT_MM");
device = g_object_new (GSD_TYPE_DEVICE,
"name", name,
"device-file", g_udev_device_get_device_file (udev_device),
"type", udev_device_get_device_type (udev_device),
"vendor-id", vendor,
"product-id", product,
"width", width,
"height", height,
NULL);
g_object_unref (parent);
return device;
return g_object_new (GSD_TYPE_DEVICE,
"name", name,
"device-file", g_udev_device_get_device_file (udev_device),
"type", udev_device_get_device_type (udev_device),
"vendor-id", vendor,
"product-id", product,
"width", width,
"height", height,
NULL);
}
static void
......@@ -166,7 +161,8 @@ static void
gsd_udev_device_manager_init (GsdUdevDeviceManager *manager)
{
const gchar *subsystems[] = { "input", NULL };
GList *devices, *l;
g_autoptr(GList) devices = NULL;
GList *l;
manager->devices = g_hash_table_new_full (NULL, NULL,
(GDestroyNotify) g_object_unref,
......@@ -180,15 +176,11 @@ gsd_udev_device_manager_init (GsdUdevDeviceManager *manager)
subsystems[0]);
for (l = devices; l; l = l->next) {
GUdevDevice *device = l->data;
g_autoptr(GUdevDevice) device = l->data;
if (device_is_evdev (device))
add_device (manager, device);
g_object_unref (device);
}
g_list_free (devices);
}
static void
......
......@@ -144,15 +144,14 @@ static void
init_devices (GsdX11DeviceManager *manager,
GdkDeviceType device_type)
{
GList *devices, *l;
g_autoptr(GList) devices = NULL;
GList *l;
devices = gdk_device_manager_list_devices (manager->device_manager,
device_type);
for (l = devices; l; l = l->next)
add_device (manager, l->data);
g_list_free (devices);
}
static void
......
......@@ -333,9 +333,8 @@ GSettings *
gsd_device_get_settings (GsdDevice *device)
{
const gchar *schema = NULL, *vendor, *product;
GSettings *settings;
GsdDeviceType type;
gchar *path = NULL;
g_autofree gchar *path = NULL;
g_return_val_if_fail (GSD_IS_DEVICE (device), NULL);
......@@ -362,13 +361,10 @@ gsd_device_get_settings (GsdDevice *device)
}
if (path) {
settings = g_settings_new_with_path (schema, path);
g_free (path);
return g_settings_new_with_path (schema, path);
} else {
settings = g_settings_new (schema);
return g_settings_new (schema);
}
return settings;
}
const gchar *
......
......@@ -157,9 +157,8 @@ supports_xinput2_devices (int *opcode)
static gboolean
device_type_is_present (GsdDeviceType type)
{
GList *l = gsd_device_manager_list_devices (gsd_device_manager_get (),
type);
g_list_free (l);
g_autoptr(GList) l = gsd_device_manager_list_devices (gsd_device_manager_get (),
type);
return l != NULL;
}
......
......@@ -97,15 +97,15 @@ remove_duplicate_dashes (char *input)
return input;
}
#define CHECK if (is_empty (result)) goto bail
#define CHECK if (is_empty (result)) return g_strdup ("localhost")
char *
pretty_hostname_to_static (const char *pretty,
gboolean for_display)
{
char *result;
char *valid_chars;
char *composed;
g_autofree gchar *result = NULL;
g_autofree gchar *valid_chars = NULL;
g_autofree gchar *composed = NULL;
g_return_val_if_fail (pretty != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (pretty, -1, NULL), NULL);
......@@ -117,56 +117,45 @@ pretty_hostname_to_static (const char *pretty,
/* Transform the pretty hostname to ASCII */
result = g_str_to_ascii (composed, NULL);
g_debug ("\ttranslit: '%s'", result);
g_free (composed);
CHECK;
/* Remove apostrophes */
result = remove_apostrophes (result);
remove_apostrophes (result);
g_debug ("\tapostrophes: '%s'", result);
CHECK;
/* Remove all the not-allowed chars */
valid_chars = allowed_chars ();
result = g_strcanon (result, valid_chars, '-');
g_free (valid_chars);
g_strcanon (result, valid_chars, '-');
g_debug ("\tcanon: '%s'", result);
CHECK;
/* Remove the leading dashes */
result = remove_leading_dashes (result);
remove_leading_dashes (result);
g_debug ("\tleading: '%s'", result);
CHECK;
/* Remove trailing dashes */
result = remove_trailing_dashes (result);
remove_trailing_dashes (result);
g_debug ("\ttrailing: '%s'", result);
CHECK;
/* Remove duplicate dashes */
result = remove_duplicate_dashes (result);
remove_duplicate_dashes (result);
g_debug ("\tduplicate: '%s'", result);
CHECK;
/* Lower case */
if (!for_display) {
char *tmp;
if (!for_display)
return g_ascii_strdown (result, -1);
tmp = g_ascii_strdown (result, -1);
g_free (result);
result = tmp;
}
return result;
bail:
g_free (result);
return g_strdup ("localhost");
return g_steal_pointer (&result);
}
#undef CHECK
......@@ -176,7 +165,6 @@ char *
pretty_hostname_to_ssid (const char *pretty)
{
const char *p, *prev;
char *ret = NULL;
if (pretty == NULL || *pretty == '\0') {