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