hostname-entry: Modernize code

Turn it into a final class, remove the private field and
update the code.
This commit is contained in:
Georges Basile Stavracas Neto 2018-01-21 10:48:56 -02:00
parent 4a34b693de
commit efe3095eaf
2 changed files with 36 additions and 85 deletions

View file

@ -24,20 +24,18 @@
#include <polkit/polkit.h> #include <polkit/polkit.h>
struct _CcHostnameEntry
G_DEFINE_TYPE (CcHostnameEntry, cc_hostname_entry, GTK_TYPE_ENTRY)
#define HOSTNAME_ENTRY_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_HOSTNAME_ENTRY, CcHostnameEntryPrivate))
#define SET_HOSTNAME_TIMEOUT 1
struct _CcHostnameEntryPrivate
{ {
GtkEntry parent;
GDBusProxy *hostnamed_proxy; GDBusProxy *hostnamed_proxy;
guint set_hostname_timeout_source_id; guint set_hostname_timeout_source_id;
}; };
G_DEFINE_TYPE (CcHostnameEntry, cc_hostname_entry, GTK_TYPE_ENTRY)
#define SET_HOSTNAME_TIMEOUT 1
static void static void
cc_hostname_entry_set_hostname (CcHostnameEntry *self) cc_hostname_entry_set_hostname (CcHostnameEntry *self)
{ {
@ -49,7 +47,7 @@ cc_hostname_entry_set_hostname (CcHostnameEntry *self)
text = gtk_entry_get_text (GTK_ENTRY (self)); text = gtk_entry_get_text (GTK_ENTRY (self));
g_debug ("Setting PrettyHostname to '%s'", text); g_debug ("Setting PrettyHostname to '%s'", text);
variant = g_dbus_proxy_call_sync (self->priv->hostnamed_proxy, variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetPrettyHostname", "SetPrettyHostname",
g_variant_new ("(sb)", text, FALSE), g_variant_new ("(sb)", text, FALSE),
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
@ -70,7 +68,7 @@ cc_hostname_entry_set_hostname (CcHostnameEntry *self)
g_assert (hostname); g_assert (hostname);
g_debug ("Setting StaticHostname to '%s'", hostname); g_debug ("Setting StaticHostname to '%s'", hostname);
variant = g_dbus_proxy_call_sync (self->priv->hostnamed_proxy, variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"SetStaticHostname", "SetStaticHostname",
g_variant_new ("(sb)", hostname, FALSE), g_variant_new ("(sb)", hostname, FALSE),
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
@ -91,14 +89,13 @@ static char *
get_hostname_property (CcHostnameEntry *self, get_hostname_property (CcHostnameEntry *self,
const char *property) const char *property)
{ {
CcHostnameEntryPrivate *priv = self->priv;
GVariant *variant; GVariant *variant;
char *str; char *str;
if (!priv->hostnamed_proxy) if (!self->hostnamed_proxy)
return g_strdup (""); return g_strdup ("");
variant = g_dbus_proxy_get_cached_property (priv->hostnamed_proxy, variant = g_dbus_proxy_get_cached_property (self->hostnamed_proxy,
property); property);
if (!variant) if (!variant)
{ {
@ -107,7 +104,7 @@ get_hostname_property (CcHostnameEntry *self,
/* Work around systemd-hostname not sending us back /* Work around systemd-hostname not sending us back
* the property value when changing values */ * the property value when changing values */
variant = g_dbus_proxy_call_sync (priv->hostnamed_proxy, variant = g_dbus_proxy_call_sync (self->hostnamed_proxy,
"org.freedesktop.DBus.Properties.Get", "org.freedesktop.DBus.Properties.Get",
g_variant_new ("(ss)", "org.freedesktop.hostname1", property), g_variant_new ("(ss)", "org.freedesktop.hostname1", property),
G_DBUS_CALL_FLAGS_NONE, G_DBUS_CALL_FLAGS_NONE,
@ -155,7 +152,7 @@ cc_hostname_entry_get_display_hostname (CcHostnameEntry *self)
static gboolean static gboolean
set_hostname_timeout (CcHostnameEntry *self) set_hostname_timeout (CcHostnameEntry *self)
{ {
self->priv->set_hostname_timeout_source_id = 0; self->set_hostname_timeout_source_id = 0;
cc_hostname_entry_set_hostname (self); cc_hostname_entry_set_hostname (self);
@ -163,24 +160,22 @@ set_hostname_timeout (CcHostnameEntry *self)
} }
static void static void
remove_hostname_timeout (CcHostnameEntry *entry) remove_hostname_timeout (CcHostnameEntry *self)
{ {
CcHostnameEntryPrivate *priv = entry->priv; if (self->set_hostname_timeout_source_id)
g_source_remove (self->set_hostname_timeout_source_id);
if (priv->set_hostname_timeout_source_id) self->set_hostname_timeout_source_id = 0;
g_source_remove (priv->set_hostname_timeout_source_id);
priv->set_hostname_timeout_source_id = 0;
} }
static void static void
reset_hostname_timeout (CcHostnameEntry *entry) reset_hostname_timeout (CcHostnameEntry *self)
{ {
remove_hostname_timeout (entry); remove_hostname_timeout (self);
entry->priv->set_hostname_timeout_source_id = g_timeout_add_seconds (SET_HOSTNAME_TIMEOUT, self->set_hostname_timeout_source_id = g_timeout_add_seconds (SET_HOSTNAME_TIMEOUT,
(GSourceFunc) set_hostname_timeout, (GSourceFunc) set_hostname_timeout,
entry); self);
} }
static void static void
@ -192,26 +187,25 @@ text_changed_cb (CcHostnameEntry *entry)
static void static void
cc_hostname_entry_dispose (GObject *object) cc_hostname_entry_dispose (GObject *object)
{ {
CcHostnameEntry *entry = CC_HOSTNAME_ENTRY (object); CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
CcHostnameEntryPrivate *priv = entry->priv;
if (priv->set_hostname_timeout_source_id) if (self->set_hostname_timeout_source_id)
{ {
remove_hostname_timeout (entry); remove_hostname_timeout (self);
set_hostname_timeout (entry); set_hostname_timeout (self);
} }
g_clear_object (&priv->hostnamed_proxy); g_clear_object (&self->hostnamed_proxy);
G_OBJECT_CLASS (cc_hostname_entry_parent_class)->dispose (object); G_OBJECT_CLASS (cc_hostname_entry_parent_class)->dispose (object);
} }
static void static void
cc_hostname_entry_constructed (GObject *self) cc_hostname_entry_constructed (GObject *object)
{ {
CcHostnameEntryPrivate *priv = CC_HOSTNAME_ENTRY (self)->priv; CcHostnameEntry *self = CC_HOSTNAME_ENTRY (object);
GError *error = NULL;
GPermission *permission; GPermission *permission;
GError *error = NULL;
char *str; char *str;
permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname", permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname",
@ -238,7 +232,7 @@ cc_hostname_entry_constructed (GObject *self)
gtk_widget_set_sensitive (GTK_WIDGET (self), gtk_widget_set_sensitive (GTK_WIDGET (self),
g_permission_get_allowed (permission)); g_permission_get_allowed (permission));
priv->hostnamed_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, self->hostnamed_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE, G_DBUS_PROXY_FLAGS_NONE,
NULL, NULL,
"org.freedesktop.hostname1", "org.freedesktop.hostname1",
@ -249,7 +243,7 @@ cc_hostname_entry_constructed (GObject *self)
/* This could only happen if the policy file was installed /* This could only happen if the policy file was installed
* but not hostnamed, which points to a system bug */ * but not hostnamed, which points to a system bug */
if (priv->hostnamed_proxy == NULL) if (self->hostnamed_proxy == NULL)
{ {
g_debug ("Couldn't get hostnamed to start, bailing: %s", error->message); g_debug ("Couldn't get hostnamed to start, bailing: %s", error->message);
g_error_free (error); g_error_free (error);
@ -264,8 +258,7 @@ cc_hostname_entry_constructed (GObject *self)
gtk_entry_set_text (GTK_ENTRY (self), ""); gtk_entry_set_text (GTK_ENTRY (self), "");
g_free (str); g_free (str);
g_signal_connect (G_OBJECT (self), "changed", G_CALLBACK (text_changed_cb), g_signal_connect (G_OBJECT (self), "changed", G_CALLBACK (text_changed_cb), self);
self);
} }
static void static void
@ -273,8 +266,6 @@ cc_hostname_entry_class_init (CcHostnameEntryClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (CcHostnameEntryPrivate));
object_class->constructed = cc_hostname_entry_constructed; object_class->constructed = cc_hostname_entry_constructed;
object_class->dispose = cc_hostname_entry_dispose; object_class->dispose = cc_hostname_entry_dispose;
} }
@ -282,7 +273,6 @@ cc_hostname_entry_class_init (CcHostnameEntryClass *klass)
static void static void
cc_hostname_entry_init (CcHostnameEntry *self) cc_hostname_entry_init (CcHostnameEntry *self)
{ {
self->priv = HOSTNAME_ENTRY_PRIVATE (self);
} }
CcHostnameEntry * CcHostnameEntry *

View file

@ -17,56 +17,17 @@
* *
*/ */
#ifndef __CC_HOSTNAME_ENTRY_H__ #pragma once
#define __CC_HOSTNAME_ENTRY_H__
#include <gtk/gtk.h> #include <gtk/gtk.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define CC_TYPE_HOSTNAME_ENTRY cc_hostname_entry_get_type() #define CC_TYPE_HOSTNAME_ENTRY (cc_hostname_entry_get_type())
#define CC_HOSTNAME_ENTRY(obj) \ G_DECLARE_FINAL_TYPE (CcHostnameEntry, cc_hostname_entry, CC, HOSTNAME_ENTRY, GtkEntry)
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \
CC_TYPE_HOSTNAME_ENTRY, CcHostnameEntry))
#define CC_HOSTNAME_ENTRY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), \
CC_TYPE_HOSTNAME_ENTRY, CcHostnameEntryClass))
#define CC_IS_HOSTNAME_ENTRY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
CC_TYPE_HOSTNAME_ENTRY))
#define CC_IS_HOSTNAME_ENTRY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), \
CC_TYPE_HOSTNAME_ENTRY))
#define CC_HOSTNAME_ENTRY_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), \
CC_TYPE_HOSTNAME_ENTRY, CcHostnameEntryClass))
typedef struct _CcHostnameEntry CcHostnameEntry;
typedef struct _CcHostnameEntryClass CcHostnameEntryClass;
typedef struct _CcHostnameEntryPrivate CcHostnameEntryPrivate;
struct _CcHostnameEntry
{
GtkEntry parent;
CcHostnameEntryPrivate *priv;
};
struct _CcHostnameEntryClass
{
GtkEntryClass parent_class;
};
GType cc_hostname_entry_get_type (void) G_GNUC_CONST;
CcHostnameEntry *cc_hostname_entry_new (void); CcHostnameEntry *cc_hostname_entry_new (void);
gchar* cc_hostname_entry_get_hostname (CcHostnameEntry *entry); gchar* cc_hostname_entry_get_hostname (CcHostnameEntry *entry);
G_END_DECLS G_END_DECLS
#endif /* __CC_HOSTNAME_ENTRY_H__ */