From 547665385751824cb3feab2e3dab4c4c1633e43e Mon Sep 17 00:00:00 2001 From: Gaetan Semet Date: Tue, 28 Apr 2015 14:42:31 +0200 Subject: [PATCH] Add "Open Link" on context menu I like the way chrome allow to open a textual link on the contextual menu. Signed-off-by: Gaetan Semet --- data/guake.glade | 18 ++++++++++++ src/guake/guake_app.py | 22 +++++++++++++- src/guake/terminal.py | 67 ++++++++++++++++++++++++------------------ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/data/guake.glade b/data/guake.glade index bd4874c29..fcbe56d18 100644 --- a/data/guake.glade +++ b/data/guake.glade @@ -174,6 +174,24 @@ False + + + Open link... + True + False + False + False + + + + True + False + gtk-find + 1 + + + + Search on Web diff --git a/src/guake/guake_app.py b/src/guake/guake_app.py index 08918eccc..725f39b18 100644 --- a/src/guake/guake_app.py +++ b/src/guake/guake_app.py @@ -445,6 +445,7 @@ def show_context_menu(self, terminal, event): if len(current_selection) > 20: current_selection = current_selection[:17] + "..." + self.get_widget('separator_search').set_visible(False) if current_selection: self.get_widget('context_search_on_web').set_label( _("Search on Web: '%s'") % current_selection) @@ -453,7 +454,15 @@ def show_context_menu(self, terminal, event): else: self.get_widget('context_search_on_web').set_label(_("Search on Web (no selection)")) self.get_widget('context_search_on_web').set_visible(False) - self.get_widget('separator_search').set_visible(False) + + link = self.getCurrentTerminalLinkUnderCursor() + if link: + self.get_widget('context_browse_on_web').set_visible(True) + self.get_widget('context_browse_on_web').set_label(_("Open Link: {}".format(link))) + self.get_widget('separator_search').set_visible(True) + else: + self.get_widget('context_browse_on_web').set_label(_("Open Link...")) + self.get_widget('context_browse_on_web').set_visible(False) context_menu = self.get_widget('context-menu') context_menu.popup(None, None, None, 3, gtk.get_current_event_time()) @@ -1452,6 +1461,17 @@ def search_on_web(self, *args): gtk.gdk.x11_get_server_time(current_term.window)) return True + def getCurrentTerminalLinkUnderCursor(self): + current_term = self.notebook.get_current_terminal() + l = current_term.found_link + print("Current link under cursor: {}".format(l)) + if l: + return l + + def browse_on_web(self, *args): + print("browsing {}...".format(self.getCurrentTerminalLinkUnderCursor())) + self.notebook.get_current_terminal().browse_link_under_cursor() + def set_tab_position(self, *args): if self.client.get_bool(KEY('/general/tab_ontop')): self.mainframe.reorder_child(self.notebook, 2) diff --git a/src/guake/terminal.py b/src/guake/terminal.py index 5fcb06d6b..817f782ba 100644 --- a/src/guake/terminal.py +++ b/src/guake/terminal.py @@ -84,6 +84,7 @@ def __init__(self): self.pid = None self.custom_bgcolor = None self.custom_fgcolor = None + self.found_link = None def get_pid(self): return self.pid @@ -135,12 +136,12 @@ def button_press(self, terminal, event): int(event.x / self.get_char_width()), int(event.y / self.get_char_height())) - if (event.button == 1 and event.get_state() & gtk.gdk.CONTROL_MASK and - matched_string): + self.found_link = None + if (event.button == 1 and (event.get_state() & gtk.gdk.CONTROL_MASK) and matched_string): print("matched string:", matched_string) value, tag = matched_string # First searching in additional matchers - found = False + found_additional_matcher = False client = gconf.client_get_default() use_quick_open = client.get_bool(KEY("/general/quick_open_enable")) quick_open_in_current_terminal = client.get_bool( @@ -178,35 +179,45 @@ def button_press(self, terminal, event): else: logging.debug("Executing it independently") subprocess.call(resolved_cmdline, shell=True) - found = True + found_additional_matcher = True break - if not found: - print("found tag:", tag) - print("found item:", value) - print("TERMINAL_MATCH_TAGS", TERMINAL_MATCH_TAGS) - if tag in TERMINAL_MATCH_TAGS: - if TERMINAL_MATCH_TAGS[tag] == 'schema': - # value here should not be changed, it is right and - # ready to be used. - pass - elif TERMINAL_MATCH_TAGS[tag] == 'http': - value = 'http://%s' % value - elif TERMINAL_MATCH_TAGS[tag] == 'https': - value = 'https://%s' % value - elif TERMINAL_MATCH_TAGS[tag] == 'ftp': - value = 'ftp://%s' % value - elif TERMINAL_MATCH_TAGS[tag] == 'email': - value = 'mailto:%s' % value - - if value: - cmd = ["xdg-open", value] - print("Opening link: {}".format(cmd)) - subprocess.Popen(cmd, shell=False) - # gtk.show_uri(self.window.get_screen(), value, - # gtk.gdk.x11_get_server_time(self.window)) + if not found_additional_matcher: + self.found_link = self.handleTerminalMatch(matched_string) + if self.found_link: + self.browse_link_under_cursor() elif event.button == 3 and matched_string: + self.found_link = self.handleTerminalMatch(matched_string) self.matched_value = matched_string[0] + def handleTerminalMatch(self, matched_string): + value, tag = matched_string + print("found tag:", tag) + print("found item:", value) + print("TERMINAL_MATCH_TAGS", TERMINAL_MATCH_TAGS) + if tag in TERMINAL_MATCH_TAGS: + if TERMINAL_MATCH_TAGS[tag] == 'schema': + # value here should not be changed, it is right and + # ready to be used. + pass + elif TERMINAL_MATCH_TAGS[tag] == 'http': + value = 'http://%s' % value + elif TERMINAL_MATCH_TAGS[tag] == 'https': + value = 'https://%s' % value + elif TERMINAL_MATCH_TAGS[tag] == 'ftp': + value = 'ftp://%s' % value + elif TERMINAL_MATCH_TAGS[tag] == 'email': + value = 'mailto:%s' % value + + if value: + return value + + def browse_link_under_cursor(self): + if not self.found_link: + return + print("Opening link: {}".format(self.found_link)) + cmd = ["xdg-open", self.found_link] + subprocess.Popen(cmd, shell=False) + def set_font(self, font): self.font = font self.set_font_scale_index(0)