changeset 88:3dc526bb4a5a

upload: plugin android gallery, first draft: - added "external" key in PLUGIN_INFO which indicate that a plugin doesn't create a widget when set to True - callback and cancel_cb method in UploadMenu now use a cleaning_cb arguments which can be None. If set, the callback will be called without argument once the file is uploaded - the AndroidGallery plugin looks for images on Android, then save the content to a temporary file an upload it, then clean the file. This plugin is only active on android platform.
author Goffi <goffi@goffi.org>
date Sun, 25 Dec 2016 22:53:50 +0100
parents 17094a075fd2
children d249df379fa3
files src/cagou/core/menu.py src/cagou/plugins/plugin_upload_android_gallery.py src/cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 117 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/cagou/core/menu.py	Sun Dec 25 16:41:25 2016 +0100
+++ b/src/cagou/core/menu.py	Sun Dec 25 22:53:50 2016 +0100
@@ -160,6 +160,7 @@
 
 
 class UploadMenu(contextmenu.ContextMenu):
+    """upload menu which handle display and callbacks"""
     # callback will be called with path to file to upload
     callback = properties.ObjectProperty()
     # cancel callback need to remove the widget for UI
@@ -191,8 +192,10 @@
     def _closeUI(self, wid):
         G.host.closeUI()
 
-    def onUploadCancelled(self, wid):
+    def onUploadCancelled(self, wid, cleaning_cb=None):
         self._closeUI(wid)
+        if cleaning_cb is not None:
+            cleaning_cb()
 
     def do_callback(self, plug_info):
         self.hide()
@@ -200,8 +203,11 @@
             log.warning(u"UploadMenu callback is not set")
         else:
             wid = None
-            def onUploadCb(file_path):
-                self._closeUI(wid)
-                self.callback(file_path)
+            external = plug_info.get('external', False)
+            def onUploadCb(file_path, cleaning_cb=None):
+                if not external:
+                    self._closeUI(wid)
+                self.callback(file_path, cleaning_cb)
             wid = plug_info['factory'](plug_info, onUploadCb, self.cancel_cb, self.profiles)
-            G.host.showExtraUI(wid)
+            if not external:
+                G.host.showExtraUI(wid)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cagou/plugins/plugin_upload_android_gallery.py	Sun Dec 25 22:53:50 2016 +0100
@@ -0,0 +1,94 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Cagou: desktop/mobile frontend for Salut à Toi XMPP client
+# Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+from sat.core import log as logging
+log = logging.getLogger(__name__)
+from sat.core.i18n import _
+import sys
+import tempfile
+import os
+import os.path
+if sys.platform=="android":
+    from jnius import autoclass
+    from android import activity, mActivity
+
+    Intent = autoclass('android.content.Intent')
+    OpenableColumns = autoclass('android.provider.OpenableColumns')
+    PHOTO_GALLERY = 1
+    RESULT_OK = -1
+
+
+
+PLUGIN_INFO = {
+    "name": _(u"photo"),
+    "main": "AndroidGallery",
+    "platforms": ('android',),
+    "external": True,
+    "description": _(u"upload a photo from photo gallery"),
+}
+
+
+class AndroidGallery(object):
+
+    def __init__(self, callback, cancel_cb):
+        self.callback = callback
+        self.cancel_cb = cancel_cb
+        activity.bind(on_activity_result=self.on_activity_result)
+        intent = Intent()
+        intent.setType('image/*')
+        intent.setAction(Intent.ACTION_GET_CONTENT)
+        mActivity.startActivityForResult(intent, PHOTO_GALLERY);
+
+    def on_activity_result(self, requestCode, resultCode, data):
+        # TODO: move file dump to a thread or use async callbacks during file writting
+        if requestCode == PHOTO_GALLERY and resultCode == RESULT_OK:
+            if data is None:
+                log.warning(u"No data found in activity result")
+                self.cancel_cb(self, None)
+                return
+            uri = data.getData()
+
+            # we get filename in the way explained at https://developer.android.com/training/secure-file-sharing/retrieve-info.html
+            cursor = mActivity.getContentResolver().query(uri, None, None, None, None )
+            name_idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
+            cursor.moveToFirst()
+            filename = cursor.getString(name_idx)
+
+            # we save data in a temporary file that we send to callback
+            # the file will be removed once upload is done (or if an error happens)
+            input_stream = mActivity.getContentResolver().openInputStream(uri)
+            tmp_dir = tempfile.mkdtemp()
+            tmp_file = os.path.join(tmp_dir, filename)
+            def cleaning():
+                os.unlink(tmp_file)
+                os.rmdir(tmp_dir)
+                log.debug(u'temporary file cleaned')
+            buff = bytearray(4096)
+            with open(tmp_file, 'wb') as f:
+                while True:
+                    ret = input_stream.read(buff, 0, 4096)
+                    if ret != -1:
+                        f.write(buff)
+                    else:
+                        break
+            input_stream.close()
+            self.callback(tmp_file, cleaning)
+        else:
+            self.cancel_cb(self, None)
--- a/src/cagou/plugins/plugin_wid_chat.py	Sun Dec 25 16:41:25 2016 +0100
+++ b/src/cagou/plugins/plugin_wid_chat.py	Sun Dec 25 22:53:50 2016 +0100
@@ -596,16 +596,22 @@
 
     def onProgressFinished(self, progress_id, metadata, profile):
         try:
-            callback = self._waiting_pids.pop(progress_id)
+            callback, cleaning_cb = self._waiting_pids.pop(progress_id)
         except KeyError:
             return
+        if cleaning_cb is not None:
+            cleaning_cb()
         callback(metadata, profile)
 
     def onProgressError(self, progress_id, err_msg, profile):
         try:
-            del self._waiting_pids[progress_id]
+            dummy, cleaning_cb = self._waiting_pids[progress_id]
         except KeyError:
             return
+        else:
+            del self._waiting_pids[progress_id]
+            if cleaning_cb is not None:
+                cleaning_cb()
         # TODO: display message to user
         log.warning(u"Can't upload file: {}".format(err_msg))
 
@@ -618,23 +624,23 @@
             profile_key=profile
             )
 
-    def fileUploadCb(self, progress_data):
+    def fileUploadCb(self, progress_data, cleaning_cb):
         try:
             progress_id = progress_data['progress']
         except KeyError:
             xmlui = progress_data['xmlui']
             G.host.showUI(xmlui)
         else:
-            self._waiting_pids[progress_id] = self.fileUploadDone
+            self._waiting_pids[progress_id] = (self.fileUploadDone, cleaning_cb)
 
-    def onUploadOK(self, file_path):
+    def onUploadOK(self, file_path, cleaning_cb):
         G.host.bridge.fileUpload(
             file_path,
             "",
             "",
             {"ignore_tls_errors": C.BOOL_TRUE},  # FIXME: should not be the default
             self.profile,
-            callback = self.fileUploadCb
+            callback = lambda progress_data: self.fileUploadCb(progress_data, cleaning_cb)
             )
 
     def _mucJoinCb(self, joined_data):