U
    B‹ú]ê  ã                   @   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r„e ¡ Ze	ed	ƒ ¡ ƒZe e¡ e ¡  e ¡  dS )
é    )ÚPango)ÚGtkÚGObjectÚGdkNc                 C   s´   t j d¡rd| g}n4t j d¡r,d| g}nt j d¡rBd| g}nd| g}t  ¡ dkrvd	t jkrvd
dt jd	 g| }n0t  ¡ dkr¦dt jkr¦d
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   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  | ¡ 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 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  |||¡ 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  d¡}| dtjjd¡}|s$q||\}}| ¡ }| ¡ r^| |¡}|dkrTq^nq^| ¡ }q4| |¡}|  	|||¡ |}q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   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 z| j ¡ \}}W n tk
r4   Y nX | ¡ | ¡ krJdS |  tjjt	|j
ƒt	|jƒ¡\}}|  ||¡}| ¡ }|D ]$}	t|	ddƒ}
|
dkr„t|
ƒ  qªq„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   |  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)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   |  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)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}|   ||¡}| ¡ }|D ] }t|ddƒ}|dkrd} q>q|| jksN| jr¢d| _|| _| jr‚|  tjj¡ 	t
j t
jj¡¡ n |  tjj¡ 	t
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    
