Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mmm_modules/buddy_panel.py
blob: 3255382f4d2376a41c5d19ff29f062d38af10c60 (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
# Copyright 2007 World Wide Workshop Foundation
#
# 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 !
#

from gi.repository import Gtk

import logging

from tube_helper import GAME_IDLE, GAME_STARTED, GAME_FINISHED, GAME_QUIT

#from sugar3.graphics.icon import CanvasIcon

BUDDYMODE_CONTEST = 0
BUDDYMODE_COLLABORATION = 1

class BuddyPanel (Gtk.ScrolledWindow):
    def __init__ (self, mode=BUDDYMODE_CONTEST):
        super(BuddyPanel, self).__init__()
        self.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)

        self.model = Gtk.ListStore(str, str, str, str)
        self.model.set_sort_column_id(0, Gtk.SortType.ASCENDING)
        self.treeview = Gtk.TreeView()

        #col = gtk.TreeViewColumn(_("Icon"))
        #r = gtk.CellRendererText()
        #col.pack_start(r, True)
        #col.set_attributes(r, stock_id=0)
        #self.treeview.append_column(col)

        col = Gtk.TreeViewColumn(_("Buddy"))
        r = Gtk.CellRendererText()
        col.pack_start(r, True, True, 0)
        col.set_attributes(r, text=0)
        self.treeview.append_column(col)

        col = Gtk.TreeViewColumn(_("Status"))
        r = Gtk.CellRendererText()
        col.pack_start(r, True, True, 0)
        col.set_attributes(r, text=1)
        self.treeview.append_column(col)
        col.set_visible(mode == BUDDYMODE_CONTEST)

        col = Gtk.TreeViewColumn(_("Play Time"))
        r = Gtk.CellRendererText()
        col.pack_start(r, True, True, 0)
        col.set_attributes(r, text=2)
        self.treeview.append_column(col)
        col.set_visible(mode == BUDDYMODE_CONTEST)

        col = Gtk.TreeViewColumn(_("Joined at"))
        r = Gtk.CellRendererText()
        col.pack_start(r, True, True, 0)
        col.set_attributes(r, text=3)
        self.treeview.append_column(col)
        col.set_visible(mode == BUDDYMODE_COLLABORATION)
        
        self.treeview.set_model(self.model)

        self.add(self.treeview)
        self.show_all()

        self.players = {}

    def add_player (self, buddy, current_clock=0):
        """ Adds a player to the panel """
        op = buddy.object_path()
        if self.players.get(op) is not None:
            return

#        buddy_color = buddy.props.color
#        if not buddy_color:
#            buddy_color = "#000000,#ffffff"
#
#        icon = CanvasIcon(
#            icon_name='computer-xo',
#            xo_color=XoColor(buddy_color))
#
        nick = buddy.props.nick
        if not nick:
            nick = ""
        self.players[op] = (buddy, self.model.append([nick,
                                                      _('synchronizing'),
                                                      '',
                                                      '']))
        return nick

    def update_player (self, buddy, status, clock_running, time_ellapsed):
        """Since the current target build (432) does not fully support the contest mode, we are removing this for now. """
        #return
        op = buddy.object_path()
        if self.players.get(op, None) is None:
            logging.debug("Player %s not found" % op)
            return
        logging.debug(self.players[op])
        if status == GAME_STARTED[1]:
            stat = clock_running and _("Playing") or _("Paused")
        elif status == GAME_FINISHED[1]:
            stat = _("Finished")
        elif status == GAME_QUIT[1]:
            stat = _("Gave up")
        else:
            stat = _("Unknown")
        self.model.set_value(self.players[op][1], 1, stat)
        self.model.set_value(self.players[op][1], 2, _("%i minutes") % (time_ellapsed/60))
        self.model.set_value(self.players[op][1], 3, '%i:%0.2i' % (int(time_ellapsed / 60), int(time_ellapsed % 60)))
        return (self.model.get_value(self.players[op][1], 0), self.model.get_value(self.players[op][1], 1))
        
    def get_buddy_from_path (self, object_path):
        logging.debug("op = " + object_path)
        logging.debug(self.players)
        return self.players.get(object_path, None)
        
    def remove_player (self, buddy):
        op = buddy.object_path()
        if self.players.get(op) is None:
            return
        nick = buddy.props.nick
        if not nick:
            nick = ""
        self.model.remove(self.players[op][1])
        del self.players[op]
        return nick