diff src/cagou/core/menu.py @ 86:c711be670ecd

core, chat: upload plugin system: - extented plugin system so it's not only used with main widget. It is also used for upload widgets and can be extended more - plugin file name is used to detect the type: plugin_wid_* for main widgets, plugin_upload_* for upload widget plugins - a new UploadMenu class allows to easily add an upload button which will use loaded plugins - plugin_info can now specify a list of allowed platforms in "platforms" key - file upload in chat has been moved to a plugin
author Goffi <goffi@goffi.org>
date Sun, 25 Dec 2016 16:41:21 +0100
parents c2a7234d13d2
children 3dc526bb4a5a
line wrap: on
line diff
--- a/src/cagou/core/menu.py	Sat Dec 24 14:20:49 2016 +0100
+++ b/src/cagou/core/menu.py	Sun Dec 25 16:41:21 2016 +0100
@@ -157,3 +157,51 @@
         about.title = ABOUT_TITLE
         about.content = AboutContent(text=ABOUT_CONTENT, markup=True)
         about.open()
+
+
+class UploadMenu(contextmenu.ContextMenu):
+    # callback will be called with path to file to upload
+    callback = properties.ObjectProperty()
+    # cancel callback need to remove the widget for UI
+    # will be called with the widget to remove as argument
+    cancel_cb = properties.ObjectProperty()
+    # profiles if set will be sent to upload widget, may be used to get specific files
+    profiles = properties.ObjectProperty()
+
+    def __init__(self, **kwargs):
+        super(UploadMenu, self).__init__(**kwargs)
+        if self.cancel_cb is None:
+            self.cancel_cb = self.onUploadCancelled
+        if self.profiles is None:
+            self.profiles = iter(G.host.profiles)
+        for plug_info in G.host.getPluggedWidgets(type_=C.PLUG_TYPE_UPLOAD):
+            item = contextmenu.ContextMenuTextItem(
+                text = plug_info['name'],
+                on_release = lambda dummy, plug_info=plug_info: self.do_callback(plug_info)
+                )
+            self.add_widget(item)
+
+    def show(self, caller_wid=None):
+        if caller_wid is not None:
+            pos = caller_wid.x, caller_wid.top + self.get_height()
+        else:
+            pos = G.host.app.root_window.mouse_pos
+        super(UploadMenu, self).show(*pos)
+
+    def _closeUI(self, wid):
+        G.host.closeUI()
+
+    def onUploadCancelled(self, wid):
+        self._closeUI(wid)
+
+    def do_callback(self, plug_info):
+        self.hide()
+        if self.callback is None:
+            log.warning(u"UploadMenu callback is not set")
+        else:
+            wid = None
+            def onUploadCb(file_path):
+                self._closeUI(wid)
+                self.callback(file_path)
+            wid = plug_info['factory'](plug_info, onUploadCb, self.cancel_cb, self.profiles)
+            G.host.showExtraUI(wid)