Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/linkscontroller.py
blob: 1004fc6c4a3279b974c9784faf3712b7fe120961 (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
# Copyright (C) 2006, Red Hat, Inc.
#
# 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

from sugar.p2p.Stream import Stream
from sugar.presence import PresenceService

class _Marshaller(object):
    def __init__(self, title, address):
        pservice = PresenceService.get_instance()
        name = pservice.get_owner().get_name()
        self._message = name + '\n' + title + '\n' + address

    def get_message(self):
        return self._message

class _Demarshaller(object):
    def __init__(self, message):
        self._pservice = PresenceService.get_instance()
        self._split_msg = message.split('\n')

    def get_buddy(self):
        return self._pservice.get_buddy_by_name(self._split_msg[0])

    def get_title(self):
        return self._split_msg[1]

    def get_address(self):
        return self._split_msg[2]

class LinksController(object):
    def __init__(self, service, model):
        self._model = model

        self._stream = Stream.new_from_service(service)
        self._stream.set_data_listener(self._recv_message)
        self._stream_writer = self._stream.new_writer()

    def post_link(self, title, address):
        marshaller = _Marshaller(title, address)
        self._stream_writer.write(marshaller.get_message())

    def _recv_message(self, address, msg):
        demarshaller = _Demarshaller(msg)
        buddy = demarshaller.get_buddy()
        if buddy:
            self._model.add_link(buddy, demarshaller.get_title(),
                                 demarshaller.get_address())