Mercurial > libervia-backend
annotate frontends/quick_frontend/quick_app.py @ 52:6455fb62ff83
Connection/disconnection signals
- wix, sortilege: management of this new signals
/!\ sortilege is bugged, the contact list window is not updated correctly
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 07 Jan 2010 01:55:30 +1100 |
parents | 8c67ea98ab91 |
children | 6dfe5bb10008 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 helper class for making a SAT frontend | |
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, error | |
23 from tools.jid import JID | |
24 from sat_bridge_frontend.DBus import DBusBridgeFrontend | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
25 import pdb |
0 | 26 |
27 class QuickApp(): | |
28 """This class contain the main methods needed for the frontend""" | |
29 | |
30 def __init__(self): | |
31 self.rosterList = {} | |
32 | |
33 ## bridge ## | |
34 self.bridge=DBusBridgeFrontend() | |
52 | 35 self.bridge.register("connected", self.connected) |
36 self.bridge.register("disconnected", self.disconnected) | |
0 | 37 self.bridge.register("newContact", self.newContact) |
38 self.bridge.register("newMessage", self.newMessage) | |
39 self.bridge.register("presenceUpdate", self.presenceUpdate) | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
40 self.bridge.register("subscribe", self.subscribe) |
0 | 41 self.bridge.register("paramUpdate", self.paramUpdate) |
42 self.bridge.register("contactDeleted", self.contactDeleted) | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
43 self.bridge.register("updatedValue", self.updatedValue, "request") |
0 | 44 self.bridge.register("askConfirmation", self.askConfirmation, "request") |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
45 self.bridge.register("actionResult", self.actionResult, "request") |
25
53e921c8a357
new plugin: gateways plugin, and first implementation of findGateways
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
46 self.bridge.register("actionResultExt", self.actionResult, "request") |
0 | 47 |
48 ###now we get the essential params### | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
49 self.whoami=JID(self.bridge.getParamA("JabberID","Connection")) |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
50 self.watched=self.bridge.getParamA("Watched", "Misc").split() #TODO: put this in a plugin |
0 | 51 |
52 ## misc ## | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
53 self.current_action_ids = set() |
28 | 54 self.current_action_ids_cb = {} |
0 | 55 self.onlineContact = set() #FIXME: temporary |
56 | |
57 if self.bridge.isConnected(): | |
58 self.setStatusOnline(True) | |
52 | 59 else: |
60 self.setStatusOnline(False) | |
61 return | |
0 | 62 |
63 ### now we fill the contact list ### | |
64 for contact in self.bridge.getContacts(): | |
65 self.newContact(contact[0], contact[1], contact[2]) | |
66 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
67 presences = self.bridge.getPresenceStatus() |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
68 for contact in presences: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
69 for res in presences[contact]: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
70 jabber_id = contact+('/'+res if res else '') |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
71 show = presences[contact][res][0] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
72 priority = presences[contact][res][1] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
73 statuses = presences[contact][res][2] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
74 self.presenceUpdate(jabber_id, show, priority, statuses) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
75 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
76 waitingSub = self.bridge.getWaitingSub() |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
77 for sub in waitingSub: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
78 self.subscribe(waitingSub[sub], sub) |
0 | 79 |
80 | |
52 | 81 def connected(self): |
82 """called when the connection is made""" | |
83 debug("Connected") | |
84 self.setStatusOnline(True) | |
85 | |
86 def disconnected(self): | |
87 """called when the connection is closed""" | |
88 debug("Disconnected") | |
89 self.CM.clear() | |
90 self.contactList.clear_contacts() | |
91 self.setStatusOnline(False) | |
92 | |
0 | 93 def newContact(self, JabberId, attributes, groups): |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
94 entity=JID(JabberId) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
95 self.rosterList[entity.short]=(dict(attributes), list(groups)) |
0 | 96 |
97 def newMessage(self, from_jid, msg, type, to_jid): | |
98 sender=JID(from_jid) | |
99 addr=JID(to_jid) | |
100 win = addr if sender.short == self.whoami.short else sender | |
101 self.chat_wins[win.short].printMessage(sender, msg) | |
102 | |
103 def setStatusOnline(self, online=True): | |
104 pass | |
105 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
106 def presenceUpdate(self, jabber_id, show, priority, statuses): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
107 debug ("presence update for %s (show=%s, statuses=%s)", jabber_id, show, statuses); |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
108 from_jid=JID(jabber_id) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
109 debug ("from_jid.short=%s whoami.short=%s", from_jid.short, self.whoami.short) |
0 | 110 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
111 if from_jid.short==self.whoami.short: |
0 | 112 if not type: |
113 self.setStatusOnline(True) | |
114 elif type=="unavailable": | |
115 self.setStatusOnline(False) | |
116 return | |
117 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
118 if show != 'unavailable': |
0 | 119 name="" |
120 group="" | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
121 if self.rosterList.has_key(from_jid.short): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
122 if self.rosterList[from_jid.short][0].has_key("name"): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
123 name=self.rosterList[from_jid.short][0]["name"] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
124 if len(self.rosterList[from_jid.short][1]): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
125 group=self.rosterList[from_jid.short][1][0] |
0 | 126 |
127 #FIXME: must be moved in a plugin | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
128 if from_jid.short in self.watched and not from_jid.short in self.onlineContact: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
129 self.showAlert("Watched jid [%s] is connected !" % from_jid.short) |
0 | 130 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
131 self.onlineContact.add(from_jid) #FIXME onlineContact is useless with CM, must be removed |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
132 self.CM.add(from_jid) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
133 self.CM.update(from_jid, 'name', name) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
134 self.CM.update(from_jid, 'show', show) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
135 self.CM.update(from_jid, 'statuses', statuses) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
136 self.CM.update(from_jid, 'group', group) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
137 cache = self.bridge.getProfileCache(from_jid) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
138 if cache.has_key('nick'): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
139 self.CM.update(from_jid, 'nick', cache['nick']) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
140 if cache.has_key('avatar'): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
141 self.CM.update(from_jid, 'avatar', self.bridge.getAvatarFile(cache['avatar'])) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
142 self.contactList.replace(from_jid) |
0 | 143 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
144 if show=="unavailable" and from_jid in self.onlineContact: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
145 self.onlineContact.remove(from_jid) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
146 self.CM.remove(from_jid) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
147 if not self.CM.isConnected(from_jid): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
148 self.contactList.disconnect(from_jid) |
0 | 149 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
150 def subscribe(self, type, raw_jid): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
151 """Called when a subsciption maangement signal is received""" |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
152 entity = JID(raw_jid) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
153 if type=="subscribed": |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
154 # this is a subscription confirmation, we just have to inform user |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
155 self.showDialog("The contact %s has accepted your subscription" % entity.short, 'Subscription confirmation') |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
156 elif type=="unsubscribed": |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
157 # this is a subscription refusal, we just have to inform user |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
158 self.showDialog("The contact %s has refused your subscription" % entity.short, 'Subscription refusal', 'error') |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
159 elif type=="subscribe": |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
160 # this is a subscriptionn request, we have to ask for user confirmation |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
161 answer = self.showDialog("The contact %s wants to subscribe to your presence.\nDo you accept ?" % entity.short, 'Subscription confirmation', 'yes/no') |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
162 if answer: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
163 self.bridge.subscription("subscribed", entity.short) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
164 else: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
165 self.bridge.subscribed("unsubscribed", entity.short) |
0 | 166 |
167 def showDialog(self, message, title, type="info"): | |
168 raise NotImplementedError | |
169 | |
170 def showAlert(self, message): | |
171 pass #FIXME | |
172 | |
173 def paramUpdate(self, name, value, namespace): | |
174 debug("param update: [%s] %s = %s", namespace, name, value) | |
175 if (namespace,name) == ("Connection", "JabberID"): | |
176 debug ("Changing ID to %s", value) | |
177 self.whoami=JID(value) | |
178 elif (namespace,name) == ("Misc", "Watched"): | |
179 self.watched=value.split() | |
180 | |
181 def contactDeleted(self, jid): | |
182 target = JID(jid) | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
183 self.CM.remove(target) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
184 self.contactList.remove(self.CM.get_full(target)) |
0 | 185 try: |
186 self.onlineContact.remove(target.short) | |
187 except KeyError: | |
188 pass | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
189 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
190 def updatedValue(self, name, data): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
191 print "toto" |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
192 print "updatedValue", name, data |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
193 if name == "profile_nick": |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
194 target = JID(data['jid']) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
195 self.CM.update(target, 'nick', data['nick']) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
196 self.contactList.replace(target) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
197 elif name == "profile_avatar": |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
198 target = JID(data['jid']) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
199 filename = self.bridge.getAvatarFile(data['avatar']) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
200 self.CM.update(target, 'avatar', filename) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
201 self.contactList.replace(target) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
28
diff
changeset
|
202 |
0 | 203 def askConfirmation(self, type, id, data): |
204 raise NotImplementedError | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
205 |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
206 def actionResult(self, type, id, data): |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
207 raise NotImplementedError |