annotate src/cagou/core/menu.py @ 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 c711be670ecd
children 5d2289127bb7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.i18n import _
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import log as logging
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 log = logging.getLogger(__name__)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from cagou.core.constants import Const as C
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
25 from kivy.uix.boxlayout import BoxLayout
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy.uix.label import Label
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from kivy.uix.popup import Popup
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy import properties
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
29 from kivy.garden import contextmenu
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat_frontends.quick_frontend import quick_menus
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from cagou import G
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import webbrowser
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME))
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{}
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 Cagou is a libre communication tool based on libre standard XMPP.
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 Cagou is part of the "Salut à Toi" project
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 more informations at [color=5500ff][ref=website]salut-a-toi.org[/ref][/color]
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 """).format(C.APP_VERSION)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class AboutContent(Label):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def on_ref_press(self, value):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if value == "website":
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 webbrowser.open("https://salut-a-toi.org")
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 class AboutPopup(Popup):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def on_touch_down(self, touch):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if self.collide_point(*touch.pos):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.dismiss()
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return super(AboutPopup, self).on_touch_down(touch)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
59 class MainMenu(contextmenu.AppMenu):
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
60 pass
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
61
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
62
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
63 class MenuItem(contextmenu.ContextMenuTextItem):
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 item = properties.ObjectProperty()
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def on_item(self, instance, item):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.text = item.name
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def on_release(self):
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
70 super(MenuItem, self).on_release()
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
71 self.parent.hide()
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 selected = G.host.selected_widget
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 profile = None
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 if selected is not None:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 try:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 profile = selected.profile
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 except AttributeError:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 pass
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if profile is None:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 profile = list(selected.profiles)[0]
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except (AttributeError, IndexError):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 try:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 profile = list(G.host.profiles)[0]
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 except IndexError:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 log.warning(u"Can't find profile")
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.item.call(selected, profile)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
91 class MenuSeparator(contextmenu.ContextMenuDivider):
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 pass
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
95 class RootMenuContainer(contextmenu.AppMenuTextItem):
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
96 pass
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
99 class MenuContainer(contextmenu.ContextMenuTextItem):
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
100 pass
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
103 class MenusWidget(BoxLayout):
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def update(self, type_, caller=None):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 """Method to call when menus have changed
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @param type_(unicode): menu type like in sat.core.sat_main.importMenu
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 @param caller(Widget): instance linked to the menus
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 self.menus_container = G.host.menus.getMainContainer(type_)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 self.createMenus(caller)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def _buildMenus(self, container, caller=None):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """Recursively build menus of the container
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 @param container(quick_menus.MenuContainer): menu container
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 @param caller(Widget): instance linked to the menus
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 """
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if caller is None:
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
121 main_menu = MainMenu()
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
122 self.add_widget(main_menu)
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
123 caller = main_menu
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
124 else:
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
125 context_menu = contextmenu.ContextMenu()
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
126 caller.add_widget(context_menu)
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
127 # FIXME: next line is needed after parent is set to avoid a display bug in contextmenu
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
128 # TODO: fix this upstream
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
129 context_menu._on_visible(False)
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
130
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
131 caller = context_menu
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
132
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 for child in container.getActiveMenus():
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if isinstance(child, quick_menus.MenuContainer):
85
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
135 if isinstance(caller, MainMenu):
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
136 menu_container = RootMenuContainer()
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
137 else:
c2a7234d13d2 menu: use of garden's contextmenu for menus
Goffi <goffi@goffi.org>
parents: 53
diff changeset
138 menu_container = MenuContainer()
51
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 menu_container.text = child.name
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 caller.add_widget(menu_container)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self._buildMenus(child, caller=menu_container)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 elif isinstance(child, quick_menus.MenuSeparator):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 wid = MenuSeparator()
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 caller.add_widget(wid)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 elif isinstance(child, quick_menus.MenuItem):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 wid = MenuItem(item=child)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 caller.add_widget(wid)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 else:
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 log.error(u"Unknown child type: {}".format(child))
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def createMenus(self, caller):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.clear_widgets()
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self._buildMenus(self.menus_container, caller)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def onAbout(self):
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 about = AboutPopup()
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 about.title = ABOUT_TITLE
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 about.content = AboutContent(text=ABOUT_CONTENT, markup=True)
3f8599d9a766 core: menus first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 about.open()
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
160
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
161
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
162 class UploadMenu(contextmenu.ContextMenu):
88
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
163 """upload menu which handle display and callbacks"""
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
164 # callback will be called with path to file to upload
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
165 callback = properties.ObjectProperty()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
166 # cancel callback need to remove the widget for UI
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
167 # will be called with the widget to remove as argument
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
168 cancel_cb = properties.ObjectProperty()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
169 # profiles if set will be sent to upload widget, may be used to get specific files
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
170 profiles = properties.ObjectProperty()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
171
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
172 def __init__(self, **kwargs):
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
173 super(UploadMenu, self).__init__(**kwargs)
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
174 if self.cancel_cb is None:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
175 self.cancel_cb = self.onUploadCancelled
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
176 if self.profiles is None:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
177 self.profiles = iter(G.host.profiles)
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
178 for plug_info in G.host.getPluggedWidgets(type_=C.PLUG_TYPE_UPLOAD):
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
179 item = contextmenu.ContextMenuTextItem(
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
180 text = plug_info['name'],
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
181 on_release = lambda dummy, plug_info=plug_info: self.do_callback(plug_info)
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
182 )
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
183 self.add_widget(item)
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
184
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
185 def show(self, caller_wid=None):
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
186 if caller_wid is not None:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
187 pos = caller_wid.x, caller_wid.top + self.get_height()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
188 else:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
189 pos = G.host.app.root_window.mouse_pos
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
190 super(UploadMenu, self).show(*pos)
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
191
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
192 def _closeUI(self, wid):
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
193 G.host.closeUI()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
194
88
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
195 def onUploadCancelled(self, wid, cleaning_cb=None):
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
196 self._closeUI(wid)
88
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
197 if cleaning_cb is not None:
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
198 cleaning_cb()
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
199
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
200 def do_callback(self, plug_info):
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
201 self.hide()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
202 if self.callback is None:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
203 log.warning(u"UploadMenu callback is not set")
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
204 else:
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
205 wid = None
88
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
206 external = plug_info.get('external', False)
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
207 def onUploadCb(file_path, cleaning_cb=None):
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
208 if not external:
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
209 self._closeUI(wid)
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
210 self.callback(file_path, cleaning_cb)
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents: 85
diff changeset
211 wid = plug_info['factory'](plug_info, onUploadCb, self.cancel_cb, self.profiles)
88
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
212 if not external:
3dc526bb4a5a upload: plugin android gallery, first draft:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
213 G.host.showExtraUI(wid)