49
49
GossipChatroom *chatroom;
50
50
GossipTelepathyGroup *group;
51
51
TpChan *room_channel;
53
GossipChatroomJoinCb join_callback;
54
gpointer join_user_data;
55
52
} TelepathyChatroom;
57
static void telepathy_chatrooms_chatroom_free (TelepathyChatroom *room);
58
static TelepathyChatroom * telepathy_chatrooms_chatroom_new (GossipChatroom *room);
59
static void telepathy_chatrooms_get_rooms_foreach (gpointer key,
60
TelepathyChatroom *room,
62
static GossipContact * telepathy_chatrooms_new_contact (GossipTelepathy *telepathy,
63
TelepathyChatroom *room);
64
static void telepathy_chatrooms_members_added_cb (GossipTelepathyGroup *group,
66
TelepathyChatroom *room);
67
static void telepathy_chatrooms_members_removed_cb (GossipTelepathyGroup *group,
69
TelepathyChatroom *room);
54
static void telepathy_chatrooms_disconnected_cb (GossipProtocol *telepathy,
55
GossipAccount *account,
57
GossipTelepathyChatrooms *chatrooms);
58
static void telepathy_chatrooms_closed_cb (TpChan *tp_chan,
59
GossipTelepathyChatrooms *chatrooms);
60
static gboolean telepathy_chatrooms_find_chan (guint key,
61
TelepathyChatroom *room,
63
static void telepathy_chatrooms_chatroom_free (TelepathyChatroom *room);
64
static void telepathy_chatrooms_get_rooms_foreach (gpointer key,
65
TelepathyChatroom *room,
67
static void telepathy_chatrooms_members_added_cb (GossipTelepathyGroup *group,
69
TelepathyChatroom *room);
70
static void telepathy_chatrooms_members_removed_cb (GossipTelepathyGroup *group,
72
TelepathyChatroom *room);
71
74
GossipTelepathyChatrooms *
72
75
gossip_telepathy_chatrooms_init (GossipTelepathy *telepathy)
76
79
g_return_val_if_fail (GOSSIP_IS_TELEPATHY (telepathy), NULL);
78
81
chatrooms = g_new0 (GossipTelepathyChatrooms, 1);
79
chatrooms->telepathy = g_object_ref (telepathy);
80
chatrooms->room_id_hash =
81
g_hash_table_new_full (NULL,//g_int_hash,
85
telepathy_chatrooms_chatroom_free);
82
chatrooms->telepathy = telepathy;
83
chatrooms->rooms = g_hash_table_new_full (g_direct_hash,
87
telepathy_chatrooms_chatroom_free);
88
chatrooms->message = gossip_telepathy_message_init (telepathy);
90
g_signal_connect (telepathy, "disconnected",
91
G_CALLBACK (telepathy_chatrooms_disconnected_cb),
98
telepathy_chatrooms_disconnected_cb (GossipProtocol *telepathy,
99
GossipAccount *account,
101
GossipTelepathyChatrooms *chatrooms)
103
g_hash_table_remove_all (chatrooms->rooms);
91
107
gossip_telepathy_chatrooms_finalize (GossipTelepathyChatrooms *chatrooms)
93
109
g_return_if_fail (chatrooms != NULL);
95
g_hash_table_destroy (chatrooms->room_id_hash);
96
g_object_unref (chatrooms->telepathy);
111
gossip_telepathy_message_finalize (chatrooms->message);
112
g_hash_table_destroy (chatrooms->rooms);
97
113
g_free (chatrooms);
106
122
GossipChatroomId room_id;
107
123
TelepathyChatroom *tp_room;
109
/* Search for existing room, callback with:
110
room = telepathy_chatrooms_chatroom_new (chatrooms, chatroom);
111
existing_room = g_hash_table_lookup (chatrooms->room_jid_hash, room->jid);
115
125
room_id = gossip_chatroom_get_id (room);
116
tp_room = g_hash_table_lookup (chatrooms->room_id_hash,
126
tp_room = g_hash_table_lookup (chatrooms->rooms,
117
127
GINT_TO_POINTER (room_id));
119
129
const char *room_names[2] = {NULL, NULL};
122
132
gchar *room_object_path;
123
133
GError *error = NULL;
135
TpChan *room_channel;
125
136
GossipAccount *account;
126
137
const gchar *bus_name;
128
tp_room = telepathy_chatrooms_chatroom_new (room);
129
139
tp_conn = gossip_telepathy_get_connection (chatrooms->telepathy);
132
gossip_debug (DEBUG_DOMAIN, "Joining a chatroom: %s@%s",
133
gossip_chatroom_get_room (room),
134
gossip_chatroom_get_server (room));
136
142
room_names[0] = g_strdup_printf ("%s@%s",
137
143
gossip_chatroom_get_room (room),
138
144
gossip_chatroom_get_server (room));
139
if (!tp_conn_request_handles (DBUS_G_PROXY (tp_conn),
140
TP_CONN_HANDLE_TYPE_ROOM,
147
147
gossip_debug (DEBUG_DOMAIN, "Joining a chatroom: %s",
148
gossip_chatroom_get_room(room));
150
/*FIXME: room_handles and room_object_path not freed ? */
148
gossip_chatroom_get_room (room));
149
gossip_chatroom_set_status (room, GOSSIP_CHATROOM_STATUS_JOINING);
150
gossip_chatroom_set_last_error (room, NULL);
152
/* FIXME: One of those tp calls do network stuff,
153
* we should make this call async */
154
/* Get a handle for the room */
152
155
room_names[0] = gossip_chatroom_get_room (room);
153
156
if (!tp_conn_request_handles (DBUS_G_PROXY (tp_conn),
154
157
TP_CONN_HANDLE_TYPE_ROOM,
161
gossip_debug (DEBUG_DOMAIN, "Error requesting room handle:%s",
163
g_clear_error (&error);
161
166
room_handle = g_array_index (room_handles, guint, 0);
162
gossip_debug (DEBUG_DOMAIN, "Got the room handle: %d", room_handle);
167
g_array_free (room_handles, TRUE);
169
/* Request the object path */
164
170
if (!tp_conn_request_channel (DBUS_G_PROXY (tp_conn),
165
171
TP_IFACE_CHANNEL_TYPE_TEXT,
166
172
TP_CONN_HANDLE_TYPE_ROOM,
169
175
&room_object_path,
177
gossip_debug (DEBUG_DOMAIN, "Error requesting room channel:%s",
179
g_clear_error (&error);
174
gossip_debug (DEBUG_DOMAIN, "Got obj path for room: %s",
183
/* Create the channel */
177
184
account = gossip_telepathy_get_account (chatrooms->telepathy);
178
185
bus_name = gossip_account_get_bus_name (account);
179
tp_room->room_channel = tp_chan_new (dbus_g_bus_get (DBUS_BUS_SESSION, NULL),
182
TP_IFACE_CHANNEL_TYPE_TEXT,
183
TP_CONN_HANDLE_TYPE_ROOM,
186
room_channel = tp_chan_new (tp_get_bus (),
189
TP_IFACE_CHANNEL_TYPE_TEXT,
190
TP_CONN_HANDLE_TYPE_ROOM,
192
g_free (room_object_path);
186
g_hash_table_insert (chatrooms->room_id_hash,
194
/* Room joined, configure it ... */
195
tp_room = g_slice_new0 (TelepathyChatroom);
196
tp_room->chatroom = g_object_ref (room);
197
tp_room->telepathy = chatrooms->telepathy;
198
tp_room->room_channel = room_channel;
199
tp_room->group = gossip_telepathy_group_new (tp_room->telepathy,
200
tp_room->room_channel);
202
g_hash_table_insert (chatrooms->rooms,
187
203
GINT_TO_POINTER (room_id),
190
gossip_chatroom_set_status (tp_room->chatroom,
191
GOSSIP_CHATROOM_STATUS_JOINING);
192
gossip_chatroom_set_last_error (tp_room->chatroom, NULL);
194
tp_room->join_callback = callback;
195
tp_room->join_user_data = user_data;
196
tp_room->telepathy = chatrooms->telepathy;
198
gossip_chatroom_set_status (tp_room->chatroom,
199
GOSSIP_CHATROOM_STATUS_ACTIVE);
200
gossip_chatroom_set_last_error (tp_room->chatroom,
206
dbus_g_proxy_connect_signal (DBUS_G_PROXY (room_channel),
208
G_CALLBACK (telepathy_chatrooms_closed_cb),
211
gossip_chatroom_set_status (room, GOSSIP_CHATROOM_STATUS_ACTIVE);
212
gossip_chatroom_set_last_error (room,
201
213
gossip_chatroom_provider_join_result_as_str (GOSSIP_CHATROOM_JOIN_OK));
203
gossip_debug (DEBUG_DOMAIN, "Calling callback");
204
(callback) (GOSSIP_CHATROOM_PROVIDER (chatrooms->telepathy),
205
GOSSIP_CHATROOM_JOIN_OK,
216
(callback) (GOSSIP_CHATROOM_PROVIDER (chatrooms->telepathy),
217
GOSSIP_CHATROOM_JOIN_OK,
208
221
g_signal_emit_by_name (chatrooms->telepathy,
209
222
"chatroom-joined",
212
gossip_telepathy_message_setup (chatrooms->telepathy,
213
tp_room->room_channel,
225
/* Setup message sending/receiving for the new text channel */
226
gossip_telepathy_message_newchannel (chatrooms->message,
227
room_channel, room_id);
216
229
/* Setup the chatroom's contact list */
217
tp_room->group = gossip_telepathy_group_new (tp_room->telepathy,
218
tp_room->room_channel);
219
230
g_signal_connect (tp_room->group, "members-added",
220
231
G_CALLBACK (telepathy_chatrooms_members_added_cb),
250
259
GossipChatroomId id,
251
260
const gchar *message)
253
TelepathyChatroom *room;
255
room = g_hash_table_lookup (chatrooms->room_id_hash,
256
GINT_TO_POINTER (id));
258
g_return_if_fail (room != NULL);
262
g_return_if_fail (chatrooms != NULL);
260
264
gossip_debug (DEBUG_DOMAIN, "ID[%d] Send message: %s", id, message);
261
gossip_telepathy_message_send (room->room_channel, message);
265
gossip_telepathy_message_send (chatrooms->message, id, message);
444
454
g_return_val_if_fail (chatrooms != NULL, NULL);
446
g_hash_table_foreach (chatrooms->room_id_hash,
456
g_hash_table_foreach (chatrooms->rooms,
447
457
(GHFunc) telepathy_chatrooms_get_rooms_foreach,
463
telepathy_chatrooms_closed_cb (TpChan *tp_chan,
464
GossipTelepathyChatrooms *chatrooms)
466
TelepathyChatroom *room;
468
room = g_hash_table_find (chatrooms->rooms,
469
(GHRFunc) telepathy_chatrooms_find_chan,
471
g_return_if_fail (room != NULL);
473
gossip_debug (DEBUG_DOMAIN, "Channel closed: %d", tp_chan->handle);
475
g_hash_table_remove (chatrooms->rooms,
476
(gchar*) gossip_chatroom_get_id (room->chatroom));
480
telepathy_chatrooms_find_chan (guint key,
481
TelepathyChatroom *room,
484
if (tp_chan->handle == room->room_channel->handle) {
453
492
telepathy_chatrooms_chatroom_free (TelepathyChatroom *room)
455
494
g_object_unref (room->chatroom);
456
495
g_object_unref (room->room_channel);
457
496
g_object_unref (room->group);
460
static TelepathyChatroom *
461
telepathy_chatrooms_chatroom_new (GossipChatroom *room)
463
TelepathyChatroom *tp_room;
465
tp_room = g_new0 (TelepathyChatroom, 1);
466
tp_room->chatroom = g_object_ref (room);
497
g_slice_free (TelepathyChatroom, room);
476
505
*list = g_list_append (*list, key);
479
static GossipContact *
480
telepathy_chatrooms_new_contact (GossipTelepathy *telepathy,
481
TelepathyChatroom *room)
483
GossipChatroomContact *contact;
484
GossipAccount *account;
486
account = gossip_telepathy_get_account (telepathy);
487
contact = gossip_chatroom_contact_new (account);
488
gossip_chatroom_contact_set_role (contact,
489
GOSSIP_CHATROOM_ROLE_PARTICIPANT);
490
gossip_chatroom_contact_set_affiliation (contact,
491
GOSSIP_CHATROOM_AFFILIATION_MEMBER);
493
return GOSSIP_CONTACT (contact);
497
509
telepathy_chatrooms_members_added_cb (GossipTelepathyGroup *group,
504
516
gossip_debug (DEBUG_DOMAIN, "adding %d members", members->len);
506
518
contacts = gossip_telepathy_get_contacts (room->telepathy);
507
added_list = gossip_telepathy_contacts_add_handles (contacts,
509
(GossipTelepathyContactsNewFunc)
510
telepathy_chatrooms_new_contact,
512
for (l = added_list; l; l = l->next) {
519
added_list = gossip_telepathy_contacts_get_from_handles (contacts,
522
for (l = added_list; l; l = l->next) {
523
GossipChatroomContact *contact;
526
gossip_chatroom_contact_set_role (contact,
527
GOSSIP_CHATROOM_ROLE_PARTICIPANT);
528
gossip_chatroom_contact_set_affiliation (contact,
529
GOSSIP_CHATROOM_AFFILIATION_MEMBER);
513
531
g_signal_emit_by_name (room->telepathy, "chatroom-contact-joined",
514
532
gossip_chatroom_get_id (room->chatroom),
515
GOSSIP_CONTACT (l->data));
533
GOSSIP_CONTACT (contact));
518
536
g_list_free (added_list);