diff options
author | Jonny Lamb <jonny.lamb@collabora.co.uk> | 2011-09-05 15:41:48 +0100 |
---|---|---|
committer | Jonny Lamb <jonny.lamb@collabora.co.uk> | 2011-09-05 15:41:48 +0100 |
commit | 76fb29dab308aa6024bdfe5db329a155635fe620 (patch) | |
tree | 6295f801c5a83ecde1162b4c999dec8b11aa0aaf /plugin-base | |
parent | 72288fb7391e7e24dabe36941b777eb603694ee6 (diff) |
salut: move some common to a separate directory
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
Diffstat (limited to 'plugin-base')
-rw-r--r-- | plugin-base/Makefile.am | 5 | ||||
-rw-r--r-- | plugin-base/caps-manager.c | 254 | ||||
-rw-r--r-- | plugin-base/caps-manager.h | 63 | ||||
-rw-r--r-- | plugin-base/utils.c | 126 | ||||
-rw-r--r-- | plugin-base/utils.h | 55 |
5 files changed, 503 insertions, 0 deletions
diff --git a/plugin-base/Makefile.am b/plugin-base/Makefile.am new file mode 100644 index 0000000..dfaa9f9 --- /dev/null +++ b/plugin-base/Makefile.am @@ -0,0 +1,5 @@ +EXTRA_DIST = \ + caps-manager.c \ + caps-manager.h \ + utils.c \ + utils.h diff --git a/plugin-base/caps-manager.c b/plugin-base/caps-manager.c new file mode 100644 index 0000000..be61ef1 --- /dev/null +++ b/plugin-base/caps-manager.c @@ -0,0 +1,254 @@ +/* + * caps-manager.c - Source for YstCapsManager + * + * Copyright (C) 2011 Intel Corp. + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "caps-manager.h" + +#include <string.h> + +#include <telepathy-glib/channel-manager.h> +#include <telepathy-glib/util.h> + +#include <wocky/wocky-data-form.h> +#include <wocky/wocky-namespaces.h> + +#ifdef SALUT +#include <salut/caps-channel-manager.h> +#else +#include <gabble/caps-channel-manager.h> +#endif + +#include "utils.h" + +#define DEBUG(msg, ...) \ + g_debug ("%s: " msg, G_STRFUNC, ##__VA_ARGS__) + +#define CHANNEL_PREFIX "com.meego.xpmn.ytstenut.Channel/" +#define UID CHANNEL_PREFIX "uid/" +#define TYPE CHANNEL_PREFIX "type/" +#define NAME CHANNEL_PREFIX "name/" +#define CAPS CHANNEL_PREFIX "caps/" +#define INTERESTED CHANNEL_PREFIX "interested/" + +static void channel_manager_iface_init (gpointer g_iface, gpointer data); +static void caps_channel_manager_iface_init (gpointer g_iface, gpointer data); + +G_DEFINE_TYPE_WITH_CODE (YtstCapsManager, ytst_caps_manager, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (TP_TYPE_CHANNEL_MANAGER, channel_manager_iface_init); + G_IMPLEMENT_INTERFACE (GABBLE_TYPE_CAPS_CHANNEL_MANAGER, caps_channel_manager_iface_init); + ) + +/* private structure */ +struct _YtstCapsManagerPrivate +{ + GHashTable *services; +}; + +static void +ytst_caps_manager_init (YtstCapsManager *self) +{ + YtstCapsManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + YTST_TYPE_CAPS_MANAGER, YtstCapsManagerPrivate); + self->priv = priv; + + priv->services = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_object_unref); +} + +static void +ytst_caps_manager_dispose (GObject *object) +{ + YtstCapsManager *self = YTST_CAPS_MANAGER (object); + + tp_clear_pointer (&(self->priv->services), g_hash_table_unref); + + if (G_OBJECT_CLASS (ytst_caps_manager_parent_class)->dispose) + G_OBJECT_CLASS (ytst_caps_manager_parent_class)->dispose (object); +} + +static void +ytst_caps_manager_class_init (YtstCapsManagerClass *klass) +{ + GObjectClass *oclass = G_OBJECT_CLASS (klass); + + oclass->dispose = ytst_caps_manager_dispose; + + g_type_class_add_private (klass, sizeof (YtstCapsManagerPrivate)); +} + +static void +add_value_to_field (gpointer data, + gpointer user_data) +{ + WockyNode *field = user_data; + WockyNode *tmp; + const gchar *content = data; + + tmp = wocky_node_add_child (field, "value"); + wocky_node_set_content (tmp, content); +} + +static WockyDataForm * +make_new_data_form (const gchar *uid, + const gchar *type, + GPtrArray *names, + GPtrArray *caps) +{ + WockyNode *node, *tmp; + WockyDataForm *out; + /* the default */ + const gchar *yts_type = type != NULL ? type : "application"; + gchar *form_type = g_strdup_printf ("%s%s", SERVICE_PREFIX, uid); + + node = wocky_node_new ("x", WOCKY_XMPP_NS_DATA); + wocky_node_add_build (node, + '@', "type", "result", + + '(', "field", + '@', "var", "FORM_TYPE", + '@', "type", "hidden", + '(', "value", + '$', form_type, + ')', + ')', + + '(', "field", + '@', "var", "type", + '(', "value", + '$', yts_type, + ')', + ')', + + NULL); + + g_free (form_type); + + /* do the next two by hand as we'll need to add the values + * manually */ + if (names->len > 0) + { + tmp = wocky_node_add_child (node, "field"); + wocky_node_set_attribute (tmp, "var", "name"); + g_ptr_array_foreach (names, add_value_to_field, tmp); + } + + if (caps->len > 0) + { + tmp = wocky_node_add_child (node, "field"); + wocky_node_set_attribute (tmp, "var", "capabilities"); + g_ptr_array_foreach (caps, add_value_to_field, tmp); + } + + out = wocky_data_form_new_from_node (node, NULL); + + wocky_node_free (node); + + return out; +} + +static void +add_to_array (gpointer key, + gpointer value, + gpointer user_data) +{ + g_ptr_array_add (user_data, g_object_ref (value)); +} + +static void +ytst_caps_manager_represent_client (GabbleCapsChannelManager *manager, + const gchar *client_name, + const GPtrArray *filters, + const gchar * const *cap_tokens, + GabbleCapabilitySet *cap_set, + GPtrArray *data_forms) +{ + YtstCapsManager *self = YTST_CAPS_MANAGER (manager); + YtstCapsManagerPrivate *priv = self->priv; + const gchar * const *t; + + const gchar *uid = NULL; + const gchar *yts_type = NULL; + GPtrArray *names = g_ptr_array_new (); + GPtrArray *caps = g_ptr_array_new (); + + for (t = cap_tokens; t != NULL && *t != NULL; t++) + { + const gchar *cap = *t; + gchar *feature; + + if (g_str_has_prefix (*t, INTERESTED)) + { + cap = *t + strlen (INTERESTED); + feature = g_strdup_printf ("%s+notify", cap); + gabble_capability_set_add (cap_set, feature); + g_free (feature); + } + else if (g_str_has_prefix (*t, UID)) + { + uid = *t + strlen (UID); + } + else if (g_str_has_prefix (*t, TYPE)) + { + yts_type = *t + strlen (TYPE); + } + else if (g_str_has_prefix (*t, NAME)) + { + g_ptr_array_add (names, (gpointer) (*t + strlen (NAME))); + } + else if (g_str_has_prefix (*t, CAPS)) + { + g_ptr_array_add (caps, (gpointer) (*t + strlen (CAPS))); + } + } + + if (uid != NULL) + { + g_hash_table_insert (priv->services, + g_strdup (client_name), + make_new_data_form (uid, yts_type, names, caps)); + } + else + { + g_hash_table_remove (priv->services, client_name); + } + + g_hash_table_foreach (priv->services, add_to_array, data_forms); + + g_ptr_array_unref (names); + g_ptr_array_unref (caps); +} + +static void +caps_channel_manager_iface_init ( + gpointer g_iface, + gpointer data G_GNUC_UNUSED) +{ + GabbleCapsChannelManagerIface *iface = g_iface; + + iface->represent_client = ytst_caps_manager_represent_client; +} + +static void +channel_manager_iface_init ( + gpointer g_iface, + gpointer data G_GNUC_UNUSED) +{ +} diff --git a/plugin-base/caps-manager.h b/plugin-base/caps-manager.h new file mode 100644 index 0000000..e25d89b --- /dev/null +++ b/plugin-base/caps-manager.h @@ -0,0 +1,63 @@ +/* + * caps-manager.h - Header for YstCapsManager + * + * Copyright (C) 2011 Intel Corp. + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <glib-object.h> + +#ifndef YTST_CAPS_MANAGER_H +#define YTST_CAPS_MANAGER_H + +G_BEGIN_DECLS + +typedef struct _YtstCapsManagerClass YtstCapsManagerClass; +typedef struct _YtstCapsManagerPrivate YtstCapsManagerPrivate; +typedef struct _YtstCapsManager YtstCapsManager; + +struct _YtstCapsManagerClass +{ + GObjectClass parent; +}; + +struct _YtstCapsManager +{ + GObject parent; + + YtstCapsManagerPrivate *priv; +}; + +GType ytst_caps_manager_get_type (void); + +#define YTST_TYPE_CAPS_MANAGER \ + (ytst_caps_manager_get_type ()) +#define YTST_CAPS_MANAGER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), YTST_TYPE_CAPS_MANAGER, YtstCapsManager)) +#define YTST_CAPS_MANAGER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), YTST_TYPE_CAPS_MANAGER, \ + YtstCapsManagerClass)) +#define YTST_IS_CAPS_MANAGER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), YTST_TYPE_CAPS_MANAGER)) +#define YTST_IS_CAPS_MANAGER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), YTST_TYPE_CAPS_MANAGER)) +#define YTST_CAPS_MANAGER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), YTST_TYPE_CAPS_MANAGER, \ + YtstCapsManagerClass)) + +G_END_DECLS + +#endif /* ifndef YTST_CAPS_MANAGER_H */ diff --git a/plugin-base/utils.c b/plugin-base/utils.c new file mode 100644 index 0000000..255f400 --- /dev/null +++ b/plugin-base/utils.c @@ -0,0 +1,126 @@ +/* + * util.c - Source for ytstenut utilities + * Copyright (C) 2011 Intel, Corp. + * @author: Stef Walter <stefw@collabora.co.uk> + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "utils.h" + +#include <telepathy-ytstenut-glib/telepathy-ytstenut-glib.h> + +GQuark +ytst_message_error_quark (void) +{ + static GQuark quark = 0; + + if (quark == 0) + quark = g_quark_from_static_string (YTST_MESSAGE_NS); + + return quark; +} + +WockyXmppErrorDomain * +ytst_message_error_get_domain (void) +{ + static WockyXmppErrorSpecialization codes[] = { + { "The recipient does not have sufficient privileges to carry out the " + "command..", + YTST_MESSAGE_ERROR_FORBIDDEN, + TRUE, + WOCKY_XMPP_ERROR_TYPE_MODIFY + }, + { "The resource could not be located.", + YTST_MESSAGE_ERROR_ITEM_NOT_FOUND, + TRUE, + WOCKY_XMPP_ERROR_TYPE_MODIFY + } + }; + static WockyXmppErrorDomain ytstenut_errors = { 0, }; + + if (G_UNLIKELY (ytstenut_errors.domain == 0)) + { + ytstenut_errors.domain = YTST_MESSAGE_ERROR; + ytstenut_errors.enum_type = YTST_TYPE_MESSAGE_ERROR; + ytstenut_errors.codes = codes; + } + + return &ytstenut_errors; +} + +GType +ytst_message_error_get_type (void) +{ + static GType etype = 0; + + if (g_once_init_enter (&etype)) + { + static const GEnumValue values[] = { + { YTST_MESSAGE_ERROR_FORBIDDEN, + "YTST_MESSAGE_ERROR_FORBIDDEN", "forbidden" }, + { YTST_MESSAGE_ERROR_ITEM_NOT_FOUND, + "YTST_MESSAGE_ERROR_ITEM_NOT_FOUND", "item-not-found" }, + { 0 } + }; + + g_once_init_leave (&etype, g_enum_register_static ("YtstMessageError", + values)); + } + + return etype; +} + +gint +ytst_message_error_type_to_wocky (guint ytstenut_type) +{ + switch (ytstenut_type) + { + case TP_YTS_ERROR_TYPE_CANCEL: + return WOCKY_XMPP_ERROR_TYPE_CANCEL; + case TP_YTS_ERROR_TYPE_CONTINUE: + return WOCKY_XMPP_ERROR_TYPE_CONTINUE; + case TP_YTS_ERROR_TYPE_MODIFY: + return WOCKY_XMPP_ERROR_TYPE_MODIFY; + case TP_YTS_ERROR_TYPE_AUTH: + return WOCKY_XMPP_ERROR_TYPE_AUTH; + case TP_YTS_ERROR_TYPE_WAIT: + return WOCKY_XMPP_ERROR_TYPE_WAIT; + default: + return -1; + } +} + +guint +ytst_message_error_type_from_wocky (gint wocky_type) +{ + switch (wocky_type) + { + case WOCKY_XMPP_ERROR_TYPE_CANCEL: + return TP_YTS_ERROR_TYPE_CANCEL; + case WOCKY_XMPP_ERROR_TYPE_CONTINUE: + return TP_YTS_ERROR_TYPE_CONTINUE; + case WOCKY_XMPP_ERROR_TYPE_MODIFY: + return TP_YTS_ERROR_TYPE_MODIFY; + case WOCKY_XMPP_ERROR_TYPE_AUTH: + return TP_YTS_ERROR_TYPE_AUTH; + case WOCKY_XMPP_ERROR_TYPE_WAIT: + return TP_YTS_ERROR_TYPE_WAIT; + default: + g_return_val_if_reached (0); + } +} diff --git a/plugin-base/utils.h b/plugin-base/utils.h new file mode 100644 index 0000000..e73ca8e --- /dev/null +++ b/plugin-base/utils.h @@ -0,0 +1,55 @@ +/* + * util.h - Header for ytstenut utilities + * Copyright (C) 2011 Intel, Corp. + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __YTST_UTIL_H__ +#define __YTST_UTIL_H__ + +#include <glib-object.h> + +#include <wocky/wocky-xmpp-error.h> + +G_BEGIN_DECLS + +#define YTST_MESSAGE_NS "urn:ytstenut:message" +#define YTST_STATUS_NS "urn:ytstenut:status" +#define YTST_CAPABILITIES_NS "urn:ytstenut:capabilities" + +GQuark ytst_message_error_quark (void); +#define YTST_MESSAGE_ERROR (ytst_message_error_quark ()) + +WockyXmppErrorDomain * ytst_message_error_get_domain (void); + +GType ytst_message_error_get_type (void); +#define YTST_TYPE_MESSAGE_ERROR (ytst_message_error_get_type ()) + +/*< prefix=YTST_MESSAGE_ERROR >*/ +typedef enum { + YTST_MESSAGE_ERROR_FORBIDDEN = 1, + YTST_MESSAGE_ERROR_ITEM_NOT_FOUND, +} YtstMessageError; + +#define SERVICE_PREFIX YTST_CAPABILITIES_NS "#" + +gint ytst_message_error_type_to_wocky (guint ytstenut_type); + +guint ytst_message_error_type_from_wocky (gint wocky_type); + +G_END_DECLS + +#endif /* #ifndef __YTST_MESSAGE_CHANNEL_H__*/ |