Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/usercode.py
blob: 21c2fe67ed5a42e48adb1f964822372c6a722556 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (C) 2009, Lucian Branescu Mihaila
#
# 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 os
import logging
from gettext import gettext as _

import gobject
import gtk
import pango
import gtksourceview2

import xpcom
from xpcom import components
from xpcom.components import interfaces

from sugar.activity import activity
from sugar.graphics import style
from sugar.graphics.icon import Icon

import viewsource


SCRIPTS_PATH = os.path.join(activity.get_activity_root(),
                            'data/userscripts')
STYLE_PATH = os.path.join(activity.get_activity_root(),
                          'data/style.user.css')

class Dialog(gtk.Window):
    def __init__(self, width=None, height=None):        
        self.width = width or int(gtk.gdk.screen_width()/2)
        self.height = height or int(gtk.gdk.screen_height()/1.5)

        gtk.Window.__init__(self)
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        self.set_default_size(self.width, self.height)

    def show(self):
        self.show_all()
        gtk.Window.show(self)

class SourceEditor(viewsource.SourceDisplay):
    def __init__(self):
        viewsource.SourceDisplay.__init__(self)
        
        self._source_view.set_editable(True)
        
    def get_text(self):
        start = self._buffer.get_start_iter()
        end = self._buffer.get_end_iter()
        return self._buffer.get_text(start, end)

    def set_text(self, text):
        self._buffer.set_text(text)

    text = property(get_text, set_text)
    
    def write(self, path=None):
        open(path or self.file_path, 'w').write(self.text)

class ScriptFileViewer(viewsource.FileViewer):
    def __init__(self, path):
        initial_filename = os.listdir(path)[0]
        viewsource.FileViewer.__init__(self, path, initial_filename)

    def get_selected_file(self):
        selection = self._tree_view.get_selection()
        model, tree_iter = selection.get_selected()
        if tree_iter is None:
            return None
        else:
            return model.get_value(tree_iter, 1)

    def remove_file(self, file_path):
        model = self._tree_view.get_model()
        for i in model:
            if i[0] == os.path.basename(file_path):
                model.remove(model.get_iter(i.path))
                break

class StyleEditor(Dialog):
    __gsignals__ = {
        'userstyle-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                              ([])),
    }
    
    def __init__(self):
        Dialog.__init__(self)
        
        # layout
        vbox = gtk.VBox()
        
        self._editor = SourceEditor()
        self._editor.file_path = STYLE_PATH
        vbox.pack_start(self._editor)
        
        # buttons
        buttonbox = gtk.HBox()
        
        self._cancel_button = gtk.Button(label=_('Cancel'))
        self._cancel_button.set_image(Icon(icon_name='dialog-cancel'))
        self._cancel_button.connect('clicked', self._cancel_button_cb)
        buttonbox.pack_start(self._cancel_button)
        
        self._save_button = gtk.Button(label=_('Save'))
        self._save_button.set_image(Icon(icon_name='dialog-ok'))
        self._save_button.connect('clicked', self._save_button_cb)
        buttonbox.pack_start(self._save_button)
                                   
        vbox.pack_start(buttonbox, expand=False)
        
        self.add(vbox)

    def _save_button_cb(self, button):
        self._editor.write()
        self.emit('userstyle-changed')
        self.destroy()
        
    def _cancel_button_cb(self, button):
        self.destroy()

class ScriptEditor(Dialog):
    def __init__(self):
        Dialog.__init__(self)
                                        
        # layout
        hbox = gtk.HBox()
        
        self._fileview = ScriptFileViewer(SCRIPTS_PATH)
        self._fileview.connect('file-selected', self._file_selected_cb)
        hbox.pack_start(self._fileview, expand=False)
        
        editbox = gtk.VBox()        
        self._editor = SourceEditor()
        editbox.pack_start(self._editor)
              
        buttonbox = gtk.HBox()
        
        self._cancel_button = gtk.Button(label=_('Close'))
        self._cancel_button.set_image(Icon(icon_name='dialog-cancel'))
        self._cancel_button.connect('clicked', self._cancel_button_cb)
        buttonbox.pack_start(self._cancel_button)
        
        self._delete_button = gtk.Button(label=_('Delete'))
        self._delete_button.set_image(Icon(icon_name='stock_delete'))
        self._delete_button.connect('clicked', self._delete_button_cb)
        buttonbox.pack_start(self._delete_button)
        
        self._save_button = gtk.Button(label=_('Save'))
        self._save_button.set_image(Icon(icon_name='dialog-ok'))
        self._save_button.connect('clicked', self._save_button_cb)
        buttonbox.pack_start(self._save_button)
        
        editbox.pack_start(buttonbox, expand=False)
        hbox.pack_start(editbox)
        
        self.add(hbox)
        
        self._file_selected_cb(self._fileview, 
                               self._fileview._initial_filename)
    
    def _save_button_cb(self, button):
        self._editor.write()
        
    def _delete_button_cb(self, button):
        file_path = self._fileview.get_selected_file()
        
        self._fileview.remove_file(file_path)
        os.remove(file_path)
        
    def _cancel_button_cb(self, button):
        self.destroy()
        
    def _file_selected_cb(self, view, file_path):
        self._editor.file_path = self._fileview.get_selected_file()


def add_script(location):
    cls = components.classes["@mozilla.org/network/io-service;1"]
    io_service = cls.getService(interfaces.nsIIOService)

    cls = components.classes[ \
                        '@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
    browser_persist = cls.getService(interfaces.nsIWebBrowserPersist)


    location_uri = io_service.newURI(location, None, None)

    file_name = os.path.basename(location_uri.path)
    file_path = os.path.join(SCRIPTS_PATH, file_name)
    file_uri = io_service.newURI('file://'+file_path, None, None)

    logging.debug('##### %s -> %s' % (location_uri.spec, file_uri.spec))

    # make sure the file doesn't already exist
    try: os.remove(file_path)
    except OSError: pass
    
    browser_persist.saveURI(location_uri, None, None, None, None, file_uri)

def script_exists(location):
    cls = components.classes["@mozilla.org/network/io-service;1"]
    io_service = cls.getService(interfaces.nsIIOService)
    location_uri = io_service.newURI(location, None, None)
    
    if os.path.isfile(os.path.join(SCRIPTS_PATH, location_uri.path)):
        return True
    else:
        return False

class Injector():
    _com_interfaces_ = interfaces.nsIDOMEventListener
    
    def __init__(self, script_path):
        self.script_path = script_path
             
        self._wrapped = xpcom.server.WrapObject(self,
                                                interfaces.nsIDOMEventListener)
    
    def handleEvent(self, event):
        self.head.appendChild(self.script)
    
    def attach_to(self, window):
        # set up the script element to be injected
        self.script = window.document.createElement('script')
        self.script.type = 'text/javascript'
        #self.script.src = 'file://' + self.script_path # XSS security fail
        text = open(self.script_path,'r').read()
        self.script.appendChild( window.document.createTextNode(text) )
        
        # reference to head
        self.head = window.document.getElementsByTagName('head').item(0)
        
        # actual attaching
        window.addEventListener('load', self._wrapped, False)
    
class ScriptListener(gobject.GObject):
    _com_interfaces_ = interfaces.nsIWebProgressListener

    __gsignals__ = {
        'userscript-found': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                             ([str])),
        'userscript-inject': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                             ([str])),
    }

    def __init__(self):
        gobject.GObject.__init__(self)

        self._wrapped = xpcom.server.WrapObject( \
                self, interfaces.nsIWebProgressListener)
    
    def onLocationChange(self, webProgress, request, location):
        if location.spec.endswith('.user.js'):
            self.emit('userscript-found', location.spec)
        else:
            # TODO load scripts according to domain regex
            for i in os.listdir(SCRIPTS_PATH):                
                script_path = os.path.join(SCRIPTS_PATH, i)
                self.emit('userscript-inject', script_path)

    def setup(self, browser):
        browser.web_progress.addProgressListener(self._wrapped, 
                                interfaces.nsIWebProgress.NOTIFY_LOCATION)