comparison browser_side/contact.py @ 17:c725b702e927

register.py and contact.py moved to new directory browser_side
author Goffi <goffi@goffi.org>
date Fri, 15 Apr 2011 15:18:38 +0200
parents contact.py@099c05a0dcab
children e8e3704eb97f
comparison
equal deleted inserted replaced
16:099c05a0dcab 17:c725b702e927
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Libervia: a Salut à Toi frontend
6 Copyright (C) 2011 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 Affero 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 Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.VerticalPanel import VerticalPanel
25 from pyjamas.ui.HorizontalPanel import HorizontalPanel
26 from pyjamas.ui.Label import Label
27 from pyjamas import Window
28 from pyjamas import DOM
29
30 from pyjamas.dnd import makeDraggable
31 from pyjamas.ui.DragWidget import DragWidget, DragContainer
32 from tools.jid import JID
33
34 class DragLabel(DragWidget):
35
36 def __init__(self, text, type):
37 DragWidget.__init__(self)
38 self._text = text
39 self._type = type
40
41 def onDragStart(self, event):
42 print "onDragStart"
43 dt = event.dataTransfer
44 #self.addMessage('types is %s' % dt.getTypes())
45 dt.setData('Text', self._text)
46 dt.setData('type', self._type)
47 #self.addMessage('after setting, len is %s' % len(dt.dataStore.items))
48 #self.addMessage('types is %s' % dt.getTypes())
49 dt.setDragImage(self.getElement(), 15, 15)
50 #dt.effectAllowed = 'copy'
51 #self.addMessage('mode is %s' % dt.dataStore.items.mode)
52
53 def onDragEnd(self, event):
54 print "onDragEnd"
55 #self.addMessage('Drag ended')
56 #self.addMessage('mode is %s' % dt._data.mode)
57
58 def addMessage(self, message):
59 print "addMessage"
60 #parent = self.getParent()
61 #while not hasattr(parent, 'addMessage'):
62 # parent = parent.getParent()
63 #parent.addMessage(message)
64
65 class GroupLabel(DragLabel, Label):
66 def __init__(self, group):
67 self.group = group
68 Label.__init__(self, group) #, Element=DOM.createElement('div')
69 self.setStyleName('group')
70 DragLabel.__init__(self, group, "GROUP")
71
72
73 class ContactLabel(Label):
74 def __init__(self, jid, name=None):
75 if not name:
76 name=jid
77 Label.__init__(self, name)
78 self.jid=jid
79 self.setStyleName('contact')
80
81 class GroupList(VerticalPanel):
82
83 def __init__(self, parent):
84 VerticalPanel.__init__(self)
85 self._parent = parent
86
87 def add(self, group):
88 _item = GroupLabel(group)
89 _item.addMouseListener(self._parent)
90 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
91 VerticalPanel.add(self, _item)
92
93 class ContactList(VerticalPanel):
94
95 def __init__(self):
96 VerticalPanel.__init__(self)
97 self.contacts=[]
98
99 def __iter__(self):
100 return self.contacts.__iter__()
101
102 def add(self, jid, name=None):
103 _item = ContactLabel(jid, name)
104 DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
105 VerticalPanel.add(self, _item)
106 self.contacts.append(_item)
107
108 class ContactTitleLabel(DragLabel, Label):
109 def __init__(self, text):
110 Label.__init__(self, text) #, Element=DOM.createElement('div')
111 self.setStyleName('contactTitle')
112 DragLabel.__init__(self, text, "CONTACT")
113
114 class ContactPanel(SimplePanel):
115 """Manage the contacts and groups"""
116
117 def __init__(self, host):
118 SimplePanel.__init__(self)
119 self.host = host
120 self.groups={}
121
122 self.vPanel = VerticalPanel()
123 _title = ContactTitleLabel('Contacts')
124 DOM.setStyleAttribute(_title.getElement(), "cursor", "pointer")
125
126 self._contactList = ContactList()
127 self._contactList.setStyleName('contactList')
128 self._groupList = GroupList(self)
129 self._groupList.setStyleName('groupList')
130
131 self.vPanel.add(_title)
132 self.vPanel.add(self._groupList)
133 self.vPanel.add(self._contactList)
134 self.add(self.vPanel)
135 self.setStyleName('contactBox')
136
137 def addContact(self, jid, attributes, groups):
138 """Add a contact to the panel
139 @param jid: jid
140 @attributes: cf SàT Bridge API's newContact
141 @param groups: list of groups"""
142 for group in groups:
143 if not self.groups.has_key(group):
144 self.groups[group] = set()
145 self._groupList.add(group)
146 self.host.uniBox.addKey("@%s: " % group)
147 self.groups[group].add(jid)
148 self._contactList.add(jid)
149
150 def isContactInGroup(self, group, contact_jid):
151 """Test if the contact_jid is in the group
152 @param group: string of single group, or list of string
153 @param contact_jid: jid to test
154 @return: True if contact_jid is in on of the groups"""
155 print "isContactInGroup: %s, %s" % (group, contact_jid)
156 print JID(contact_jid)
157 print self.groups[group]
158 if self.groups.has_key(group) and contact_jid in self.groups[group]:
159 return True
160 return False
161
162 def onMouseMove(self, sender, x, y):
163 pass
164
165 def onMouseDown(self, sender, x, y):
166 pass
167
168 def onMouseUp(self, sender, x, y):
169 pass
170
171 def onMouseEnter(self, sender):
172 if isinstance(sender, GroupLabel):
173 for contact in self._contactList:
174 if contact.jid in self.groups[sender.group]:
175 contact.addStyleName("selected")
176
177 def onMouseLeave(self, sender):
178 if isinstance(sender, GroupLabel):
179 for contact in self._contactList:
180 if contact.jid in self.groups[sender.group]:
181 contact.removeStyleName("selected")