Skip to content

Commit

Permalink
Add "Open Link" on context menu
Browse files Browse the repository at this point in the history
I like the way chrome allow to open a textual link
on the contextual menu.

Signed-off-by: Gaetan Semet <gaetan@xeberon.net>
  • Loading branch information
gsemet committed Apr 28, 2015
1 parent 7fff6f5 commit 5476653
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
18 changes: 18 additions & 0 deletions data/guake.glade
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@
<property name="can_focus">False</property>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="context_browse_on_web">
<property name="label" translatable="yes">Open link...</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">False</property>
<signal name="activate" handler="browse_on_web"/>
<child internal-child="image">
<widget class="GtkImage" id="browse_on_web_icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-find</property>
<property name="icon-size">1</property>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="context_search_on_web">
<property name="label" translatable="yes">Search on Web</property>
Expand Down
22 changes: 21 additions & 1 deletion src/guake/guake_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
67 changes: 39 additions & 28 deletions src/guake/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5476653

Please sign in to comment.