Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ShareStats.py
blob: 281c2473b1c71367c876b4c051622e875b4c4646 (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
# -*- coding: utf-8 -*-
#Copyright (c) 2013 Walter Bender

# 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 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA


from gi.repository import Gtk
from gi.repository import Gdk

import os

from sugar3 import profile

from sugar3.activity import activity
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton

from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.alert import NotifyAlert
from sugar3.graphics.icon import CanvasIcon
from sugar3.graphics import style
from sugar3.graphics.xocolor import XoColor

from sugar3.datastore import datastore

from toolbar_utils import separator_factory
from journalstats import JournalReader
import chart as charts

from gettext import gettext as _

import logging
_logger = logging.getLogger("share-stats")

import json

import telepathy
from dbus.service import signal
from dbus.gobject_service import ExportedGObject
from sugar3.presence import presenceservice
from sugar3.presence.tubeconn import TubeConnection


SERVICE = 'org.sugarlabs.ShareStats'
IFACE = SERVICE
PATH = '/org/sugarlabs/ShareStats'


class ChartArea(Gtk.DrawingArea):

    def __init__(self, chart):
        '''A class for Draw the chart'''
        super(ChartArea, self).__init__()
        self._chart = chart
        self.add_events(Gdk.EventMask.EXPOSURE_MASK |
                        Gdk.EventMask.VISIBILITY_NOTIFY_MASK)
        self.connect('draw', self._draw_cb)

    def _draw_cb(self, widget, context):
        alloc = self.get_allocation()

        # White Background:
        context.rectangle(0, 0, alloc.width, alloc.height)
        context.set_source_rgb(255, 255, 255)
        context.fill()

        # Paint the chart:
        chart_width = self._chart.width
        chart_height = self._chart.height

        cxpos = alloc.width / 2 - chart_width / 2
        cypos = alloc.height / 2 - chart_height / 2

        context.set_source_surface(self._chart.surface, cxpos, cypos)
        context.paint()


class ShareStats(activity.Activity):
    ''' Share journal stats '''

    def __init__(self, handle):
        ''' Initialize the toolbars and the work surface '''
        super(ShareStats, self).__init__(handle)

        self.initiating = None  # sharing (True) or joining (False)
        self._old_cursor = self.get_window().get_cursor()
        self._buddy_count = 0
        self._hboxes = []

        self._setup_toolbars()
        self._setup_canvas()
        self._setup_presence_service()

        # Start with the Neighborhood View icon in the center of the screen
        self._icon = self._create_icon('#FFFFFF,#000000',
                                       name='zoom-neighborhood')
        self._icon.show()
        self._vbox.pack_end(self._icon, True, True, 0)
        self._vbox.show()

    def _setup_canvas(self):
        ''' Create a canvas '''
        sw = Gtk.ScrolledWindow()
        sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
        sw.show()

        self.set_canvas(sw)

        self._vbox = Gtk.VBox(False, 0)
        self._vbox.set_size_request(Gdk.Screen.width(),
                                    Gdk.Screen.height() - style.GRID_CELL_SIZE)
        sw.add(self._vbox)
        self._vbox.show()

    def _create_chart(self, data):
        chart = charts.Chart(charts.HORIZONTAL_BAR)
        colors = profile.get_color()
        logging.debug(colors)
        chart_color = colors.get_fill_color()
        logging.debug(chart_color)
        chart_line_color = colors.get_stroke_color()
        logging.debug(chart_line_color)

        eventbox = Gtk.EventBox()
        charts_area = ChartArea(chart)
        eventbox.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('white'))
        eventbox.add(charts_area)
        eventbox.show()
        self._vbox.pack_end(eventbox, True, True, 0)

        chart_data = []
        i = 0
        for entry in data:
            label = entry[0]
            value = len(entry[1])
            chart_data.append((label, float(value)))
            i += 1
            if i > 15:
                break
        chart.data_set(chart_data)
        chart.width = Gdk.Screen.width() - 2 * style.GRID_CELL_SIZE
        chart.height = Gdk.Screen.height() - 2 * style.GRID_CELL_SIZE
        chart.set_color_scheme(color=chart_color)
        chart.set_line_color(chart_line_color)
        chart.render()
        charts_area.queue_draw()
        self.show_all()

    def _create_icon(self, color, name='computer-xo'):
        return CanvasIcon(icon_name=name,
                          xo_color=XoColor(color),
                          pixel_size=style.STANDARD_ICON_SIZE)

    def _add_buddy(self, icon, nick):
        ''' Add buddies to sharer's canavs to show whom has shared data '''

        n = int(Gdk.Screen.width() / (3 * style.STANDARD_ICON_SIZE))
        if self._buddy_count % n == 0:
            self._hboxes.append(Gtk.HBox(False, 0))
            self._hboxes[-1].show()
            self._vbox.pack_end(self._hboxes[-1], True, False, 0)

        self._buddy_count += 1

        vbox = Gtk.VBox(False, 0)
        label = Gtk.Label(nick)
        vbox.pack_start(icon, False, False, 0)
        vbox.pack_start(label, False, False, 10)
        icon.show()
        label.show()
        vbox.show()
        self._hboxes[-1].pack_end(vbox, True, False, 0)

    def _setup_toolbars(self):
        ''' Setup the toolbars. '''
        self.max_participants = 5

        toolbox = ToolbarBox()

        # Activity toolbar
        activity_button_toolbar = ActivityToolbarButton(self)

        toolbox.toolbar.insert(activity_button_toolbar, 0)
        activity_button_toolbar.show()

        self.set_toolbar_box(toolbox)
        toolbox.show()
        self.toolbar = toolbox.toolbar

        separator_factory(toolbox.toolbar, True, False)

        stop_button = StopButton(self)
        stop_button.props.accelerator = '<Ctrl>q'
        toolbox.toolbar.insert(stop_button, -1)
        stop_button.show()

        toolbox.toolbar.show_all()

    def _restore_cursor(self):
        ''' No longer waiting, so restore standard cursor. '''
        if not hasattr(self, 'get_window'):
            return
        self.get_window().set_cursor(self._old_cursor)

    def _waiting_cursor(self):
        ''' Waiting, so set watch cursor. '''
        if not hasattr(self, 'get_window'):
            return
        self._old_cursor = self.get_window().get_cursor()
        self.get_window().set_cursor(Gdk.Cursor.new(Gdk.CursorType.WATCH))

    def _notify_alert(self, title='', msg='', action=None):
        def _notification_alert_response_cb(alert, response_id, self, action):
            self.remove_alert(alert)
            if action is not None:
                action()
            self._restore_cursor()

        alert = NotifyAlert()
        alert.props.title = title
        alert.connect('response', _notification_alert_response_cb, self,
                      action)
        alert.props.msg = msg
        self.add_alert(alert)
        alert.show()

    # When stats are shared, the joiner sends stats to the sharer.
    def _generate_stats(self):
        stats_path = os.path.join(activity.get_activity_root(), 'instance',
                                  'journalstats')
        JournalReader(stats_path)
        stats = json.load(open(stats_path))
        return stats

    def _save_stats(self, stats, nick=_('NONE'), colors='#FFFFFF,#000000'):
        stats_path = os.path.join(activity.get_activity_root(), 'instance',
                                  'journalstats')
        json.dump(stats, open(stats_path, 'w'))
        # Create a datastore object to go with these data
        dsobject = datastore.create()
        dsobject.metadata['title'] = '%s %s' % (nick, _('Journal Statistics'))
        dsobject.metadata['icon-color'] = colors
        dsobject.metadata['mime_type'] = 'text/csv'
        dsobject.set_file_path(stats_path)
        dsobject.metadata['activity'] = 'org.sugarlabs.ShareStats'
        datastore.write(dsobject)
        dsobject.destroy()
        self._notify_alert(title=_('Share Stats'),
                           msg='%s %s' %
                           (_('Received journal statistics from'), nick))
        icon = self._create_icon(colors)
        self._add_buddy(icon, nick)

    def _setup_presence_service(self):
        ''' Setup the Presence Service. '''
        self.pservice = presenceservice.get_instance()

        owner = self.pservice.get_owner()
        self.owner = owner
        self.buddies = [owner]
        self._share = ''
        self.connect('shared', self._shared_cb)
        self.connect('joined', self._joined_cb)

    def _shared_cb(self, activity):
        ''' Either set up initial share...'''
        if self.get_shared_activity() is None:
            _logger.error('Failed to share or join activity ... \
                shared_activity is null in _shared_cb()')
            return

        self.initiating = True
        self.waiting = False
        _logger.debug('I am sharing...')

        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)

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

        self._waiting_cursor()

        if self._icon in self._vbox:
            self._vbox.remove(self._icon)

        self._notify_alert(title=_('Share Journal Statistics'),
                           msg=_('Please wait as statistics come in.'))

    def _joined_cb(self, activity):
        ''' ...or join an exisiting share. '''
        if self.get_shared_activity() is None:
            _logger.error('Failed to share or join activity ... \
                shared_activity is null in _shared_cb()')
            return

        self.initiating = False
        _logger.debug('I joined a shared activity.')

        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)

        _logger.debug('I am joining an 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)

        self.waiting = True
        self._waiting_cursor()

        if self._icon in self._vbox:
            self._vbox.remove(self._icon)

        self._notify_alert(title=_('Share Journal Statistics'),
                           msg=_('Generating statistics... please wait.'),
                           action=self._share_stats)

    def _list_tubes_reply_cb(self, tubes):
        ''' Reply to a list request. '''
        for tube_info in tubes:
            self._new_tube_cb(*tube_info)

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

    def _new_tube_cb(self, id, initiator, type, service, params, state):
        ''' Create a new tube. '''
        _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.chattube = ChatTube(tube_conn, self.initiating,
                                     self.event_received_cb)

    def event_received_cb(self, text):
        ''' Data is passed as tuples: cmd:text '''
        dispatch_table = {'S': self._receive_stats,
                          }
        _logger.debug('<<< %s' % (text[0]))
        dispatch_table[text[0]](text[2:])

    def _new_join(self, data):
        if self.initiating:
            self._share_stats()

    def _share_stats(self):
        stats = self._generate_stats()
        nick = profile.get_nick_name()
        colors = profile.get_color().to_string()
        self._send_event('S:%s' % (json.dumps([nick, colors, stats])))
        self._restore_cursor()
        self._create_chart(stats)

    def _receive_stats(self, data=None):
        if self.initiating:
            data_array = json.loads(data)
            nick = data_array[0]
            colors = data_array[1].encode('ascii', 'replace')
            stats = data_array[2]
            self._save_stats(stats, nick=nick, colors=colors)

    def _send_event(self, text):
        ''' Send event through the tube. '''
        if hasattr(self, 'chattube') and self.chattube is not None:
            _logger.debug('>>> %s' % (text[0]))
            self.chattube.SendText(text)


class ChatTube(ExportedGObject):
    ''' Class for setting up tube for sharing '''
    def __init__(self, tube, is_initiator, stack_received_cb):
        super(ChatTube, self).__init__(tube, PATH)
        self.tube = tube
        self.is_initiator = is_initiator  # Are we sharing or joining activity?
        self.stack_received_cb = stack_received_cb
        self.stack = ''

        self.tube.add_signal_receiver(self.send_stack_cb, 'SendText', IFACE,
                                      path=PATH, sender_keyword='sender')

    def send_stack_cb(self, text, sender=None):
        if sender == self.tube.get_unique_name():
            return
        self.stack = text
        self.stack_received_cb(text)

    @signal(dbus_interface=IFACE, signature='s')
    def SendText(self, text):
        self.stack = text