3
s_\                 @   s   d dl mZ d dl mZmZmZ d dlZd dlZdd ZG dd dejZ	e
dkrej Ze	ed	j Zeje ej  ej  dS )
    )Pango)GtkGObjectGdkNc             C   s   t jjdrd| g}n4t jjdr,d| g}nt jjdrBd| g}nd| g}t j dkrvd	t jkrvd
dt jd	 g| }n0t j dkrdt jkrd
dddt jd  g| }tj| dS )z#Open the specified URL in a browserz/usr/bin/xdg-openzxdg-openz/usr/bin/exo-openzexo-openz/usr/bin/gnome-openz
gnome-openzx-www-browserr   Z	SUDO_USERZsudoz-uZ
PKEXEC_UIDz-H#N)ospathexistsgetuidenviron
subprocessPopen)urlZcommand r   @/usr/lib/python3/dist-packages/DistUpgrade/ReleaseNotesViewer.pyopen_url!   s    


r   c               @   sD   e Z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 )ReleaseNotesViewerc             C   s   t j j|  d| _d| _| jdd | jd | jtjd t	j
 | _| j| j | jj| | jd| j | jd| j | jd| j | j  dS )	zInit the ReleaseNotesViewer as an Inheritance of the Gtk.TextView.
           Load the notes into the buffer and make links clickableFTZeditableZ	monospacezbutton-press-eventzmotion-notify-eventzvisibility-notify-eventN)r   __init__hoveringfirstZset_propertyZset_cursor_visibleZmodify_fontr   ZFontDescriptionr   Z
TextBufferbufferZ
set_bufferZset_textZconnectbutton_press_eventmotion_notify_eventvisibility_notify_eventsearch_links)selfZnotesr   r   r   r   6   s    

zReleaseNotesViewer.__init__c             C   s0   | j jddtjjd}||_| j j||| dS )z@Apply the tag that marks links to the specified buffer selectionNZblue)Z
foregroundZ	underline)r   Z
create_tagr   Z	UnderlineZSINGLEr   Z	apply_tag)r   startendr   tagr   r   r   tag_linkK   s    
zReleaseNotesViewer.tag_linkc             C   s   | j jd}xv|jdtjjd}|s&P |\}}|j }x,|j rV|j|}|d	krXP nP |j }q8W |j|}| j	||| |}qW dS )
zlSearch for http URLs in the buffer and call the tag_link method
           for each one to tag them as linksr   zhttp://N )]
	)r    r!   r"   r#   r$   )
r   Zget_iter_at_offsetZforward_searchr   ZTextSearchFlagsZVISIBLE_ONLYcopyZforward_charZget_textr   )r   iterZretZmatch_startZ	match_endZ	match_tmptextr   r   r   r   r   R   s&    

zReleaseNotesViewer.search_linksc             C   s   |j dkrdS y| jj \}}W n tk
r4   Y nX |j |j krJdS | jtjjt	|j
t	|j\}}| j||}|j }x*|D ]"}	t|	dd}
|
dkrt|
 P qW dS )zcallback for mouse click events   Fr   N )Zbuttonr   Zget_selection_bounds
ValueErrorZ
get_offsetwindow_to_buffer_coordsr   TextWindowTypeWIDGETintxyget_iter_at_locationget_tagsgetattrr   )r   	text_vieweventr   r   r/   r0   r&   tagsr   r   r   r   r   r   q   s"    


z%ReleaseNotesViewer.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)r+   r   r,   r-   r.   r/   r0   check_hovering
get_windowTEXTget_pointer)r   r4   r5   r/   r0   r   r   r   r      s
    
z&ReleaseNotesViewer.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)r8   r   r,   r9   r:   r+   r-   r7   )
r   r4   r5   ZwindowZscreenZwxZwymodZbxZbyr   r   r   r      s    z*ReleaseNotesViewer.visibility_notify_eventc             C   s   d}| j ||}|j }x&|D ]}t|dd}|dkrd}P qW || jksP| 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   Nr)   T)r1   r2   r3   r   r   r8   r   r,   r9   Z
set_cursorr   ZCursornewZ
CursorTypeZHAND2ZLEFT_PTR)r   r/   r0   Z	_hoveringr&   r6   r   r   r   r   r   r7      s     
z!ReleaseNotesViewer.check_hoveringN)
__name__
__module____qualname__r   r   r   r   r   r   r7   r   r   r   r   r   5   s   	r   __main__z"../DistUpgrade/ReleaseAnnouncement)Zgi.repositoryr   r   r   r   r   r   r   ZTextViewr   r=   ZWindowwinopenreadrvaddZshow_allmainr   r   r   r   <module>   s    
