JFT: app sub-module no more create the session himself
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Fri, 20 Aug 2010 21:10:23 +0200
changeset 152 a8623ca21574
parent 151 b1acef78e4f5
child 153 eab91df480d3
JFT: app sub-module no more create the session himself
jingle-ft/filetransfer.c
jingle/jingle.c
jingle/sessions.c
jingle/sessions.h
--- a/jingle-ft/filetransfer.c	Thu Aug 19 14:05:08 2010 +0200
+++ b/jingle-ft/filetransfer.c	Fri Aug 20 21:10:23 2010 +0200
@@ -32,7 +32,6 @@
 #include <mcabber/compl.h>
 #include <mcabber/commands.h>
 #include <mcabber/roster.h>
-#include <mcabber/utils.h>
 
 #include <jingle/jingle.h>
 #include <jingle/check.h>
@@ -438,11 +437,8 @@
     return;
   
   {
-    JingleSession *sess;
-    gchar *sid = jingle_generate_sid();
     gchar *ressource, *recipientjid;
     const gchar *namespaces[] = {NS_JINGLE, NS_JINGLE_APP_FT, NULL};
-    const gchar *myjid = g_strdup(lm_connection_get_jid(lconnection));
 
     if (CURRENT_JID == NULL) { // CURRENT_JID = the jid of the user which has focus
       scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: Please, choose a valid JID in the roster");
@@ -450,28 +446,21 @@
     }
     ressource = jingle_find_compatible_res(CURRENT_JID, namespaces);
     if (ressource == NULL) {
-      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: Cannot send file, because there is no ressource available");
+      scr_LogPrint(LPRINT_LOGNORM, "Jingle File Transfer: Cannot send file,"
+                                   " because there is no ressource available");
       return;
     }
 
     recipientjid = g_strdup_printf("%s/%s", CURRENT_JID, ressource);
 
-    sess = session_new(sid, myjid, recipientjid, JINGLE_SESSION_OUTGOING);
-    session_add_content(sess, "file", JINGLE_SESSION_STATE_PENDING);
-
-    session_add_app(sess, "file", NS_JINGLE_APP_FT, jft);
+    new_session_with_apps(recipientjid, (const gchar*[]){"file", NULL},
+                          (gconstpointer[]){jft, NULL},
+                          (const gchar*[]){NS_JINGLE_APP_FT, NULL});
 
-    {
-      JingleFTInfo *jfti = g_new0(JingleFTInfo, 1);
-      jfti->index = _next_index();
-      jfti->jft = jft;
-      info_list = g_slist_append(info_list, jfti);
-    }
-
-    jingle_handle_app(sess, "file", NS_JINGLE_APP_FT, jft, recipientjid);
-
+    jingle_handle_app("file", NS_JINGLE_APP_FT, jft, recipientjid);
+    
+    g_free(recipientjid);
     g_free(ressource);
-    g_free(sid);
   }
 }
 
--- a/jingle/jingle.c	Thu Aug 19 14:05:08 2010 +0200
+++ b/jingle/jingle.c	Fri Aug 20 21:10:23 2010 +0200
@@ -470,7 +470,7 @@
 
 void handle_trans_data(gconstpointer data, const gchar *data2, guint len)
 {
-  SessionContent *sc = session_find_by_transport(data);
+  SessionContent *sc = sessioncontent_find_by_transport(data);
   if (sc == NULL) {
     return;  
   }
--- a/jingle/sessions.c	Thu Aug 19 14:05:08 2010 +0200
+++ b/jingle/sessions.c	Fri Aug 20 21:10:23 2010 +0200
@@ -22,6 +22,7 @@
 #include <glib.h>
 
 #include <mcabber/logprint.h>
+#include <mcabber/xmpp.h>
 
 #include <jingle/jingle.h>
 #include <jingle/sessions.h>
@@ -176,7 +177,39 @@
   return NULL;
 }
 
-SessionContent *session_find_by_transport(gconstpointer data)
+JingleSession *session_find_by_transport(gconstpointer data)
+{
+  GSList *el, *el1;
+  JingleSession *sess;
+  SessionContent *sc;
+  for (el1 = sessions; el1; el1 = el1->next) {
+    sess = (JingleSession*) el1->data;
+    for (el = sess->content; el; el = el->next) {
+      sc = (SessionContent*) el->data;
+      if (data == sc->transport)
+        return sess;
+    }
+  }
+  return NULL;
+}
+
+JingleSession *session_find_by_app(gconstpointer data)
+{
+  GSList *el, *el1;
+  JingleSession *sess;
+  SessionContent *sc;
+  for (el1 = sessions; el1; el1 = el1->next) {
+    sess = (JingleSession*) el1->data;
+    for (el = sess->content; el; el = el->next) {
+      sc = (SessionContent*) el->data;
+      if (data == sc->description)
+        return sess;
+    }
+  }
+  return NULL;
+}
+
+SessionContent *sessioncontent_find_by_transport(gconstpointer data)
 {
   GSList *el, *el1;
   JingleSession *sess;
@@ -192,7 +225,7 @@
   return NULL;
 }
 
-SessionContent *session_find_by_app(gconstpointer data)
+SessionContent *sessioncontent_find_by_app(gconstpointer data)
 {
   GSList *el, *el1;
   JingleSession *sess;
@@ -208,6 +241,22 @@
   return NULL;
 }
 
+JingleSession *session_find_by_sessioncontent(SessionContent *data)
+{
+  GSList *el, *el1;
+  JingleSession *sess;
+  SessionContent *sc;
+  for (el1 = sessions; el1; el1 = el1->next) {
+    sess = (JingleSession*) el1->data;
+    for (el = sess->content; el; el = el->next) {
+      sc = (SessionContent*) el->data;
+      if (data == sc)
+        return sess;
+    }
+  }
+  return NULL;
+}
+
 gint session_remove_sessioncontent(JingleSession *sess, const gchar *name)
 {
   SessionContent *sc;
@@ -270,10 +319,11 @@
   g_free(sess);
 }
 
-void jingle_handle_app(JingleSession *sess, const gchar *name,
+void jingle_handle_app(const gchar *name,
                        const gchar *xmlns_app, gconstpointer app,
                        const gchar *to)
 {
+  JingleSession *sess = session_find_by_app(app);
   const gchar *xmlns = jingle_transport_for_app(xmlns_app, NULL);
   JingleTransportFuncs *trans = jingle_get_transportfuncs(xmlns);
   
@@ -346,3 +396,21 @@
   else
     sc->transfuncs->end(sc2, sc->transport);
 }
+
+void new_session_with_apps(const gchar *recipientjid, const gchar **names,
+                           gconstpointer *datas, const gchar **ns)
+{
+  const gchar *myjid = g_strdup(lm_connection_get_jid(lconnection));
+  gchar *sid = jingle_generate_sid();
+  JingleSession *sess = session_new(sid, myjid, recipientjid, JINGLE_SESSION_OUTGOING);
+  const gchar **name, **el1 = ns;
+  gconstpointer *data1 = datas;
+
+  for (name = names; name; ++name) {
+    session_add_content(sess, *name, JINGLE_SESSION_STATE_PENDING);
+    session_add_app(sess, *name, *el1, *data1);
+    if (!++data1 || !++el1)
+      break;
+  }
+  g_free(sid);
+}
--- a/jingle/sessions.h	Thu Aug 19 14:05:08 2010 +0200
+++ b/jingle/sessions.h	Fri Aug 20 21:10:23 2010 +0200
@@ -83,8 +83,9 @@
                            const gchar *xmlns, gconstpointer data);
 SessionContent* session_add_content_from_jinglecontent(JingleSession *sess,
                            JingleContent *cn, SessionState state, GError **err);
-SessionContent *session_find_by_transport(gconstpointer data);
-SessionContent *session_find_by_app(gconstpointer data);
+SessionContent *sessioncontent_find_by_transport(gconstpointer data);
+SessionContent *sessioncontent_find_by_app(gconstpointer data);
+JingleSession *session_find_by_sessioncontent(SessionContent *sc);
 SessionContent *session_find_sessioncontent(JingleSession *sess,
                                             const gchar *name);
 int session_remove_sessioncontent(JingleSession *sess, const gchar *name);
@@ -94,7 +95,7 @@
 void session_remove(JingleSession *sess);
 void session_free(JingleSession *sess);
 
-void jingle_handle_app(JingleSession *sess, const gchar *name,
+void jingle_handle_app(const gchar *name,
                        const gchar *xmlns_app, gconstpointer app,
                        const gchar *to);
 LmMessage *lm_message_from_jinglesession(const JingleSession *js,
@@ -102,4 +103,5 @@
 
 void handle_app_data(const gchar *sid, const gchar* from, const gchar *name, gchar *data, gsize size);
 
+void new_session_with_apps(const gchar *recipientjid, const gchar **name, gconstpointer *datas, const gchar **ns);
 #endif