Skip to content

Commit

Permalink
Do not advertise files when on a metered network connection
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsie committed Sep 7, 2023
1 parent 8b0454c commit e589f6d
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 15 deletions.
57 changes: 53 additions & 4 deletions libpassim/passim-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
typedef struct {
GDBusProxy *proxy;
gchar *version;
PassimStatus status;
} PassimClientPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(PassimClient, passim_client, G_TYPE_OBJECT)
Expand All @@ -51,6 +52,53 @@ passim_client_get_version(PassimClient *self)
return priv->version;
}

/**
* passim_client_get_status:
* @self: a #PassimClient
*
* Gets the daemon status.
*
* Returns: the #PassimStatus
*
* Since: 0.1.2
**/
PassimStatus
passim_client_get_status(PassimClient *self)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(PASSIM_IS_CLIENT(self), PASSIM_STATUS_UNKNOWN);
return priv->status;
}

static void
passim_client_load_proxy_properties(PassimClient *self)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GVariant) status = NULL;
g_autoptr(GVariant) version = NULL;

version = g_dbus_proxy_get_cached_property(priv->proxy, "DaemonVersion");
if (version != NULL) {
g_free(priv->version);
priv->version = g_variant_dup_string(version, NULL);
}
status = g_dbus_proxy_get_cached_property(priv->proxy, "Status");
if (status != NULL)
priv->status = g_variant_get_uint32(status);
}

static void
passim_client_proxy_signal_cb(GDBusProxy *proxy,
const gchar *sender_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
PassimClient *self = PASSIM_CLIENT(user_data);
if (g_strcmp0(signal_name, "Changed") == 0)
passim_client_load_proxy_properties(self);
}

/**
* passim_client_load:
* @self: a #PassimClient
Expand All @@ -66,7 +114,6 @@ gboolean
passim_client_load(PassimClient *self, GError **error)
{
PassimClientPrivate *priv = GET_PRIVATE(self);
g_autoptr(GVariant) version = NULL;

g_return_val_if_fail(PASSIM_IS_CLIENT(self), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
Expand All @@ -84,9 +131,11 @@ passim_client_load(PassimClient *self, GError **error)
g_dbus_error_strip_remote_error(*error);
return FALSE;
}
version = g_dbus_proxy_get_cached_property(priv->proxy, "DaemonVersion");
if (version != NULL)
priv->version = g_variant_dup_string(version, NULL);
g_signal_connect(G_DBUS_PROXY(priv->proxy),
"g-signal",
G_CALLBACK(passim_client_proxy_signal_cb),
self);
passim_client_load_proxy_properties(self);

/* success */
return TRUE;
Expand Down
10 changes: 10 additions & 0 deletions libpassim/passim-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ struct _PassimClientClass {
#define PASSIM_DBUS_INTERFACE "org.freedesktop.Passim"
#define PASSIM_DBUS_PATH "/"

typedef enum {
PASSIM_STATUS_UNKNOWN,
PASSIM_STATUS_STARTING,
PASSIM_STATUS_LOADING,
PASSIM_STATUS_RUNNING,
PASSIM_STATUS_DISABLED_METERED,
} PassimStatus;

PassimClient *
passim_client_new(void);
const gchar *
passim_client_get_version(PassimClient *self);
PassimStatus
passim_client_get_status(PassimClient *self);
gboolean
passim_client_load(PassimClient *self, GError **error);
GPtrArray *
Expand Down
1 change: 1 addition & 0 deletions libpassim/passim.map
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ LIBPASSIM_0.1.0 {

LIBPASSIM_0.1.2 {
global:
passim_client_get_status;
passim_item_get_size;
passim_item_set_size;
local: *;
Expand Down
9 changes: 9 additions & 0 deletions src/org.freedesktop.Passim.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
</doc:description>
</doc:doc>
</property>
<property name='Status' type='u' access='read'>
<doc:doc>
<doc:description>
<doc:para>
The daemon current status.
</doc:para>
</doc:description>
</doc:doc>
</property>
<method name='GetItems'>
<doc:doc>
<doc:description>
Expand Down
23 changes: 19 additions & 4 deletions src/passim-avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,11 @@ passim_avahi_register_subtype(PassimAvahi *self, const gchar *hash, GError **err
}

gboolean
passim_avahi_register(PassimAvahi *self, gchar **keys, GError **error)
passim_avahi_unregister(PassimAvahi *self, GError **error)
{
g_autoptr(GVariant) val1 = NULL;
g_autoptr(GVariant) val2 = NULL;
g_autoptr(GVariant) val4 = NULL;

g_return_val_if_fail(PASSIM_IS_AVAHI(self), FALSE);
g_return_val_if_fail(keys != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
g_return_val_if_fail(self->proxy != NULL, FALSE);

Expand All @@ -162,6 +159,24 @@ passim_avahi_register(PassimAvahi *self, gchar **keys, GError **error)
g_prefix_error(error, "failed to reset entry group: ");
return FALSE;
}

/* success */
return TRUE;
}

gboolean
passim_avahi_register(PassimAvahi *self, gchar **keys, GError **error)
{
g_autoptr(GVariant) val2 = NULL;
g_autoptr(GVariant) val4 = NULL;

g_return_val_if_fail(PASSIM_IS_AVAHI(self), FALSE);
g_return_val_if_fail(keys != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
g_return_val_if_fail(self->proxy != NULL, FALSE);

if (!passim_avahi_unregister(self, error))
return FALSE;
val2 = g_dbus_proxy_call_sync(self->proxy_eg,
"AddService",
g_variant_new("(iiussssqaay)",
Expand Down
2 changes: 2 additions & 0 deletions src/passim-avahi.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ passim_avahi_new(GKeyFile *config);
gboolean
passim_avahi_connect(PassimAvahi *self, GError **error);
gboolean
passim_avahi_unregister(PassimAvahi *self, GError **error);
gboolean
passim_avahi_register(PassimAvahi *self, gchar **keys, GError **error);
const gchar *
passim_avahi_get_name(PassimAvahi *self);
Expand Down
13 changes: 13 additions & 0 deletions src/passim-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,21 @@ passim_cli_cmd_array_to_string(GPtrArray *array)
static gboolean
passim_cli_dump(PassimCli *self, gchar **values, GError **error)
{
PassimStatus status = passim_client_get_status(self->client);
g_autoptr(GPtrArray) items = NULL;

/* global status */
if (status == PASSIM_STATUS_STARTING || status == PASSIM_STATUS_LOADING) {
g_print("passimd is loading…\n");
} else if (status == PASSIM_STATUS_DISABLED_METERED) {
g_print("passimd is disabled as a metered network connection is detected\n");
} else if (status == PASSIM_STATUS_RUNNING) {
g_print("passimd is running\n");
} else {
g_print("passimd status: %s\n", passim_status_to_string(status));
}

/* all items */
items = passim_client_get_items(self->client, error);
if (items == NULL)
return FALSE;
Expand Down
14 changes: 14 additions & 0 deletions src/passim-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
#define PASSIM_CONFIG_PATH "Path"
#define PASSIM_CONFIG_MAX_ITEM_SIZE "MaxItemSize"

const gchar *
passim_status_to_string(PassimStatus status)
{
if (status == PASSIM_STATUS_STARTING)
return "starting";
if (status == PASSIM_STATUS_LOADING)
return "loading";
if (status == PASSIM_STATUS_DISABLED_METERED)
return "disabled-metered";
if (status == PASSIM_STATUS_RUNNING)
return "running";
return NULL;
}

GKeyFile *
passim_config_load(GError **error)
{
Expand Down
4 changes: 3 additions & 1 deletion src/passim-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#pragma once

#include <gio/gio.h>
#include <passim.h>

const gchar *
passim_status_to_string(PassimStatus status);
GKeyFile *
passim_config_load(GError **error);
guint16
Expand Down
Loading

0 comments on commit e589f6d

Please sign in to comment.