Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activity.py133
-rw-r--r--sharing.py190
-rw-r--r--utils.py17
3 files changed, 62 insertions, 278 deletions
diff --git a/activity.py b/activity.py
index 03d77d9..f2896f2 100644
--- a/activity.py
+++ b/activity.py
@@ -32,6 +32,10 @@ import utils
from gettext import gettext as _
+from dbus.service import signal
+from dbus.gobject_service import ExportedGObject
+from StringIO import StringIO
+
from sugar.presence import presenceservice
from sugar.presence.tubeconn import TubeConnection
from sugar.activity import activity
@@ -50,9 +54,6 @@ from sugar.datastore import datastore
from charts import Chart
from readers import StopWatchReader
from readers import MeasureReader
-from sharing import Receive
-from sharing import Send
-from sharing import ChatTube
# Mime types
STOPWATCH_MIME_TYPE = "application/x-stopwatch-activity"
@@ -106,13 +107,8 @@ class ChartArea(gtk.DrawingArea):
cx = x + w / 2 - cw / 2
cy = y + h / 2 - ch / 2
- try:
- context.set_source_surface(self._parent.current_chart.surface,
- cx, cy)
- context.paint()
-
- except TypeError:
- pass
+ context.set_source_surface(self._parent.current_chart.surface, cx, cy)
+ context.paint()
class SimpleGraph(activity.Activity):
@@ -338,28 +334,18 @@ class SimpleGraph(activity.Activity):
self.show_all()
- def _add_value(self, widget, label="", value="0.0", path='auto'):
+ def _add_value(self, widget, label="", value="0.0"):
data = (label, float(value))
if not data in self.chart_data:
- pos = self.labels_and_values.add_value(label, value, path)
+ pos = self.labels_and_values.add_value(label, value)
self.chart_data.insert(pos, data)
self._update_chart_data()
- # For sharing
- if self._sharing:
- data = (label, value, path)
- self.send.add_value(data)
-
def _remove_value(self, widget):
path = self.labels_and_values.remove_selected_value()
del self.chart_data[path]
self._update_chart_data()
- # For sharing
- if self._sharing:
- data = (path)
- self.send.remove_value(data)
-
def _add_chart_cb(self, widget, type="vbar"):
self.current_chart = Chart(type)
@@ -417,17 +403,16 @@ class SimpleGraph(activity.Activity):
if self.current_chart is None:
return
self.current_chart.data_set(self.chart_data)
- self._update_chart_labels()
self._render_chart()
+ # Send data
+ self._send_chart_data(self.chart_data)
+
def _update_chart_labels(self):
if self.current_chart is None:
return
- self.current_chart.set_title(self.metadata["title"])
self.current_chart.set_x_label(self.x_label)
self.current_chart.set_y_label(self.y_label)
- self.h_label.entry.set_text(self.x_label)
- self.v_label.entry.set_text(self.y_label)
self._render_chart()
def update_chart(self):
@@ -443,21 +428,11 @@ class SimpleGraph(activity.Activity):
self.chart_data[path] = (new_label, self.chart_data[path][1])
self._update_chart_data()
- # For sharing
- if self._sharing:
- data = (path, new_label)
- self.send.label_changed(data)
-
def _value_changed(self, tw, path, new_value):
path = int(path)
self.chart_data[path] = (self.chart_data[path][0], float(new_value))
self._update_chart_data()
- # For sharing
- if self._sharing:
- data = (path, new_value)
- self.send.value_changed(data)
-
def _set_h_label(self, widget):
new_text = widget.get_text()
@@ -465,11 +440,6 @@ class SimpleGraph(activity.Activity):
self.x_label = new_text
self._update_chart_labels()
- # For sharing
- if self._sharing:
- data = (new_text)
- self.send.set_x_label(data)
-
def _set_v_label(self, widget):
new_text = widget.get_text()
@@ -477,29 +447,14 @@ class SimpleGraph(activity.Activity):
self.y_label = new_text
self._update_chart_labels()
- # For sharing
- if self._sharing:
- data = (new_text)
- self.send.set_y_label(data)
-
def _set_chart_color(self, widget, pspec):
self.chart_color = utils.rgb_to_html(widget.get_color())
self._render_chart()
- # For sharing
- if self._sharing:
- data = (self.chart_color)
- self.send.set_chart_color(data)
-
def _set_chart_line_color(self, widget, pspec):
self.chart_line_color = utils.rgb_to_html(widget.get_color())
self._render_chart()
- # For sharing
- if self._sharing:
- data = (self.chart_line_color)
- self.send.set_line_color(data)
-
# Sharing activity
def _setup_presence_service(self):
"""Setup the Presence Service."""
@@ -520,7 +475,7 @@ class SimpleGraph(activity.Activity):
def _joined_cb(self, activity):
"""...or join an exisiting share."""
self._new_tube_common(False)
- self._sharing = True
+ #self._sharing = True: FIXME, Mysterious bugs
def _new_tube_common(self, sharer):
"""Joining and sharing are mostly the same..."""
@@ -536,9 +491,6 @@ class SimpleGraph(activity.Activity):
self.tubes_chan = self._shared_activity.telepathy_tubes_chan
self.text_chan = self._shared_activity.telepathy_text_chan
- self.receive = Receive(self, _logger)
- self.send = Send(self)
-
self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal(
'NewTube', self._new_tube_cb)
@@ -576,7 +528,28 @@ params=%r state=%d' % (id, initiator, type, service, params, state))
group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP])
self.chattube = ChatTube(tube_conn, self.initiating, \
- self.receive.event_received_cb)
+ self.chart_data_received_cb)
+
+ def chart_data_received_cb(self, data):
+ _io_str = StringIO(data)
+ chart_data = list(simplejson.load(_io_str))
+
+ self.labels_and_values.model.clear()
+ self.chart_data = []
+
+ # Load the data
+ for row in chart_data:
+ self._add_value(None, label=row[0], value=float(row[1]))
+
+ self.update_chart()
+
+ def _send_chart_data(self, data):
+ if self._sharing:
+ _io_str = StringIO()
+ simplejson.dump(tuple(data), _io_str)
+ _logger.info('Sending chart data...')
+
+ self.chattube.SendText(_io_str.getvalue())
def _object_chooser(self, mime_type, type_name):
chooser = ObjectChooser()
@@ -730,6 +703,30 @@ params=%r state=%d' % (id, initiator, type, service, params, state))
self.update_chart()
+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
+
+
class ChartData(gtk.TreeView):
__gsignals__ = {
@@ -767,11 +764,9 @@ class ChartData(gtk.TreeView):
self.append_column(column)
self.set_enable_search(False)
- self.activity = activity
-
self.show_all()
- def add_value(self, label, value, pos='auto'):
+ def add_value(self, label, value):
selected = self.get_selection().get_selected()[1]
if not selected:
path = 0
@@ -779,15 +774,11 @@ class ChartData(gtk.TreeView):
elif selected:
path = self.model.get_path(selected)[0] + 1
- if pos != 'auto':
- path = pos
-
iter = self.model.insert(path, [label, value])
- if not self.activity._sharing:
- self.set_cursor(self.model.get_path(iter),
- self.get_column(1),
- True)
+ self.set_cursor(self.model.get_path(iter),
+ self.get_column(1),
+ True)
_logger.info("Added: %s, Value: %s" % (label, value))
diff --git a/sharing.py b/sharing.py
deleted file mode 100644
index 00bc161..0000000
--- a/sharing.py
+++ /dev/null
@@ -1,190 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-# sharing.py by:
-# Agustin Zubiaga <aguz@sugarlabs.org>
-
-# 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.
-#
-# 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
-
-import utils
-
-from dbus.service import signal
-from dbus.gobject_service import ExportedGObject
-
-# Tube
-SERVICE = 'org.sugarlabs.SimpleGraph'
-IFACE = SERVICE
-PATH = '/org/sugarlabs/SimpleGraph'
-
-
-class Receive(object):
-
- def __init__(self, parent, logger):
- """A class for receive (and process)"""
- super(Receive, self).__init__()
-
- self._parent = parent
- self._logger = logger
- self._processing_methods = None
-
- self._setup_dispatch_table()
-
- def _setup_dispatch_table(self):
- """Associate tokens with commands."""
- self._processing_methods = {
- 'a': [self._add_value, 'value added'],
- 'r': [self._remove_value, 'value removed'],
- 'v': [self._value_changed, 'value changed'],
- 'l': [self._label_changed, 'label changed'],
- 't': [self._type_changed, 'chart type changed'],
- 'x': [self._set_x_label, 'x label changed'],
- 'y': [self._set_y_label, 'y label changed'],
- 'cc': [self._set_chart_color, 'chart color changed'],
- 'lc': [self._set_line_color, 'line color changed'],
- }
-
- def event_received_cb(self, event_message):
- """Data from a tube has arrived."""
- if len(event_message) == 0:
- return
- try:
- command, payload = event_message.split('|', 2)
- except ValueError:
- self._logger.debug('Could not split event \
- message %s' % (event_message))
- return
-
- data = utils.json_load(payload)
- event = self._processing_methods[command]
-
- self._logger.info('Event received: %s' % (event[1]))
-
- event[0](payload)
- self._logger.info('Processing data: %s' % (data))
-
- def _add_value(self, data):
- self._parent._add_value(None, data[0], data[1], data[2])
-
- def _remove_value(self, data):
- self._parent.labels_and_values.model.remove(data[0])
- del self._parent.chart_data[data[0]]
- self._parent._update_chart_data()
-
- def _value_changed(self, data):
- self._parent._value_changed(None, data[0], data[1])
-
- def _label_changed(self, data):
- self._parent._label_changed(None, data[0], data[1])
-
- def _type_changed(self, data):
- self._parent.current_chart.type = data
- self._parent._render_chart()
-
- def _set_x_label(self, data):
- self._parent.x_label = data
- self._parent._update_chart_labels()
-
- def _set_y_label(self, data):
- self._parent.y_label = data
- self._parent._update_chart_labels()
-
- def _set_chart_color(self, data):
- self._parent.chart_color = data
- self._parent._render_chart()
-
- def _set_line_color(self, data):
- self._parent.chart_line_color = data
- self._parent._render_chart()
-
-
-class Send(object):
-
- def __init__(self, parent):
- """A class for send data"""
- super(Send, self).__init__()
-
- self._parent = parent
-
- def _send(self, data):
- self._parent.chattube.SendText(data)
-
- def add_value(self, data):
- dump = utils.json_dump(data)
- data = "a|%s" % (dump)
- self._send(data)
-
- def remove_value(self, data):
- dump = utils.json_dump(data)
- data = "r|%s" % (dump)
- self._send(data)
-
- def value_changed(self, data):
- dump = utils.json_dump(data)
- data = "v|%s" % (dump)
- self._send(data)
-
- def label_changed(self, data):
- dump = utils.json_dump(data)
- data = "l|%s" % (dump)
- self._send(data)
-
- def type_changed(self, data):
- dump = utils.json_dump(data)
- data = "t|%s" % (dump)
- self._send(data)
-
- def set_x_label(self, data):
- dump = utils.json_dump(data)
- data = "x|%s" % (dump)
- self._send(data)
-
- def set_y_label(self, data):
- dump = utils.json_dump(data)
- data = "y|%s" % (dump)
- self._send(data)
-
- def set_chart_color(self, data):
- dump = utils.json_dump(data)
- data = "cc|%s" % (dump)
- self._send(data)
-
- def set_line_color(self, data):
- dump = utils.json_dump(data)
- data = "lc|%s" % (dump)
- self._send(data)
-
-
-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
diff --git a/utils.py b/utils.py
index a6f0545..01741df 100644
--- a/utils.py
+++ b/utils.py
@@ -20,9 +20,6 @@
import os
import gconf
-import json
-
-from StringIO import StringIO
def rgb_to_html(color):
@@ -67,17 +64,3 @@ def get_chart_file(activity_dir):
def get_decimals(number):
"""Returns the decimals count of a number"""
return str(len(number.split('.')[1]))
-
-
-def json_load(data):
- _io_str = StringIO(data)
- data = json.load(_io_str)
-
- return data
-
-
-def json_dump(data):
- _io_str = StringIO()
- json.dump(data, _io_str)
-
- return _io_str.getvalue()