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
29 #include "sipe-dialog.h"
30 #include "sipe-session.h"
31 #include "sipe-utils.h"
34 sipe_session_add_chat(struct sipe_account_data
*sip
)
36 struct sip_session
*session
= g_new0(struct sip_session
, 1);
37 session
->callid
= gencallid();
38 session
->is_multiparty
= TRUE
;
39 session
->chat_id
= rand();
40 session
->unconfirmed_messages
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
41 session
->conf_unconfirmed_messages
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
42 sip
->sessions
= g_slist_append(sip
->sessions
, session
);
47 sipe_session_find_or_add_chat_by_callid(struct sipe_account_data
*sip
,
50 struct sip_session
*session
= sipe_session_find_chat_by_callid(sip
, callid
);
52 purple_debug_info("sipe", "sipe_session_find_or_add_chat_by_callid: new session for %s\n", callid
);
53 session
= sipe_session_add_chat(sip
);
54 session
->callid
= g_strdup(callid
);
60 sipe_session_find_chat_by_callid(struct sipe_account_data
*sip
,
63 if (sip
== NULL
|| callid
== NULL
) {
67 SIPE_SESSION_FOREACH
{
68 if (session
->callid
&&
69 !g_ascii_strcasecmp(callid
, session
->callid
)) {
72 } SIPE_SESSION_FOREACH_END
;
77 sipe_session_find_chat_by_id(struct sipe_account_data
*sip
,
84 SIPE_SESSION_FOREACH
{
85 if (id
== session
->chat_id
) {
88 } SIPE_SESSION_FOREACH_END
;
93 sipe_session_find_chat_by_title(struct sipe_account_data
*sip
,
96 if (sip
== NULL
|| name
== NULL
) {
100 SIPE_SESSION_FOREACH
{
101 if (session
->chat_title
&&
102 !g_strcasecmp(name
, session
->chat_title
)) {
105 } SIPE_SESSION_FOREACH_END
;
110 sipe_session_find_conference(struct sipe_account_data
*sip
,
111 const gchar
*focus_uri
)
113 if (sip
== NULL
|| focus_uri
== NULL
) {
117 SIPE_SESSION_FOREACH
{
118 if (session
->focus_uri
&&
119 !g_ascii_strcasecmp(focus_uri
, session
->focus_uri
)) {
122 } SIPE_SESSION_FOREACH_END
;
127 sipe_session_find_im(struct sipe_account_data
*sip
, const gchar
*who
)
129 if (sip
== NULL
|| who
== NULL
) {
133 SIPE_SESSION_FOREACH
{
134 if (session
->with
&& !g_ascii_strcasecmp(who
, session
->with
)) {
137 } SIPE_SESSION_FOREACH_END
;
142 sipe_session_find_or_add_im(struct sipe_account_data
*sip
,
145 struct sip_session
*session
= sipe_session_find_im(sip
, who
);
147 purple_debug_info("sipe", "sipe_session_find_or_add_im: new session for %s\n", who
);
148 session
= g_new0(struct sip_session
, 1);
149 session
->is_multiparty
= FALSE
;
150 session
->with
= g_strdup(who
);
151 session
->unconfirmed_messages
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
152 sip
->sessions
= g_slist_append(sip
->sessions
, session
);
158 sipe_session_remove(struct sipe_account_data
*sip
, struct sip_session
*session
)
162 sip
->sessions
= g_slist_remove(sip
->sessions
, session
);
164 sipe_dialog_remove_all(session
);
165 sipe_dialog_free(session
->focus_dialog
);
167 entry
= session
->outgoing_message_queue
;
169 struct queued_message
*msg
= entry
->data
;
171 g_free(msg
->content_type
);
175 g_slist_free(session
->outgoing_message_queue
);
177 entry
= session
->pending_invite_queue
;
182 g_slist_free(session
->pending_invite_queue
);
184 g_hash_table_destroy(session
->unconfirmed_messages
);
185 g_hash_table_destroy(session
->conf_unconfirmed_messages
);
187 g_free(session
->with
);
188 g_free(session
->chat_title
);
189 g_free(session
->callid
);
190 g_free(session
->roster_manager
);
191 g_free(session
->focus_uri
);
192 g_free(session
->im_mcu_uri
);
193 g_free(session
->subject
);
198 sipe_session_remove_all(struct sipe_account_data
*sip
)
201 while ((entry
= sip
->sessions
) != NULL
) {
202 sipe_session_remove(sip
, entry
->data
);
207 sipe_session_enqueue_message(struct sip_session
*session
,
208 const gchar
*body
, const gchar
*content_type
)
210 struct queued_message
*msg
= g_new0(struct queued_message
,1);
211 msg
->body
= g_strdup(body
);
212 if (content_type
!= NULL
)
213 msg
->content_type
= g_strdup(content_type
);
215 session
->outgoing_message_queue
= g_slist_append(session
->outgoing_message_queue
, msg
);
219 sipe_session_dequeue_message(struct sip_session
*session
)
221 if (session
->outgoing_message_queue
== NULL
)
224 struct queued_message
*msg
= session
->outgoing_message_queue
->data
;
225 session
->outgoing_message_queue
= g_slist_remove(session
->outgoing_message_queue
, msg
);
227 g_free(msg
->content_type
);
230 return session
->outgoing_message_queue
;