
Y¯”]é  ã               @   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 Gd d „  d e j ƒ Z	 e
 d k rº e 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 } nN t  j j d ƒ rB d |  g } n- t  j j d ƒ rc d |  g } n d |  g } t  j ƒ  d k r­ d	 t  j k r­ d
 d t  j d	 g | } nB t  j ƒ  d k rï d t  j k rï d
 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)	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   sG   |  j  j d d d d t j j ƒ} | | _ |  j  j | | | ƒ d S)z@Apply the tag that marks links to the specified buffer selectionNZ
foregroundZblueZ	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 ƒ } x¥ | j d t j j d ƒ } | s7 P| \ } } | j ƒ  } x< | j ƒ  r} | j | ƒ } | d	 k r~ Pn P| j ƒ  } qR W| j | ƒ } |  j	 | | | ƒ | } q Wd 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 Sy |  j j ƒ  \ } } Wn t k
 r@ Yn X| j ƒ  | j ƒ  k r] d S|  j t j j t	 | j
 ƒ t	 | j ƒ ƒ \ } } |  j | | ƒ } | j ƒ  } x7 | D]/ }	 t |	 d d ƒ }
 |
 d k rµ t |
 ƒ Pqµ Wd 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   s`   | 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   sb   | j  t j j ƒ } | j ƒ  \ } } } } | j t j 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 } |  j  | | ƒ } | j ƒ  } x3 | D]+ } t | d d ƒ } | d k r+ d } Pq+ W| |  j k sr |  j rì d |  _ | |  _ |  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   †
