mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-04 08:23:38 +02:00
socketservice: add an "active" property
We already have start, stop and is_active methods, but turning it into a real property is useful for a few reasons: - it allows us to bind the property to an UI or a setting - it allows us to get notified when the state changes - it allows us to instantiate objects directly in the stopped state https://bugzilla.gnome.org/show_bug.cgi?id=752089
This commit is contained in:
@@ -63,6 +63,7 @@
|
||||
#include <gio/gio.h>
|
||||
#include "gsocketlistener.h"
|
||||
#include "gsocketconnection.h"
|
||||
#include "glibintl.h"
|
||||
|
||||
struct _GSocketServicePrivate
|
||||
{
|
||||
@@ -77,6 +78,12 @@ G_LOCK_DEFINE_STATIC(active);
|
||||
|
||||
G_DEFINE_TYPE_WITH_PRIVATE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER)
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_ACTIVE
|
||||
};
|
||||
|
||||
static void g_socket_service_ready (GObject *object,
|
||||
GAsyncResult *result,
|
||||
gpointer user_data);
|
||||
@@ -117,6 +124,90 @@ do_accept (GSocketService *service)
|
||||
service->priv->outstanding_accept = TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
get_active (GSocketService *service)
|
||||
{
|
||||
gboolean active;
|
||||
|
||||
G_LOCK (active);
|
||||
active = service->priv->active;
|
||||
G_UNLOCK (active);
|
||||
|
||||
return active;
|
||||
}
|
||||
|
||||
static void
|
||||
set_active (GSocketService *service, gboolean active)
|
||||
{
|
||||
gboolean notify = FALSE;
|
||||
|
||||
active = !!active;
|
||||
|
||||
G_LOCK (active);
|
||||
|
||||
if (active != service->priv->active)
|
||||
{
|
||||
service->priv->active = active;
|
||||
notify = TRUE;
|
||||
|
||||
if (active)
|
||||
{
|
||||
if (service->priv->outstanding_accept)
|
||||
g_cancellable_cancel (service->priv->cancellable);
|
||||
else
|
||||
do_accept (service);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (service->priv->outstanding_accept)
|
||||
g_cancellable_cancel (service->priv->cancellable);
|
||||
}
|
||||
}
|
||||
|
||||
G_UNLOCK (active);
|
||||
|
||||
if (notify)
|
||||
g_object_notify (G_OBJECT (service), "active");
|
||||
}
|
||||
|
||||
static void
|
||||
g_socket_service_get_property (GObject *object,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GSocketService *service = G_SOCKET_SERVICE (object);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_ACTIVE:
|
||||
g_value_set_boolean (value, get_active (service));
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_socket_service_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GSocketService *service = G_SOCKET_SERVICE (object);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_ACTIVE:
|
||||
set_active (service, g_value_get_boolean (value));
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_socket_service_changed (GSocketListener *listener)
|
||||
{
|
||||
@@ -151,12 +242,9 @@ g_socket_service_changed (GSocketListener *listener)
|
||||
gboolean
|
||||
g_socket_service_is_active (GSocketService *service)
|
||||
{
|
||||
gboolean active;
|
||||
g_return_val_if_fail (G_IS_SOCKET_SERVICE (service), FALSE);
|
||||
|
||||
G_LOCK (active);
|
||||
active = service->priv->active;
|
||||
G_UNLOCK (active);
|
||||
return active;
|
||||
return get_active (service);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,19 +262,9 @@ g_socket_service_is_active (GSocketService *service)
|
||||
void
|
||||
g_socket_service_start (GSocketService *service)
|
||||
{
|
||||
G_LOCK (active);
|
||||
g_return_if_fail (G_IS_SOCKET_SERVICE (service));
|
||||
|
||||
if (!service->priv->active)
|
||||
{
|
||||
service->priv->active = TRUE;
|
||||
|
||||
if (service->priv->outstanding_accept)
|
||||
g_cancellable_cancel (service->priv->cancellable);
|
||||
else
|
||||
do_accept (service);
|
||||
}
|
||||
|
||||
G_UNLOCK (active);
|
||||
set_active (service, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,20 +288,11 @@ g_socket_service_start (GSocketService *service)
|
||||
void
|
||||
g_socket_service_stop (GSocketService *service)
|
||||
{
|
||||
G_LOCK (active);
|
||||
g_return_if_fail (G_IS_SOCKET_SERVICE (service));
|
||||
|
||||
if (service->priv->active)
|
||||
{
|
||||
service->priv->active = FALSE;
|
||||
|
||||
if (service->priv->outstanding_accept)
|
||||
g_cancellable_cancel (service->priv->cancellable);
|
||||
}
|
||||
|
||||
G_UNLOCK (active);
|
||||
set_active (service, FALSE);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
g_socket_service_incoming (GSocketService *service,
|
||||
GSocketConnection *connection,
|
||||
@@ -243,6 +312,8 @@ g_socket_service_class_init (GSocketServiceClass *class)
|
||||
GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
|
||||
|
||||
gobject_class->finalize = g_socket_service_finalize;
|
||||
gobject_class->set_property = g_socket_service_set_property;
|
||||
gobject_class->get_property = g_socket_service_get_property;
|
||||
listener_class->changed = g_socket_service_changed;
|
||||
class->incoming = g_socket_service_real_incoming;
|
||||
|
||||
@@ -271,6 +342,20 @@ g_socket_service_class_init (GSocketServiceClass *class)
|
||||
g_signal_accumulator_true_handled, NULL,
|
||||
NULL, G_TYPE_BOOLEAN,
|
||||
2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
|
||||
|
||||
/**
|
||||
* GSocketService:active:
|
||||
*
|
||||
* Whether the service is currently accepting connections.
|
||||
*
|
||||
* Since: 2.46
|
||||
*/
|
||||
g_object_class_install_property (gobject_class, PROP_ACTIVE,
|
||||
g_param_spec_boolean ("active",
|
||||
P_("Active"),
|
||||
P_("Whether the service is currently accepting connections"),
|
||||
TRUE,
|
||||
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -309,7 +394,6 @@ g_socket_service_ready (GObject *object,
|
||||
G_UNLOCK (active);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* g_socket_service_new:
|
||||
*
|
||||
|
Reference in New Issue
Block a user