o
    >I¼cê  ã                   @   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rDe ¡ Ze	ed	ƒ ¡ ƒZe e¡ e ¡  e ¡  dS dS )
é    )ÚPango)ÚGtkÚGObjectÚGdkNc                 C   s´   t j d¡rd| g}nt j d¡rd| g}nt j d¡r!d| g}nd| g}t  ¡ dkr;d	t jv r;d
dt jd	 g| }nt  ¡ dkrSdt jv rSd
dddt jd  g| }t |¡ 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   Ú	SUDO_USERÚsudoz-uÚ
PKEXEC_UIDz-Hú#N)ÚosÚpathÚexistsÚgetuidÚenvironÚ
subprocessÚPopen)ÚurlÚ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  | ¡ d| _d| _|  dd¡ |  d¡ |  t d¡¡ t	 
¡ | _|  | j¡ | j |¡ |  d| j¡ |  d| j¡ |  d| j¡ |  ¡  dS )	z…Init the ReleaseNotesViewer as an Inheritance of the Gtk.TextView.
           Load the notes into the buffer and make links clickableFTÚeditableÚ	monospacezbutton-press-eventzmotion-notify-eventzvisibility-notify-eventN)r   Ú__init__ÚhoveringÚfirstÚset_propertyÚset_cursor_visibleÚmodify_fontr   ÚFontDescriptionr   Ú
TextBufferÚbufferÚ
set_bufferÚset_textÚconnectÚbutton_press_eventÚmotion_notify_eventÚvisibility_notify_eventÚsearch_links)ÚselfÚnotesr   r   r   r   6   s   

zReleaseNotesViewer.__init__c                 C   s0   | j jddtjjd�}||_| j  |||¡ dS )z@Apply the tag that marks links to the specified buffer selectionNÚblue)Ú
foregroundÚ	underline)r!   Ú
create_tagr   Ú	UnderlineÚSINGLEr   Ú	apply_tag)r)   ÚstartÚendr   Útagr   r   r   Útag_linkK   s
   
ÿzReleaseNotesViewer.tag_linkc                 C   s‚   | j  d¡}	 | dtjjd¡}|sdS |\}}| ¡ }	 | ¡ r,| |¡}|dv r+nnn| ¡ }q| |¡}|  	|||¡ |}q)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!   Úget_iter_at_offsetÚforward_searchr   ÚTextSearchFlagsÚVISIBLE_ONLYÚcopyÚforward_charÚget_textr5   )r)   ÚiterÚretÚmatch_startÚ	match_endÚ	match_tmpÚtextr   r   r   r   r(   R   s.   þ
ÿø

èzReleaseNotesViewer.search_linksc                 C   s®   |j dkrdS z	| j ¡ \}}W n	 ty   Y nw | ¡ | ¡ kr$dS |  tjjt	|j
ƒt	|jƒ¡\}}|  ||¡}| ¡ }|D ]}	t|	ddƒ}
|
dkrTt|
ƒ  dS qAdS )zcallback for mouse click eventsr6   Fr   NÚ )Úbuttonr!   Úget_selection_boundsÚ
ValueErrorÚ
get_offsetÚwindow_to_buffer_coordsr   ÚTextWindowTypeÚWIDGETÚintÚxÚyÚget_iter_at_locationÚget_tagsÚgetattrr   )r)   Ú	text_viewÚeventr2   r3   rR   rS   rC   Útagsr4   r   r   r   r   r%   q   s*   
ÿ
ÿþþz%ReleaseNotesViewer.button_press_eventc                 C   sD   |  tjjt|jƒt|jƒ¡\}}|  ||¡ |  tjj	¡ 
¡  dS )z{callback for the mouse movement event, that calls the
           check_hovering method with the mouse postition coordiantesF)rN   r   rO   rP   rQ   rR   rS   Úcheck_hoveringÚ
get_windowÚTEXTÚget_pointer)r)   rW   rX   rR   rS   r   r   r   r&   Œ   s   
ÿz&ReleaseNotesViewer.motion_notify_eventc           
      C   sD   |  tjj¡}| ¡ \}}}}| tjj||¡\}}	|  ||	¡ dS )z¢callback if the widgets gets visible (e.g. moves to the foreground)
           that calls the check_hovering method with the mouse position
           coordinatesF)r[   r   rO   r\   r]   rN   rP   rZ   )
r)   rW   rX   ÚwindowÚscreenÚwxÚwyÚmodÚbxÚbyr   r   r   r'   •   s   
ÿz*ReleaseNotesViewer.visibility_notify_eventc                 C   s¬   d}|   ||¡}| ¡ }|D ]}t|ddƒ}|dkrd} nq|| jks'| jrTd| _|| _| jrB|  tjj¡ 	t
j t
jj¡¡ dS |  tjj¡ 	t
j t
jj¡¡ dS dS )zRCheck if the mouse is above a tagged link and if yes show
           a hand cursorFr   NrI   T)rT   rU   rV   r   r   r[   r   rO   r\   Ú
set_cursorr   ÚCursorÚnewÚ
CursorTypeÚHAND2ÚLEFT_PTR)r)   rR   rS   Ú	_hoveringrC   rY   r4   r   r   r   r   rZ       s(   þÿÿøz!ReleaseNotesViewer.check_hoveringN)
Ú__name__Ú
__module__Ú__qualname__r   r5   r(   r%   r&   r'   rZ   r   r   r   r   r   5   s    	r   Ú__main__z"../DistUpgrade/ReleaseAnnouncement)Úgi.repositoryr   r   r   r   r
   r   r   ÚTextViewr   rl   ÚWindowÚwinÚopenÚreadÚrvÚaddÚshow_allÚmainr   r   r   r   Ú<module>   s    
ú