Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/poll.py
blob: f4a96a97f832c5d43ec55457c55718d66f39997b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
# Copyright 2007 World Wide Workshop Foundation
# Copyright 2007 Collabora Ltd
# Copyright 2008 Morgan Collett
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# If you find this activity useful or end up using parts of it in one of
# your own creations we would love to hear from you at
# info@WorldWideWorkshop.org !
#

# init gthreads before using abiword
import gobject
gobject.threads_init()

import os
import cPickle
import gtk
import hippo
import pango
import locale
import logging
from datetime import date
from gettext import gettext as _
import telepathy
import telepathy.client
from dbus import Interface
from dbus.service import method, signal
from dbus.gobject_service import ExportedGObject
from sugar.presence.tubeconn import TubeConnection

try:
    from hashlib import sha1
except ImportError:
    # Python < 2.5
    from sha import new as sha1

from sugar.activity import activity
from sugar.graphics import style
try:
    from sugar.graphics.alert import NotifyAlert
except:
    pass  # FIXME remove this once compatibility with Trial 3 not required
from sugar.presence import presenceservice
from abiword import Canvas as AbiCanvas

SERVICE = "org.worldwideworkshop.olpc.PollBuilder"
IFACE = SERVICE
PATH = "/org/worldwideworkshop/olpc/PollBuilder"

# Theme definitions - colors
LIGHT_GREEN = '#66CC00'
DARK_GREEN = '#027F01'
PINK = '#FF0198'
YELLOW = '#FFFF00'
GRAY = '#ACACAC'
LIGHT_GRAY = '#E2E2E3'
RED = '#FF0000'
PAD = 10

COLOR_FG_BUTTONS = (
    (gtk.STATE_NORMAL,"#CCFF99"),
    (gtk.STATE_ACTIVE,"#CCFF99"),
    (gtk.STATE_PRELIGHT,"#CCFF99"),
    (gtk.STATE_SELECTED,"#CCFF99"),
    (gtk.STATE_INSENSITIVE,"#CCFF99"),
    )
COLOR_BG_BUTTONS = (
    (gtk.STATE_NORMAL,"#027F01"),
    (gtk.STATE_ACTIVE,"#014D01"),
    (gtk.STATE_PRELIGHT,"#016D01"),
    (gtk.STATE_SELECTED,"#027F01"),
    (gtk.STATE_INSENSITIVE,"#027F01"),
    )
COLOR_BG_RADIOBUTTONS = (
    (gtk.STATE_NORMAL,LIGHT_GRAY),
    (gtk.STATE_ACTIVE,LIGHT_GRAY),
    (gtk.STATE_PRELIGHT,LIGHT_GRAY),
    (gtk.STATE_SELECTED,LIGHT_GRAY),
    (gtk.STATE_INSENSITIVE,LIGHT_GRAY),
    )
COLOR_FG_RADIOBUTTONS = (
    (gtk.STATE_NORMAL,DARK_GREEN),
    (gtk.STATE_ACTIVE,DARK_GREEN),
    (gtk.STATE_PRELIGHT,DARK_GREEN),
    (gtk.STATE_SELECTED,DARK_GREEN),
    (gtk.STATE_INSENSITIVE,DARK_GREEN),
    )

GRAPH_WIDTH = gtk.gdk.screen_width() / 3
GRAPH_TEXT_WIDTH = 50
RADIO_SIZE = 32

def theme_button(btn, w=-1, h=-1, highlight=False):
    """Apply colors to gtk Buttons
    
    btn is the button
    w and h are optional width and height for resizing the button
    highlight is a boolean to override the theme and apply a
        different color to show "you are here".

    returns the modified button.
    """
    for state, color in COLOR_BG_BUTTONS:
        if highlight:
            btn.modify_bg(state, gtk.gdk.color_parse("#CCFF99"))
        else:
            btn.modify_bg(state, gtk.gdk.color_parse(color))
    c = btn.get_child()
    if c is not None:
        for state, color in COLOR_FG_BUTTONS:
            if highlight:
                c.modify_fg(state, gtk.gdk.color_parse(DARK_GREEN))
            else:
                c.modify_fg(state, gtk.gdk.color_parse(color))
    else:
        for state, color in COLOR_FG_BUTTONS:
            btn.modify_fg(state, gtk.gdk.color_parse(color))
    if w>0 or h>0:
        btn.set_size_request(w, h)
    return btn

def theme_radiobutton(btn):
    """Apply colors and font to gtk RadioButtons
    
    btn -- gtk RadioButton

    returns the modified button.
    """
    for state, color in COLOR_BG_RADIOBUTTONS:
        btn.modify_bg(state, gtk.gdk.color_parse(color))
    c = btn.get_child()
    if c is not None:
        for state, color in COLOR_FG_RADIOBUTTONS:
            c.modify_fg(state, gtk.gdk.color_parse(color))
    else:
        for state, color in COLOR_FG_RADIOBUTTONS:
            btn.modify_fg(state, gtk.gdk.color_parse(color))
    return btn


class PollBuilder(activity.Activity):
    """Sugar activity for polls

    Poll implements a simple tool that allows children to express
    their opinions on a given topic by selecting one of five
    answer choices and submitting a vote. The results are tallied
    by total number of votes and percentage of total votes cast.
    
    A future version of this activity will be networked over the
    OLPC mesh to allow sharing of the poll.
    
    """
    def __init__(self, handle):
        activity.Activity.__init__(self, handle)

        self._logger = logging.getLogger('poll-activity')
        self._logger.debug('Starting Poll activity')

        # get the Presence Service
        self.pservice = presenceservice.get_instance()
        self.initiating = False
        
        # Buddy object for you
        owner = self.pservice.get_owner()
        self.owner = owner
        self.nick = owner.props.nick
        self.nick_sha1 = sha1(self.nick).hexdigest()

        self._basepath = activity.get_bundle_path()
        os.chdir(self._basepath)  # required for i18n.py to work

        # setup example poll
        self._polls = set()
        # Removed default polls since it creates too much noise
        # when shared with many on the mesh
        #self._make_default_poll()
        self._has_voted = False
        self._previewing = False
        self._current_view = None  # so we can switch back

        # Lesson plan widget
        self._lessonplan_widget = None

        toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()

        # Show poll screen
        # Setup screen
        self._root = hippo.CanvasBox(orientation = hippo.ORIENTATION_VERTICAL)
        canvas = hippo.Canvas()
        canvas.set_root(self._root)
        self.set_canvas(canvas)
        self.show_all()

        self.set_root(self._select_canvas())

        self.poll_session = None  # PollSession
        self.connect('shared', self._shared_cb)
        self.connect('joined', self._joined_cb)

    def set_root(self, hippo_widget):
        self._root.clear()
        self._root.append(hippo_widget, hippo.PACK_EXPAND)

    def read_file(self, file_path):
        """Implement reading from journal
        
        This is called within sugar.activity.Activity code
        which provides file_path.
        """
        self._logger.debug('Reading file from datastore via Journal: %s' %
                           file_path)
        self._polls = set()
        f = open(file_path, 'r')
        num_polls = cPickle.load(f)
        for p in range(num_polls):
            title = cPickle.load(f)
            author = cPickle.load(f)
            active = cPickle.load(f)
            createdate_i = cPickle.load(f)
            maxvoters = cPickle.load(f)
            question = cPickle.load(f)
            number_of_options = cPickle.load(f)
            options = cPickle.load(f)
            data = cPickle.load(f)
            votes = cPickle.load(f)
            poll = Poll(self, title, author, active, 
                        date.fromordinal(int(createdate_i)),
                        maxvoters, question, number_of_options, options,
                        data, votes)
            self._polls.add(poll)
        f.close()

    def write_file(self, file_path):
        """Implement writing to the journal

        This is called within sugar.activity.Activity code
        which provides the file_path.
        """
        s = cPickle.dumps(len(self._polls))
        for poll in self._polls:
            s += poll.dump()
        f = open(file_path, 'w')
        f.write(s)
        f.close()

    def alert(self, title, text=None):
        """Show an alert above the activity."""
        # FIXME: remove try/except once compatibility with Trial 3 is
        #        no longer required
        try:
            alert = NotifyAlert(timeout=10)
        except NameError:
            return
        alert.props.title = title
        alert.props.msg = text
        self.add_alert(alert)
        alert.connect('response', self._alert_cancel_cb)
        alert.show()

    def _alert_cancel_cb(self, alert, response_id):
        """Callback for alert events"""
        self.remove_alert(alert)

    def _poll_canvas(self):
        """Show the poll canvas where children vote on an existing poll."""
        self._current_view = 'poll'
        canvasbox = self._canvas_root()

        # pollbuilderbox is centered within canvasbox
        pollbuilderbox = self._canvas_pollbuilder_box()
        canvasbox.append(pollbuilderbox, hippo.PACK_EXPAND)

        pollbuilderbox.append(self._canvas_topbox())

        mainbox = self._canvas_mainbox()
        pollbuilderbox.append(mainbox, hippo.PACK_EXPAND)

        if not self._previewing:
            mainbox.append(self._text_mainbox(_('VOTE!')))
        else:
            mainbox.append(self._text_mainbox(_('Poll Preview')))

        poll_details_box = hippo.CanvasBox(spacing=8,
            background_color=style.COLOR_WHITE.get_int(),
            border=4,
            border_color=style.Color(PINK).get_int(),
            padding=PAD*2,
            orientation=hippo.ORIENTATION_VERTICAL)
        mainbox.append(poll_details_box, hippo.PACK_EXPAND)

        self.poll_details_box_head = hippo.CanvasBox(
            orientation=hippo.ORIENTATION_VERTICAL)
        poll_details_box.append(self.poll_details_box_head)

        self.poll_details_box = hippo.CanvasBox(
            orientation=hippo.ORIENTATION_VERTICAL)
        poll_details_scroll = hippo.CanvasScrollbars()
        poll_details_scroll.set_policy(
            hippo.ORIENTATION_HORIZONTAL, hippo.SCROLLBAR_NEVER)
        poll_details_scroll.set_root(self.poll_details_box)
        poll_details_box.append(poll_details_scroll, hippo.PACK_EXPAND)

        self.poll_details_box_tail = hippo.CanvasBox(
            orientation=hippo.ORIENTATION_HORIZONTAL)
        poll_details_box.append(self.poll_details_box_tail)

        self.current_vote = None
        self.draw_poll_details_box()

        button_box = self._canvas_buttonbox()
        mainbox.append(button_box, hippo.PACK_END)

        return canvasbox

    def _select_canvas(self):
        """Show the select canvas where children choose an existing poll."""
        self._current_view = 'select'
        canvasbox = self._canvas_root()

        # pollbuilderbox is centered within canvasbox
        pollbuilderbox = self._canvas_pollbuilder_box()
        canvasbox.append(pollbuilderbox, hippo.PACK_EXPAND)

        pollbuilderbox.append(self._canvas_topbox())

        mainbox = self._canvas_mainbox()
        pollbuilderbox.append(mainbox, hippo.PACK_EXPAND)

        mainbox.append(self._text_mainbox(_('Choose a Poll')))

        poll_details_box = hippo.CanvasBox(spacing=8,
            background_color=style.COLOR_WHITE.get_int(),
            border=4,
            border_color=style.Color(PINK).get_int(),  # XXXX
            padding=PAD,
            orientation=hippo.ORIENTATION_VERTICAL)
        mainbox.append(poll_details_box, hippo.PACK_EXPAND)

        # add scroll window
        scrolledwindow = hippo.CanvasScrollbars()
        scrolledwindow.set_policy(
            hippo.ORIENTATION_HORIZONTAL, hippo.SCROLLBAR_NEVER)

        poll_selector_box = hippo.CanvasBox(
            background_color=style.COLOR_WHITE.get_int(),
            orientation=hippo.ORIENTATION_VERTICAL)
        scrolledwindow.set_root(poll_selector_box)
        poll_details_box.append(scrolledwindow,
                                hippo.PACK_EXPAND)

        row_number = 0
        for poll in self._polls:
            sha = poll.sha
            if row_number % 2:
                row_bgcolor=style.COLOR_WHITE.get_int()
            else:
                row_bgcolor=style.COLOR_SELECTION_GREY.get_int()
            row_number += 1
            poll_row = hippo.CanvasBox(
                padding_top=4, padding_bottom=4,
                spacing=PAD,
                background_color=row_bgcolor,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            poll_selector_box.append(poll_row)

            sized_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_HORIZONTAL)
            poll_row.append(sized_box, hippo.PACK_EXPAND)
            title = hippo.CanvasText(
                text=poll.title+' ('+poll.author+')',
                size_mode=hippo.CANVAS_SIZE_ELLIPSIZE_END,
                xalign=hippo.ALIGNMENT_START,
                color=style.Color(DARK_GREEN).get_int())
            sized_box.append(title, hippo.PACK_EXPAND)

            sized_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_HORIZONTAL)
            poll_row.append(sized_box)
            if poll.active:
                button = gtk.Button(_('VOTE'))
            else:
                button = gtk.Button(_('SEE RESULTS'))
            button.connect('clicked', self._select_poll_button_cb, sha)
            sized_box.append(hippo.CanvasWidget(widget=theme_button(button)))

            sized_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_HORIZONTAL)
            poll_row.append(sized_box)
            if poll.author == self._pservice.get_owner().props.nick:
                button = gtk.Button(_('DELETE'))
                button.connect('clicked', self._delete_poll_button_cb, sha)
                sized_box.append(hippo.CanvasWidget(widget=theme_button(button)))
            poll_row.append(hippo.CanvasText(
                text=poll.createdate.strftime('%d/%m/%y'),
                color=style.Color(DARK_GREEN).get_int()))

        button_box = self._canvas_buttonbox(button_to_highlight=2)
        mainbox.append(button_box, hippo.PACK_END)

        return canvasbox

    def _lessonplan_canvas(self):
        """Show the select canvas where children choose an existing poll."""
        previous_view = self._current_view
        self._current_view = 'lessonplan'
        canvasbox = self._canvas_root()

        # pollbuilderbox is centered within canvasbox
        pollbuilderbox = self._canvas_pollbuilder_box()
        canvasbox.append(pollbuilderbox, hippo.PACK_EXPAND)

        pollbuilderbox.append(self._canvas_topbox(lesson_return=previous_view))

        mainbox = self._canvas_mainbox()
        pollbuilderbox.append(mainbox, hippo.PACK_EXPAND)

        mainbox.append(self._text_mainbox(_('Lesson Plans')))

        poll_details_box = hippo.CanvasBox(spacing=8,
            background_color=style.COLOR_WHITE.get_int(),
            border=4,
            border_color=style.Color(PINK).get_int(),
            padding=PAD,
            orientation=hippo.ORIENTATION_VERTICAL)
        mainbox.append(poll_details_box, hippo.PACK_EXPAND)

        lessonplan = LessonPlanWidget(self._basepath)
        self._lessonplan_widget = lessonplan
        poll_details_box.append(hippo.CanvasWidget(widget=lessonplan),
                                hippo.PACK_EXPAND)

        button_box = self._canvas_buttonbox()
        mainbox.append(button_box, hippo.PACK_END)

        return canvasbox

    def _select_poll_button_cb(self, button, sha=None):
        """A VOTE or SEE RESULTS button was clicked."""
        if not sha:
            self._logger.debug('Strange, which button was clicked?')
            return
        self._switch_to_poll(sha)
        self._has_voted = False
        self.set_root(self._poll_canvas())
        self.show_all()

    def _delete_poll_button_cb(self, button, sha=None):
        """A DELETE button was clicked."""
        if not sha:
            self._logger.debug('Strange, which button was clicked?')
            return
        self.delete_poll(sha)
        self.set_root(self._select_canvas())
        self.show_all()

    def delete_poll(self, sha=None, poll=None):
        """Delete a poll, either by passing sha or the actual poll object.

        sha -- string, sha property of the poll
        poll -- Poll
        """
        if poll:
            if self._poll == poll:
                self._make_blank_poll
            self._polls.remove(poll)
        if sha:
            if self._poll.sha == sha:
                self._logger.debug('delete_poll: removing current poll')
                self._make_blank_poll()
            for poll in self._polls.copy():
                if poll.sha == sha:
                    self._polls.remove(poll)
        
    def draw_poll_details_box(self):
        """(Re)draw the poll details box
        
        self.poll_details_box should be already defined on the canvas.
        """
        poll_details_box = self.poll_details_box
        poll_details_box.remove_all()
        self.poll_details_box_head.remove_all()
        self.poll_details_box_tail.remove_all()

        votes_total = self._poll.vote_count

        title = hippo.CanvasText(
            text=self._poll.title,
            xalign=hippo.ALIGNMENT_START,
            color=style.Color(DARK_GREEN).get_int())
        title.props.size_mode = 'wrap-word'
        self.poll_details_box_head.append(title)
        question = hippo.CanvasText(
            text=self._poll.question,
            xalign=hippo.ALIGNMENT_START,
            color=style.Color(DARK_GREEN).get_int())
        question.props.size_mode = 'wrap-word'
        self.poll_details_box_head.append(question)

        answer_box = hippo.CanvasBox(
            orientation=hippo.ORIENTATION_VERTICAL)

        answer_box = hippo.CanvasBox(spacing=8,
            background_color=style.COLOR_WHITE.get_int(),
            padding=PAD,
            orientation=hippo.ORIENTATION_VERTICAL)
        poll_details_box.append(answer_box, hippo.PACK_EXPAND)

        group = gtk.RadioButton()  # required for radio button group

        for choice in range(self._poll.number_of_options):
            self._logger.debug(self._poll.options[choice])

            answer_row = hippo.CanvasBox(spacing=8,
                    orientation=hippo.ORIENTATION_HORIZONTAL)

            radio_box = hippo.CanvasBox(
                    box_width = RADIO_SIZE + RADIO_SIZE/2,
                    box_height = RADIO_SIZE,
                    orientation = hippo.ORIENTATION_HORIZONTAL)
            answer_row.append(radio_box)

            if self._poll.active:
                button = gtk.RadioButton(group, '')
                button.connect('toggled', self.vote_choice_radio_button, choice)
                radio_box.append(hippo.CanvasWidget(
                        widget = theme_radiobutton(button)),
                        hippo.PACK_EXPAND)
                if choice == self.current_vote:
                    button.set_active(True)

            answer_row.append(hippo.CanvasText(
                    text = self._poll.options[choice],
                    color = style.Color(DARK_GREEN).get_int(),
                    xalign=hippo.ALIGNMENT_START,
                    size_mode = 'wrap-word'),
                    hippo.PACK_EXPAND)

            if votes_total > 0:
                self._logger.debug(str(self._poll.data[choice] * 1.0 / votes_total))

                graph_box = hippo.CanvasBox(
                        box_width = GRAPH_WIDTH,
                        orientation = hippo.ORIENTATION_HORIZONTAL)
                answer_row.append(graph_box)

                graph_box.append(hippo.CanvasText(
                        text=justify(self._poll.data, choice),
                        xalign=hippo.ALIGNMENT_END,
                        padding_right = 2,
                        color=style.Color(DARK_GREEN).get_int(),
                        box_width = GRAPH_TEXT_WIDTH))


                graph_box.append(hippo.CanvasBox(
                        orientation=hippo.ORIENTATION_HORIZONTAL,
                        background_color=style.Color(PINK).get_int(),
                        box_width = int(float(self._poll.data[choice]) *
                            (GRAPH_WIDTH - GRAPH_TEXT_WIDTH*2) / votes_total)))

                graph_box.append(hippo.CanvasText(
                        text=str(self._poll.data[choice] * 100 / votes_total)+'%',
                        xalign=hippo.ALIGNMENT_START,
                        padding_left = 2,
                        color=style.Color(DARK_GREEN).get_int(),
                        box_width = GRAPH_TEXT_WIDTH))

            answer_box.append(answer_row)

        if (self._poll.active and self._has_voted) or\
            not self._poll.active:

            # Line above total
            line_box = hippo.CanvasBox(
                xalign=hippo.ALIGNMENT_END,
                spacing=8,
                box_height=4,
                padding_left = GRAPH_TEXT_WIDTH,
                padding_right = GRAPH_TEXT_WIDTH,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            line = hippo.CanvasBox(
                background_color=style.Color(DARK_GREEN).get_int(),
                box_width = GRAPH_WIDTH - GRAPH_TEXT_WIDTH*2,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            line_box.append(line)
            answer_box.append(line_box)

            # total votes
            totals_box = hippo.CanvasBox(
                xalign=hippo.ALIGNMENT_END,
                box_width = GRAPH_WIDTH,
                spacing=8,
                padding_left = GRAPH_TEXT_WIDTH,
                padding_right = GRAPH_TEXT_WIDTH,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            answer_box.append(totals_box)

            spacer = hippo.CanvasBox(
                box_width=100, orientation=hippo.ORIENTATION_VERTICAL)

            spacer.append(hippo.CanvasText(
                text=str(votes_total),
                xalign=hippo.ALIGNMENT_END,
                color=style.Color(DARK_GREEN).get_int()))
            totals_box.append(spacer)

            totals_box.append(hippo.CanvasText(
                text=' '+_('votes'),
                xalign=hippo.ALIGNMENT_START,
                color=style.Color(DARK_GREEN).get_int()))
            if votes_total < self._poll.maxvoters:
                totals_box.append(hippo.CanvasText(
                    text=' ('+str(self._poll.maxvoters-votes_total)+
                         ' votes left to collect)',
                    color=style.Color(DARK_GREEN).get_int()))

        # Button area
        if self._poll.active and not self._previewing:
            button_box = hippo.CanvasBox(spacing=8,
                padding = 8,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            button = gtk.Button(_("Vote"))
            button.connect('clicked', self._button_vote_cb)
            button_box.append(hippo.CanvasWidget(widget=theme_button(button)))
            self.poll_details_box_tail.append(button_box)
        elif self._previewing:
            button_box = hippo.CanvasBox(spacing=8,
                padding = 8,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            button = gtk.Button(_("Edit Poll"))
            button.connect('clicked', self.button_edit_clicked)
            button_box.append(hippo.CanvasWidget(widget=theme_button(button)))
            button = gtk.Button(_("Save Poll"))
            button.connect('clicked', self._button_save_cb)
            button_box.append(hippo.CanvasWidget(widget=theme_button(button)))
            self.poll_details_box_tail.append(button_box)

    def vote_choice_radio_button(self, widget, data=None):
        """Track which radio button has been selected

        This is connected to the vote choice radio buttons.
        data contains the choice (0 - 4) selected.
        """
        self.current_vote = data

    def _button_vote_cb(self, button):
        """Register a vote

        Take the selected option from self.current_vote
        and increment the poll_data.
        """
        if self.current_vote is not None:
            if self._poll.vote_count >= self._poll.maxvoters:
                self._logger.debug(
                    'Hit the max voters, ignoring this vote.')
                return
            self._logger.debug('Voted '+str(self.current_vote))
            self._has_voted = True
            try:
                self._poll.register_vote(self.current_vote, self.nick_sha1)
            except OverflowError:
                self._logger.debug('Local vote failed: '
                    'maximum votes already registered.')
            except ValueError:
                self._logger.debug('Local vote failed: '
                    'poll closed.')
            self._logger.debug('Results: '+str(self._poll.data))
            self.draw_poll_details_box()

    def button_select_clicked(self, button):
        """Show Choose a Poll canvas"""
        self.set_root(self._select_canvas())
        self.show_all()

    def button_new_clicked(self, button):
        """Show Build a Poll canvas.
        """
        # Reset vote data to 0
        self._make_blank_poll()
        owner = self._pservice.get_owner()
        self._poll.author = owner.props.nick
        self._poll.active = False
        self.set_root(self._build_canvas())
        self.show_all()

    def button_edit_clicked(self, button):
        """Go back from preview to edit"""
        self.set_root(self._build_canvas())
        self.show_all()

    def _build_canvas(self, editing=False, highlight=[]):
        """Show the canvas to set up a new poll.
        
        editing is False to start a new poll, or
        True to edit the current poll

        highlight is a list of strings denoting items failing validation.
        """
        self._current_view = 'build'
        canvasbox = self._canvas_root()

        # pollbuilderbox is centered within canvasbox
        pollbuilderbox = self._canvas_pollbuilder_box()
        canvasbox.append(pollbuilderbox, hippo.PACK_EXPAND)

        pollbuilderbox.append(self._canvas_topbox())

        mainbox = self._canvas_mainbox()
        pollbuilderbox.append(mainbox, hippo.PACK_EXPAND)

        mainbox.append(self._text_mainbox(_('Build a Poll')))

        poll_details_box = hippo.CanvasBox(spacing=8,
            background_color=style.COLOR_WHITE.get_int(),
            border=4,
            border_color=style.Color(PINK).get_int(),
            padding=PAD,
            orientation=hippo.ORIENTATION_VERTICAL)
        mainbox.append(poll_details_box, hippo.PACK_EXPAND)

        buildbox = hippo.CanvasBox(spacing=8,
            #xalign=hippo.ALIGNMENT_CENTER,
            orientation=hippo.ORIENTATION_VERTICAL)
        poll_details_box.append(buildbox, hippo.PACK_EXPAND)
        
        hbox = hippo.CanvasBox(spacing=8,
            orientation=hippo.ORIENTATION_HORIZONTAL)
        hbox.append(self._text_mainbox(_('Poll Title:'),
                                       warn='title' in highlight))
        entrybox = gtk.Entry()
        entrybox.set_text(self._poll.title)
        entrybox.modify_bg(gtk.STATE_INSENSITIVE,
                style.COLOR_WHITE.get_gdk_color())
        entrybox.connect('changed', self._entry_activate_cb, 'title')
        hbox.append(hippo.CanvasWidget(widget=entrybox), hippo.PACK_EXPAND)
        buildbox.append(hbox, hippo.PACK_EXPAND)

        hbox = hippo.CanvasBox(spacing=8,
            orientation=hippo.ORIENTATION_HORIZONTAL)
        hbox.append(self._text_mainbox(_('Question:'),
                                       warn='question' in highlight))
        entrybox = gtk.Entry()
        entrybox.set_text(self._poll.question)
        entrybox.modify_bg(gtk.STATE_INSENSITIVE,
                style.COLOR_WHITE.get_gdk_color())
        entrybox.connect('changed', self._entry_activate_cb, 'question')
        hbox.append(hippo.CanvasWidget(widget=entrybox), hippo.PACK_EXPAND)
        buildbox.append(hbox, hippo.PACK_EXPAND)

        hbox = hippo.CanvasBox(spacing=8,
            orientation=hippo.ORIENTATION_HORIZONTAL)
        hbox.append(self._text_mainbox(_('Number of votes to collect:'),
                                       warn='maxvoters' in highlight))
        entrybox = gtk.Entry()
        entrybox.set_text(str(self._poll.maxvoters))
        entrybox.modify_bg(gtk.STATE_INSENSITIVE,
                style.COLOR_WHITE.get_gdk_color())
        entrybox.connect('changed', self._entry_activate_cb, 'maxvoters')
        hbox.append(hippo.CanvasWidget(widget=entrybox))
        buildbox.append(hbox)

        for choice in self._poll.options.keys():
            hbox = hippo.CanvasBox(spacing=8,
                orientation=hippo.ORIENTATION_HORIZONTAL)
            hbox.append(self._text_mainbox(_('Answer') + ' ' + str(choice+1) +
                                           ':',
                                           warn=str(choice) in highlight))
            entrybox = gtk.Entry()
            entrybox.set_text(self._poll.options[choice])
            entrybox.modify_bg(gtk.STATE_INSENSITIVE,
                    style.COLOR_WHITE.get_gdk_color())
            entrybox.connect('changed', self._entry_activate_cb, str(choice))
            hbox.append(hippo.CanvasWidget(widget=entrybox), hippo.PACK_EXPAND)
            buildbox.append(hbox, hippo.PACK_EXPAND)

        # PREVIEW & SAVE buttons
        hbox = hippo.CanvasBox(spacing=8,
            orientation=hippo.ORIENTATION_HORIZONTAL)
        button = gtk.Button(_("Step 1: Preview"))
        button.connect('clicked', self._button_preview_cb)
        hbox.append(hippo.CanvasWidget(widget=theme_button(button)))
        button = gtk.Button(_("Step 2: Save"))
        button.connect('clicked', self._button_save_cb)
        hbox.append(hippo.CanvasWidget(widget=theme_button(button)))
        buildbox.append(hbox)
        
        button_box = self._canvas_buttonbox(button_to_highlight=1)
        mainbox.append(button_box, hippo.PACK_END)

        return canvasbox

    def _button_preview_cb(self, button, data=None):
        """Preview button clicked."""
        # Validate data
        failed_items = self._validate()
        if failed_items:
            self.set_root(self._build_canvas(highlight=failed_items))
            self.show_all()
            return
        # Data OK
        self._poll.active = True  # Show radio buttons
        self._previewing = True
        self.set_root(self._poll_canvas())
        self.show_all()

    def _button_save_cb(self, button, data=None):
        """Save button clicked."""
        # Validate data
        failed_items = self._validate()
        if failed_items:
            self.set_root(self._build_canvas(highlight=failed_items))
            self.show_all()
            return
        # Data OK
        self._previewing = False
        self._poll.active = True
        self._polls.add(self._poll)
        self._poll.broadcast_on_mesh()
        self.set_root(self._poll_canvas())
        self.show_all()

    def _entry_activate_cb(self, entrycontrol, data=None):
        text = entrycontrol.props.text
        if data:
            if text:
                if data=='title':
                    self._poll.title = text
                elif data=='question':
                    self._poll.question = text
                elif data=='maxvoters':
                    try:
                        self._poll.maxvoters = int(text)
                    except ValueError:
                        self._poll.maxvoters = 0  # invalid, will be trapped
                else:
                    self._poll.options[int(data)] = text

    def _make_blank_poll(self):
        """Initialize the poll state."""
        self._poll = Poll(activity=self)
        self.current_vote = None

    def _make_default_poll(self):
        """A hardcoded poll for first time launch."""
        self._poll = Poll(
            activity=self, title=self.nick + ' ' + _('Favorite Color'),
            author=self.nick, active=True,
            question=_('What is your favorite color?'),
            options = {0: _('Green'), 1: _('Red'), 2: _('Blue'),
                3: _('Orange'), 4: _('None of the above')})
        self.current_vote = None
        self._polls.add(self._poll)

    def _validate(self):
        failed_items = []
        if self._poll.title == '':
            failed_items.append('title')
        if self._poll.question == '':
            failed_items.append('question')
        if self._poll.maxvoters == 0:
            failed_items.append('maxvoters')
        if self._poll.options[0] == '':
            failed_items.append('0')
        if self._poll.options[1] == '':
            failed_items.append('1')
        if self._poll.options[3] != '' and self._poll.options[2] == '':
            failed_items.append('2')
        if self._poll.options[4] != '' and self._poll.options[3] == '':
            failed_items.append('3')
        if self._poll.options[2] == '':
            self._poll.number_of_options = 2
        elif self._poll.options[3] == '':
            self._poll.number_of_options = 3
        elif self._poll.options[4] == '':
            self._poll.number_of_options = 4
        else:
            self._poll.number_of_options = 5
        return failed_items
            
    def _get_sha(self):
        """Return a sha1 hash of something about this poll.
        
        Currently we sha1 the poll title and author.
        This is used for the filename of the saved poll.
        It will probably be used for the mesh networking too.
        """
        return self._poll.sha
    
    def _switch_to_poll(self, sha):
        """Set self._poll to the specified poll with sha

        sha -- string
        """
        for poll in self._polls:
            if poll.sha == sha:
                self._poll = poll

    def get_my_polls(self):
        """Return list of Polls for all polls I created."""
        return [poll for poll in self._polls if poll.author==self.nick] 

    def vote_on_poll(self, author, title, choice, votersha):
        """Register a vote on a poll from the mesh.
        
        author -- string
        title -- string
        choice -- integer 0-4
        votersha -- string
          sha1 of the voter nick
        """
        for poll in self._polls:
            if poll.author == author and poll.title == title:
                try:
                    poll.register_vote(choice, votersha)
                    self.alert(_('Vote'),
                               _('Somebody voted on %s') % title)
                except OverflowError:
                    self._logger.debug('Ignored mesh vote %u from %s:'
                        ' poll reached maximum votes.',
                        choice, votersha)
                except ValueError:
                    self._logger.debug('Ignored mesh vote %u from %s:'
                        ' poll closed.',
                        choice, votersha)

    def _canvas_pollbuilder_box(self):
        """CanvasBox definition for pollbuilderbox.
        
        Called from _poll_canvas, _select_canvas, _build_canvas
        """
        pollbuilderbox = hippo.CanvasBox(
            border=4,
            border_color=style.Color(GRAY).get_int(),
            orientation=hippo.ORIENTATION_VERTICAL)
        return pollbuilderbox

    def _canvas_root(self):
        """CanvasBox definition for main canvas.
        
        Called from _poll_canvas, _select_canvas, _build_canvas
        """
        canvasbox = hippo.CanvasBox(
            background_color=style.COLOR_SELECTION_GREY.get_int(),
            orientation=hippo.ORIENTATION_VERTICAL)
        return canvasbox

    def _canvas_topbox(self, lesson_return=None):
        """Render topbox.

        lesson_return is the view we want to return to from
        lesson plan if the lesson plan button is clicked.
        """
        topbox = hippo.CanvasBox(
            background_color=style.Color(LIGHT_GREEN).get_int(),
            orientation=hippo.ORIENTATION_HORIZONTAL)
        topbox.append(hippo.CanvasWidget(widget=self._logo()))
        lessonplanbox = self._canvas_lessonplanbox(lesson_return)
        topbox.append(lessonplanbox, hippo.PACK_EXPAND)
        return topbox

    def _logo(self):
        logoimage = gtk.Image()
        logoimage.set_from_file(os.path.join(
            self._basepath,
            'GameLogoCharacter.png'))
        return logoimage

    def _canvas_lessonplanbox(self, lesson_return=None):
        """Render the lessonplanbox.

        disconnect_lp True does not connect the button.
        """
        lessonplanbox = hippo.CanvasBox(
            background_color=style.Color(LIGHT_GREEN).get_int(),
            border_top=4, border_left=4, border_right=4,
            border_color=style.Color(YELLOW).get_int(),
            padding_top=12, padding_bottom=12,
            padding_left=30, padding_right=30,
            orientation=hippo.ORIENTATION_VERTICAL)
        if lesson_return:
            highlight = True
            button = gtk.Button(_("Close Lessons"))
        else:
            highlight = False
            button = gtk.Button(_("Lesson Plans"))
        if lesson_return:
            button.connect('clicked', self._button_closelessonplan_cb, lesson_return)
        else:
            button.connect('clicked', self._button_lessonplan_cb)
        lessonplanbox.append(hippo.CanvasWidget(widget=theme_button(
            button, highlight=highlight)))
        return lessonplanbox

    def _button_lessonplan_cb(self, button):
        """Lesson Plan button clicked."""
        self._logger.debug('%s -> Lesson Plan' % self._current_view)
        self.set_root(self._lessonplan_canvas())
        self.show_all()

    def _button_closelessonplan_cb(self, button, lesson_return):
        """Lesson Plan button clicked in Lesson Plan view.
        
        Go back to the view we had previously.
        """
        self._logger.debug('Lesson plans -> %s' % lesson_return)
        if lesson_return == 'poll':
            self.set_root(self._poll_canvas())
        elif lesson_return == 'select':
            self.set_root(self._select_canvas())
        elif lesson_return == 'build':
            self.set_root(self._build_canvas())
        self.show_all()
        del self._lessonplan_widget
        self._lessonplan_widget = None

    def _canvas_mainbox(self):
        mainbox = hippo.CanvasBox(spacing=4,
            background_color=style.Color(LIGHT_GREEN).get_int(),
            border=4,
            border_color=style.Color(YELLOW).get_int(),
            padding_top=PAD, padding_left=40, padding_right=40,
            padding_bottom=PAD,
            orientation=hippo.ORIENTATION_VERTICAL)
        return mainbox

    def _text_mainbox(self, text, warn=False):
        """Main text style.
        
        warn=True makes the text color RED and appends ???.
        """
        if warn:
            text_color = RED
            text = text + '???'
        else:
            text_color = DARK_GREEN
        return hippo.CanvasText(
            text=text,
            xalign=hippo.ALIGNMENT_START,
            color=style.Color(text_color).get_int())

    def _canvas_buttonbox(self, button_to_highlight=None):
        button_box = hippo.CanvasBox(
            spacing=8,
            padding=8,
            orientation=hippo.ORIENTATION_HORIZONTAL)
        button = gtk.Button(_("Build a Poll"))
        button.connect('clicked', self.button_new_clicked)
        button_box.append(hippo.CanvasWidget(
            widget=theme_button(button,
                               highlight=(button_to_highlight==1))))
        button = gtk.Button(_("Choose a Poll"))
        button.connect('clicked', self.button_select_clicked)
        button_box.append(hippo.CanvasWidget(
            widget=theme_button(button,
                               highlight=(button_to_highlight==2))))
        return button_box

    def _shared_cb(self, activity):
        """Callback for completion of sharing this activity."""
        self._logger.debug('My activity was shared')
        self.initiating = True
        self._sharing_setup()

        self._logger.debug('This is my activity: making a tube...')
        id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube(
            SERVICE, {})

    def _sharing_setup(self):
        """Setup my Tubes channel.
        
        Called from _shared_cb or _joined_cb.
        """
        if self._shared_activity is None:
            self._logger.error('Failed to share or join activity')
            return

        self.conn = self._shared_activity.telepathy_conn
        self.tubes_chan = self._shared_activity.telepathy_tubes_chan
        self.text_chan = self._shared_activity.telepathy_text_chan

        self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal(
            'NewTube', self._new_tube_cb)

        self._shared_activity.connect('buddy-joined', self._buddy_joined_cb)
        self._shared_activity.connect('buddy-left', self._buddy_left_cb)

    def _list_tubes_reply_cb(self, tubes):
        for tube_info in tubes:
            self._new_tube_cb(*tube_info)

    def _list_tubes_error_cb(self, e):
        self._logger.error('ListTubes() failed: %s', e)

    def _joined_cb(self, activity):
        """Callback for completion of joining the activity."""
        if not self._shared_activity:
            return

        self._logger.debug('Joined an existing shared activity')
        self.alert(_('Joined'))
        self.initiating = False
        self._sharing_setup()

        self._logger.debug('This is not my activity: waiting for a tube...')
        self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes(
            reply_handler=self._list_tubes_reply_cb,
            error_handler=self._list_tubes_error_cb)

    def _new_tube_cb(self, id, initiator, type, service, params, state):
        """Callback for when we have a Tube."""
        self._logger.debug('New tube: ID=%d initator=%d type=%d service=%s '
                     'params=%r state=%d', id, initiator, type, service,
                     params, state)

        if (type == telepathy.TUBE_TYPE_DBUS and
            service == SERVICE):
            if state == telepathy.TUBE_STATE_LOCAL_PENDING:
                self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].AcceptDBusTube(id)

            tube_conn = TubeConnection(self.conn,
                self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES],
                id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP])
            self.poll_session = PollSession(tube_conn, self.initiating, self._get_buddy, self)

    def _buddy_joined_cb (self, activity, buddy):
        self.alert(buddy.props.nick, _('Joined'))
        self._logger.debug('Buddy %s joined' % buddy.props.nick)

    def _buddy_left_cb (self, activity, buddy):
        self.alert(buddy.props.nick, _('Left'))
        self._logger.debug('Buddy %s left' % buddy.props.nick)

    def _get_buddy(self, cs_handle):
        """Get a Buddy from a channel specific handle."""
        self._logger.debug('Trying to find owner of handle %u...', cs_handle)
        group = self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]
        my_csh = group.GetSelfHandle()
        self._logger.debug('My handle in that group is %u', my_csh)
        if my_csh == cs_handle:
            handle = self.conn.GetSelfHandle()
            self._logger.debug('CS handle %u belongs to me, %u', cs_handle, handle)
        elif group.GetGroupFlags() & telepathy.CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES:
            handle = group.GetHandleOwners([cs_handle])[0]
            self._logger.debug('CS handle %u belongs to %u', cs_handle, handle)
        else:
            handle = cs_handle
            self._logger.debug('non-CS handle %u belongs to itself', handle)
            assert handle != 0
        return self.pservice.get_buddy_by_telepathy_handle(
            self.conn.service_name, self.conn.object_path, handle)


class Poll:
    """Represent the data of one poll."""
    def __init__(self, activity=None, title='', author='', active=False,
                 createdate=date.today(), maxvoters=20, question='',
                 number_of_options=5, options=None, data=None, votes=None):
        """Create the Poll."""
        self.activity = activity
        self.title = title
        self.author = author
        self.active = active
        self.createdate = createdate
        self.maxvoters = maxvoters
        self.question = question
        self.number_of_options = number_of_options
        self.options = (options or {0: '', 1: '', 2: '', 3: '', 4: ''})
        self.data = (data or {0:0, 1:0, 2:0, 3:0, 4:0})
        self.votes = (votes or {})
        self._logger = logging.getLogger('poll-activity.Poll')
        self._logger.debug('Creating Poll(%s by %s)' % (title, author))

    def dump(self):
        """Dump a pickled version for the journal"""
        # The attributes may be dbus types. These are not serialisable
        # with pickle at the moment, so convert them to builtin types.
        # Pay special attention to dicts - we need to convert the keys
        # and values too.
        s = cPickle.dumps(str(self.title))
        s += cPickle.dumps(str(self.author))
        s += cPickle.dumps(bool(self.active))
        s += cPickle.dumps(self.createdate.toordinal())
        s += cPickle.dumps(int(self.maxvoters))
        s += cPickle.dumps(str(self.question))
        s += cPickle.dumps(int(self.number_of_options))
        options = {}
        for key in self.options:
            value = self.options[key]
            options[int(key)] = str(value)
        data = {}
        for key in self.data:
            value = self.data[key]
            data[int(key)] = int(value)
        votes = {}
        for key in self.votes:
            value = self.votes[key]
            votes[str(key)] = int(value)
        s += cPickle.dumps(options)
        s += cPickle.dumps(data)
        s += cPickle.dumps(votes)
        return s

    @property
    def vote_count(self):
        """Return the total votes cast."""
        total = 0
        for choice in self.options.keys():
            total += self.data[choice]
        return total

    @property
    def sha(self):
        """Return a sha1 hash of something about this poll.

        Currently we sha1 the poll title and author.
        """
        return sha1(self.title + self.author).hexdigest()

    def register_vote(self, choice, votersha):
        """Register a vote on the poll.

        votersha -- string
          sha1 of the voter nick
        """
        self._logger.debug('In Poll.register_vote')
        if self.active:
            if self.vote_count < self.maxvoters:
                self._logger.debug('About to vote')
                # XXX 27/10/07 Morgan: Allowing multiple votes per XO
                #                      per Shannon's request.
                ## if voter already voted, change their vote:
                #if votersha in self.votes:
                #    self._logger.debug('%s already voted, decrementing their '
                #        'old choice %d' % (votersha, self.votes[votersha]))
                #    self.data[self.votes[votersha]] -= 1
                self.votes[votersha] = choice
                self.data[choice] += 1
                self._logger.debug(
                    'Recording vote %d by %s on %s by %s' %
                    (choice, votersha, self.title, self.author))
                # Close poll:
                if self.vote_count >= self.maxvoters:
                    self.active = False
                    self._logger.debug('Poll hit maxvoters, closing')
                if self.activity.poll_session:
                    # We are shared so we can send the Vote signal if I voted
                    if votersha == self.activity.nick_sha1:
                        self._logger.debug(
                            'Shared, I voted so sending signal')
                        self.activity.poll_session.Vote(
                            self.author, self.title, choice, votersha)
            else:
                raise OverflowError, 'Poll reached maxvoters'
        else:
            raise ValueError, 'Poll closed'

    def broadcast_on_mesh(self):
        if self.activity.poll_session:
            # We are shared so we can broadcast this poll
            self.activity.poll_session.UpdatedPoll(
                self.title, self.author, self.active,
                self.createdate.toordinal(),
                self.maxvoters, self.question, self.number_of_options,
                self.options, self.data, self.votes) 


class PollSession(ExportedGObject):
    """The bit that talks over the TUBES!!!"""

    def __init__(self, tube, is_initiator, get_buddy, activity):
        """Initialise the PollSession.

        tube -- TubeConnection
        is_initiator -- boolean, True = we are sharing, False = we are joining
        get_buddy -- function
        activity -- PollBuilder (sugar.activity.Activity)
        """
        super(PollSession, self).__init__(tube, PATH)
        self._logger = logging.getLogger('poll-activity.PollSession')
        self.tube = tube
        self.is_initiator = is_initiator
        self.entered = False  # Have we set up the tube?
        self._get_buddy = get_buddy  # Converts handle to Buddy object
        self.activity = activity  # PollBuilder
        self.tube.watch_participants(self.participant_change_cb)

    def participant_change_cb(self, added, removed):
        """Callback when tube participants change."""
        self._logger.debug('In participant_change_cb')
        if added:
            self._logger.debug('Adding participants: %r' % added)
        if removed:
            self._logger.debug('Removing participants: %r' % removed)
        for handle, bus_name in added:
            buddy = self._get_buddy(handle)
            if buddy is not None:
                self._logger.debug('Buddy %s was added' % buddy.props.nick)
        for handle in removed:
            buddy = self._get_buddy(handle)
            if buddy is not None:
                self._logger.debug('Buddy %s was removed' % buddy.props.nick)
                # Set buddy's polls to not active so I can't vote on them
                for poll in self.activity._polls:
                    if poll.author == buddy.props.nick:
                        poll.active = False
                        self._logger.debug(
                            'Closing poll %s of %s who just left.' %
                            (poll.title, poll.author))

        if not self.entered:
            if self.is_initiator:
                self._logger.debug("I'm initiating the tube")
            else:
                self._logger.debug('Joining, sending Hello')
                self.Hello()
            self.tube.add_signal_receiver(self.hello_cb, 'Hello', IFACE,
                path=PATH, sender_keyword='sender')
            self.tube.add_signal_receiver(self.vote_cb, 'Vote', IFACE,
                path=PATH, sender_keyword='sender')
            self.tube.add_signal_receiver(self.helloback_cb, 'HelloBack',
                IFACE, path=PATH, sender_keyword='sender')
            self.tube.add_signal_receiver(self.updatedpoll_cb, 
                'UpdatedPoll', IFACE, path=PATH, sender_keyword='sender')
            self.my_bus_name = self.tube.get_unique_name()
            self.entered = True

    @signal(dbus_interface=IFACE, signature='')
    def Hello(self):
        """Request that my UpdatePoll method is called to let me know about
        other known polls.
        """

    @signal(dbus_interface=IFACE, signature='ssus')
    def Vote(self, author, title, choice, votersha):
        """Send my vote on author's poll.

        author -- string, buddy name
        title -- string, poll title
        choice -- integer 0-4, selected vote
        votersha -- string, sha1 of voter's nick
        """

    @signal(dbus_interface=IFACE, signature='s')
    def HelloBack(self, recipient):
        """Respond to Hello.

        recipient -- string, sender of Hello.
        """

    @signal(dbus_interface=IFACE, signature='ssuuusua{us}a{uu}a{su}')
    def UpdatedPoll(self, title, author, active, createdate, maxvoters,
                   question, number_of_options, options, data, votes):
        """Broadcast a new poll to the mesh."""

    def hello_cb(self, sender=None):
        """Tell the newcomer what's going on."""
        assert sender is not None
        self._logger.debug('Newcomer %s has joined and sent Hello', sender)
        # sender is a bus name - check if it's me:
        if sender == self.my_bus_name:
            # then I don't want to respond to my own Hello
            return
        # Send my polls
        for poll in self.activity.get_my_polls():
            self._logger.debug('Telling %s about my %s' % 
                               (sender, poll.title))
            self.tube.get_object(sender, PATH).UpdatePoll(
                poll.title, poll.author, int(poll.active),
                poll.createdate.toordinal(),
                poll.maxvoters, poll.question, poll.number_of_options,
                poll.options, poll.data, poll.votes, dbus_interface=IFACE)
        # Ask for other's polls back
        self.HelloBack(sender)

    def helloback_cb(self, recipient, sender):
        """Reply to Hello.
        
        recipient -- string, the XO who send the original Hello.
        
        Other XOs should ignore this signal.
        """
        self._logger.debug('*** In helloback_cb: recipient: %s, sender: %s' %
                           (recipient, sender))
        if sender == self.my_bus_name:
            # Ignore my own signal
            return
        if recipient != self.my_bus_name:
            # This is not for me
            return
        self._logger.debug('*** It was for me, so sending my polls back.')
        for poll in self.activity.get_my_polls():
            self._logger.debug('Telling %s about my %s' % 
                               (sender, poll.title))
            self.tube.get_object(sender, PATH).UpdatePoll(
                poll.title, poll.author, int(poll.active),
                poll.createdate.toordinal(),
                poll.maxvoters, poll.question, poll.number_of_options,
                poll.options, poll.data, poll.votes, dbus_interface=IFACE)

    def updatedpoll_cb(self, title, author, active, createdate, maxvoters,
                       question, number_of_options, options_d, data_d,
                       votes_d, sender):
        """Handle an UpdatedPoll signal by creating a new Poll."""
        self._logger.debug('Received UpdatedPoll from %s' % sender)
        if sender == self.my_bus_name:
            # Ignore my own signal
            return
        # We get the parameters as dbus types. These are not serialisable
        # with pickle at the moment, so convert them to builtin types.
        # Pay special attention to dicts - we need to convert the keys
        # and values too.
        title = str(title)
        author = str(author)
        active = bool(active)
        createdate = date.fromordinal(int(createdate))
        maxvoters = int(maxvoters)
        question = str(question)
        number_of_options = int(number_of_options)
        options = {}
        for key in options_d:
            value = options_d[key]
            options[int(key)] = str(value)
        data = {}
        for key in data_d:
            value = data_d[key]
            data[int(key)] = int(value)
        votes = {}
        for key in votes_d:
            value = votes_d[key]
            votes[str(key)] = int(value)
        poll = Poll(self.activity, title, author, active, 
                    createdate, maxvoters, question, number_of_options,
                    options, data, votes)
        self.activity._polls.add(poll)
        self.activity.alert(_('New Poll'),
                            _("%(author)s shared a poll "
                              "'%(title)s' with you.") % {'author': author,
                                                          'title': title})

    def vote_cb(self, author, title, choice, votersha, sender=None):
        """Receive somebody's vote signal.

        author -- string, buddy name
        title -- string, poll title
        choice -- integer 0-4, selected vote
        votersha -- string, sha1 hash of voter nick
        """
        # FIXME: validate the choices, set the vote.
        # XXX We could possibly get the nick of sender and sha1 it
        #     to verify the vote is coming from the voter.
        if sender == self.my_bus_name:
            # Don't respond to my own Vote signal
            return
        self._logger.debug('In vote_cb. sender: %r' % sender)
        self._logger.debug('%s voted %d on %s by %s' % (votersha, choice,
                                                        title, author))
        self.activity.vote_on_poll(author, title, choice, votersha)

    @method(dbus_interface=IFACE, in_signature='ssuuusua{us}a{uu}a{su}',
            out_signature='')
    def UpdatePoll(self, title, author, active, createdate, maxvoters,
                   question, number_of_options, options_d, data_d, votes_d):
        """To be called on the incoming buddy by the other participants
        to inform you of their polls and state."""
        # We get the parameters as dbus types. These are not serialisable
        # with pickle at the moment, so convert them to builtin types.
        # Pay special attention to dicts - we need to convert the keys
        # and values too.
        title = str(title)
        author = str(author)
        active = bool(active)
        createdate = date.fromordinal(int(createdate))
        maxvoters = int(maxvoters)
        question = str(question)
        number_of_options = int(number_of_options)
        options = {}
        for key in options_d:
            value = options_d[key]
            options[int(key)] = str(value)
        data = {}
        for key in data_d:
            value = data_d[key]
            data[int(key)] = int(value)
        votes = {}
        for key in votes_d:
            value = votes_d[key]
            votes[str(key)] = int(value)
        poll = Poll(self.activity, title, author, active, 
                    createdate, maxvoters, question, number_of_options,
                    options, data, votes)
        self.activity._polls.add(poll)
        self.activity.alert(_('New Poll'),
                            _("%(author)s shared a poll "
                              "'%(title)s' with you.") % {'author': author,
                                                          'title': title})

    @method(dbus_interface=IFACE, in_signature='s', out_signature='')
    def PollsWanted(self, sender):
        """Notification to send my polls to sender."""
        for poll in self.activity.get_my_polls():
            self.tube.get_object(sender, PATH).UpdatePoll(
                poll.title, poll.author, int(poll.active),
                poll.createdate.toordinal(),
                poll.maxvoters, poll.question, poll.number_of_options,
                poll.options, poll.data, poll.votes, dbus_interface=IFACE)


def justify(textdict, choice):
    """Take a {} of numbers, and right justify the chosen item.

    textdict is a dict of {n: m} where n and m are integers.
    choice is one of textdict.keys()

    Returns a string of '   m' with m right-justified
    so that the longest value in the dict can fit.
    """
    max_len = 0
    for num in textdict.values():
        if len(str(num)) > max_len:
            max_len = len(str(num))
    value = str(textdict[choice])
    return value.rjust(max_len)


class LessonPlanWidget (gtk.Notebook):
    def __init__ (self, basepath):
        """Create a Notebook widget for displaying lesson plans in tabs.

        basepath -- string, path of directory containing lesson plans.
        """
        super(LessonPlanWidget, self).__init__()
        lessons = filter(lambda x: os.path.isdir(os.path.join(basepath,
                                                              'lessons', x)),
                         os.listdir(os.path.join(basepath, 'lessons')))
        lessons.sort()
        for lesson in lessons:
            self._load_lesson(os.path.join(basepath, 'lessons', lesson),
                              _(lesson))

    def _load_lesson (self, path, name):
        """Load the lesson content from a .abw, taking l10n into account.

        path -- string, path of lesson plan file, e.g. lessons/Introduction
        lesson -- string, name of lesson
        """
        code, encoding = locale.getdefaultlocale()
        canvas = AbiCanvas()
        canvas.show()
        files = map(lambda x: os.path.join(path, '%s.abw' % x),
                    ('_'+code.lower(), '_'+code.split('_')[0].lower(), 
                     'default'))
        files = filter(lambda x: os.path.exists(x), files)
        canvas.load_file('file://%s' % files[0], '')
        canvas.view_online_layout()
        canvas.zoom_width()
        canvas.set_show_margin(False)
        self.append_page(canvas, gtk.Label(name))