changeset 60:35abe494e6c7

core: bridge selection + improvments for android - bridge specified in sat.conf is now imported, in the same way as for backend - when loading, a label showing "Loading please wait" is shown - postInit is now call when backend is ready - avoid division by zero when icon is not available in cagou_widget.kv - twisted reactor is used
author Goffi <goffi@goffi.org>
date Sun, 04 Dec 2016 18:16:10 +0100
parents 2aa44a82d0e7
children 0b7f10de416e
files src/cagou.py src/cagou/core/cagou_main.py src/cagou/kv/cagou_widget.kv
diffstat 3 files changed, 40 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/cagou.py	Sun Oct 02 13:29:37 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-#!/usr//bin/env python2
-# -*- 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/>.
-
-import cagou
-
-if __name__ == "__main__":
-    cagou.run()
--- a/src/cagou/core/cagou_main.py	Sun Oct 02 13:29:37 2016 +0200
+++ b/src/cagou/core/cagou_main.py	Sun Dec 04 18:16:10 2016 +0100
@@ -29,11 +29,17 @@
 from sat_frontends.quick_frontend import quick_widgets
 from sat_frontends.quick_frontend import quick_chat
 from sat_frontends.quick_frontend import quick_utils
-from sat_frontends.bridge.DBus import DBusBridgeFrontend
+from sat.tools import config
+from sat.tools.common import dynamic_import
 import kivy
 kivy.require('1.9.1')
 import kivy.support
-kivy.support.install_gobject_iteration()
+bridge_name = config.getConfig(config.parseMainConf(), '', 'bridge', 'dbus')
+# FIXME: event loop is choosen according to bridge_name, a better way should be used
+if bridge_name == 'embedded':
+    kivy.support.install_twisted_reactor()
+else:
+    kivy.support.install_gobject_iteration()
 from kivy.app import App
 from kivy.lang import Builder
 from kivy import properties
@@ -54,6 +60,8 @@
 import glob
 import cagou.plugins
 import cagou.kv
+from sat.core import sat_main
+import sys
 
 
 class NotifsIcon(IconButton):
@@ -194,8 +202,11 @@
     """Kivy App for Cagou"""
 
     def build(self):
+        return CagouRootWidget(Label(text=u"Loading please wait"))
+
+    def showWidget(self):
         self._profile_manager = ProfileManager()
-        return CagouRootWidget(self._profile_manager)
+        self.root.changeWidget(self._profile_manager)
 
     def expand(self, path, *args, **kwargs):
         """expand path and replace known values
@@ -213,7 +224,15 @@
     MB_HANDLE = False
 
     def __init__(self):
-        super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui, check_options=quick_utils.check_options)
+        if bridge_name == 'embedded':
+            self.sat = sat_main.SAT()
+        bridge_module = dynamic_import.bridge(bridge_name, 'sat_frontends.bridge')
+        if bridge_module is None:
+            log.error(u"Can't import {} bridge".format(bridge_name))
+            sys.exit(1)
+        else:
+            log.info(u"Loading {} bridge".format(bridge_name))
+        super(Cagou, self).__init__(create_bridge=bridge_module.Bridge, xmlui=xmlui, check_options=quick_utils.check_options)
         self._import_kv()
         self.app = CagouApp()
         self.app.host = self
@@ -230,10 +249,23 @@
                 yield w
 
     def run(self):
-        Clock.schedule_once(self.postInit)
+        self.bridge.getReady(self.onBackendReady)
+        self.app.bind(on_stop=self.onStop)
         self.app.run()
 
-    def postInit(self, dummy):
+    def onStop(self, obj):
+        try:
+            sat_instance = self.sat
+        except AttributeError:
+            pass
+        else:
+            sat_instance.stopService()
+
+    def onBackendReady(self):
+        self.app.showWidget()
+        self.postInit()
+
+    def postInit(self, dummy=None):
         profile_manager = self.app._profile_manager
         del self.app._profile_manager
         super(Cagou, self).postInit(profile_manager)
--- a/src/cagou/kv/cagou_widget.kv	Sun Oct 02 13:29:37 2016 +0200
+++ b/src/cagou/kv/cagou_widget.kv	Sun Dec 04 18:16:10 2016 +0100
@@ -29,7 +29,7 @@
         size_hint: None, 1
         source: root.plugin_info['icon_medium']
         allow_stretch: True
-        width: self.texture_size[0]*self.height/self.texture_size[1]
+        width: self.texture_size[0]*self.height/(self.texture_size[1] or 1)
     Label:
         size_hint: 1, 1
         text: root.plugin_info['name']
@@ -52,7 +52,7 @@
             source: root.plugin_info['icon_small']
             size_hint: None, 1
             allow_stretch: True
-            width: self.texture_size[0]*self.height/self.texture_size[1]
+            width: self.texture_size[0]*self.height/(self.texture_size[1] or 1)
         TextInput:
             id: header_input
             multiline: False