~ubuntu-branches/ubuntu/wily/telepathy-glib/wily

« back to all changes in this revision

Viewing changes to examples/cm/echo-message-parts/conn.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2009-03-24 22:06:52 UTC
  • mfrom: (1.3.1 upstream) (17.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20090324220652-c8dvom0nsqomp23d
Tags: 0.7.28-1
* New upstream version (ABI, API added)
* Put the -dbg package in section debug, as per recent archive changes
* Remove obsolete Conflicts/Replaces with libtelepathy-glib-static-dev, which
  was never in a stable release (and probably never in Debian at all)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * conn.c - an example connection
 
3
 *
 
4
 * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
 
5
 * Copyright (C) 2007 Nokia Corporation
 
6
 *
 
7
 * Copying and distribution of this file, with or without modification,
 
8
 * are permitted in any medium without royalty provided the copyright
 
9
 * notice and this notice are preserved.
 
10
 */
 
11
 
 
12
#include "conn.h"
 
13
 
 
14
#include <dbus/dbus-glib.h>
 
15
 
 
16
#include <telepathy-glib/dbus.h>
 
17
#include <telepathy-glib/errors.h>
 
18
#include <telepathy-glib/handle-repo-dynamic.h>
 
19
#include <telepathy-glib/interfaces.h>
 
20
 
 
21
#include "im-manager.h"
 
22
 
 
23
G_DEFINE_TYPE (ExampleEcho2Connection,
 
24
    example_echo_2_connection,
 
25
    TP_TYPE_BASE_CONNECTION)
 
26
 
 
27
enum
 
28
{
 
29
  PROP_ACCOUNT = 1,
 
30
  N_PROPS
 
31
};
 
32
 
 
33
struct _ExampleEcho2ConnectionPrivate
 
34
{
 
35
  gchar *account;
 
36
};
 
37
 
 
38
static void
 
39
example_echo_2_connection_init (ExampleEcho2Connection *self)
 
40
{
 
41
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
 
42
      EXAMPLE_TYPE_ECHO_2_CONNECTION,
 
43
      ExampleEcho2ConnectionPrivate);
 
44
}
 
45
 
 
46
static void
 
47
get_property (GObject *object,
 
48
              guint property_id,
 
49
              GValue *value,
 
50
              GParamSpec *spec)
 
51
{
 
52
  ExampleEcho2Connection *self = EXAMPLE_ECHO_2_CONNECTION (object);
 
53
 
 
54
  switch (property_id) {
 
55
    case PROP_ACCOUNT:
 
56
      g_value_set_string (value, self->priv->account);
 
57
      break;
 
58
    default:
 
59
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
 
60
  }
 
61
}
 
62
 
 
63
static void
 
64
set_property (GObject *object,
 
65
              guint property_id,
 
66
              const GValue *value,
 
67
              GParamSpec *spec)
 
68
{
 
69
  ExampleEcho2Connection *self = EXAMPLE_ECHO_2_CONNECTION (object);
 
70
 
 
71
  switch (property_id) {
 
72
    case PROP_ACCOUNT:
 
73
      g_free (self->priv->account);
 
74
      self->priv->account = g_utf8_strdown (g_value_get_string (value), -1);
 
75
      break;
 
76
    default:
 
77
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
 
78
  }
 
79
}
 
80
 
 
81
static void
 
82
finalize (GObject *object)
 
83
{
 
84
  ExampleEcho2Connection *self = EXAMPLE_ECHO_2_CONNECTION (object);
 
85
 
 
86
  g_free (self->priv->account);
 
87
 
 
88
  G_OBJECT_CLASS (example_echo_2_connection_parent_class)->finalize (object);
 
89
}
 
90
 
 
91
static gchar *
 
92
get_unique_connection_name (TpBaseConnection *conn)
 
93
{
 
94
  ExampleEcho2Connection *self = EXAMPLE_ECHO_2_CONNECTION (conn);
 
95
 
 
96
  return g_strdup (self->priv->account);
 
97
}
 
98
 
 
99
static gchar *
 
100
example_normalize_contact (TpHandleRepoIface *repo,
 
101
                           const gchar *id,
 
102
                           gpointer context,
 
103
                           GError **error)
 
104
{
 
105
  if (id[0] == '\0')
 
106
    {
 
107
      g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_HANDLE,
 
108
          "ID must not be empty");
 
109
      return NULL;
 
110
    }
 
111
 
 
112
  return g_utf8_strdown (id, -1);
 
113
}
 
114
 
 
115
static void
 
116
create_handle_repos (TpBaseConnection *conn,
 
117
                     TpHandleRepoIface *repos[NUM_TP_HANDLE_TYPES])
 
118
{
 
119
  repos[TP_HANDLE_TYPE_CONTACT] = tp_dynamic_handle_repo_new
 
120
      (TP_HANDLE_TYPE_CONTACT, example_normalize_contact, NULL);
 
121
}
 
122
 
 
123
static GPtrArray *
 
124
create_channel_managers (TpBaseConnection *conn)
 
125
{
 
126
  GPtrArray *ret = g_ptr_array_sized_new (1);
 
127
 
 
128
  g_ptr_array_add (ret, g_object_new (EXAMPLE_TYPE_ECHO_2_IM_MANAGER,
 
129
        "connection", conn,
 
130
        NULL));
 
131
 
 
132
  return ret;
 
133
}
 
134
 
 
135
static gboolean
 
136
start_connecting (TpBaseConnection *conn,
 
137
                  GError **error)
 
138
{
 
139
  ExampleEcho2Connection *self = EXAMPLE_ECHO_2_CONNECTION (conn);
 
140
  TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (conn,
 
141
      TP_HANDLE_TYPE_CONTACT);
 
142
 
 
143
  /* In a real connection manager we'd ask the underlying implementation to
 
144
   * start connecting, then go to state CONNECTED when finished, but here
 
145
   * we can do it immediately. */
 
146
 
 
147
  conn->self_handle = tp_handle_ensure (contact_repo, self->priv->account,
 
148
      NULL, NULL);
 
149
 
 
150
  tp_base_connection_change_status (conn, TP_CONNECTION_STATUS_CONNECTED,
 
151
      TP_CONNECTION_STATUS_REASON_REQUESTED);
 
152
 
 
153
  return TRUE;
 
154
}
 
155
 
 
156
static void
 
157
shut_down (TpBaseConnection *conn)
 
158
{
 
159
  /* In a real connection manager we'd ask the underlying implementation to
 
160
   * start shutting down, then call this function when finished, but here
 
161
   * we can do it immediately. */
 
162
  tp_base_connection_finish_shutdown (conn);
 
163
}
 
164
 
 
165
static void
 
166
example_echo_2_connection_class_init (ExampleEcho2ConnectionClass *klass)
 
167
{
 
168
  static const gchar *interfaces_always_present[] = {
 
169
      TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
 
170
      NULL };
 
171
  TpBaseConnectionClass *base_class =
 
172
      (TpBaseConnectionClass *) klass;
 
173
  GObjectClass *object_class = (GObjectClass *) klass;
 
174
  GParamSpec *param_spec;
 
175
 
 
176
  object_class->get_property = get_property;
 
177
  object_class->set_property = set_property;
 
178
  object_class->finalize = finalize;
 
179
  g_type_class_add_private (klass, sizeof (ExampleEcho2ConnectionPrivate));
 
180
 
 
181
  base_class->create_handle_repos = create_handle_repos;
 
182
  base_class->get_unique_connection_name = get_unique_connection_name;
 
183
  base_class->create_channel_managers = create_channel_managers;
 
184
  base_class->start_connecting = start_connecting;
 
185
  base_class->shut_down = shut_down;
 
186
  base_class->interfaces_always_present = interfaces_always_present;
 
187
 
 
188
  param_spec = g_param_spec_string ("account", "Account name",
 
189
      "The username of this user", NULL,
 
190
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
 
191
      G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB);
 
192
  g_object_class_install_property (object_class, PROP_ACCOUNT, param_spec);
 
193
}