]> git.0d.be Git - empathy.git/blob - src/empathy-chat-manager.c
chat-manager: document the queue
[empathy.git] / src / empathy-chat-manager.c
1 /*
2  * empathy-chat-manager.c - Source for EmpathyChatManager
3  * Copyright (C) 2010 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <libempathy/empathy-dispatcher.h>
21
22 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
23 #include <libempathy/empathy-debug.h>
24
25 #include "empathy-chat-manager.h"
26
27 enum {
28   CHATS_CHANGED,
29   LAST_SIGNAL
30 };
31
32 static guint signals[LAST_SIGNAL];
33
34 G_DEFINE_TYPE(EmpathyChatManager, empathy_chat_manager, G_TYPE_OBJECT)
35
36 /* private structure */
37 typedef struct _EmpathyChatManagerPriv EmpathyChatManagerPriv;
38
39 struct _EmpathyChatManagerPriv
40 {
41   /* Queue of (ChatData *) representing the closed chats */
42   GQueue *queue;
43 };
44
45 #define GET_PRIV(o) \
46   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CHAT_MANAGER, \
47     EmpathyChatManagerPriv))
48
49 static EmpathyChatManager *chat_manager_singleton = NULL;
50
51 typedef struct
52 {
53   TpAccount *account;
54   gchar *id;
55   gboolean room;
56 } ChatData;
57
58 static ChatData *
59 chat_data_new (EmpathyChat *chat)
60 {
61   ChatData *data = NULL;
62
63   data = g_slice_new0 (ChatData);
64
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);
68
69   return data;
70 }
71
72 static void
73 chat_data_free (ChatData *data)
74 {
75   if (data->account != NULL)
76     {
77       g_object_unref (data->account);
78       data->account = NULL;
79     }
80
81   if (data->id != NULL)
82     {
83       g_free (data->id);
84       data->id = NULL;
85     }
86
87   g_slice_free (ChatData, data);
88 }
89
90 static void
91 empathy_chat_manager_init (EmpathyChatManager *self)
92 {
93   EmpathyChatManagerPriv *priv = GET_PRIV (self);
94
95   priv->queue = g_queue_new ();
96 }
97
98 static void
99 empathy_chat_manager_finalize (GObject *object)
100 {
101   EmpathyChatManager *self = EMPATHY_CHAT_MANAGER (object);
102   EmpathyChatManagerPriv *priv = GET_PRIV (self);
103
104   if (priv->queue != NULL)
105     {
106       g_queue_foreach (priv->queue, (GFunc) chat_data_free, NULL);
107       g_queue_free (priv->queue);
108       priv->queue = NULL;
109     }
110
111   G_OBJECT_CLASS (empathy_chat_manager_parent_class)->finalize (object);
112 }
113
114 static GObject *
115 empathy_chat_manager_constructor (GType type,
116     guint n_construct_params,
117     GObjectConstructParam *construct_params)
118 {
119   GObject *retval;
120
121   if (!chat_manager_singleton)
122     {
123       retval = G_OBJECT_CLASS (empathy_chat_manager_parent_class)->constructor
124         (type, n_construct_params, construct_params);
125
126       chat_manager_singleton = EMPATHY_CHAT_MANAGER (retval);
127       g_object_add_weak_pointer (retval, (gpointer) &chat_manager_singleton);
128     }
129   else
130     {
131       retval = g_object_ref (chat_manager_singleton);
132     }
133
134   return retval;
135 }
136
137 static void
138 empathy_chat_manager_class_init (
139   EmpathyChatManagerClass *empathy_chat_manager_class)
140 {
141   GObjectClass *object_class = G_OBJECT_CLASS (empathy_chat_manager_class);
142
143   object_class->finalize = empathy_chat_manager_finalize;
144   object_class->constructor = empathy_chat_manager_constructor;
145
146   signals[CHATS_CHANGED] =
147     g_signal_new ("chats-changed",
148         G_TYPE_FROM_CLASS (object_class),
149         G_SIGNAL_RUN_LAST,
150         0,
151         NULL, NULL,
152         g_cclosure_marshal_VOID__UINT,
153         G_TYPE_NONE,
154         1, G_TYPE_UINT, NULL);
155
156   g_type_class_add_private (empathy_chat_manager_class,
157     sizeof (EmpathyChatManagerPriv));
158 }
159
160 EmpathyChatManager *
161 empathy_chat_manager_dup_singleton (void)
162 {
163   return g_object_new (EMPATHY_TYPE_CHAT_MANAGER, NULL);
164 }
165
166 void
167 empathy_chat_manager_closed_chat (EmpathyChatManager *self,
168     EmpathyChat *chat)
169 {
170   EmpathyChatManagerPriv *priv = GET_PRIV (self);
171   ChatData *data;
172
173   data = chat_data_new (chat);
174
175   DEBUG ("Adding %s to queue: %s", data->room ? "room" : "contact", data->id);
176
177   g_queue_push_tail (priv->queue, data);
178
179   g_signal_emit (self, signals[CHATS_CHANGED], 0,
180       g_queue_get_length (priv->queue));
181 }
182
183 static void
184 connection_ready_cb (TpConnection *connection,
185     const GError *error,
186     gpointer user_data)
187 {
188   ChatData *data = user_data;
189   EmpathyChatManager *self = chat_manager_singleton;
190   EmpathyChatManagerPriv *priv;
191
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))
196     goto out;
197
198   priv = GET_PRIV (self);
199
200   if (error == NULL)
201     {
202       if (data->room)
203         empathy_dispatcher_join_muc (connection, data->id,
204           EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
205       else
206         empathy_dispatcher_chat_with_contact_id (connection, data->id,
207             EMPATHY_DISPATCHER_NON_USER_ACTION, NULL, NULL);
208
209       g_signal_emit (self, signals[CHATS_CHANGED], 0,
210           g_queue_get_length (priv->queue));
211     }
212   else
213     {
214       DEBUG ("Error readying connection, no chat: %s", error->message);
215     }
216
217 out:
218   chat_data_free (data);
219 }
220
221 void
222 empathy_chat_manager_undo_closed_chat (EmpathyChatManager *self)
223 {
224   EmpathyChatManagerPriv *priv = GET_PRIV (self);
225   ChatData *data;
226   TpConnection *connection;
227
228   data = g_queue_pop_tail (priv->queue);
229
230   if (data == NULL)
231     return;
232
233   DEBUG ("Removing %s from queue and starting a chat with: %s",
234       data->room ? "room" : "contact", data->id);
235
236   connection = tp_account_get_connection (data->account);
237
238   if (connection != NULL)
239     {
240       tp_connection_call_when_ready (connection, connection_ready_cb, data);
241     }
242   else
243     {
244       DEBUG ("No connection, no chat.");
245       chat_data_free (data);
246     }
247 }
248
249 guint
250 empathy_chat_manager_get_num_chats (EmpathyChatManager *self)
251 {
252   EmpathyChatManagerPriv *priv = GET_PRIV (self);
253
254   return g_queue_get_length (priv->queue);
255 }