3
[0                 @   s   d dl mZ d dlZejdd d dlmZ d dlmZ d dlmZ d dlmZ d d	l	m	Z
 d d
lmZ G dd dejZedkrej Ze Zej Zejdejjd ejd eje ej  ej  dS )    )absolute_importNGtkz3.0)r   )Gdk)GObject)Pango)gettext)open_urlc               @   sV   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )ChangelogViewerNc             C   s   t j j|  d| _d| _| jdd | jd | jd | jd | jd | j	d t
j | _| j| j | jd| j | jd| j | jd| j | jjd| j |d	k	r| jj| d	S )
z>Init the ChangelogViewer as an Inheritance of the Gtk.TextViewFTZeditable   zbutton-press-eventzmotion-notify-eventzvisibility-notify-eventzinsert-textN)r   __init__hoveringfirstZset_propertyZset_cursor_visibleZset_right_marginZset_left_marginZset_pixels_above_linesZset_vexpandr   Z
TextBufferbufferZ
set_bufferconnectbutton_press_eventmotion_notify_eventvisibility_notify_eventZconnect_afteron_insert_textset_text)selfZ	changelog r   ?/usr/lib/python3/dist-packages/UpdateManager/ChangelogViewer.pyr   ,   s"    





zChangelogViewer.__init__c             C   s   t j | _t j }|j| |jd| jd| t j }t j }|jtd |jd| jd| t j }|jtd |jd| jd| | jj| | jj| | jj| | jj| | jj	  dS )zOCreate the context menu to be displayed when links are right
           clickedZactivateopenzOpen Link in BrowserzCopy Link to ClipboardcopyN)
r   ZMenumenuZMenuItemZ	set_labelr   handle_context_menu_addshow_all)r   urlZitem_grey_linkZitem_seperatorZitem_open_linkZitem_copy_linkr   r   r   create_context_menuG   s&    

z#ChangelogViewer.create_context_menuc             C   sV   |dkrt | |dkrRtjj }tjjdd}tjj||}|j	|d |j
  dS )z1Handle activate event for the links' context menur   r   Z	CLIPBOARDF   N)r   r   ZDisplayZget_defaultZAtominternr   Z	ClipboardZget_for_displayr   Zstore)r   Zmenuitemactionr   ZdisplayZ	selectioncbr   r   r   r   b   s    
z#ChangelogViewer.handle_context_menuc             C   s^   |j  }x$|D ]}t|dd}|dkrdS qW | jjddtjjd}||_| jj||| dS )z@Apply the tag that marks links to the specified buffer selectionr   N Zblue)Z
foregroundZ	underline)	get_tagsgetattrr   
create_tagr   Z	UnderlineZSINGLEr   Z	apply_tag)r   startendr   tagstagr   r   r   tag_linkq   s    

zChangelogViewer.tag_linkc             G   s&  d}d}d}dddg}ddd	g}	d
ddg}
dg|
 }dg}d||	 |
 | dfd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fd||	 | |fg}x|D ]\}}}|j |j t| }x|j|tjj|}|sP |\}}|j }|j }xX|j r|j	|}||krP |j  |j	|}||krP |j
  nP |j }qW ||j	| }| j||| |}qjW qHW dS ) zOSearch for http URLs in newly inserted text
           and tag them accordinglyzhttps://launchpad.net/bugs/zhttp://bugs.debian.org/z2http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE- 	
)]>,!:.z.
zhttp://zLP#zlp#zLP: #zlp: #zLP:#zlp:#z	Malone: #zMalone:#z	Ubuntu: #zUbuntu:#z	Closes: #zCloses:#zcloses:#z	closes: #zCVE-N)Zget_iter_at_offset
get_offsetlenZforward_searchr   ZTextSearchFlagsZVISIBLE_ONLYr   Zforward_charZget_textZbackward_charr.   )r   r   Ziter_endZcontentargsZMALONEZDEBIANZCVEZwsZbrakpunctdotZdot_crZsearch_itemsZ	start_strZend_listZ
url_prefixiterZretZmatch_startZ	match_endZmatch_suffixZ	match_tmptextr   r   r   r   r   }   sf    










zChangelogViewer.on_insert_textc             C   s"  |j dkr|j dkrdS y| jj \}}W n tk
r>   Y nX |j |j krTdS | jtjjt	|j
t	|j\}}y| j||}|j }W n, tk
r   | j||\}	}|j }Y nX x`|D ]X}
t|
dr|j dkrt|
j P |j dkr| j|
j | jjdddd|j |j dS qW dS )zcallback for mouse click eventsr!      Fr   NT)Zbuttonr   Zget_selection_bounds
ValueErrorr9   window_to_buffer_coordsr   TextWindowTypeWIDGETintxyget_iter_at_locationr'   AttributeErrorhasattrr   r   r    r   ZpopupZtime)r   	text_vieweventr*   r+   rF   rG   r>   r,   	over_textr-   r   r   r   r      s4    





z"ChangelogViewer.button_press_eventc             C   sD   |j tjjt|jt|j\}}| j|| | jtjj	j
  dS )z{callback for the mouse movement event, that calls the
           check_hovering method with the mouse postition coordiantesF)rB   r   rC   rD   rE   rF   rG   check_hovering
get_windowTEXTget_pointer)r   rK   rL   rF   rG   r   r   r   r      s
    
z#ChangelogViewer.motion_notify_eventc       
      C   sD   |j tjj}|j \}}}}|jtjj||\}}	| j||	 dS )zcallback if the widgets gets visible (e.g. moves to the foreground)
           that calls the check_hovering method with the mouse position
           coordinatesF)rO   r   rC   rP   rQ   rB   rD   rN   )
r   rK   rL   ZwindowZscreenZwxZwymodZbxZbyr   r   r   r      s    
z'ChangelogViewer.visibility_notify_eventc             C   s   d}y| j ||}|j }W n, tk
rH   | j ||\}}|j }Y nX x|D ]}t|drPd}P qPW || jksx| jrd| _|| _| jr| jtjj	j
tjjtjj n | jtjj	j
tjjtjj dS )zRCheck if the mouse is above a tagged link and if yes show
           a hand cursorFr   TN)rH   r'   rI   rJ   r   r   rO   r   rC   rP   Z
set_cursorr   ZCursornewZ
CursorTypeZHAND2ZLEFT_PTR)r   rF   rG   Z	_hoveringr>   r,   rM   r-   r   r   r   rN     s&    

zChangelogViewer.check_hovering)N)__name__
__module____qualname__r   r    r   r.   r   r   r   r   rN   r   r   r   r   r	   +   s   
M&	r	   __main__Z
versiontag)Zweightz

Version 6-14-0ubuntu1.9.04:

  * New upstream version. LP: #382918.
    Release notes at http://java.sun.com/javase/6/webnotes/ReleaseNotes.html.

)Z
__future__r   ZgiZrequire_versionZgi.repositoryr   r   r   r   r   r   ZDistUpgrade.ReleaseNotesViewerr   ZTextViewr	   rT   ZWindowwZcvZ
get_bufferZchangesr)   ZWeightZBOLDr   r   r   mainr   r   r   r   <module>   s(    z
