comparison browser_side/contact_group.py @ 256:0e7f3944bd27

browser_side: added contact group manager based on ListManager
author souliane <souliane@mailoo.org>
date Sat, 09 Nov 2013 09:39:45 +0100
parents
children d3c734669577
comparison
equal deleted inserted replaced
255:da0487f0a2e7 256:0e7f3944bd27
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Libervia: a Salut à Toi frontend
6 Copyright (C) 2013 Adrien Cossa <souliane@mailoo.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 from pyjamas.ui.FlexTable import FlexTable
23 from browser_side.dialog import ConfirmDialog
24 from list_manager import ListManager
25 import contact
26 from pyjamas.Timer import Timer
27 from pyjamas.ui.Button import Button
28 from pyjamas.ui.HorizontalPanel import HorizontalPanel
29 from pyjamas.ui.VerticalPanel import VerticalPanel
30 from pyjamas.ui.DialogBox import DialogBox
31 import dialog
32
33
34 class ContactGroupManager(ListManager):
35 """A manager for sub-panels to assign contacts to each group."""
36
37 def __init__(self, parent, keys_dict, contact_list, offsets, style):
38 ListManager.__init__(self, parent, keys_dict, contact_list, offsets, style)
39 self.registerPopupMenuPanel(entries={"Remove group": {}},
40 callback=lambda sender, key: Timer(5, lambda: self.removeContactKey(sender, key)))
41
42 def removeContactKey(self, sender, key):
43 key = sender.getText()
44
45 def confirm_cb(answer):
46 if answer:
47 (y, x) = self._parent.getIndex(self.__children[key]["button"])
48 self._parent.removeCell(y, x + 1)
49 self._parent.removeCell(y, x)
50 del self.__keys_dict[key]
51 del self.__children[key]
52 self._parent.add_group_panel.groups.remove(key)
53
54 _dialog = ConfirmDialog(confirm_cb, text="Do you really want to delete the group '%s'?" % key)
55 _dialog.show()
56
57 def removeFromRemainingList(self, contact_):
58 ListManager.removeFromRemainingList(self, contact_)
59 self._parent.updateContactList(contact_=contact_)
60
61 def addToRemainingList(self, contact_):
62 ListManager.addToRemainingList(self, contact_)
63 self._parent.updateContactList(contact_=contact_)
64
65
66 class ContactGroupEditor(FlexTable):
67 """Panel for the contact groups manager."""
68
69 def __init__(self, host, parent=None, onCloseCallback=None):
70 # This must be done before FlexTable.__init__ because it is used by setVisible
71 self.host = host
72 if parent is None:
73 parent = DialogBox(autoHide=False, centered=True)
74 parent.setHTML("Manage contact groups")
75
76 self._parent = parent
77 self._on_close_callback = onCloseCallback
78
79 groups_list = self.host.contact_panel.groups.keys()
80 groups_list.sort()
81 FlexTable.__init__(self, len(groups_list) + 2, 3)
82 self.addStyleName('contactGroupEditor')
83
84 def cb(text):
85 nb_keys = len(self.groups.keys)
86 self.getFlexCellFormatter().setColSpan(nb_keys + 1, 0, 1)
87 self.getFlexCellFormatter().setColSpan(nb_keys + 2, 0, 1)
88 self.remove(self.add_group_panel)
89 self.remove(self.command)
90 self.groups.addContactKey(text)
91 refresh()
92
93 # overwrite the default style which has been set for rich text editor
94 style = {
95 "keyItem": "group",
96 "popupMenuItem": "popupMenuItem",
97 "removeButton": "contactGroupRemoveButton",
98 "buttonCell": "contactGroupButtonCell",
99 "keyPanel": "contactGroupPanel"
100 }
101 self.all_contacts = self.host.contact_panel.getContacts()
102 self.groups = ContactGroupManager(self, groups_list, self.all_contacts, style=style)
103 self.groups.createWidgets()
104
105 self.add_group_panel = dialog.AddGroupPanel(groups_list, cb)
106 self.add_group_panel.addStyleName("addContactGroupPanel")
107
108 self.command = HorizontalPanel()
109 self.command.addStyleName("marginAuto")
110 self.command.add(Button("Cancel", listener=self.cancelWithoutSaving))
111 self.command.add(Button("Save", listener=self.closeAndSave))
112
113 contact_panel = VerticalPanel()
114
115 # checkbox has been replaced by a button
116 self.checkbox = Button("", self.toggleContacts)
117 self.checkbox.getChecked = lambda: self.checkbox.checked if hasattr(self.checkbox, "checked") else None
118 self.checkbox.addStyleName("toggleAssignedContacts")
119 contact_panel.add(self.checkbox)
120 self.contacts = contact.GenericContactList(host)
121 contact_panel.add(self.contacts)
122 for contact in self.all_contacts:
123 self.contacts.add(contact)
124 self.setWidget(0, 2, contact_panel)
125
126 def refresh():
127 nb_keys = len(self.groups.keys)
128 self.getFlexCellFormatter().setColSpan(nb_keys + 1, 0, 2) # add group panel
129 self.setWidget(nb_keys + 1, 0, self.add_group_panel)
130 self.getFlexCellFormatter().setColSpan(nb_keys + 2, 0, 3) # buttons panel
131 self.setWidget(nb_keys + 2, 0, self.command)
132 self.getFlexCellFormatter().setRowSpan(0, 2, nb_keys + 2) # contact list
133
134 self.groups.setContacts(self.host.contact_panel.groups)
135 refresh()
136 self.restore_contact_panel = False
137 if self.host.contact_panel.getVisible():
138 self.restore_contact_panel = True
139 self.host.panel._contactsSwitch()
140 self.toggleContacts()
141 parent.add(self)
142 parent.setVisible(True)
143 if isinstance(parent, DialogBox):
144 parent.center()
145
146 def toggleContacts(self, sender=None):
147 if sender is None:
148 sender = self.checkbox
149 if sender.getChecked():
150 sender.checked = False
151 sender.setText("Hide assigned")
152 else:
153 sender.checked = True
154 sender.setText("Show assigned")
155 self.updateContactList(sender)
156
157 def updateContactList(self, sender=None, contact_=None):
158 sender = self.checkbox
159 if sender.getChecked() is None:
160 # do not update during initialization
161 return
162 if contact_ is not None:
163 if contact_ not in self.all_contacts or not sender.getChecked():
164 return
165 all_contacts = [contact_]
166 else:
167 all_contacts = self.all_contacts
168 for contact_ in all_contacts:
169 if sender.getChecked():
170 if contact_ in self.groups.remaining_list:
171 self.contacts.getContactLabel(contact_).setVisible(True)
172 else:
173 self.contacts.getContactLabel(contact_).setVisible(False)
174 else:
175 self.contacts.getContactLabel(contact_).setVisible(True)
176
177 def __close(self):
178 """Remove the widget from parent or close the popup."""
179 if isinstance(self._parent, DialogBox):
180 self._parent.hide()
181 self._parent.remove(self)
182 if self._on_close_callback is not None:
183 self._on_close_callback()
184 if self.restore_contact_panel:
185 self.host.panel._contactsSwitch()
186
187 def cancelWithoutSaving(self):
188 """Ask for confirmation before closing the dialog."""
189 def confirm_cb(answer):
190 if answer:
191 self.__close()
192
193 _dialog = ConfirmDialog(confirm_cb, text="Do you really want to cancel without saving?")
194 _dialog.show()
195
196 def closeAndSave(self):
197 map_ = {}
198 for contact_ in self.all_contacts:
199 map_[contact_] = set()
200 contacts = self.groups.getContacts()
201 for group in contacts.keys():
202 for contact_ in contacts[group]:
203 map_[contact_].add(group)
204 for contact_ in map_.keys():
205 groups = map_[contact_]
206 current_groups = self.host.contact_panel.getContactGroups(contact_)
207 if groups != current_groups:
208 self.host.bridge.call('updateContact', None, contact_, '', list(groups))
209 self.__close()