o
    >Ic                     @   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 )	zInit 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 )zcallback 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    
