home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / UpdateManager / ReleaseNotesViewer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  5.8 KB  |  165 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import pygtk
  5. import gtk
  6. import pango
  7. import subprocess
  8. import os
  9.  
  10. class ReleaseNotesViewer(gtk.TextView):
  11.     
  12.     def __init__(self, notes):
  13.         '''Init the ReleaseNotesViewer as an Inheritance of the gtk.TextView.
  14.            Load the notes into the buffer and make links clickable'''
  15.         gtk.TextView.__init__(self)
  16.         self.hovering = False
  17.         self.first = True
  18.         self.set_property('editable', False)
  19.         self.set_cursor_visible(False)
  20.         self.buffer = gtk.TextBuffer()
  21.         self.set_buffer(self.buffer)
  22.         self.buffer.set_text(notes)
  23.         self.connect('event-after', self.event_after)
  24.         self.connect('motion-notify-event', self.motion_notify_event)
  25.         self.connect('visibility-notify-event', self.visibility_notify_event)
  26.         self.search_links()
  27.  
  28.     
  29.     def tag_link(self, start, end, url):
  30.         '''Apply the tag that marks links to the specified buffer selection'''
  31.         tag = self.buffer.create_tag(None, foreground = 'blue', underline = pango.UNDERLINE_SINGLE)
  32.         tag.set_data('url', url)
  33.         self.buffer.apply_tag(tag, start, end)
  34.  
  35.     
  36.     def search_links(self):
  37.         '''Search for http URLs in the buffer and call the tag_link method
  38.            for each one to tag them as links'''
  39.         iter = self.buffer.get_iter_at_offset(0)
  40.         while None:
  41.             ret = iter.forward_search('http://', gtk.TEXT_SEARCH_VISIBLE_ONLY, None)
  42.             if not ret:
  43.                 break
  44.             
  45.             (match_start, match_end) = ret
  46.             match_tmp = match_end.copy()
  47.             while match_tmp.forward_char():
  48.                 text = match_end.get_text(match_tmp)
  49.                 if text in (' ', ')', ']', '\n', '\t'):
  50.                     break
  51.                 
  52.             break
  53.             match_end = match_tmp.copy()
  54.             continue
  55.             url = match_start.get_text(match_end)
  56.             self.tag_link(match_start, match_end, url)
  57.             iter = match_end
  58.             continue
  59.             return None
  60.  
  61.     
  62.     def event_after(self, text_view, event):
  63.         '''callback for mouse click events'''
  64.         if event.type != gtk.gdk.BUTTON_RELEASE:
  65.             return False
  66.         if event.button != 1:
  67.             return False
  68.         
  69.         try:
  70.             (start, end) = self.buffer.get_selection_bounds()
  71.         except ValueError:
  72.             event.button != 1
  73.             event.button != 1
  74.             event.type != gtk.gdk.BUTTON_RELEASE
  75.         except:
  76.             event.button != 1
  77.  
  78.         if start.get_offset() != end.get_offset():
  79.             return False
  80.         (x, y) = self.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  81.         iter = self.get_iter_at_location(x, y)
  82.         tags = iter.get_tags()
  83.         for tag in tags:
  84.             url = tag.get_data('url')
  85.             if url != '':
  86.                 self.open_url(url)
  87.                 break
  88.                 continue
  89.             start.get_offset() != end.get_offset()
  90.         
  91.  
  92.     
  93.     def open_url(self, url):
  94.         '''Open the specified URL in a browser'''
  95.         if os.path.exists('/usr/bin/exo-open'):
  96.             command = [
  97.                 'exo-open',
  98.                 url]
  99.         elif os.path.exists('usr/bin/gnome-open'):
  100.             command = [
  101.                 'gnome-open',
  102.                 url]
  103.         else:
  104.             command = [
  105.                 'x-www-browser',
  106.                 url]
  107.         if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
  108.             command = [
  109.                 'sudo',
  110.                 '-u',
  111.                 os.environ['SUDO_USER']] + command
  112.         
  113.         subprocess.Popen(command)
  114.  
  115.     
  116.     def motion_notify_event(self, text_view, event):
  117.         '''callback for the mouse movement event, that calls the
  118.            check_hovering method with the mouse postition coordiantes'''
  119.         (x, y) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  120.         self.check_hovering(x, y)
  121.         self.window.get_pointer()
  122.         return False
  123.  
  124.     
  125.     def visibility_notify_event(self, text_view, event):
  126.         '''callback if the widgets gets visible (e.g. moves to the foreground)
  127.            that calls the check_hovering method with the mouse position
  128.            coordinates'''
  129.         (wx, wy, mod) = text_view.window.get_pointer()
  130.         (bx, by) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
  131.         self.check_hovering(bx, by)
  132.         return False
  133.  
  134.     
  135.     def check_hovering(self, x, y):
  136.         '''Check if the mouse is above a tagged link and if yes show
  137.            a hand cursor'''
  138.         _hovering = False
  139.         iter = self.get_iter_at_location(x, y)
  140.         tags = iter.get_tags()
  141.         for tag in tags:
  142.             url = tag.get_data('url')
  143.             if url != '':
  144.                 _hovering = True
  145.                 break
  146.                 continue
  147.         
  148.         if _hovering != self.hovering or self.first == True:
  149.             self.first = False
  150.             self.hovering = _hovering
  151.             if self.hovering:
  152.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))
  153.             else:
  154.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR))
  155.         
  156.  
  157.  
  158. if __name__ == '__main__':
  159.     win = gtk.Window()
  160.     rv = ReleaseNotesViewer(open('../DistUpgrade/ReleaseAnnouncement').read())
  161.     win.add(rv)
  162.     win.show_all()
  163.     gtk.main()
  164.  
  165.