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 |
|
25 from quick_frontend.quick_contact_management import QuickContactManagement |
|
26 |
|
27 |
|
28 |
|
29 class QuickApp(): |
|
30 """This class contain the main methods needed for the frontend""" |
|
31 |
|
32 def __init__(self): |
|
33 self.rosterList = {} |
|
34 self.CM = QuickContactManagement() #a short name if more handy |
|
35 |
|
36 ## bridge ## |
|
37 self.bridge=DBusBridgeFrontend() |
|
38 self.bridge.register("newContact", self.newContact) |
|
39 self.bridge.register("newMessage", self.newMessage) |
|
40 self.bridge.register("presenceUpdate", self.presenceUpdate) |
|
41 self.bridge.register("paramUpdate", self.paramUpdate) |
|
42 self.bridge.register("contactDeleted", self.contactDeleted) |
|
43 self.bridge.register("askConfirmation", self.askConfirmation, "request") |
|
44 |
|
45 ###now we get the essential params### |
|
46 self.whoami=JID(self.bridge.getParam("JabberID","Connection")[0]) |
|
47 self.watched=self.bridge.getParam("Watched", "Misc")[0].split() #TODO: put this in a plugin |
|
48 |
|
49 ## misc ## |
|
50 self.onlineContact = set() #FIXME: temporary |
|
51 |
|
52 if self.bridge.isConnected(): |
|
53 self.setStatusOnline(True) |
|
54 |
|
55 ### now we fill the contact list ### |
|
56 for contact in self.bridge.getContacts(): |
|
57 self.newContact(contact[0], contact[1], contact[2]) |
|
58 |
|
59 for status in self.bridge.getPresenceStatus(): |
|
60 self.presenceUpdate(status[0], status[1], status[2], status[3], status[4]) |
|
61 |
|
62 |
|
63 def newContact(self, JabberId, attributes, groups): |
|
64 jid=JID(JabberId) |
|
65 self.rosterList[jid.short]=(dict(attributes), list(groups)) |
|
66 |
|
67 def newMessage(self, from_jid, msg, type, to_jid): |
|
68 sender=JID(from_jid) |
|
69 addr=JID(to_jid) |
|
70 win = addr if sender.short == self.whoami.short else sender |
|
71 self.chat_wins[win.short].printMessage(sender, msg) |
|
72 |
|
73 def setStatusOnline(self, online=True): |
|
74 pass |
|
75 |
|
76 def presenceUpdate(self, jabber_id, type, show, status, priority): |
|
77 debug ("presence update for %s (type=%s, show=%s, status=%s)", jabber_id, type, show, status); |
|
78 jid=JID(jabber_id) |
|
79 debug ("jid.short=%s whoami.short=%s", jid.short, self.whoami.short) |
|
80 |
|
81 ### subscription management ### |
|
82 if type=="subscribed": |
|
83 # this is a subscription confirmation, we just have to inform user |
|
84 self.showDialog("The contact %s has accepted your subscription" % jid.short, 'Subscription confirmation') |
|
85 return |
|
86 elif type=="unsubscribed": |
|
87 # this is a subscription refusal, we just have to inform user |
|
88 self.showDialog("The contact %s has refused your subscription" % jid.short, 'Subscription refusal', 'error') |
|
89 return |
|
90 elif type=="subscribe": |
|
91 # this is a subscrition request, we have to ask for user confirmation |
|
92 answer = self.showDialog("The contact %s wants to subscribe to your presence.\nDo you accept ?" % jid.short, 'Subscription confirmation', 'question') |
|
93 if answer: |
|
94 self.bridge.setPresence(type="subscribed", to=jid.short) |
|
95 else: |
|
96 self.bridge.setPresence(type="unsubscribed", to=jid.short) |
|
97 return |
|
98 ### subscription management end ### |
|
99 |
|
100 if jid.short==self.whoami.short: |
|
101 if not type: |
|
102 self.setStatusOnline(True) |
|
103 elif type=="unavailable": |
|
104 self.setStatusOnline(False) |
|
105 return |
|
106 |
|
107 if not type: |
|
108 name="" |
|
109 group="" |
|
110 if self.rosterList.has_key(jid.short): |
|
111 if self.rosterList[jid.short][0].has_key("name"): |
|
112 name=self.rosterList[jid.short][0]["name"] |
|
113 if self.rosterList[jid.short][0].has_key("show"): |
|
114 name=self.rosterList[jid.short][0]["show"] |
|
115 if self.rosterList[jid.short][0].has_key("status"): |
|
116 name=self.rosterList[jid.short][0]["status"] |
|
117 if len(self.rosterList[jid.short][1]): |
|
118 group=self.rosterList[jid.short][1][0] |
|
119 |
|
120 #FIXME: must be moved in a plugin |
|
121 if jid.short in self.watched and not jid.short in self.onlineContact: |
|
122 self.showAlert("Watched jid [%s] is connected !" % jid.short) |
|
123 |
|
124 self.onlineContact.add(jid) #FIXME onlineContact is useless with CM, must be removed |
|
125 self.CM.add(jid) |
|
126 self.contactList.replace(jid, show=show, status=status, name=name, group=group) |
|
127 |
|
128 |
|
129 if type=="unavailable" and jid.short in self.onlineContact: |
|
130 self.onlineContact.remove(jid.short) |
|
131 self.CM.remove(jid) |
|
132 self.contactList.remove(jid) |
|
133 |
|
134 |
|
135 def showDialog(self, message, title, type="info"): |
|
136 raise NotImplementedError |
|
137 |
|
138 def showAlert(self, message): |
|
139 pass #FIXME |
|
140 |
|
141 def paramUpdate(self, name, value, namespace): |
|
142 debug("param update: [%s] %s = %s", namespace, name, value) |
|
143 if (namespace,name) == ("Connection", "JabberID"): |
|
144 debug ("Changing ID to %s", value) |
|
145 self.whoami=JID(value) |
|
146 elif (namespace,name) == ("Misc", "Watched"): |
|
147 self.watched=value.split() |
|
148 |
|
149 def contactDeleted(self, jid): |
|
150 target = JID(jid) |
|
151 try: |
|
152 self.onlineContact.remove(target.short) |
|
153 except KeyError: |
|
154 pass |
|
155 self.contactList.remove(self.CM.get_full(jid)) |
|
156 self.CM.remove(target) |
|
157 |
|
158 def askConfirmation(self, type, id, data): |
|
159 raise NotImplementedError |