71 | | def handleEvent(self, event): |
72 | | if event.button != 2: |
73 | | return |
| 73 | def __button_press_cb(self, browser, event): |
| 74 | logging.debug('===> event: %s', event.button) |
| 75 | if event.button != 3: |
| 76 | return False |
| 77 | hit_test = self._browser.get_hit_test_result(event) |
| 78 | if hit_test.props.context & WebKit.HitTestResultContext.LINK: |
| 79 | logging.debug('===> click LINK') |
| 80 | link_uri = hit_test.props.link_uri |
| 81 | if isinstance(hit_test.props.inner_node, |
| 82 | WebKit.DOMHTMLImageElement): |
| 83 | title = hit_test.props.inner_node.get_title() |
| 84 | elif isinstance(hit_test.props.inner_node, WebKit.DOMNode): |
| 85 | title = hit_test.props.inner_node.get_text_content() |
| 86 | self.palette = LinkPalette(self._browser, title, link_uri, None) |
| 87 | self.notify_right_click() |
| 88 | elif hit_test.props.context & WebKit.HitTestResultContext.IMAGE: |
| 89 | logging.debug('===> click IMAGE %s %s', |
| 90 | hit_test.props.image_uri, hit_test.props.inner_node) |
| 91 | title = hit_test.props.inner_node.get_title() |
| 92 | self.palette = ImagePalette(self._browser, title, |
| 93 | hit_test.props.image_uri, '') |
| 94 | self.notify_right_click() |
| 95 | elif hit_test.props.context & WebKit.HitTestResultContext.SELECTION: |
| 96 | # TODO: find a way to get the selected text so we can use |
| 97 | # it as the title of the Palette. |
| 98 | # The function webkit_web_view_get_selected_text was removed |
| 99 | # https://bugs.webkit.org/show_bug.cgi?id=62512 |
| 100 | title = None |
| 101 | |
| 102 | # text = hit_test.props.inner_node.get_text_content() |
| 103 | # import epdb;epdb.set_trace() |
| 104 | # if len(text) > 20: |
| 105 | # title = text[:20] + '...' |
| 106 | # else: |
| 107 | # title = text |
| 108 | self.palette = SelectionPalette(self._browser, title, None, None) |
| 109 | self.notify_right_click() |
84 | | self.palette = LinkPalette(self._browser, title, target.href, |
85 | | target.ownerDocument) |
86 | | self.notify_right_click() |
87 | | elif target.tagName.lower() == 'img': |
88 | | if target.title: |
89 | | title = target.title |
90 | | elif target.title: |
91 | | title = target.alt |
92 | | elif target.name: |
93 | | title = target.name |
94 | | else: |
95 | | title = os.path.basename(urlparse.urlparse(target.src).path) |
96 | | |
97 | | self.palette = ImagePalette(title, target.src, |
98 | | target.ownerDocument) |
99 | | self.notify_right_click() |
100 | | else: |
101 | | return |
| 118 | self._browser = browser |
| 119 | self._title = title |
| 120 | self._url = url |
| 121 | self._owner_document = owner_document |
| 122 | |
| 123 | menu_box = Gtk.VBox() |
| 124 | self.set_content(menu_box) |
| 125 | menu_box.show() |
| 126 | self._content.set_border_width(1) |
107 | | self._mouseout_listener = MouseOutListener(target) |
108 | | wrapper = xpcom.server.WrapObject(self._mouseout_listener, |
109 | | interfaces.nsIDOMEventListener) |
110 | | target.addEventListener('mouseout', wrapper, False) |
111 | | self._mouseout_listener.connect('mouse-out', self.__moved_out_cb) |
| 130 | menu_item = SugarMenuItem(_('Copy text'), 'edit-copy') |
| 131 | menu_item.icon.props.xo_color = profile.get_color() |
| 132 | menu_item.connect('clicked', self.__copy_activate_cb) |
| 133 | menu_box.pack_end(menu_item, False, False, 0) |
| 134 | menu_item.show() |
140 | | menu_item = MenuItem(_('Follow link'), 'browse-follow-link') |
141 | | menu_item.connect('activate', self.__follow_activate_cb) |
142 | | self.menu.append(menu_item) |
| 160 | menu_box = Gtk.VBox() |
| 161 | self.set_content(menu_box) |
| 162 | menu_box.show() |
| 163 | self._content.set_border_width(1) |
| 164 | |
| 165 | menu_item = SugarMenuItem(_('Follow link'), 'browse-follow-link') |
| 166 | menu_item.connect('clicked', self.__follow_activate_cb) |
| 167 | menu_box.pack_end(menu_item, False, False, 0) |
145 | | menu_item = MenuItem(_('Follow link in new tab'), |
146 | | 'browse-follow-link-new-tab') |
147 | | menu_item.connect('activate', self.__follow_activate_cb, True) |
148 | | self.menu.append(menu_item) |
| 170 | menu_item = SugarMenuItem(_('Follow link in new tab'), |
| 171 | 'browse-follow-link-new-tab') |
| 172 | menu_item.connect('clicked', self.__follow_activate_cb, True) |
| 173 | menu_box.pack_end(menu_item, False, False, 0) |
151 | | menu_item = MenuItem(_('Keep link')) |
152 | | icon = Icon(icon_name='document-save', xo_color=profile.get_color(), |
153 | | icon_size=Gtk.IconSize.MENU) |
154 | | menu_item.set_image(icon) |
155 | | menu_item.connect('activate', self.__download_activate_cb) |
156 | | self.menu.append(menu_item) |
| 176 | menu_item = SugarMenuItem(_('Keep link'), 'document-save') |
| 177 | menu_item.icon.props.xo_color = profile.get_color() |
| 178 | menu_item.connect('clicked', self.__download_activate_cb) |
| 179 | menu_box.pack_end(menu_item, False, False, 0) |
159 | | menu_item = MenuItem(_('Copy link')) |
160 | | icon = Icon(icon_name='edit-copy', xo_color=profile.get_color(), |
161 | | icon_size=Gtk.IconSize.MENU) |
162 | | menu_item.set_image(icon) |
163 | | menu_item.connect('activate', self.__copy_activate_cb) |
164 | | self.menu.append(menu_item) |
| 182 | menu_item = SugarMenuItem(_('Copy link'), 'edit-copy') |
| 183 | menu_item.icon.props.xo_color = profile.get_color() |
| 184 | menu_item.connect('clicked', self.__copy_activate_cb) |
| 185 | menu_box.pack_end(menu_item, False, False, 0) |
175 | | clipboard = Gtk.Clipboard() |
176 | | targets = Gtk.target_list_add_uri_targets() |
177 | | targets = Gtk.target_list_add_text_targets(targets) |
178 | | targets.append(('text/x-moz-url', 0, 0)) |
179 | | |
180 | | clipboard.set_with_data(targets, |
181 | | self.__clipboard_get_func_cb, |
182 | | self.__clipboard_clear_func_cb) |
183 | | |
184 | | def __clipboard_get_func_cb(self, clipboard, selection_data, info, data): |
185 | | uri_targets = \ |
186 | | [target[0] for target in Gtk.target_list_add_uri_targets()] |
187 | | text_targets = \ |
188 | | [target[0] for target in Gtk.target_list_add_text_targets()] |
189 | | |
190 | | if selection_data.target in uri_targets: |
191 | | selection_data.set_uris([self._url]) |
192 | | elif selection_data.target in text_targets: |
193 | | selection_data.set_text(self._url) |
194 | | elif selection_data.target == 'text/x-moz-url': |
195 | | selection_data.set('text/x-moz-url', 8, self._url) |
196 | | |
197 | | def __clipboard_clear_func_cb(self, clipboard, data): |
198 | | pass |
| 198 | self.popdown(immediate=True) |
| 199 | |
| 200 | clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) |
| 201 | clipboard.set_text(self._url, -1) |
212 | | self.props.primary_text = title |
213 | | self.props.secondary_text = url |
214 | | |
215 | | menu_item = MenuItem(_('Keep image')) |
216 | | icon = Icon(icon_name='document-save', xo_color=profile.get_color(), |
217 | | icon_size=Gtk.IconSize.MENU) |
218 | | menu_item.set_image(icon) |
219 | | menu_item.connect('activate', self.__download_activate_cb) |
220 | | self.menu.append(menu_item) |
221 | | menu_item.show() |
222 | | |
223 | | menu_item = MenuItem(_('Copy image')) |
224 | | icon = Icon(icon_name='edit-copy', xo_color=profile.get_color(), |
225 | | icon_size=Gtk.IconSize.MENU) |
226 | | menu_item.set_image(icon) |
227 | | menu_item.connect('activate', self.__copy_activate_cb) |
228 | | self.menu.append(menu_item) |
229 | | menu_item.show() |
230 | | |
231 | | def __copy_activate_cb(self, menu_item): |
232 | | file_name = os.path.basename(urlparse.urlparse(self._url).path) |
233 | | if '.' in file_name: |
234 | | base_name, extension = file_name.split('.') |
235 | | extension = '.' + extension |
| 221 | if title not in (None, ''): |
| 222 | self.props.primary_text = title |
| 223 | self.props.secondary_text = url |
237 | | base_name = file_name |
238 | | extension = '' |
239 | | |
240 | | temp_path = os.path.join(activity.get_activity_root(), 'instance') |
241 | | fd, temp_file = tempfile.mkstemp(dir=temp_path, prefix=base_name, |
242 | | suffix=extension) |
243 | | os.close(fd) |
244 | | os.chmod(temp_file, 0664) |
245 | | |
246 | | cls = components.classes['@mozilla.org/network/io-service;1'] |
247 | | io_service = cls.getService(interfaces.nsIIOService) |
248 | | uri = io_service.newURI(self._url, None, None) |
249 | | |
250 | | cls = components.classes['@mozilla.org/file/local;1'] |
251 | | target_file = cls.createInstance(interfaces.nsILocalFile) |
252 | | target_file.initWithPath(temp_file) |
253 | | |
254 | | cls = components.classes[ \ |
255 | | '@mozilla.org/embedding/browser/nsWebBrowserPersist;1'] |
256 | | persist = cls.createInstance(interfaces.nsIWebBrowserPersist) |
257 | | persist.persistFlags = 1 # PERSIST_FLAGS_FROM_CACHE |
258 | | listener = xpcom.server.WrapObject(_ImageProgressListener(temp_file), |
259 | | interfaces.nsIWebProgressListener) |
260 | | persist.progressListener = listener |
261 | | persist.saveURI(uri, None, None, None, None, target_file) |
262 | | |
263 | | def __download_activate_cb(self, menu_item): |
264 | | downloadmanager.save_link(self._url, self._title, self._owner_document) |
265 | | |
266 | | |
267 | | class _ImageProgressListener(object): |
268 | | _com_interfaces_ = interfaces.nsIWebProgressListener |
269 | | |
270 | | def __init__(self, temp_file): |
271 | | self._temp_file = temp_file |
272 | | |
273 | | def onLocationChange(self, webProgress, request, location): |
274 | | pass |
| 225 | self.props.primary_text = url |
280 | | def onSecurityChange(self, webProgress, request, state): |
281 | | pass |
| 232 | menu_item = SugarMenuItem(_('Copy image'), 'edit-copy') |
| 233 | menu_item.icon.props.xo_color = profile.get_color() |
| 234 | menu_item.connect('clicked', self.__copy_activate_cb) |
| 235 | menu_box.pack_end(menu_item, False, False, 0) |
| 236 | menu_item.show() |
283 | | def onStatusChange(self, webProgress, request, status, message): |
284 | | pass |
| 238 | menu_item = SugarMenuItem(_('Keep image'), 'document-save') |
| 239 | menu_item.icon.props.xo_color = profile.get_color() |
| 240 | menu_item.connect('clicked', self.__download_activate_cb) |
| 241 | menu_box.pack_end(menu_item, False, False, 0) |
| 242 | menu_item.show() |
286 | | def onStateChange(self, webProgress, request, stateFlags, status): |
287 | | if (stateFlags & interfaces.nsIWebProgressListener.STATE_IS_REQUEST and |
288 | | stateFlags & interfaces.nsIWebProgressListener.STATE_STOP): |
289 | | clipboard = Gtk.Clipboard() |
290 | | clipboard.set_with_data([('text/uri-list', 0, 0)], |
291 | | _clipboard_get_func_cb, |
292 | | _clipboard_clear_func_cb, |
293 | | self._temp_file) |
| 244 | def __copy_activate_cb(self, menu_item): |
| 245 | self.popdown(immediate=True) |