51
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
|
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) |
|
6 |
|
7 # This program is free software: you can redistribute it and/or modify |
|
8 # it under the terms of the GNU Affero General Public License as published by |
|
9 # the Free Software Foundation, either version 3 of the License, or |
|
10 # (at your option) any later version. |
|
11 |
|
12 # This program is distributed in the hope that it will be useful, |
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 # GNU Affero General Public License for more details. |
|
16 |
|
17 # You should have received a copy of the GNU Affero General Public License |
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 |
|
20 |
|
21 from sat.core.i18n import _ |
|
22 from sat.core import log as logging |
|
23 log = logging.getLogger(__name__) |
|
24 from cagou.core.constants import Const as C |
|
25 from kivy.uix.scrollview import ScrollView |
|
26 from kivy.uix.gridlayout import GridLayout |
|
27 from kivy.uix.widget import Widget |
|
28 from kivy.uix.label import Label |
|
29 from kivy.uix.button import Button |
|
30 from kivy.uix.popup import Popup |
|
31 from kivy.uix.dropdown import DropDown |
|
32 from kivy import properties |
|
33 from sat_frontends.quick_frontend import quick_menus |
|
34 from cagou import G |
|
35 import webbrowser |
|
36 |
|
37 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME)) |
|
38 ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{} |
|
39 |
|
40 Cagou is a libre communication tool based on libre standard XMPP. |
|
41 |
|
42 Cagou is part of the "Salut à Toi" project |
|
43 more informations at [color=5500ff][ref=website]salut-a-toi.org[/ref][/color] |
|
44 """).format(C.APP_VERSION) |
|
45 |
|
46 |
|
47 class AboutContent(Label): |
|
48 |
|
49 def on_ref_press(self, value): |
|
50 if value == "website": |
|
51 webbrowser.open("https://salut-a-toi.org") |
|
52 |
|
53 |
|
54 class AboutPopup(Popup): |
|
55 |
|
56 def on_touch_down(self, touch): |
|
57 if self.collide_point(*touch.pos): |
|
58 self.dismiss() |
|
59 return super(AboutPopup, self).on_touch_down(touch) |
|
60 |
|
61 |
|
62 class MenuItem(Button): |
|
63 item = properties.ObjectProperty() |
|
64 |
|
65 def on_item(self, instance, item): |
|
66 self.text = item.name |
|
67 |
|
68 def on_release(self): |
|
69 try: |
|
70 self.parent.parent.dismiss() |
|
71 except AttributeError: |
|
72 pass |
|
73 selected = G.host.selected_widget |
|
74 profile = None |
|
75 if selected is not None: |
|
76 try: |
|
77 profile = selected.profile |
|
78 except AttributeError: |
|
79 pass |
|
80 |
|
81 if profile is None: |
|
82 try: |
|
83 profile = list(selected.profiles)[0] |
|
84 except (AttributeError, IndexError): |
|
85 try: |
|
86 profile = list(G.host.profiles)[0] |
|
87 except IndexError: |
|
88 log.warning(u"Can't find profile") |
|
89 self.item.call(selected, profile) |
|
90 |
|
91 |
|
92 class MenuSeparator(Widget): |
|
93 pass |
|
94 |
|
95 |
|
96 class MenuContainer(Button): |
|
97 |
|
98 def __init__(self, **kwargs): |
|
99 super(MenuContainer, self).__init__(**kwargs) |
|
100 self.dropdown = DropDown(auto_width=False, size_hint_x=None, width=400) |
|
101 |
|
102 def on_release(self): |
|
103 self.dropdown.open(self) |
|
104 |
|
105 def add_widget(self, widget): |
|
106 widget.size_hint_y = None |
|
107 self.dropdown.add_widget(widget) |
|
108 |
|
109 |
|
110 class MenusWidget(ScrollView): |
|
111 |
|
112 def __init__(self, **kwargs): |
|
113 super(MenusWidget, self).__init__(**kwargs) |
|
114 self._grid = GridLayout(rows=1, size_hint=(None, 1)) |
|
115 self._grid.width = self._grid.minimum_width |
|
116 super(MenusWidget, self).add_widget(self._grid) |
|
117 |
|
118 def add_widget(self, widget): |
|
119 self._grid.add_widget(widget) |
|
120 |
|
121 def clear_widgets(self, children=None): |
|
122 self._grid.clear_widgets(children) |
|
123 |
|
124 def update(self, type_, caller=None): |
|
125 """Method to call when menus have changed |
|
126 |
|
127 @param type_(unicode): menu type like in sat.core.sat_main.importMenu |
|
128 @param caller(Widget): instance linked to the menus |
|
129 """ |
|
130 self.menus_container = G.host.menus.getMainContainer(type_) |
|
131 self.createMenus(caller) |
|
132 |
|
133 def _buildMenus(self, container, caller=None): |
|
134 """Recursively build menus of the container |
|
135 |
|
136 @param container(quick_menus.MenuContainer): menu container |
|
137 @param caller(Widget): instance linked to the menus |
|
138 """ |
|
139 if caller is None: |
|
140 caller = self |
|
141 for child in container.getActiveMenus(): |
|
142 if isinstance(child, quick_menus.MenuContainer): |
|
143 menu_container = MenuContainer() |
|
144 menu_container.text = child.name |
|
145 caller.add_widget(menu_container) |
|
146 self._buildMenus(child, caller=menu_container) |
|
147 elif isinstance(child, quick_menus.MenuSeparator): |
|
148 wid = MenuSeparator() |
|
149 caller.add_widget(wid) |
|
150 elif isinstance(child, quick_menus.MenuItem): |
|
151 wid = MenuItem(item=child) |
|
152 caller.add_widget(wid) |
|
153 else: |
|
154 log.error(u"Unknown child type: {}".format(child)) |
|
155 |
|
156 def createMenus(self, caller): |
|
157 self.clear_widgets() |
|
158 self._buildMenus(self.menus_container, caller) |
|
159 |
|
160 def onAbout(self): |
|
161 about = AboutPopup() |
|
162 about.title = ABOUT_TITLE |
|
163 about.content = AboutContent(text=ABOUT_CONTENT, markup=True) |
|
164 about.open() |