home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import pygtk
- import gtk
- import pango
- import subprocess
- import os
- from gettext import gettext as _
-
- class ChangelogViewer(gtk.TextView):
-
- def __init__(self, changelog = None):
- '''Init the ChangelogViewer as an Inheritance of the gtk.TextView'''
- gtk.TextView.__init__(self)
- self.hovering = False
- self.first = True
- self.set_property('editable', False)
- self.set_cursor_visible(False)
- self.set_right_margin(4)
- self.set_left_margin(4)
- self.set_pixels_above_lines(4)
- self.buffer = gtk.TextBuffer()
- self.set_buffer(self.buffer)
- self.connect('button-press-event', self.button_press_event)
- self.connect('motion-notify-event', self.motion_notify_event)
- self.connect('visibility-notify-event', self.visibility_notify_event)
- self.buffer.connect_after('insert-text', self.on_insert_text)
- if changelog != None:
- self.buffer.set_text(changelog)
-
-
-
- def create_context_menu(self, url):
- '''Create the context menu to be displayed when links are right clicked'''
- self.menu = gtk.Menu()
- item_grey_link = gtk.MenuItem(url)
- item_grey_link.connect('activate', self.handle_context_menu, 'open', url)
- item_seperator = gtk.MenuItem()
- item_open_link = gtk.MenuItem(_('Open Link in Browser'))
- item_open_link.connect('activate', self.handle_context_menu, 'open', url)
- item_copy_link = gtk.MenuItem(_('Copy Link to Clipboard'))
- item_copy_link.connect('activate', self.handle_context_menu, 'copy', url)
- self.menu.add(item_grey_link)
- self.menu.add(item_seperator)
- self.menu.add(item_open_link)
- self.menu.add(item_copy_link)
- self.menu.show_all()
-
-
- def handle_context_menu(self, menuitem, action, url):
- """Handle activate event for the links' context menu"""
- if action == 'open':
- self.open_url(url)
-
- if action == 'copy':
- cb = gtk.Clipboard()
- cb.set_text(url)
- cb.store()
-
-
-
- def tag_link(self, start, end, url):
- '''Apply the tag that marks links to the specified buffer selection'''
- tagged = False
- tags = start.get_tags()
- for tag in tags:
- url = tag.get_data('url')
- if url != '':
- return None
-
- tag = self.buffer.create_tag(None, foreground = 'blue', underline = pango.UNDERLINE_SINGLE)
- tag.set_data('url', url)
- self.buffer.apply_tag(tag, start, end)
-
-
- def on_insert_text(self, buffer, iter_end, text, *args):
- '''Search for http URLs in newly inserted text
- and tag them accordingly'''
- MALONE = 'https://launchpad.net/bugs/'
- DEBIAN = 'http://bugs.debian.org/'
- CVE = 'http://cve.mitre.org/cgi-bin/cvename.cgi?name='
- ws = [
- ' ',
- '\t',
- '\n']
- brak = [
- ')',
- ']',
- '>']
- punct = [
- ',',
- '!',
- ':']
- dot = [
- '.'] + punct
- search_items = [
- ('http://', ws + brak + punct, 'http://'),
- ('LP#', ws + brak + dot, MALONE),
- ('LP: #', ws + brak + dot, MALONE),
- ('lp: #', ws + brak + dot, MALONE),
- ('LP:#', ws + brak + dot, MALONE),
- ('Malone: #', ws + brak + dot, MALONE),
- ('Malone:#', ws + brak + dot, MALONE),
- ('Ubuntu: #', ws + brak + dot, MALONE),
- ('Ubuntu:#', ws + brak + dot, MALONE),
- ('Closes: #', ws + brak + dot, DEBIAN),
- ('Closes:#', ws + brak + dot, DEBIAN),
- ('closes:#', ws + brak + dot, DEBIAN),
- ('closes: #', ws + brak + dot, DEBIAN),
- ('CVE-', ws + brak + dot, CVE)]
- iter = buffer.get_iter_at_offset(iter_end.get_offset() - len(text))
- iter_real_end = buffer.get_end_iter()
- for start_str, end_list, url_prefix in search_items:
- while True:
- ret = iter.forward_search(start_str, gtk.TEXT_SEARCH_VISIBLE_ONLY, iter_end)
- if not ret:
- break
-
- (match_start, match_end) = ret
- match_suffix = match_end.copy()
- match_tmp = match_end.copy()
- while True:
- if match_tmp.forward_char():
- text = match_end.get_text(match_tmp)
- if text in end_list:
- break
-
- else:
- break
- match_end = match_tmp.copy()
- url = url_prefix + match_suffix.get_text(match_end)
- self.tag_link(match_start, match_end, url)
- iter = match_end
-
-
-
- def button_press_event(self, text_view, event):
- '''callback for mouse click events'''
- if event.button != 1 and event.button != 3:
- return False
-
- try:
- (start, end) = self.buffer.get_selection_bounds()
- except ValueError:
- event.button != 3
- event.button != 3
- except:
- event.button != 3
-
- if start.get_offset() != end.get_offset():
- return False
- (x, y) = self.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
- iter = self.get_iter_at_location(x, y)
- tags = iter.get_tags()
- for tag in tags:
- url = tag.get_data('url')
- if url != None:
- if event.button == 3:
- self.create_context_menu(url)
- self.menu.popup(None, None, None, event.button, event.time)
- return True
- continue
- event.button == 3
-
-
-
- def open_url(self, url):
- '''Open the specified URL in a browser'''
- if os.path.exists('/usr/bin/exo-open'):
- command = [
- 'exo-open',
- url]
- elif os.path.exists('/usr/bin/gnome-open'):
- command = [
- 'gnome-open',
- url]
- else:
- command = [
- 'x-www-browser',
- url]
- if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
- command = [
- 'sudo',
- '-u',
- os.environ['SUDO_USER']] + command
-
- subprocess.Popen(command)
-
-
- def motion_notify_event(self, text_view, event):
- '''callback for the mouse movement event, that calls the
- check_hovering method with the mouse postition coordiantes'''
- (x, y) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
- self.check_hovering(x, y)
- self.window.get_pointer()
- return False
-
-
- def visibility_notify_event(self, text_view, event):
- '''callback if the widgets gets visible (e.g. moves to the foreground)
- that calls the check_hovering method with the mouse position
- coordinates'''
- (wx, wy, mod) = text_view.window.get_pointer()
- (bx, by) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
- self.check_hovering(bx, by)
- return False
-
-
- def check_hovering(self, x, y):
- '''Check if the mouse is above a tagged link and if yes show
- a hand cursor'''
- _hovering = False
- iter = self.get_iter_at_location(x, y)
- tags = iter.get_tags()
- for tag in tags:
- url = tag.get_data('url')
- if url != None:
- _hovering = True
- break
- continue
-
- if _hovering != self.hovering or self.first == True:
- self.first = False
- self.hovering = _hovering
- if self.hovering:
- self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))
- else:
- self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR))
-
-
-
-