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
41 /* Queue of (ChatData *) representing the closed chats */
46 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
47 EmpathyChatManagerPriv))
49 static EmpathyChatManager *chat_manager_singleton = NULL;
59 chat_data_new (EmpathyChat *chat)
61 ChatData *data = NULL;
63 data = g_slice_new0 (ChatData);
65 data->account = g_object_ref (empathy_chat_get_account (chat));
66 data->id = g_strdup (empathy_chat_get_id (chat));
67 data->room = empathy_chat_is_room (chat);
73 chat_data_free (ChatData *data)
75 if (data->account != NULL)
77 g_object_unref (data->account);
87 g_slice_free (ChatData, data);
91 empathy_chat_manager_init (EmpathyChatManager *self)
93 EmpathyChatManagerPriv *priv = GET_PRIV (self);
95 priv->queue = g_queue_new ();
99 empathy_chat_manager_finalize (GObject *object)
101 EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
102 EmpathyChatManagerPriv *priv = GET_PRIV (self);
104 if (priv->queue != NULL)
106 g_queue_foreach (priv->queue, (GFunc) chat_data_free, NULL);
107 g_queue_free (priv->queue);
111 G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
115 empathy_chat_manager_constructor (GType type,
116 guint n_construct_params,
117 GObjectConstructParam *construct_params)
121 if (!chat_manager_singleton)
123 retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
124 (type, n_construct_params, construct_params);
126 chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
127 g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
131 retval = g_object_ref (chat_manager_singleton);
138 empathy_chat_manager_class_init (
139 EmpathyChatManagerClass *empathy_chat_manager_class)
141 GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
143 object_class->finalize = empathy_chat_manager_finalize;
144 object_class->constructor = empathy_chat_manager_constructor;
146 signals[CHATS_CHANGED] =
147 g_signal_new ("chats-changed",
148 G_TYPE_FROM_CLASS (object_class),
152 g_cclosure_marshal_VOID__UINT,
154 1, G_TYPE_UINT, NULL);
156 g_type_class_add_private (empathy_chat_manager_class,
157 sizeof (EmpathyChatManagerPriv));
161 empathy_chat_manager_dup_singleton (void)
163 return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
167 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
170 EmpathyChatManagerPriv *priv = GET_PRIV (self);
173 data = chat_data_new (chat);
175 DEBUG ("Adding %s to queue: %s", data->room ? "room" : "contact", data->id);
177 g_queue_push_tail (priv->queue, data);
179 g_signal_emit (self, signals[CHATS_CHANGED], 0,
180 g_queue_get_length (priv->queue));
184 connection_ready_cb (TpConnection *connection,
188 ChatData *data = user_data;
189 EmpathyChatManager *self = chat_manager_singleton;
190 EmpathyChatManagerPriv *priv;
192 /* Extremely unlikely to happen, but I don't really want to keep refs to the
193 * chat manager in the ChatData structs as it'll then prevent the manager
194 * from being finalized. */
195 if (G_UNLIKELY (self == NULL))
198 priv = GET_PRIV (self);
203 empathy_dispatcher_join_muc (connection, data->id,
204 EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
206 empathy_dispatcher_chat_with_contact_id (connection, data->id,
207 EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
209 g_signal_emit (self, signals[CHATS_CHANGED], 0,
210 g_queue_get_length (priv->queue));
214 DEBUG ("Error readying connection, no chat: %s", error->message);
218 chat_data_free (data);
222 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
224 EmpathyChatManagerPriv *priv = GET_PRIV (self);
226 TpConnection *connection;
228 data = g_queue_pop_tail (priv->queue);
233 DEBUG ("Removing %s from queue and starting a chat with: %s",
234 data->room ? "room" : "contact", data->id);
236 connection = tp_account_get_connection (data->account);
238 if (connection != NULL)
240 tp_connection_call_when_ready (connection, connection_ready_cb, data);
244 DEBUG ("No connection, no chat.");
245 chat_data_free (data);
250 empathy_chat_manager_get_num_chats (EmpathyChatManager *self)
252 EmpathyChatManagerPriv *priv = GET_PRIV (self);
254 return g_queue_get_length (priv->queue);