gclue-service-client.c 7.63 KB
Newer Older
Zeeshan Ali's avatar
Zeeshan Ali committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* vim: set et ts=8 sw=8: */
/* gclue-service-client.c
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 */

#include <glib/gi18n.h>

#include "gclue-service-client.h"

static void
27 28 29
gclue_service_client_client_iface_init (GClueClientIface *iface);
static void
gclue_service_client_initable_iface_init (GInitableIface *iface);
Zeeshan Ali's avatar
Zeeshan Ali committed
30 31 32 33 34

G_DEFINE_TYPE_WITH_CODE (GClueServiceClient,
                         gclue_service_client,
                         GCLUE_TYPE_CLIENT_SKELETON,
                         G_IMPLEMENT_INTERFACE (GCLUE_TYPE_CLIENT,
35 36 37
                                                gclue_service_client_client_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                gclue_service_client_initable_iface_init));
Zeeshan Ali's avatar
Zeeshan Ali committed
38 39 40 41

struct _GClueServiceClientPrivate
{
        const char *path;
42
        GDBusConnection *connection;
Zeeshan Ali's avatar
Zeeshan Ali committed
43 44 45 46 47 48
};

enum
{
        PROP_0,
        PROP_PATH,
49
        PROP_CONNECTION,
Zeeshan Ali's avatar
Zeeshan Ali committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

static gboolean
gclue_service_client_handle_start (GClueClient           *client,
                                   GDBusMethodInvocation *invocation,
                                   gpointer               user_data)
{
        gclue_client_complete_start (client, invocation);

        return TRUE;
}

static gboolean
gclue_service_client_handle_stop (GClueClient           *client,
                                  GDBusMethodInvocation *invocation,
                                  gpointer               user_data)
{
        gclue_client_complete_stop (client, invocation);

        return TRUE;
}

static void
gclue_service_client_finalize (GObject *object)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (object)->priv;

        if (priv->path) {
                g_free (priv->path);
                priv->path = NULL;
        }
84
        g_clear_object (&priv->connection);
Zeeshan Ali's avatar
Zeeshan Ali committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        /* Chain up to the parent class */
        G_OBJECT_CLASS (gclue_service_client_parent_class)->finalize (object);
}

static void
gclue_service_client_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (object);

        switch (prop_id) {
        case PROP_PATH:
                g_value_set_string (value, client->priv->path);
                break;

103 104 105 106
        case PROP_CONNECTION:
                g_value_set_object (value, client->priv->connection);
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_service_client_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (object);

        switch (prop_id) {
        case PROP_PATH:
                client->priv->path = g_value_dup_string (value);
                break;

125 126 127 128
        case PROP_CONNECTION:
                client->priv->connection = g_value_dup_object (value);
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_service_client_class_init (GClueServiceClientClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gclue_service_client_finalize;
        object_class->get_property = gclue_service_client_get_property;
        object_class->set_property = gclue_service_client_set_property;

        g_type_class_add_private (object_class, sizeof (GClueServiceClientPrivate));

        gParamSpecs[PROP_PATH] = g_param_spec_string ("path",
                                                      "Path",
                                                      "Path",
                                                      NULL,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_PATH,
                                         gParamSpecs[PROP_PATH]);
155 156 157 158 159 160 161 162 163 164

        gParamSpecs[PROP_CONNECTION] = g_param_spec_object ("connection",
                                                            "Connection",
                                                            "DBus Connection",
                                                            G_TYPE_DBUS_CONNECTION,
                                                            G_PARAM_READWRITE |
                                                            G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_CONNECTION,
                                         gParamSpecs[PROP_CONNECTION]);
Zeeshan Ali's avatar
Zeeshan Ali committed
165 166 167
}

static void
168
gclue_service_client_client_iface_init (GClueClientIface *iface)
Zeeshan Ali's avatar
Zeeshan Ali committed
169 170 171 172 173
{
        iface->handle_start = gclue_service_client_handle_start;
        iface->handle_stop = gclue_service_client_handle_stop;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static gboolean
gclue_service_client_initable_init (GInitable    *initable,
                                    GCancellable *cancellable,
                                    GError      **error)
{
        GDBusInterfaceSkeleton *skeleton = G_DBUS_INTERFACE_SKELETON (initable);

        return (g_dbus_connection_register_object
                        (GCLUE_SERVICE_CLIENT (initable)->priv->connection,
                         GCLUE_SERVICE_CLIENT (initable)->priv->path,
                         g_dbus_interface_skeleton_get_info (skeleton),
                         g_dbus_interface_skeleton_get_vtable (skeleton),
                         initable,
                         NULL,
                         error) != 0);
}

static void
gclue_service_client_initable_iface_init (GInitableIface *iface)
{
        iface->init = gclue_service_client_initable_init;
}

Zeeshan Ali's avatar
Zeeshan Ali committed
197 198 199 200 201 202 203 204 205
static void
gclue_service_client_init (GClueServiceClient *client)
{
        client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
                                                    GClUE_TYPE_SERVICE_CLIENT,
                                                    GClueServiceClientPrivate);
}

GClueServiceClient *
206 207 208
gclue_service_client_new (const char      *path,
                          GDBusConnection *connection,
                          GError         **error)
Zeeshan Ali's avatar
Zeeshan Ali committed
209
{
210 211 212 213 214 215
        return g_initable_new (GClUE_TYPE_SERVICE_CLIENT,
                               NULL,
                               error,
                               "path", path,
                               "connection", connection,
                               NULL);
Zeeshan Ali's avatar
Zeeshan Ali committed
216 217 218 219 220 221 222 223 224
}

const gchar *
gclue_service_client_get_path (GClueServiceClient *client)
{
        g_return_val_if_fail (GClUE_IS_SERVICE_CLIENT(client), NULL);

        return client->priv->path;
}