libvirt-sandbox-config-network-filterref.c 7.48 KB
Newer Older
Ian Main's avatar
Ian Main 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
/*
 * libvirt-sandbox-config-network-filterref.c: libvirt sandbox filterr reference
 * configuration
 *
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This library 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 library 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 Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Ian Main <imain@redhat.com>
 */

#include <config.h>
#include <string.h>

27
#include "libvirt-sandbox/libvirt-sandbox-config-all.h"
Ian Main's avatar
Ian Main committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/**
 * SECTION: libvirt-sandbox-config-network-filterref
 * @short_description: Add a network filter to a network interface.
 * @include: libvirt-sandbox/libvirt-sandbox.h
 * @see_aloso: #GVirSandboxConfig
 *
 * Provides an object to store the name of the filter reference.
 *
 * The GVirSandboxConfigNetworkFilterref object stores the name of the filter
 * references associated with a network interface.
 */

#define GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF_GET_PRIVATE(obj)                         \
        (G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_SANDBOX_TYPE_CONFIG_NETWORK_FILTERREF, GVirSandboxConfigNetworkFilterrefPrivate))

struct _GVirSandboxConfigNetworkFilterrefPrivate
{
    gchar *filter;
    GList *parameters;
};

50
G_DEFINE_TYPE_WITH_PRIVATE(GVirSandboxConfigNetworkFilterref, gvir_sandbox_config_network_filterref, G_TYPE_OBJECT);
Ian Main's avatar
Ian Main committed
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213


enum {
    PROP_0,
    PROP_NAME
};

enum {
    LAST_SIGNAL
};

//static gint signals[LAST_SIGNAL];

static void gvir_sandbox_config_network_filterref_get_property(GObject *object,
                                                               guint prop_id,
                                                               GValue *value,
                                                               GParamSpec *pspec)
{
    GVirSandboxConfigNetworkFilterref *config = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF(object);
    GVirSandboxConfigNetworkFilterrefPrivate *priv = config->priv;

    switch (prop_id) {
    case PROP_NAME:
        g_value_set_string(value, priv->filter);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}


static void gvir_sandbox_config_network_filterref_set_property(GObject *object,
                                                               guint prop_id,
                                                               const GValue *value,
                                                               GParamSpec *pspec)
{
    GVirSandboxConfigNetworkFilterref *filterref = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF(object);

    switch (prop_id) {
    case PROP_NAME:
        gvir_sandbox_config_network_filterref_set_name(filterref, g_value_get_string(value));
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    }
}



static void gvir_sandbox_config_network_filterref_finalize(GObject *object)
{
    GVirSandboxConfigNetworkFilterref *config = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF(object);
    GVirSandboxConfigNetworkFilterrefPrivate *priv = config->priv;

    g_free(priv->filter);
    g_list_foreach(priv->parameters, (GFunc)g_object_unref, NULL);
    g_list_free(priv->parameters);

    G_OBJECT_CLASS(gvir_sandbox_config_network_filterref_parent_class)->finalize(object);
}


static void gvir_sandbox_config_network_filterref_class_init(GVirSandboxConfigNetworkFilterrefClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->finalize = gvir_sandbox_config_network_filterref_finalize;
    object_class->get_property = gvir_sandbox_config_network_filterref_get_property;
    object_class->set_property = gvir_sandbox_config_network_filterref_set_property;

    g_object_class_install_property(object_class,
                                    PROP_NAME,
                                    g_param_spec_string("name",
                                                        "Filter name",
                                                        "The filter reference name",
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS));
}

/**
 * gvir_sandbox_config_network_filterref_new:
 *
 * Create a new network filterref config.
 *
 * Returns: (transfer full): a new sandbox network_filterref object
 */
GVirSandboxConfigNetworkFilterref *gvir_sandbox_config_network_filterref_new(void)
{
    return GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF(g_object_new(GVIR_SANDBOX_TYPE_CONFIG_NETWORK_FILTERREF,
                                                              NULL));
}


static void gvir_sandbox_config_network_filterref_init(GVirSandboxConfigNetworkFilterref *config)
{
    config->priv = GVIR_SANDBOX_CONFIG_NETWORK_FILTERREF_GET_PRIVATE(config);
}


/**
 * gvir_sandbox_config_network_filterref_get_name:
 * @config: (transfer none): the network filter reference name
 *
 * Retrieves the network filter reference name.
 *
 * Returns: (transfer none): the network filter reference name.
 */
const gchar *gvir_sandbox_config_network_filterref_get_name(GVirSandboxConfigNetworkFilterref *filterref)
{
    g_return_val_if_fail(GVIR_SANDBOX_IS_CONFIG_NETWORK_FILTERREF(filterref), NULL);

    return filterref->priv->filter;
}

void gvir_sandbox_config_network_filterref_set_name(GVirSandboxConfigNetworkFilterref *filterref,
                                                    const gchar *name)
{
    g_return_if_fail(GVIR_SANDBOX_IS_CONFIG_NETWORK_FILTERREF(filterref));

    g_free(filterref->priv->filter);
    filterref->priv->filter = g_strdup(name);
    g_object_notify(G_OBJECT(filterref), "name");
}

/**
 * gvir_sandbox_config_network_filterref_add_parameter:
 * @filter: (transfer none): the network filter reference.
 * @param: (transfer none): the filter parameter
 *
 * Add a parameter to a network filter reference.
 */
void gvir_sandbox_config_network_filterref_add_parameter(GVirSandboxConfigNetworkFilterref *filter,
                                                         GVirSandboxConfigNetworkFilterrefParameter *param)
{
    GVirSandboxConfigNetworkFilterrefPrivate *priv;

    g_return_if_fail(GVIR_SANDBOX_IS_CONFIG_NETWORK_FILTERREF(filter));
    g_return_if_fail(GVIR_SANDBOX_IS_CONFIG_NETWORK_FILTERREF_PARAMETER(param));
    priv = filter->priv;
    priv->parameters = g_list_append(priv->parameters, g_object_ref(param));
}


/**
 * gvir_sandbox_config_network_filterref_get_parameters:
 * @filter: (transfer none): the filter reference configuration
 *
 * Retrieve the list of parameters associated with a network filter reference
 *
 * Returns: (transfer full)(element-type GVirSandboxConfigNetworkFilterrefParameter): the parameter list
 */
GList *gvir_sandbox_config_network_filterref_get_parameters(GVirSandboxConfigNetworkFilterref *filter)
{
    GVirSandboxConfigNetworkFilterrefPrivate *priv;

    g_return_val_if_fail(GVIR_SANDBOX_IS_CONFIG_NETWORK_FILTERREF(filter), NULL);
    priv = filter->priv;
    g_list_foreach(priv->parameters, (GFunc)g_object_ref, NULL);
    return g_list_copy(priv->parameters);
}