-
Notifications
You must be signed in to change notification settings - Fork 44
/
palettes.py
265 lines (220 loc) · 9.9 KB
/
palettes.py
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
# Copyright (C) 2008, One Laptop Per Child
# Copyright (C) 2009, Tomeu Vizoso, Simon Schampijer
# Copyright (C) 2015, Sam Parkinson
#
# 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 os
import tempfile
import urllib.request
import urllib.error
import urllib.parse
from gettext import gettext as _
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import SugarGestures
from sugar3.graphics.palette import Palette, Invoker
from sugar3.graphics.palettemenu import PaletteMenuItem
from sugar3.graphics.palettemenu import PaletteMenuItemSeparator
from sugar3 import profile
class ContentInvoker(Invoker):
def __init__(self, browser):
Invoker.__init__(self)
self._position_hint = self.AT_CURSOR
self._browser = browser
self._browser.connect('context-menu', self.__context_menu_cb)
self._browser.connect('realize', self.__browser_realize_cb)
def __browser_realize_cb(self, browser):
x11_window = browser.get_window()
x11_window.set_events(x11_window.get_events() |
Gdk.EventMask.POINTER_MOTION_MASK |
Gdk.EventMask.TOUCH_MASK)
lp = SugarGestures.LongPressController()
lp.connect('pressed', self.__long_pressed_cb)
lp.attach(browser, SugarGestures.EventControllerFlags.NONE)
def __long_pressed_cb(self, controller, x, y):
# We can't force a context menu, but we can fake a right mouse click
event = Gdk.Event()
event.type = Gdk.EventType.BUTTON_PRESS
b = event.button
b.type = Gdk.EventType.BUTTON_PRESS
b.window = self._browser.get_window()
b.time = Gtk.get_current_event_time()
b.button = 3 # Right
b.x = x
b.y = y
b.x_root, b.y_root = self._browser.get_window().get_root_coords(x, y)
Gtk.main_do_event(event)
return True
def get_default_position(self):
return self.AT_CURSOR
def get_rect(self):
allocation = self._browser.get_allocation()
window = self._browser.get_window()
if window is not None:
res, x, y = window.get_origin()
else:
logging.warning(
"Trying to position palette with invoker that's not realized.")
x = 0
y = 0
x += allocation.x
y += allocation.y
width = allocation.width
height = allocation.height
rect = Gdk.Rectangle()
rect.x = x
rect.y = y
rect.width = width
rect.height = height
return rect
def get_toplevel(self):
return None
def __context_menu_cb(self, webview, context_menu, event, hit_test):
self.palette = BrowsePalette(self._browser, hit_test)
self.notify_right_click()
# Don't show the default menu
return True
class BrowsePalette(Palette):
def __init__(self, browser, hit):
Palette.__init__(self)
self._browser = browser
self._hit = hit
# Have to set document.title,
# see http://comments.gmane.org/gmane.os.opendarwin.webkit.gtk/1981
self._browser.run_javascript('''
document.SugarBrowseOldTitle = document.title;
document.title = (function () {
if (window.getSelection) {
return window.getSelection().toString();
} else if (document.selection &&
document.selection.type != "Control") {
return document.selection.createRange().text;
}
return '';
})()''', None, self.__after_get_text_cb, None)
def __after_get_text_cb(self, browser, async_result, user_data):
self._all_text = self._browser.props.title
self._browser.run_javascript(
'document.title = document.SugarBrowseOldTitle')
self._link_text = self._hit.props.link_label \
or self._hit.props.link_title
self._title = self._link_text or self._all_text
self._url = self._hit.props.link_uri or self._hit.props.image_uri \
or self._hit.props.media_uri
self._image_url = self._hit.props.image_uri \
or self._hit.props.media_uri
if self._title not in (None, ''):
self.props.primary_text = GLib.markup_escape_text(self._title)
if self._url is not None:
self.props.secondary_text = GLib.markup_escape_text(self._url)
else:
if self._url is not None:
self.props.primary_text = GLib.markup_escape_text(self._url)
if not self._all_text and not self._url:
self.popdown(immediate=True)
return # Nothing to see here!
menu_box = Gtk.VBox()
self.set_content(menu_box)
menu_box.show()
self._content.set_border_width(1)
first_section_added = False
if self._hit.context_is_link():
first_section_added = True
menu_item = PaletteMenuItem(_('Follow link'), 'browse-follow-link')
menu_item.connect('activate', self.__follow_activate_cb)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
menu_item = PaletteMenuItem(_('Follow link in new tab'),
'browse-follow-link-new-tab')
menu_item.connect('activate', self.__follow_activate_cb, True)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
# Add "keep link" only if it is not an image. "Keep
# image" will be shown in that case.
if not self._hit.context_is_image():
menu_item = PaletteMenuItem(_('Keep link'), 'document-save')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__download_activate_cb)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
menu_item = PaletteMenuItem(_('Copy link'), 'edit-copy')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__copy_cb, self._url)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
if self._link_text:
menu_item = PaletteMenuItem(_('Copy link text'), 'edit-copy')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__copy_cb, self._link_text)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
if self._hit.context_is_image():
if not first_section_added:
first_section_added = True
else:
separator = PaletteMenuItemSeparator()
menu_box.pack_start(separator, False, False, 0)
separator.show()
# FIXME: Copy image is broken
menu_item = PaletteMenuItem(_('Copy image'), 'edit-copy')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__copy_image_activate_cb)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
menu_item = PaletteMenuItem(_('Keep image'), 'document-save')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__download_activate_cb,
self._image_url)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
if self._hit.context_is_selection() and self._all_text:
if not first_section_added:
first_section_added = True
else:
separator = PaletteMenuItemSeparator()
menu_box.pack_start(separator, False, False, 0)
separator.show()
menu_item = PaletteMenuItem(_('Copy text'), 'edit-copy')
menu_item.icon.props.xo_color = profile.get_color()
menu_item.connect('activate', self.__copy_cb, self._all_text)
menu_box.pack_start(menu_item, False, False, 0)
menu_item.show()
def __follow_activate_cb(self, menu_item, new_tab=False):
if new_tab:
self._browser.open_new_tab(self._url)
else:
self._browser.load_uri(self._url)
self._browser.grab_focus()
def __download_activate_cb(self, menu_item, url=None):
self._browser.download_uri(url or self._url)
def __copy_image_activate_cb(self, menu_item):
# Download the image
temp_file = tempfile.NamedTemporaryFile(delete=False)
user_agent = self._browser.get_settings().props.user_agent
req = urllib.request.Request(self._image_url)
req.add_header('User-Agent', user_agent)
data = urllib.request.urlopen(req).read()
temp_file.write(data)
temp_file.close()
# Copy it inside the clipboard
image = Gtk.Image.new_from_file(temp_file.name)
os.unlink(temp_file.name)
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_image(image.get_pixbuf())
def __copy_cb(self, menu_item, text):
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)