~ubuntu-branches/ubuntu/precise/telepathy-glib/precise-201202222208

« back to all changes in this revision

Viewing changes to examples/cm/callable/protocol.c

  • Committer: Ken VanDine
  • Date: 2012-02-22 18:08:37 UTC
  • mfrom: (1.6.39)
  • Revision ID: ken.vandine@canonical.com-20120222180837-02um6fex0eg073lf
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * protocol.c - an example Protocol
3
 
 *
4
 
 * Copyright © 2007-2010 Collabora Ltd.
5
 
 *
6
 
 * Copying and distribution of this file, with or without modification,
7
 
 * are permitted in any medium without royalty provided the copyright
8
 
 * notice and this notice are preserved.
9
 
 */
10
 
 
11
 
#include "protocol.h"
12
 
 
13
 
#include <telepathy-glib/telepathy-glib.h>
14
 
 
15
 
#include "conn.h"
16
 
#include "media-manager.h"
17
 
 
18
 
G_DEFINE_TYPE (ExampleCallableProtocol,
19
 
    example_callable_protocol,
20
 
    TP_TYPE_BASE_PROTOCOL)
21
 
 
22
 
static void
23
 
example_callable_protocol_init (
24
 
    ExampleCallableProtocol *self)
25
 
{
26
 
}
27
 
 
28
 
gboolean
29
 
example_callable_protocol_check_contact_id (const gchar *id,
30
 
    gchar **normal,
31
 
    GError **error)
32
 
{
33
 
  g_return_val_if_fail (id != NULL, FALSE);
34
 
 
35
 
  if (id[0] == '\0')
36
 
    {
37
 
      g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_HANDLE,
38
 
          "ID must not be empty");
39
 
      return FALSE;
40
 
    }
41
 
 
42
 
  if (normal != NULL)
43
 
    *normal = g_utf8_normalize (id, -1, G_NORMALIZE_ALL_COMPOSE);
44
 
 
45
 
  return TRUE;
46
 
}
47
 
 
48
 
static gboolean
49
 
account_param_filter (const TpCMParamSpec *paramspec,
50
 
                      GValue *value,
51
 
                      GError **error)
52
 
{
53
 
  const gchar *id = g_value_get_string (value);
54
 
 
55
 
  return example_callable_protocol_check_contact_id (id, NULL, error);
56
 
}
57
 
 
58
 
static const TpCMParamSpec example_callable_example_params[] = {
59
 
  { "account", "s", G_TYPE_STRING,
60
 
    TP_CONN_MGR_PARAM_FLAG_REQUIRED | TP_CONN_MGR_PARAM_FLAG_REGISTER,
61
 
    NULL,                               /* no default */
62
 
    0,                                  /* unused, formerly struct offset */
63
 
    account_param_filter,
64
 
    NULL,                               /* filter data, unused here */
65
 
    NULL },                             /* setter data, now unused */
66
 
  { "simulation-delay", "u", G_TYPE_UINT,
67
 
    TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT,
68
 
    GUINT_TO_POINTER (1000),            /* default */
69
 
    0,                                  /* unused, formerly struct offset */
70
 
    NULL,                               /* no filter */
71
 
    NULL,                               /* filter data, unused here */
72
 
    NULL },                             /* setter data, now unused */
73
 
  { NULL }
74
 
};
75
 
 
76
 
static const TpCMParamSpec *
77
 
get_parameters (TpBaseProtocol *self)
78
 
{
79
 
  return example_callable_example_params;
80
 
}
81
 
 
82
 
static TpBaseConnection *
83
 
new_connection (TpBaseProtocol *protocol,
84
 
    GHashTable *asv,
85
 
    GError **error)
86
 
{
87
 
  ExampleCallableConnection *conn;
88
 
  const gchar *account;
89
 
  guint sim_delay;
90
 
 
91
 
  account = tp_asv_get_string (asv, "account");
92
 
  /* telepathy-glib checked this for us */
93
 
  g_assert (account != NULL);
94
 
 
95
 
  sim_delay = tp_asv_get_uint32 (asv, "simulation-delay", NULL);
96
 
 
97
 
  conn = EXAMPLE_CALLABLE_CONNECTION (
98
 
      g_object_new (EXAMPLE_TYPE_CALLABLE_CONNECTION,
99
 
        "account", account,
100
 
        "protocol", tp_base_protocol_get_name (protocol),
101
 
        "simulation-delay", sim_delay,
102
 
        NULL));
103
 
 
104
 
  return (TpBaseConnection *) conn;
105
 
}
106
 
 
107
 
static gchar *
108
 
normalize_contact (TpBaseProtocol *self G_GNUC_UNUSED,
109
 
    const gchar *contact,
110
 
    GError **error)
111
 
{
112
 
  gchar *normal;
113
 
 
114
 
  if (example_callable_protocol_check_contact_id (contact, &normal, error))
115
 
    return normal;
116
 
  else
117
 
    return NULL;
118
 
}
119
 
 
120
 
static gchar *
121
 
identify_account (TpBaseProtocol *self G_GNUC_UNUSED,
122
 
    GHashTable *asv,
123
 
    GError **error)
124
 
{
125
 
  const gchar *account = tp_asv_get_string (asv, "account");
126
 
 
127
 
  if (account != NULL)
128
 
    return normalize_contact (self, account, error);
129
 
 
130
 
  g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
131
 
      "'account' parameter not given");
132
 
  return NULL;
133
 
}
134
 
 
135
 
static GStrv
136
 
get_interfaces (TpBaseProtocol *self)
137
 
{
138
 
  return NULL;
139
 
}
140
 
 
141
 
static void
142
 
get_connection_details (TpBaseProtocol *self G_GNUC_UNUSED,
143
 
    GStrv *connection_interfaces,
144
 
    GType **channel_managers,
145
 
    gchar **icon_name,
146
 
    gchar **english_name,
147
 
    gchar **vcard_field)
148
 
{
149
 
  if (connection_interfaces != NULL)
150
 
    {
151
 
      *connection_interfaces = g_strdupv (
152
 
          (GStrv) example_callable_connection_get_possible_interfaces ());
153
 
    }
154
 
 
155
 
  if (channel_managers != NULL)
156
 
    {
157
 
      GType types[] = { EXAMPLE_TYPE_CALLABLE_MEDIA_MANAGER, G_TYPE_INVALID };
158
 
 
159
 
      *channel_managers = g_memdup (types, sizeof (types));
160
 
    }
161
 
 
162
 
  if (icon_name != NULL)
163
 
    *icon_name = g_strdup ("face-smile");
164
 
 
165
 
  if (english_name != NULL)
166
 
    *english_name = g_strdup ("Example with StreamedMedia calls");
167
 
 
168
 
  if (vcard_field != NULL)
169
 
    *vcard_field = g_strdup ("x-telepathy-example");
170
 
}
171
 
 
172
 
static void
173
 
example_callable_protocol_class_init (
174
 
    ExampleCallableProtocolClass *klass)
175
 
{
176
 
  TpBaseProtocolClass *base_class =
177
 
      (TpBaseProtocolClass *) klass;
178
 
 
179
 
  base_class->get_parameters = get_parameters;
180
 
  base_class->new_connection = new_connection;
181
 
 
182
 
  base_class->normalize_contact = normalize_contact;
183
 
  base_class->identify_account = identify_account;
184
 
  base_class->get_interfaces = get_interfaces;
185
 
  base_class->get_connection_details = get_connection_details;
186
 
}