diff src/plugins/plugin_xep_0234.py @ 1585:846a39900fa6

plugins XEP-0096, XEP-0260, file: sendFile method is managed by file plugin, which choose the best available method + progress_id fix
author Goffi <goffi@goffi.org>
date Sat, 14 Nov 2015 19:18:05 +0100
parents 833bdb227b16
children b144babc2658
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0234.py	Fri Nov 13 16:46:32 2015 +0100
+++ b/src/plugins/plugin_xep_0234.py	Sat Nov 14 19:18:05 2015 +0100
@@ -49,6 +49,7 @@
 
 class XEP_0234(object):
     # TODO: assure everything is closed when file is sent or session terminate is received
+    # TODO: call self._f.unregister when unloading order will be managing (i.e. when depenencies will be unloaded at the end)
 
     def __init__(self, host):
         log.info(_("plugin Jingle File Transfer initialization"))
@@ -56,27 +57,50 @@
         self._j = host.plugins["XEP-0166"] # shortcut to access jingle
         self._j.registerApplication(NS_JINGLE_FT, self)
         self._f = host.plugins["FILE"]
+        self._f.register(NS_JINGLE_FT, self.fileJingleSend, priority = 10000, method_name=u"Jingle")
         host.bridge.addMethod("fileJingleSend", ".plugin", in_sign='sssss', out_sign='', method=self._fileJingleSend)
 
     def getHandler(self, profile):
         return XEP_0234_handler()
 
+    def _getProgressId(self, session, content_name):
+        """Return a unique progress ID
+
+        @param session(dict): jingle session
+        @param content_name(unicode): name of the content
+        @return (unicode): unique progress id
+        """
+        return u'{}_{}'.format(session['id'], content_name)
+
     def _fileJingleSend(self, peer_jid, filepath, name="", file_desc="", profile=C.PROF_KEY_NONE):
         return self.fileJingleSend(jid.JID(peer_jid), filepath, name or None, file_desc or None, profile)
 
-    def fileJingleSend(self, peer_jid, filepath, name=None, file_desc=None, profile=C.PROF_KEY_NONE):
+    def fileJingleSend(self, peer_jid, filepath, name, file_desc=None, profile=C.PROF_KEY_NONE):
+        """Send a file using jingle file transfer
+
+        @param peer_jid(jid.JID): destinee jid
+        @param filepath(str): absolute path of the file
+        @param name(unicode, None): name of the file
+        @param file_desc(unicode, None): description of the file
+        @param profile: %(doc_profile)s
+        @return (D(unicode)): progress id
+        """
+        progress_id_d = defer.Deferred()
         self._j.initiate(peer_jid,
                          [{'app_ns': NS_JINGLE_FT,
                            'senders': self._j.ROLE_INITIATOR,
                            'app_kwargs': {'filepath': filepath,
                                           'name': name,
-                                          'file_desc': file_desc},
+                                          'file_desc': file_desc,
+                                          'progress_id_d': progress_id_d},
                          }],
                          profile=profile)
+        return progress_id_d
 
     # jingle callbacks
 
-    def jingleSessionInit(self, session, content_name, filepath, name=None, file_desc=None, profile=C.PROF_KEY_NONE):
+    def jingleSessionInit(self, session, content_name, filepath, name, file_desc, progress_id_d, profile=C.PROF_KEY_NONE):
+        progress_id_d.callback(self.getProgressId(session, content_name))
         content_data = session['contents'][content_name]
         application_data = content_data['application_data']
         assert 'file_path' not in application_data
@@ -106,7 +130,7 @@
             file_elt = desc_elt.elements(NS_JINGLE_FT, 'file').next()
         except StopIteration:
             raise failure.Failure(exceptions.DataError)
-        file_data = {}
+        file_data = {'progress_id': self._getProgressId(session, content_name)}
         for name in ('date', 'desc', 'media-type', 'name', 'range', 'size'):
             try:
                 file_data[name] = unicode(file_elt.elements(NS_JINGLE_FT, name).next())
@@ -159,6 +183,7 @@
             size = application_data['file_data']['size']
             content_data['file_obj'] = self._f.File(self.host,
                                        file_path,
+                                       uid=self._getProgressId(session, content_name),
                                        size=size,
                                        profile=profile
                                        )
@@ -170,7 +195,7 @@
         return desc_elt
 
     def _finishedCb(self, dummy, session, content_name, content_data, profile):
-        log.debug(u"File transfer completed successfuly")
+        log.info(u"File transfer completed successfuly")
         if content_data['senders'] != session['role']:
             # we terminate the session only if we are the received,
             # as recommanded in XEP-0234 ยง2 (after example 6)