6 * Copyright (C) 2009 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "sipe-backend-debug.h"
31 #include "sipe-dialog.h"
32 #include "sipe-session.h"
33 #include "sipe-utils.h"
37 sipe_free_queued_message(struct queued_message
*message
)
39 g_free(message
->body
);
40 g_free(message
->content_type
);
45 sipe_session_add_chat(struct sipe_account_data
*sip
)
47 struct sip_session
*session
= g_new0(struct sip_session
, 1);
48 session
->callid
= gencallid();
49 session
->is_multiparty
= TRUE
;
50 session
->chat_id
= rand();
51 session
->unconfirmed_messages
= g_hash_table_new_full(
52 g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
)sipe_free_queued_message
);
53 session
->conf_unconfirmed_messages
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
54 sip
->sessions
= g_slist_append(sip
->sessions
, session
);
59 sipe_session_find_or_add_chat_by_callid(struct sipe_account_data
*sip
,
62 struct sip_session
*session
= sipe_session_find_chat_by_callid(sip
, callid
);
64 SIPE_DEBUG_INFO("sipe_session_find_or_add_chat_by_callid: new session for %s", callid
);
65 session
= sipe_session_add_chat(sip
);
66 session
->callid
= g_strdup(callid
);
72 sipe_session_find_chat_by_callid(struct sipe_account_data
*sip
,
75 if (sip
== NULL
|| callid
== NULL
) {
79 SIPE_SESSION_FOREACH
{
80 if (session
->callid
&&
81 sipe_strcase_equal(callid
, session
->callid
)) {
84 } SIPE_SESSION_FOREACH_END
;
89 sipe_session_find_chat_by_id(struct sipe_account_data
*sip
,
96 SIPE_SESSION_FOREACH
{
97 if (id
== session
->chat_id
) {
100 } SIPE_SESSION_FOREACH_END
;
105 sipe_session_find_chat_by_title(struct sipe_account_data
*sip
,
108 if (sip
== NULL
|| name
== NULL
) {
112 SIPE_SESSION_FOREACH
{
113 if (session
->chat_title
&&
114 !g_strcasecmp(name
, session
->chat_title
)) {
117 } SIPE_SESSION_FOREACH_END
;
122 sipe_session_find_conference(struct sipe_account_data
*sip
,
123 const gchar
*focus_uri
)
125 if (sip
== NULL
|| focus_uri
== NULL
) {
129 SIPE_SESSION_FOREACH
{
130 if (session
->focus_uri
&&
131 sipe_strcase_equal(focus_uri
, session
->focus_uri
)) {
134 } SIPE_SESSION_FOREACH_END
;
139 sipe_session_find_im(struct sipe_account_data
*sip
, const gchar
*who
)
141 if (sip
== NULL
|| who
== NULL
) {
145 SIPE_SESSION_FOREACH
{
146 if (session
->with
&& sipe_strcase_equal(who
, session
->with
)) {
149 } SIPE_SESSION_FOREACH_END
;
154 sipe_session_find_or_add_im(struct sipe_account_data
*sip
,
157 struct sip_session
*session
= sipe_session_find_im(sip
, who
);
159 SIPE_DEBUG_INFO("sipe_session_find_or_add_im: new session for %s", who
);
160 session
= g_new0(struct sip_session
, 1);
161 session
->is_multiparty
= FALSE
;
162 session
->with
= g_strdup(who
);
163 session
->unconfirmed_messages
= g_hash_table_new_full(
164 g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
)sipe_free_queued_message
);
165 sip
->sessions
= g_slist_append(sip
->sessions
, session
);
171 sipe_session_remove(struct sipe_account_data
*sip
, struct sip_session
*session
)
175 sip
->sessions
= g_slist_remove(sip
->sessions
, session
);
177 sipe_dialog_remove_all(session
);
178 sipe_dialog_free(session
->focus_dialog
);
180 entry
= session
->outgoing_message_queue
;
182 struct queued_message
*msg
= entry
->data
;
184 g_free(msg
->content_type
);
188 g_slist_free(session
->outgoing_message_queue
);
190 entry
= session
->pending_invite_queue
;
195 g_slist_free(session
->pending_invite_queue
);
197 g_hash_table_destroy(session
->unconfirmed_messages
);
198 g_hash_table_destroy(session
->conf_unconfirmed_messages
);
200 g_free(session
->with
);
201 g_free(session
->chat_title
);
202 g_free(session
->callid
);
203 g_free(session
->roster_manager
);
204 g_free(session
->focus_uri
);
205 g_free(session
->im_mcu_uri
);
206 g_free(session
->subject
);
211 sipe_session_remove_all(struct sipe_account_data
*sip
)
214 while ((entry
= sip
->sessions
) != NULL
) {
215 sipe_session_remove(sip
, entry
->data
);
220 sipe_session_enqueue_message(struct sip_session
*session
,
221 const gchar
*body
, const gchar
*content_type
)
223 struct queued_message
*msg
= g_new0(struct queued_message
,1);
224 msg
->body
= g_strdup(body
);
225 if (content_type
!= NULL
)
226 msg
->content_type
= g_strdup(content_type
);
228 session
->outgoing_message_queue
= g_slist_append(session
->outgoing_message_queue
, msg
);
232 sipe_session_dequeue_message(struct sip_session
*session
)
234 struct queued_message
*msg
;
236 if (session
->outgoing_message_queue
== NULL
)
239 msg
= session
->outgoing_message_queue
->data
;
240 session
->outgoing_message_queue
= g_slist_remove(session
->outgoing_message_queue
, msg
);
242 g_free(msg
->content_type
);
245 return session
->outgoing_message_queue
;