comparison src/cagou/core/profile_manager.py @ 16:ba14b596b90e

host can now be get as a global value: instead of always copying host from class to class, it can now be gotten from a global class with: from cagou import G then G.host will give host. This will probably be used on the long term on all SàT (backend + frontends). As host is currently needed in several places (most importantly in QuickFrontend), the argument is still present, and must be there even is unused on class inheriting from QuickSomething.
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 18:41:52 +0200
parents 56838ad5c84b
children 817a45e6d7e3
comparison
equal deleted inserted replaced
15:56838ad5c84b 16:ba14b596b90e
26 from kivy.uix import listview 26 from kivy.uix import listview
27 from kivy.uix.button import Button 27 from kivy.uix.button import Button
28 from kivy.uix.screenmanager import ScreenManager, Screen 28 from kivy.uix.screenmanager import ScreenManager, Screen
29 from kivy.adapters import listadapter 29 from kivy.adapters import listadapter
30 from kivy import properties 30 from kivy import properties
31 from cagou import G
31 32
32 33
33 class ProfileItem(listview.ListItemButton): 34 class ProfileItem(listview.ListItemButton):
34 pass 35 pass
35 36
37 class ProfileListAdapter(listadapter.ListAdapter): 38 class ProfileListAdapter(listadapter.ListAdapter):
38 39
39 def __init__(self, pm, *args, **kwargs): 40 def __init__(self, pm, *args, **kwargs):
40 super(ProfileListAdapter, self).__init__(*args, **kwargs) 41 super(ProfileListAdapter, self).__init__(*args, **kwargs)
41 self.pm = pm 42 self.pm = pm
42 self.host = pm.host
43 43
44 def closeUI(self, xmlui): 44 def closeUI(self, xmlui):
45 self.pm.screen_manager.transition.direction = 'right' 45 self.pm.screen_manager.transition.direction = 'right'
46 self.pm.screen_manager.current = 'profiles' 46 self.pm.screen_manager.current = 'profiles'
47 47
56 56
57 def select_item_view(self, view): 57 def select_item_view(self, view):
58 def authenticate_cb(data, cb_id, profile): 58 def authenticate_cb(data, cb_id, profile):
59 if C.bool(data.pop('validated', C.BOOL_FALSE)): 59 if C.bool(data.pop('validated', C.BOOL_FALSE)):
60 super(ProfileListAdapter, self).select_item_view(view) 60 super(ProfileListAdapter, self).select_item_view(view)
61 self.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile) 61 G.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile)
62 62
63 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text) 63 G.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text)
64 64
65 65
66 class ConnectButton(Button): 66 class ConnectButton(Button):
67 67
68 def __init__(self, profile_screen): 68 def __init__(self, profile_screen):
78 error_msg = properties.StringProperty('') 78 error_msg = properties.StringProperty('')
79 79
80 def __init__(self, pm): 80 def __init__(self, pm):
81 super(NewProfileScreen, self).__init__(name=u'new_profile') 81 super(NewProfileScreen, self).__init__(name=u'new_profile')
82 self.pm = pm 82 self.pm = pm
83 self.host = pm.host
84 83
85 def onCreationFailure(self, failure): 84 def onCreationFailure(self, failure):
86 msg = [l for l in unicode(failure).split('\n') if l][-1] 85 msg = [l for l in unicode(failure).split('\n') if l][-1]
87 self.error_msg = unicode(msg) 86 self.error_msg = unicode(msg)
88 87
89 def onCreationSuccess(self, profile): 88 def onCreationSuccess(self, profile):
90 self.pm.profiles_screen.reload() 89 self.pm.profiles_screen.reload()
91 self.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure) 90 G.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure)
92 91
93 def _sessionStarted(self, profile): 92 def _sessionStarted(self, profile):
94 jid = self.jid.text.strip() 93 jid = self.jid.text.strip()
95 self.host.bridge.setParam("JabberID", jid, "Connection", -1, profile) 94 G.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
96 self.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile) 95 G.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile)
97 self.pm.screen_manager.transition.direction = 'right' 96 self.pm.screen_manager.transition.direction = 'right'
98 self.pm.screen_manager.current = 'profiles' 97 self.pm.screen_manager.current = 'profiles'
99 98
100 def doCreate(self): 99 def doCreate(self):
101 name = self.profile_name.text.strip() 100 name = self.profile_name.text.strip()
102 # XXX: we use XMPP password for profile password to simplify 101 # XXX: we use XMPP password for profile password to simplify
103 # if user want to change profile password, he can do it in preferences 102 # if user want to change profile password, he can do it in preferences
104 self.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure) 103 G.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure)
105 104
106 105
107 class DeleteProfilesScreen(Screen): 106 class DeleteProfilesScreen(Screen):
108 107
109 def __init__(self, pm): 108 def __init__(self, pm):
110 self.pm = pm 109 self.pm = pm
111 self.host = pm.host
112 super(DeleteProfilesScreen, self).__init__(name=u'delete_profiles') 110 super(DeleteProfilesScreen, self).__init__(name=u'delete_profiles')
113 111
114 def doDelete(self): 112 def doDelete(self):
115 """This method will delete *ALL* selected profiles""" 113 """This method will delete *ALL* selected profiles"""
116 to_delete = self.pm.getProfiles() 114 to_delete = self.pm.getProfiles()
123 self.pm.screen_manager.transition.direction = 'right' 121 self.pm.screen_manager.transition.direction = 'right'
124 self.pm.screen_manager.current = 'profiles' 122 self.pm.screen_manager.current = 'profiles'
125 123
126 for profile in to_delete: 124 for profile in to_delete:
127 log.info(u"Deleteing profile [{}]".format(profile)) 125 log.info(u"Deleteing profile [{}]".format(profile))
128 self.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc) 126 G.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc)
129 127
130 128
131 class ProfilesScreen(Screen): 129 class ProfilesScreen(Screen):
132 layout = properties.ObjectProperty(None) 130 layout = properties.ObjectProperty(None)
133 131
134 def __init__(self, pm): 132 def __init__(self, pm):
135 self.pm = pm 133 self.pm = pm
136 profiles = pm.host.bridge.getProfilesList() 134 profiles = G.host.bridge.getProfilesList()
137 profiles.sort() 135 profiles.sort()
138 self.list_adapter = ProfileListAdapter(pm, 136 self.list_adapter = ProfileListAdapter(pm,
139 data=profiles, 137 data=profiles,
140 cls=ProfileItem, 138 cls=ProfileItem,
141 selection_mode='multiple', 139 selection_mode='multiple',
146 connect_btn = ConnectButton(self) 144 connect_btn = ConnectButton(self)
147 self.layout.add_widget(connect_btn) 145 self.layout.add_widget(connect_btn)
148 146
149 def reload(self): 147 def reload(self):
150 """Reload profiles list""" 148 """Reload profiles list"""
151 profiles = self.pm.host.bridge.getProfilesList() 149 profiles = G.host.bridge.getProfilesList()
152 profiles.sort() 150 profiles.sort()
153 self.list_adapter.data = profiles 151 self.list_adapter.data = profiles
154 152
155 153
156 class ProfileManager(QuickProfileManager, BoxLayout): 154 class ProfileManager(QuickProfileManager, BoxLayout):
157 155
158 def __init__(self, host, autoconnect=None): 156 def __init__(self, autoconnect=None):
159 QuickProfileManager.__init__(self, host, autoconnect) 157 QuickProfileManager.__init__(self, G.host, autoconnect)
160 BoxLayout.__init__(self, orientation="vertical") 158 BoxLayout.__init__(self, orientation="vertical")
161 self.screen_manager = ScreenManager() 159 self.screen_manager = ScreenManager()
162 self.profiles_screen = ProfilesScreen(self) 160 self.profiles_screen = ProfilesScreen(self)
163 self.new_profile_screen = NewProfileScreen(self) 161 self.new_profile_screen = NewProfileScreen(self)
164 self.delete_profiles_screen = DeleteProfilesScreen(self) 162 self.delete_profiles_screen = DeleteProfilesScreen(self)