Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/webactivity.py
blob: ccb893fa11f781f643aed97aaed0e73acc917c82 (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
# 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 gettext import gettext as _
import gtk
import gtkmozembed
import logging
import dbus

import _sugar
from sugar.activity import ActivityFactory
from sugar.activity.Activity import Activity
from sugar.clipboard import clipboardservice
from sugar import env
from sugar.graphics import style

import stylesheet
from webview import WebView
from toolbar import Toolbar
from linksmodel import LinksModel
from linksview import LinksView
from linkscontroller import LinksController

_HOMEPAGE = 'http://www.google.com'

class WebActivity(Activity):
    def __init__(self, browser=None):
        Activity.__init__(self)

        logging.debug('Starting the web activity')

        self.set_title(_('Web Activity'))

        vbox = gtk.VBox()

        if browser:
            self._browser = browser
        else:
            self._browser = WebView()
        self._browser.connect('notify::title', self._title_changed_cb)

        self._links_model = LinksModel()
        links_view = LinksView(self._links_model, self._browser)

        self._toolbar = Toolbar(self._browser)
        vbox.pack_start(self._toolbar, False)
        self._toolbar.show()

        hbox = gtk.HBox()

        hbox.pack_start(links_view, False)
        hbox.pack_start(self._browser)
        self._browser.show()

        vbox.pack_start(hbox)
        hbox.show()

        self.add(vbox)
        vbox.show()

        self._browser.load_url(_HOMEPAGE)

    def _setup_links_controller(self):
        links_controller = LinksController(self._service, self._links_model)
        self._toolbar.set_links_controller(links_controller)

    def join(self, activity_ps):
        Activity.join(self, activity_ps)

        self._setup_links_controller()

        url = self._service.get_published_value('URL')
        if url:
            self._browser.load_url(url)

    def share(self):
        Activity.share(self)

        self._setup_links_controller()

        url = self._browser.get_location()
        if url:
            self._service.set_published_value('URL', url)

    def execute(self, command, args):
        if command == "load-uri":
            self._browser.load_url(args[0])

    def _title_changed_cb(self, embed, pspec):
        self.set_title(embed.props.title)

def start():
    gtkmozembed.set_profile_path(env.get_profile_path(), 'gecko')

    gtkmozembed.push_startup()
    if not _sugar.startup_browser():
        raise "Error when initializising the web activity."

    style.load_stylesheet(stylesheet)
    
    download_manager = _sugar.get_download_manager()
    download_manager.connect('download-started', download_started_cb)
    download_manager.connect('download-completed', download_completed_cb)
    download_manager.connect('download-cancelled', download_started_cb)
    download_manager.connect('download-progress', download_progress_cb)

def stop():
    gtkmozembed.pop_startup()

def download_started_cb(download_manager, download):
    name = download.get_url().rsplit('/', 1)[1]
    object_id = download.get_file_name() # The file name passed is already unique.

    cb_service = clipboardservice.get_instance()
    cb_service.add_object(object_id, name)
    cb_service.add_object_format(object_id,
                                 download.get_mime_type(),
                                 download.get_file_name(),
                                 on_disk = True)

def download_completed_cb(download_manager, download):
    cb_service = clipboardservice.get_instance()
    cb_service.set_object_state(download.get_file_name(), 100)

def download_cancelled_cb(download_manager, download):
    #FIXME: Needs to update the state of the object to 'download stopped'.
    #FIXME: Will do it when we complete progress on the definition of the
    #FIXME: clipboard API.
    raise "Cancelling downloads still not implemented."

def download_progress_cb(download_manager, download):
    cb_service = clipboardservice.get_instance()
    cb_service.set_object_state(download.get_file_name(), download.get_percent())