nm-vpn-editor-plugin-call.h 7.85 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2016 Red Hat, Inc.
 */

#ifndef __NM_VPN_EDITOR_PLUGIN_CALL_H__
#define __NM_VPN_EDITOR_PLUGIN_CALL_H__

/* This header is an internal, header-only file that can be copied to
 * other projects to call well-known service functions on VPN plugins.
 *
 * This uses the NMVpnEditorPluginVT and allows a user (nm-applet)
 * to directly communicate with a VPN plugin using API that is newer
 * then the current libnm version. That is, it allows to call to a VPN
 * plugin bypassing libnm. */

#include <NetworkManager.h>

/* we make use of other internal header files, you need those too. */
#include "nm-macros-internal.h"

/*****************************************************************************/

/**
 * NMVpnEditorPluginServiceFlags:
 * @NM_VPN_EDITOR_PLUGIN_SERVICE_FLAGS_NONE: no flags
 * @NM_VPN_EDITOR_PLUGIN_SERVICE_FLAGS_CAN_ADD: whether the plugin can
 *   add a new connection for the given service-type.
 **/
typedef enum { /*< skip >*/
	NM_VPN_EDITOR_PLUGIN_SERVICE_FLAGS_NONE     = 0x00,
	NM_VPN_EDITOR_PLUGIN_SERVICE_FLAGS_CAN_ADD  = 0x01,
} NMVpnEditorPluginServiceFlags;

struct _NMVpnEditorPluginVT {
	gboolean (*fcn_get_service_info) (NMVpnEditorPlugin *plugin,
	                                  const char *service_type,
	                                  char **out_short_name,
	                                  char **out_pretty_name,
	                                  char **out_description,
	                                  NMVpnEditorPluginServiceFlags *out_flags);
	char **(*fcn_get_service_add_details) (NMVpnEditorPlugin *plugin,
	                                       const char *service_name);
	gboolean (*fcn_get_service_add_detail) (NMVpnEditorPlugin *plugin,
	                                        const char *service_type,
	                                        const char *add_detail,
	                                        char **out_pretty_name,
	                                        char **out_description,
	                                        char **out_add_detail_key,
	                                        char **out_add_detail_val,
	                                        guint *out_flags);
};

/*****************************************************************************
 * Call
 *
 * The following wrap the calling of generic functions for a VPN plugin.
 * They are used by callers (for example nm-connection-editor).
 *****************************************************************************/

static inline gboolean
nm_vpn_editor_plugin_get_service_info (NMVpnEditorPlugin *plugin,
                                       const char *service_type,
                                       char **out_short_name,
                                       char **out_pretty_name,
                                       char **out_description,
                                       NMVpnEditorPluginServiceFlags *out_flags)
{
	NMVpnEditorPluginVT vt;
	gs_free char *short_name_local = NULL;
	gs_free char *pretty_name_local = NULL;
	gs_free char *description_local = NULL;
	guint flags_local = 0;

	g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), FALSE);
	g_return_val_if_fail (service_type, FALSE);

	nm_vpn_editor_plugin_get_vt (plugin, &vt, sizeof (vt));
	if (   !vt.fcn_get_service_info
	    || !vt.fcn_get_service_info (plugin,
	                                 service_type,
	                                 out_short_name  ? &short_name_local : NULL,
	                                 out_pretty_name ? &pretty_name_local : NULL,
	                                 out_description ? &description_local : NULL,
	                                 out_flags       ? &flags_local : NULL))
		return FALSE;
	NM_SET_OUT (out_short_name, g_steal_pointer (&short_name_local));
	NM_SET_OUT (out_pretty_name, g_steal_pointer (&pretty_name_local));
	NM_SET_OUT (out_description, g_steal_pointer (&description_local));
	NM_SET_OUT (out_flags, flags_local);
	return TRUE;
}

static inline char **
nm_vpn_editor_plugin_get_service_add_details (NMVpnEditorPlugin *plugin,
                                              const char *service_name)
{
	NMVpnEditorPluginVT vt;
	char **details = NULL;

	g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), NULL);
	g_return_val_if_fail (service_name, NULL);

	nm_vpn_editor_plugin_get_vt (plugin, &vt, sizeof (vt));
	if (vt.fcn_get_service_add_details)
		details = vt.fcn_get_service_add_details (plugin, service_name);
	if (!details)
		return g_new0 (char *, 1);
	return details;
}

static inline gboolean
nm_vpn_editor_plugin_get_service_add_detail (NMVpnEditorPlugin *plugin,
                                             const char *service_type,
                                             const char *add_detail,
                                             char **out_pretty_name,
                                             char **out_description,
                                             char **out_add_detail_key,
                                             char **out_add_detail_val,
                                             guint *out_flags)
{
	NMVpnEditorPluginVT vt;
	gs_free char *pretty_name_local = NULL;
	gs_free char *description_local = NULL;
	gs_free char *add_detail_key_local = NULL;
	gs_free char *add_detail_val_local = NULL;
	guint flags_local = 0;

	g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), FALSE);
	g_return_val_if_fail (service_type, FALSE);
	g_return_val_if_fail (add_detail, FALSE);

	nm_vpn_editor_plugin_get_vt (plugin, &vt, sizeof (vt));
	if (   !vt.fcn_get_service_add_detail
	    || !vt.fcn_get_service_add_detail (plugin,
	                                       service_type,
	                                       add_detail,
	                                       out_pretty_name    ? &pretty_name_local : NULL,
	                                       out_description    ? &description_local : NULL,
	                                       out_add_detail_key ? &add_detail_key_local : NULL,
	                                       out_add_detail_val ? &add_detail_val_local : NULL,
	                                       out_flags          ? &flags_local : NULL))
		return FALSE;
	NM_SET_OUT (out_pretty_name, g_steal_pointer (&pretty_name_local));
	NM_SET_OUT (out_description, g_steal_pointer (&description_local));
	NM_SET_OUT (out_add_detail_key, g_steal_pointer (&add_detail_key_local));
	NM_SET_OUT (out_add_detail_val, g_steal_pointer (&add_detail_val_local));
	NM_SET_OUT (out_flags, flags_local);
	return TRUE;
}

/*****************************************************************************
 * Implementation
 *
 * The following glue code can be used to implement calls in a VPN plugin.
 *****************************************************************************/

#define NM_VPN_EDITOR_PLUGIN_VT_DEFINE(vt_name, get_vt, ...) \
static const NMVpnEditorPluginVT vt_name = { \
		__VA_ARGS__ \
	}; \
static const NMVpnEditorPluginVT * \
get_vt (NMVpnEditorPlugin *plugin, \
        gsize *out_vt_size) \
{ \
	nm_assert (NM_IS_VPN_EDITOR_PLUGIN (plugin)); \
	nm_assert (out_vt_size); \
	\
	*out_vt_size = sizeof (vt_name); \
	return &vt_name; \
}

#endif /* __NM_VPN_EDITOR_PLUGIN_CALL_H__ */