comparison browser_side/menu.py @ 78:12680e220b35

browser side: refactoring: menu is now in its own module
author Goffi <goffi@goffi.org>
date Sun, 19 Jun 2011 01:33:29 +0200
parents
children d57e0d6b1ced
comparison
equal deleted inserted replaced
77:975e6be24e11 78:12680e220b35
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.DialogBox import DialogBox
27 from pyjamas.ui.MenuBar import MenuBar
28 from pyjamas.ui.MenuItem import MenuItem
29 from pyjamas.ui.ListBox import ListBox
30 from pyjamas.ui.Label import Label
31 from pyjamas.ui.TextBox import TextBox
32 from pyjamas.ui.Button import Button
33 from pyjamas.ui.HTML import HTML
34 from pyjamas.ui.Frame import Frame
35 from pyjamas import Window
36 from jid import JID
37 from tools import html_sanitize
38 import dialog
39 import re
40
41 class MenuCmd:
42
43 def __init__(self, object, handler):
44 self._object = object
45 self._handler = handler
46
47 def execute(self):
48 handler = getattr(self._object, self._handler)
49 handler()
50
51 class LiberviaMenuBar(MenuBar):
52
53 def __init__(self):
54 MenuBar.__init__(self, vertical=False)
55
56 def doItemAction(self, item, fireCommand):
57 MenuBar.doItemAction(self, item, fireCommand)
58 if item == self.items[-1] and self.popup:
59 self.popup.setPopupPosition(Window.getClientWidth() -
60 self.popup.getOffsetWidth() - 22,
61 self.getAbsoluteTop() +
62 self.getOffsetHeight() - 1)
63 self.popup.addStyleName('menuLastPopup')
64
65
66 class Menu(SimplePanel):
67
68 def __init__(self, host):
69 self.host = host
70 SimplePanel.__init__(self)
71 self.setStyleName('menuContainer')
72
73 menu_general = MenuBar(vertical=True)
74 menu_general.addItem("Social contract", MenuCmd(self, "onSocialContract"))
75 menu_general.addItem("About", MenuCmd(self, "onAbout"))
76
77 menu_contacts = MenuBar(vertical=True)
78 menu_contacts.addItem("add contact", MenuCmd(self, "onAddContact"))
79 menu_contacts.addItem("update contact", MenuCmd(self, "onUpdateContact"))
80 menu_contacts.addItem("remove contact", MenuCmd(self, "onRemoveContact"))
81
82 menu_group = MenuBar(vertical=True)
83 menu_group.addItem("join room", MenuCmd(self, "onJoinRoom"))
84
85 menu_games = MenuBar(vertical=True)
86 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame"))
87 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame"))
88
89 menubar = LiberviaMenuBar()
90 menubar.addItem(MenuItem("General", menu_general))
91 menubar.addItem(MenuItem("Contacts", menu_contacts))
92 menubar.addItem(MenuItem("Groups", menu_group))
93 _separator = MenuItem('', None)
94 _separator.setStyleName('menuSeparator')
95 menubar.addItem(_separator, None)
96 menubar.addItem(MenuItem("Games", True, menu_games))
97 self.add(menubar)
98
99 #General menu
100 def onSocialContract(self):
101 _frame = Frame('contrat_social.html')
102 _frame.setStyleName('infoFrame')
103 _dialog = dialog.GenericDialog("Contrat Social", _frame)
104 _dialog.setSize('80%', '80%')
105 _dialog.show()
106
107 def onAbout(self):
108 _about = HTML("""<b>Libervia</b>, a Salut à Toi project<br />
109 <br />
110 You can contact the author at <a href="mailto:goffi@goffi.org">goffi@goffi.org</a><br />
111 Blog available (mainly in french) at <a href="http://www.goffi.org" target="_blank">http://www.goffi.org</a><br />
112 Project page: <a href="http://wiki.goffi.org/wiki/Salut_à_Toi"target="_blank">http://wiki.goffi.org/wiki/Salut_à_Toi</a><br />
113 <br />
114 Any help welcome :)
115 <p style='font-size:small;text-align:center'>This project is dedicated to Roger Poisson</p>
116 """)
117 _dialog = dialog.GenericDialog("About", _about)
118 _dialog.show()
119
120 #Contact menu
121 def onAddContact(self):
122 """Q&D contact addition"""
123 _dialog = None
124 edit = TextBox()
125
126 def addContactCb(sender):
127 if not re.match(r'^.+@.+\..+',edit.getText(), re.IGNORECASE):
128 Window.alert('You must enter a valid contact JID (like "contact@libervia.org")')
129 _dialog.show()
130 else:
131 self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() )
132
133 label = Label("new contact identifier (JID):")
134 edit.setText('@libervia.org')
135 edit.setWidth('100%')
136 _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [], addContactCb)
137 _dialog.setHTML('Adding contact')
138 _dialog.show()
139
140 def onUpdateContact(self):
141 _dialog = None
142 _contacts_list = ListBox()
143
144 def updateContactCb(sender):
145 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
146 self.host.bridge.call('updateContact', None, _jid, '', _dialog.getSelectedGroups())
147
148 def onContactChange(_list):
149 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
150 groups = self.host.contact_panel.getContactGroups(_jid)
151 _dialog.setGroupsSelected(groups)
152
153 for contact in self.host.contact_panel.getContacts():
154 _contacts_list.addItem(contact)
155 _contacts_list.addChangeListener(onContactChange)
156 _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex())
157 _selected_groups = self.host.contact_panel.getContactGroups(_jid)
158 _dialog = dialog.GroupSelector([Label('Which contact do you want to update ?'), _contacts_list],
159 self.host.contact_panel.getGroups(), _selected_groups, updateContactCb)
160 _dialog.setHTML('Updating contact')
161 _dialog.show()
162
163 def onRemoveContact(self):
164 _dialog = None
165 _contacts_list = ListBox()
166
167 def secondConfirmCb(confirm):
168 if confirm:
169 for contact in _contacts_list.getSelectedValues():
170 self.host.bridge.call('delContact', None, contact)
171 else:
172 _dialog.show()
173
174 def dialogCb(confirm):
175 if confirm:
176 html_list = ''.join(['<li>%s</li>' % html_sanitize(contact) for contact in _contacts_list.getSelectedValues()])
177 html_body = "Are you sure to remove the following contacts from your contact list ?<ul>%s</ul>" % html_list
178 dialog.ConfirmDialog(secondConfirmCb, html_body).show()
179
180 for contact in self.host.contact_panel.getContacts():
181 _contacts_list.addItem(contact)
182 _dialog = dialog.GenericConfirmDialog([_contacts_list], dialogCb, "Who do you want to remove from your contacts ?")
183 _dialog.show()
184
185 #Group menu
186 def onJoinRoom(self):
187 _dialog = None
188 _edit = None
189
190 def onOK(sender):
191 if not _edit.getText():
192 Window.alert('You must enter a room jid in the form libervia@conference.libervia.org')
193 if self.host.whoami:
194 nick = self.host.whoami.node
195 self.host.bridge.call('joinMUC', None, _edit.getText(), nick)
196 _dialog.hide()
197
198 def onCancel(sender):
199 _dialog.hide()
200
201 _main_panel = VerticalPanel()
202 _label = Label("Discussion room:")
203 _edit = TextBox()
204 _edit.setText('libervia@conference.libervia.org')
205 hpanel = HorizontalPanel()
206 hpanel.add(_label)
207 hpanel.add(_edit)
208 _main_panel.add(hpanel)
209 button_panel = HorizontalPanel()
210 button_panel.add(Button("Join", onOK))
211 button_panel.add(Button("Cancel", onCancel))
212 _main_panel.add(button_panel)
213 _dialog = DialogBox(centered=True)
214 _dialog.setHTML('Group discussions')
215 _dialog.setWidget(_main_panel)
216 _dialog.show()
217
218
219 #Game menu
220
221 def onTarotGame(self):
222 #Window.alert("Tarot selected")
223 #self.host.tab_panel.add(EmptyPanel(self.host), "Tarot")
224 def onPlayersSelected(other_players):
225 self.host.bridge.call('launchTarotGame', None, other_players)
226 dialog.ContactsChooser(self.host, onPlayersSelected, 3, text="Please select 3 other players").getContacts()
227
228 def onXiangqiGame(self):
229 Window.alert("A Xiangqi game is planed, but not available yet")