Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/wirelessgraphactivity.py
blob: 12e18143cb94aaf3f6980fa284228cfec45753b3 (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
# Copyright (C) 2009, Martin Dengler <martin@martindengler.com>
#
# 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

import logging

import gtk

from gettext import gettext as _
from sugar import profile
from sugar.activity import activity
from sugar.graphics import style
from sugar.graphics import xocolor
from sugar.graphics.icon import Icon

from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton

from wirelessgraph import Graph
from wirelessdataseries import WirelessDataSeries, get_wireless_interfaces

class WirelessGraphActivity(activity.Activity):
    def __init__(self, handle):
        activity.Activity.__init__(self, handle)

        self._logger = logging.getLogger("WirelessGraph")
        self._logger.info("WirelessGraphActivity self.show()")

        self.set_title(_('WirelessGraph'))

        self.max_participants = 1

        self._build_toolbox()
        self._build_mainbox()
        self._build_series()

    def _build_mainbox(self):
        self.vbox = gtk.VBox()

        if len(get_wireless_interfaces()) > 0:
            self.graph = Graph()
            self.vbox.pack_start(self.graph)
        else:
            icon = Icon(pixel_size=style.XLARGE_ICON_SIZE,
                        icon_name='icon-missing',
                        xo_color=xocolor.XoColor("%s,%s" % (
                        style.COLOR_BUTTON_GREY.get_svg(),
                        style.COLOR_TRANSPARENT.get_svg())))
            text = gtk.Label()
            text.set_markup('<span size="xx-large">'
                            'No wireless devices found</span>')
            text.set_line_wrap(True)
            text.set_alignment(0.5, 0)
            text.set_justify(gtk.JUSTIFY_CENTER)
            self.vbox.pack_start(icon)
            self.vbox.pack_start(text)
            self.vbox.show_all()

        self.set_canvas(self.vbox)
        self.vbox.show_all()

    def _build_series(self):
        self.series = []

        interfaces = get_wireless_interfaces()
        if len(interfaces) > 0:
            series = WirelessDataSeries(series_name=profile.get_nick_name(),
                                        series_color=profile.get_color(),
                                        interface=interfaces[0])
            self.series.append(series)
            self.graph.add_series(series)
            for interface in interfaces[1:]:
                series_name = "%s-%s" % (profile.get_nick_name(), interface)
                series_colors = xocolor.colors[hash(series_name)
                                               % len(xocolor.colors)]
                series_xocolor = xocolor.XoColor(",".join(series_colors))
                series = WirelessDataSeries(series_name=series_name,
                                            series_color=series_xocolor,
                                            interface=interface)
                self.series.append(series)
                self.graph.add_series(series)

    def _build_toolbox(self):
        toolbox = ToolbarBox()
        toolbox.show()
        self.set_toolbox(toolbox)
        
        activity_button = ActivityToolbarButton(self)
        toolbox.toolbar.insert(activity_button, -1)
        activity_button.show()

        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbox.toolbar.insert(separator, -1)

        stop_button = StopButton(self)
        toolbox.toolbar.insert(stop_button, -1)
        stop_button.show()

        self.set_toolbar_box(toolbox)

        toolbox.show_all()