Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/PollSession.py
blob: af01a7fa34e43aa828b23dfe8c8a154f45d9f1b5 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 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 !

import os
import cPickle
import logging
import base64

from datetime import date
from gettext import gettext as _

from gi.repository import GdkPixbuf

from hashlib import sha1
    
from dbus.service import method, signal
from dbus.gobject_service import ExportedGObject

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

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,
        images=None, images_ds_objects=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.images = (images or {0: '', 1: '', 2: '', 3: '', 4: ''})
        
        self.images_ds_objects = (images_ds_objects 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)
            
        images_objects_id = {}
        
        for key in self.images_ds_objects:
            if not self.images_ds_objects[key] == {}:
                value = self.images_ds_objects[key]['id']
                images_objects_id[int(key)] = str(value)
                
            else:
                images_objects_id[int(key)] = ''
                
        s += cPickle.dumps(options)
        s += cPickle.dumps(data)
        s += cPickle.dumps(votes)
        s += cPickle.dumps(images_objects_id)
        
        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 get_buffer(self, pixbuf):

        path = "/dev/shm/pix.png"
        pixbuf.savev(path, "png", [], [])
        
        pixbuf_file = open(path, 'rb')
        image_string = base64.b64encode(pixbuf_file.read())
        pixbuf_file.close()
        
        os.remove(path)
        
        return image_string
    
    def broadcast_on_mesh(self):
        
        if self.activity.poll_session:
            images_buf = {}
            
            for img_number, img_pixbuf in self.images.iteritems():
                if not img_pixbuf == '':
                    images_buf[img_number] = self.get_buffer(img_pixbuf)
                        
                else:
                    images_buf[img_number] = img_pixbuf
                    
            # 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, images_buf)

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 (sugar3.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}a{us}')
    def UpdatedPoll(self, title, author, active, createdate, maxvoters,
        question, number_of_options, options, data, votes,
        images_buf):
        """
        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))
                
            #images_properties = poll.simplify_images_dictionary()
            images_buf = {}
            
            for img_number, img_pixbuf in poll.images.iteritems():
                if not img_pixbuf == '':
                    images_buf[img_number] = poll.get_buffer(img_pixbuf)
                        
                else:
                    images_buf[img_number] = img_pixbuf
                    
            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, images_buf,
                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))
                
            images_buf = {}
            
            for img_number, img_pixbuf in poll.images.iteritems():
                if not img_pixbuf == '':
                    images_buf[img_number] = poll.get_buffer(img_pixbuf)
                        
                else:
                    images_buf[img_number] = img_pixbuf
                    
            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, images_buf,
                dbus_interface=IFACE)

    def get_pixbuf(self, img_encode_buf):
        
        decode_img_buf = base64.b64decode(img_encode_buf)
        loader = GdkPixbuf.PixbufLoader()
        loader.write(decode_img_buf)
        loader.close()
        pixbuf = loader.get_pixbuf()
        
        return pixbuf

    def __updatedpoll_cb(self, title, author, active, createdate, maxvoters,
        question, number_of_options, options_d, data_d,
        votes_d, images_buf_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)
            
        images = {}
        
        for key in images_buf_d:
            if not images_buf_d[key] == '':
                images[int(key)] = self.get_pixbuf(images_buf_d[key])
                
            else:
                images[int(key)] = ''
                
        poll = Poll(self.activity, title, author, active,
            createdate, maxvoters, question, number_of_options,
            options, data, votes, images)
            
        self.activity._polls.add(poll)
        
        self.activity.get_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}a{us}',
        out_signature='')
    def UpdatePoll(self, title, author, active, createdate, maxvoters,
        question, number_of_options, options_d, data_d, votes_d,
        images_buf_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)
            
        images = {}
        
        for key in images_buf_d:
            if not images_buf_d[key] == '':
                images[int(key)] = self.get_pixbuf(images_buf_d[key])
                
            else:
                images[int(key)] = ''
                
        poll = Poll(self.activity, title, author, active,
            createdate, maxvoters, question, number_of_options,
            options, data, votes, images)
            
        self.activity._polls.add(poll)
        
        self.activity.get_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():
            images_buf = {}
            
            for img_number, img_pixbuf in poll.images.iteritems():
                if not img_pixbuf == '':
                    images_buf[img_number] = poll.get_buffer(img_pixbuf)
                        
                else:
                    images_buf[img_number] = img_pixbuf
                    
            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, images_buf,
                dbus_interface=IFACE)