2 * empathy-chat-manager.c - Source for EmpathyChatManager
3 * Copyright (C) 2010 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <libempathy/empathy-dispatcher.h>
22 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
23 #include <libempathy/empathy-debug.h>
25 #include "empathy-chat-manager.h"
32 static guint signals[LAST_SIGNAL];
34 G_DEFINE_TYPE(EmpathyChatManager, empathy_chat_manager, G_TYPE_OBJECT)
36 /* private structure */
37 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
39 struct _EmpathyChatManagerPriv
45 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
46 EmpathyChatManagerPriv))
48 static EmpathyChatManager *chat_manager_singleton = NULL;
58 chat_data_new (EmpathyChat *chat)
60 ChatData *data = NULL;
62 data = g_slice_new0 (ChatData);
64 data->account = g_object_ref (empathy_chat_get_account (chat));
65 data->id = g_strdup (empathy_chat_get_id (chat));
66 data->room = empathy_chat_is_room (chat);
72 chat_data_free (ChatData *data)
74 if (data->account != NULL)
76 g_object_unref (data->account);
86 g_slice_free (ChatData, data);
90 empathy_chat_manager_init (EmpathyChatManager *self)
92 EmpathyChatManagerPriv *priv = GET_PRIV (self);
94 priv->queue = g_queue_new ();
98 empathy_chat_manager_finalize (GObject *object)
100 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
101 EmpathyChatManagerPriv *priv = GET_PRIV (self);
103 if (priv->queue != NULL)
105 g_queue_foreach (priv->queue, (GFunc) chat_data_free, NULL);
106 g_queue_free (priv->queue);
110 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
114 empathy_chat_manager_constructor (GType type,
115 guint n_construct_params,
116 GObjectConstructParam *construct_params)
120 if (!chat_manager_singleton)
122 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
123 (type, n_construct_params, construct_params);
125 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
126 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
130 retval = g_object_ref (chat_manager_singleton);
137 empathy_chat_manager_class_init (
138 EmpathyChatManagerClass *empathy_chat_manager_class)
140 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
142 object_class->finalize = empathy_chat_manager_finalize;
143 object_class->constructor = empathy_chat_manager_constructor;
145 signals[CHATS_CHANGED] =
146 g_signal_new ("chats-changed",
147 G_TYPE_FROM_CLASS (object_class),
151 g_cclosure_marshal_VOID__UINT,
153 1, G_TYPE_UINT, NULL);
155 g_type_class_add_private (empathy_chat_manager_class,
156 sizeof (EmpathyChatManagerPriv));
160 empathy_chat_manager_dup_singleton (void)
162 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
166 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
169 EmpathyChatManagerPriv *priv = GET_PRIV (self);
172 data = chat_data_new (chat);
174 DEBUG ("Adding %s to queue: %s", data->room ? "room" : "contact", data->id);
176 g_queue_push_tail (priv->queue, data);
178 g_signal_emit (self, signals[CHATS_CHANGED], 0,
179 g_queue_get_length (priv->queue));
183 connection_ready_cb (TpConnection *connection,
187 ChatData *data = user_data;
188 EmpathyChatManager *self = chat_manager_singleton;
189 EmpathyChatManagerPriv *priv;
191 /* Extremely unlikely to happen, but I don't really want to keep refs to the
192 * chat manager in the ChatData structs as it'll then prevent the manager
193 * from being finalized. */
194 if (G_UNLIKELY (self == NULL))
197 priv = GET_PRIV (self);
202 empathy_dispatcher_join_muc (connection, data->id, NULL, NULL);
204 empathy_dispatcher_chat_with_contact_id (connection, data->id,
207 g_signal_emit (self, signals[CHATS_CHANGED], 0,
208 g_queue_get_length (priv->queue));
212 DEBUG ("Error readying connection, no chat: %s", error->message);
216 chat_data_free (data);
220 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
222 EmpathyChatManagerPriv *priv = GET_PRIV (self);
224 TpConnection *connection;
226 data = g_queue_pop_tail (priv->queue);
231 DEBUG ("Removing %s from queue and starting a chat with: %s",
232 data->room ? "room" : "contact", data->id);
234 connection = tp_account_get_connection (data->account);
236 if (connection != NULL)
238 tp_connection_call_when_ready (connection, connection_ready_cb, data);
242 DEBUG ("No connection, no chat.");
243 chat_data_free (data);
248 empathy_chat_manager_get_num_chats (EmpathyChatManager *self)
250 EmpathyChatManagerPriv *priv = GET_PRIV (self);
252 return g_queue_get_length (priv->queue);