3
_                 @   s  d Z dZd;ZddlZddlZddlZddlZddlZddlZej	dd ddl
Z
ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlT ddlmZ dd lmZ d!d" Zeed#\ZZG d$d dejZG d%d deZ G d&d	 d	eZ!G d'd
 d
ej"Z#G d(d dej"Z$G d)d dej%Z&G d*d+ d+ej'Z(G d,d dej)Z*G d-d dej+Z,G d.d/ d/ej-Z.G d0d dej/Z0G d1d2 d2ej/Z1G d3d dej2Z3G d4d dej/Z4G d5d de1Z5d6Z6G d7d dej7Z8G d8d9 d9e1Z9G d:d de9Z:dS )<zE
This module provides widgets to use aptdaemon in a GTK application.
z%Sebastian Heinlein <devel@glatzor.de>AptConfigFileConflictDialogAptCancelButtonAptConfirmDialogAptProgressDialogAptTerminalExpanderAptStatusIconAptRoleIconAptStatusAnimationAptRoleLabelAptStatusLabelAptMediumRequiredDialogAptMessageDialogAptErrorDialogAptProgressBarDiffViewAptTerminal    NVtez2.91)GObject)GLib)Gdk)Gtk)Pango)r      )client)*)inline_callbacks)	deferablec             C   s   t jd| S )NZ	aptdaemon)gettextdgettext)msg r    7/usr/lib/python3/dist-packages/aptdaemon/gtk3widgets.py<lambda>9   s    r"      c               @   s:   e Zd ZdZdejjfddZdd Zdd Z	d	d
 Z
dS )r   zi
    Provides a Gtk.Image which shows an icon representing the status of a
    aptdaemon transaction
    Nc             C   s@   t jj|  || _d | _g | _| jdd |d k	r<| j| d S )Nr   )r   Image__init__
_icon_size	icon_name_signalsset_alignmentset_transaction)selftransactionsizer    r    r!   r%   D   s    zAptStatusIcon.__init__c             C   s:   x| j D ]}tj| qW g | _ | j j|jd| j dS )z Connect to the given transactionzstatus-changedN)r(   r   source_removeappendconnect_on_status_changed)r+   r,   sigr    r    r!   r*   O   s
    zAptStatusIcon.set_transactionc             C   s
   || _ dS )z.Set the icon size to gtk stock icon size valueN)r&   )r+   r-   r    r    r!   set_icon_sizeW   s    zAptStatusIcon.set_icon_sizec             C   s8   t |}|dkrtj}|| jkr4| j|| j || _dS )z3Set the status icon according to the changed statusN)get_status_icon_name_from_enumr   STOCK_MISSING_IMAGEr'   set_from_icon_namer&   )r+   r,   statusr'   r    r    r!   r1   [   s    
z AptStatusIcon._on_status_changed)__name__
__module____qualname____doc__r   IconSizeDIALOGr%   r*   r3   r1   r    r    r    r!   r   ?   s
   c               @   s    e Zd ZdZdd Zdd ZdS )r   zh
    Provides a Gtk.Image which shows an icon representing the role of an
    aptdaemon transaction
    c             C   sH   x| j D ]}tj| qW g | _ | j j|jd| j | j||j d S )Nzrole-changed)r(   r   r.   r/   r0   _on_role_changedrole)r+   r,   r2   r    r    r!   r*   j   s    
zAptRoleIcon.set_transactionc             C   s8   t |}|dkrtj}|| jkr4| j|| j || _dS )z"Show an icon representing the roleN)Zget_role_icon_name_from_enumr   r5   r'   r6   r&   )r+   r,   	role_enumr'   r    r    r!   r>   r   s    
zAptRoleIcon._on_role_changedN)r8   r9   r:   r;   r*   r>   r    r    r    r!   r   e   s   c               @   sL   e Zd ZdZdejjfddZdddZdd Z	d	d
 Z
dd Zdd ZdS )r   z_
    Provides a Gtk.Image which shows an animation representing the
    transaction status
    Nc             C   sF   t j| || g | _d| _d| _d| _tt}tt}| j	|| d S )Nr   )
r   r%   	animationtickerZframe_counteriter#get_status_animation_name_from_enumSTATUS_WAITINGr4   set_animation)r+   r,   r-   namefallbackr    r    r!   r%      s    zAptStatusAnimation.__init__c             C   s>  || j krdS |dk	r|| _| j  g }tj| j\}}tjj }|dk	oR|j|r|j||d}|j	 | }	|j
 | }
x@t|	D ]4}x.t|
D ]"}|j|j|| || || qW qW t|dkr|| _d| _| j| jd  | j  n
| j| || _ n8|dk	r.|j|r.| j|| j || _ n| jtj dS )z7Show and start the animation of the given name and sizeNr   )r'   r&   stop_animationr   Zicon_size_lookupZ	IconThemeZget_defaultZhas_iconZ	load_iconZ
get_heightZ	get_widthranger/   Z	subpixbuflenrA   rC   set_from_pixbufstart_animationr6   r5   )r+   rG   rH   r-   rA   widthheightZthemeZpixbufZrowsZcolsrcr    r    r!   rF      s6    



z AptStatusAnimation.set_animationc             C   s   | j dkrtjd| j| _ dS )zStart the animationr      N)rB   r   Ztimeout_add_advance)r+   r    r    r!   rM      s    
z"AptStatusAnimation.start_animationc             C   s    | j dkrtj| j  d| _ dS )zStop the animationr   N)rB   r   r.   )r+   r    r    r!   rI      s    
z!AptStatusAnimation.stop_animationc             C   sP   | j ddkrd| _dS | jd | _| jt| jkr:d| _| j| j| j  dS )zx
        Show the next frame of the animation and stop the animation if the
        widget is no longer visible
        visibleFr   r   T)Zget_propertyrB   rC   rK   rA   rL   )r+   r    r    r!   rS      s    zAptStatusAnimation._advancec             C   s    t |}t|}| j|| dS )zC
        Set the animation according to the changed status
        N)rD   r4   rF   )r+   r,   r7   rG   rH   r    r    r!   r1      s    z%AptStatusAnimation._on_status_changed)NN)r8   r9   r:   r;   r   r<   r=   r%   rF   rM   rI   rS   r1   r    r    r    r!   r   |   s   

 c               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
r	   z<
    Status label for the running aptdaemon transaction
    Nc             C   sJ   t j|  | jdd | jtjj | jd g | _|d k	rF| j	| d S )Nr      )
ZGtkLabelr%   r)   set_ellipsizer   EllipsizeModeENDset_max_width_charsr(   r*   )r+   r,   r    r    r!   r%      s    

zAptRoleLabel.__init__c             C   sH   x| j D ]}tj| qW g | _ | j||j | j j|jd| j dS )z;Connect the status label to the given aptdaemon transactionzrole-changedN)r(   r   r.   r>   r?   r/   r0   )r+   r,   r2   r    r    r!   r*      s    zAptRoleLabel.set_transactionc             C   s   | j t| dS )zSet the role text.N)
set_markup$get_role_localised_present_from_enum)r+   r,   r?   r    r    r!   r>      s    zAptRoleLabel._on_role_changed)N)r8   r9   r:   r;   r%   r*   r>   r    r    r    r!   r	      s   
		c               @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )r
   z<
    Status label for the running aptdaemon transaction
    Nc             C   sL   t jj|  | jdd | jtjj | jd g | _	|d k	rH| j
| d S )Nr   rU   )r   Labelr%   r)   rV   r   rW   rX   rY   r(   r*   )r+   r,   r    r    r!   r%      s    
zAptStatusLabel.__init__c             C   sP   x| j D ]}tj| qW g | _ | j j|jd| j | j j|jd| j dS )z;Connect the status label to the given aptdaemon transactionzstatus-changedzstatus-details-changedN)r(   r   r.   r/   r0   r1   _on_status_details_changed)r+   r,   r2   r    r    r!   r*      s    zAptStatusLabel.set_transactionc             C   s   | j t| dS )z3Set the status text according to the changed statusN)rZ   Zget_status_string_from_enum)r+   r,   r7   r    r    r!   r1      s    z!AptStatusLabel._on_status_changedc             C   s   | j | dS )z.Set the status text to the one reported by aptN)rZ   )r+   r,   textr    r    r!   r]     s    z)AptStatusLabel._on_status_details_changed)N)r8   r9   r:   r;   r%   r*   r1   r]   r    r    r    r!   r
      s
   
	c               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )r   z`
    Provides a Gtk.Progress which represents the progress of an aptdaemon
    transactions
    Nc             C   sJ   t jj|  | jtjj | jd | jd g | _	|d k	rF| j
| d S )N g?)r   ProgressBarr%   rV   r   rW   rX   set_textZset_pulse_stepr(   r*   )r+   r,   r    r    r!   r%     s    

zAptProgressBar.__init__c             C   sf   x| j D ]}tj| qW g | _ | j j|jd| j | j j|jd| j | j j|jd| j dS )z;Connect the progress bar to the given aptdaemon transactionfinishedzprogress-changedzprogress-details-changedN)r(   r   r.   r/   r0   _on_finished_on_progress_changed_on_progress_details)r+   r,   r2   r    r    r!   r*     s    zAptProgressBar.set_transactionc             C   s$   |dkr| j   n| j|d  dS )zR
        Update the progress according to the latest progress information
        d   g      Y@N)pulseset_fraction)r+   r,   progressr    r    r!   rd   #  s    
z#AptProgressBar._on_progress_changedc             C   sx   |dkr|dkr| j d dS |dkrR| j tdtj|tj|tj|f  n"| j tdtj|tj|f  dS )zW
        Update the progress bar text according to the latest progress details
        r   r_   NzDownloaded %sB of %sB at %sB/szDownloaded %sB of %sB)ra   _r   get_size_string)r+   r,   Z
items_doneZitems_totalZ
bytes_doneZbytes_totalZspeedZetar    r    r!   re   ,  s    


z#AptProgressBar._on_progress_detailsc             C   s   | j d dS )z9Set the progress to 100% when the transaction is completer   N)rh   )r+   r,   exitr    r    r!   rc   >  s    zAptProgressBar._on_finished)N)	r8   r9   r:   r;   r%   r*   rd   re   rc   r    r    r    r!   r   	  s   
		c               @   s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )AptDetailsExpanderNTc             C   s   t jj| tdd || _g | _| jd | jd | jrDt | _	nd | _	t
 | _t j | _| jjt jj | jjt jjt jj | jj| j | jjd t j }|j| jddd | j	r|j| j	ddd | j| |d k	r| j| d S )NZDetails)labelFrR   Tr   )r   Expanderr%   rj   Zshow_terminalr(   set_sensitiveset_expandedr   terminalAptDownloadsViewdownload_viewScrolledWindowdownload_scrolledset_shadow_type
ShadowType	ETCHED_IN
set_policy
PolicyTypeNEVER	AUTOMATICaddZset_min_content_heightHBox
pack_startr*   )r+   r,   rr   hboxr    r    r!   r%   E  s,    





zAptDetailsExpander.__init__c             C   sh   x| j D ]}tj| qW | j j|jd| j | j j|jd| j | jrX| jj| | j	j| dS )z;Connect the status label to the given aptdaemon transactionzstatus-changedzterminal-attached-changedN)
r(   r   r.   r/   r0   r1   _on_terminal_attached_changedrr   r*   rt   )r+   r,   r2   r    r    r!   r*   ^  s    
z"AptDetailsExpander.set_transactionc             C   s   |t tfkr2| jd | jj  | jr| jj  nr|tkrv| jj  | jr`| jj  | jd q| jd | jd n.| jj  | jr| jj  | jd | jd d S )NTF)	ZSTATUS_DOWNLOADINGZSTATUS_DOWNLOADING_REPOrp   rv   Zshowrr   hideSTATUS_COMMITTINGrq   )r+   transr7   r    r    r!   r1   k  s"    







z%AptDetailsExpander._on_status_changedc             C   s   |r| j r| jd dS )z&Connect the terminal to the pty deviceTN)rr   rp   )r+   r,   attachedr    r    r!   r     s    
z0AptDetailsExpander._on_terminal_attached_changed)NT)r8   r9   r:   r%   r*   r1   r   r    r    r    r!   rm   C  s   
rm   c               @   s&   e Zd ZdddZdd Zdd ZdS )	r   Nc             C   sp   t jj|  g | _tj \| _| _tj	| j| _
| jdd | jd | jt jj| j |d k	rl| j| d S )NP      T)r   Terminalr%   r(   ptyopenptyZ_masterZ_slaveosttyname_ttynameZset_sizeZset_vexpandZset_ptyZPtyZnew_foreign_syncr*   )r+   r,   r    r    r!   r%     s    
zAptTerminal.__init__c             C   sH   x| j D ]}tj| qW | j j|jd| j || _| jj| j dS )z;Connect the status label to the given aptdaemon transactionzterminal-attached-changedN)	r(   r   r.   r/   r0   r   _transactionZset_terminalr   )r+   r,   r2   r    r    r!   r*     s    
zAptTerminal.set_transactionc             C   s   | j | dS )zShow the terminalN)rp   )r+   r,   r   r    r    r!   r     s    z)AptTerminal._on_terminal_attached_changed)N)r8   r9   r:   r%   r*   r   r    r    r    r!   r     s   

c               @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )r   zZ
    Provides a Gtk.Button which allows to cancel a running aptdaemon
    transaction
    Nc             C   sH   t jj|  | jd | jt j | jd g | _|d k	rD| j| d S )NT)	r   Buttonr%   Zset_use_stockZ	set_labelSTOCK_CANCELrp   r(   r*   )r+   r,   r    r    r!   r%     s    

zAptCancelButton.__init__c             C   s`   x| j D ]}tj| qW g | _ | j j|jd| j | j j|jd| j | jd| j| dS )z;Connect the status label to the given aptdaemon transactionrb   zcancellable-changedclickedN)r(   r   r.   r/   r0   rc   _on_cancellable_changed_on_clicked)r+   r,   r2   r    r    r!   r*     s    
zAptCancelButton.set_transactionc             C   s   | j | dS )zY
        Enable the button if cancel is allowed and disable it in the other case
        N)rp   )r+   r,   Zcancellabler    r    r!   r     s    z'AptCancelButton._on_cancellable_changedc             C   s   | j d d S )NF)rp   )r+   r,   r7   r    r    r!   rc     s    zAptCancelButton._on_finishedc             C   s   |j   | jd d S )NF)cancelrp   )r+   Zbuttonr,   r    r    r!   r     s    zAptCancelButton._on_clicked)N)	r8   r9   r:   r;   r%   r*   r   rc   r   r    r    r    r!   r     s   
	c               @   sD   e Zd ZdZeed\ZZZdddZ	dd Z
dd	 Zd
d ZdS )rs   z`A Gtk.TreeView which displays the progress and status of each dowload
    of a transaction.
       Nc             C   s   t jj|  t jtjtjtj}| j| d| j_	| j
d i | _g | _|d k	rZ| j| t j }tjj|j_t jtd}|j|d |j|d| j t j }t jtd}|j|d |j|| jd  | j| | j| | j| j d S )NFTZFilemarkup%)r   TreeViewr%   Z	ListStorer   TYPE_STRINGZTYPE_INT	set_modelpropsZheaders_visibleset_rules_hint_download_mapr(   r*   CellRendererTextr   rW   rX   Z	ellipsizeTreeViewColumnrj   r   Zadd_attributeCOL_TEXTZCellRendererProgressset_cell_data_func_data_progressappend_columnZset_tooltip_columnCOL_URI)r+   r,   modelZcell_uriZcolumn_downloadZcell_progressZcolumn_progressr    r    r!   r%     s.    





zAptDownloadsView.__init__c             C   s:   x| j D ]}tj| qW g | _ | j j|jd| j dS )z<Connect the download view to the given aptdaemon transactionzprogress-download-changedN)r(   r   r.   r/   r0   _on_download_changed)r+   r,   r2   r    r    r!   r*     s
    z AptDownloadsView.set_transactionc             C   s  yt |d | }W n tk
r,   d}Y nX |tkr:d}|dkrFd}|dd }	|	d7 }	|tkr|	tdtj|tj|f 7 }	nB|tkr|dkr|	tdtj| 7 }	q|	td7 }	n|	t|7 }	|	d	7 }	| j }
|
sdS y| j	| }W nt t
k
r`   |
j|	||f}|| j	|< | j }|r\|j |j  |j k}|r\| j|
j|dd
d
d
 Y n"X |
j|| j|	 |
j|| j| dS )z)Callback for a changed download progress.rf   r   Nz
<small>zDownloaded %sB of %sBr   zDownloaded %sBZ
Downloadedz</small>F)intZeroDivisionErrorZDOWNLOAD_DONEZDOWNLOAD_FETCHINGrj   r   rk   Zget_download_status_from_enumZ	get_modelr   KeyErrorr/   Zget_vadjustment	get_valueZget_page_sizeZ	get_upperZscroll_to_cellget_pathZ	set_valuer   COL_PROGRESS)r+   r,   Zurir7   descZ	full_sizeZ
downloadedmessageri   r^   r   rC   ZadjZis_scrolled_downr    r    r!   r     sJ    

z%AptDownloadsView._on_download_changedc             C   s,   |j || j}|dkr ||j_n||j_d S )Nr   r   )r   r   r   rg   value)r+   columncellr   rC   datari   r    r    r!   r   )  s    
zAptDownloadsView._data_progress)N)r8   r9   r:   r;   listrJ   r   r   r   r%   r*   r   r   r    r    r    r!   rs     s   
5rs   c               @   s   e Zd ZdZdejejf fiZdddZdd Z	d	d
 Z
dd Zdd ZedddZedd Zdd Zdd Zdd Zdd Zdd ZdS ) r   z
    Complete progress dialog for long taking aptdaemon transactions, which
    features a progress bar, cancel button, status icon and label
    rb   NTc       	      C   s&  t jj| |d d | _|| _| jd | jd | j jd t	|| _
| j j| j
ddd t j }|jd |jd t | _|j| jddd t j }|jd t j | _| jjdd |j| jddd t j }|jd t | _|j| jddd t | _| jjd t |j| jddd |j|ddd |j|ddd t|d| _| jjd| j |j| jddd | j j|ddd d | _g | _| j d	 | j!  | jj"d
d t#j$j%t#j$j&B }y| j' j(| W n* t)k
r   | j' j(t#j$| Y nX |d k	r| j*| | jd| j+ d S )N)parent   Fr      T)rr   znotify::expanded i^  r   zdelete-eventr   ),r   Dialogr%   _expanded_sizedebconfset_border_widthset_resizableget_content_areaset_spacingr   button_cancelZget_action_arear   r   r   iconVBoxr\   
label_roler)   r   ri   r
   rn   r1   rE   rm   expanderr0   _on_expandedr   r(   	set_titlerealizeset_size_requestr   Z
WMFunctionZMOVEZRESIZEZ
get_windowZset_functions	TypeErrorr*   _on_dialog_delete_event)	r+   r,   r   rr   r   r   ZvboxZvbox_progressZ	functionsr    r    r!   r%   :  sX    










zAptProgressDialog.__init__c             C   s   | j j  dS )NT)r   r   )r+   dialogZeventr    r    r!   r   n  s    
z)AptProgressDialog._on_dialog_delete_eventc             C   s   |j  s*| jjj | j f| _| jd nX| jrp| jd | j\}\}}|| jjj krb| j  q| j|| n| jd | j  d S )NFT)	get_expandedr   rr   get_visibleget_sizer   r   _resize_to_show_detailsresize)r+   r   paramZterm_visibleZstored_widthZstored_heightr    r    r!   r   r  s    



zAptProgressDialog._on_expandedc             C   s   | j  \}}| jj j}| jj j}| jjj rn| jjj d }| jjj d }| j	|| | || |  n| j	|d |d  dS )a  Resize the window to show the expanded details.

        Unfortunately the expander only expands to the preferred size of the
        child widget (e.g showing all 80x24 chars of the Vte terminal) if
        the window is rendered the first time and the terminal is also visible.
        If the expander is expanded afterwards the window won't change its
        size anymore. So we have to do this manually. See LP#840942
        r   r   rf   rR   N)
r   r   Zget_allocationrN   rO   rr   r   Zget_char_widthZget_char_heightr   )r+   Z	win_widthZ
win_heightZ	exp_widthZ
exp_heightZterminal_widthZterminal_heightr    r    r!   r     s    	z)AptProgressDialog._resize_to_show_detailsc             C   s    |t kr| jjj r| j  d S )N)r   r   rr   r   r   )r+   r   r7   r    r    r!   r1     s    z$AptProgressDialog._on_status_changedFc             C   s   | j |||||S )ax  Run the transaction and show the progress in the dialog.

        Keyword arguments:
        attach -- do not start the transaction but instead only monitor
                  an already running one
        close_on_finished -- if the dialog should be closed when the
                  transaction is complete
        show_error -- show a dialog with the error message
        )_run)r+   attachclose_on_finished
show_errorreply_handlererror_handlerr    r    r!   run  s    
zAptProgressDialog.runc             c   s   y^| j jd| j||}| jj| |r4| j j V  n | jrH| j jdV  | j j V  | j	  W n2 t
k
r } z|r~|| n W Y d d }~X nX |r|  d S )Nrb   Zgnome)r   r0   rc   r(   r/   syncr   Zset_debconf_frontendr   show_all	Exception)r+   r   r   r   r   r   r2   errorr    r    r!   r     s     
zAptProgressDialog._runc             C   s&   t |}| j| | jjd|  dS )z8Show the role of the transaction in the dialog interfacez<big><b>%s</b></big>N)r[   r   r   rZ   )r+   r,   r@   r?   r    r    r!   r>     s    
z"AptProgressDialog._on_role_changedc             C   s   x| j D ]}tj| qW g | _ | j j|jd| j | j j|jd| j | j j|jd| j | j j|jd| j	 | j||j
 | jj| | jj| | jj| | jj| || _dS )z5Connect the dialog to the given aptdaemon transactionzstatus-changedzrole-changedzmedium-requiredzconfig-file-conflictN)r(   r   r.   r/   Zconnect_afterr1   r0   r>   _on_medium_required_on_config_file_conflictr?   ri   r*   r   rn   r   r   )r+   r,   r2   r    r    r!   r*     s$    



z!AptProgressDialog.set_transactionc             C   sD   t ||| }|j }|j  |tjjkr6| jj| n
| jj  d S )N)	r   r   r   r   ResponseTypeOKr   Zprovide_mediumr   )r+   r,   mediumdriver   resr    r    r!   r     s    z%AptProgressDialog._on_medium_requiredc             C   sJ   t ||| }|j }|j  |tjjkr8| jj|d n| jj|d d S )NreplaceZkeep)r   r   r   r   r   YESr   Zresolve_config_file_conflict)r+   r,   oldnewr   r   r    r    r!   r     s    z*AptProgressDialog._on_config_file_conflictc             C   sD   |r| j   |tkr6|r6t| jj| }|j  |j   | jd d S )Nrb   )r   ZEXIT_FAILEDr   r   r   r   emit)r+   r,   r7   closer   Zerr_diar    r    r!   rc     s    zAptProgressDialog._on_finished)NNTT)FTTNN)r8   r9   r:   r;   r   ZSIGNAL_RUN_FIRSTZ	TYPE_NONEZ__gsignals__r%   r   r   r   r1   r   r   r   r   r>   r*   r   r   rc   r    r    r    r!   r   1  s$    
3 		c               @   s"   e Zd ZdZdddZdd ZdS )_ExpandableDialogzDialog with an expander.Nc             C   s  |st jt jjf}t jj| |d | jd | j|  | jd | j	d | j
 jd |sbt j}t jj|t jj}|jdd t j }	|	jd |	j	d t j }
|
jd |	j|ddd |	j|
ddd t j | _| jjd | jjdd | jjd |
j| jddd | j
 j|	ddd t j|d| _| jjd | jjd | jjd	| j d
| _|
j| jddd d}|rd| }|r|r|d7 }||7 }| jj| |r| jj | n| jj!d d
S )a3  Return an _AptDaemonDialog instance.

        Keyword arguments:
        parent -- set the dialog transient for the given Gtk.Window
        stock_type -- type of the Dialog, defaults to Gtk.STOCK_DIALOG_QUESTION
        expanded_child -- Widget which should be expanded
        expander_label -- label for the expander
        title -- a news header like title of the dialog
        message -- the message which should be shown in the dialog
        buttons -- tuple containing button text/reponse id pairs, defaults
                   to a close button
        )r   r   Fr   r   r   T)rn   znotify::expandedNz<b><big>%s</big></b>z

)"r   ZSTOCK_CLOSEr   ZCLOSEr   r%   r   add_buttonsr   r   r   r   STOCK_DIALOG_QUESTIONr$   new_from_stockr<   r=   r)   r   r   r   r\   rn   set_selectableZset_line_wrapro   r   Zset_use_underliner0   r   r   rZ   r~   rp   )r+   r   
stock_typeexpanded_childexpander_labeltitler   buttonsr   	hbox_base	vbox_leftr^   r    r    r!   r%     sT    







z_ExpandableDialog.__init__c             C   sh   |j  rP| jd | jrdy| j| jd | jd  W qd ttfk
rL   Y qdX n| j | _| jd d S )NTr   r   F)r   r   r   r   
IndexErrorr   r   )r+   r   r   r    r    r!   r   @  s    

z_ExpandableDialog._on_expanded)NNNNNNN)r8   r9   r:   r;   r%   r   r    r    r    r!   r      s    
;r   c               @   s   e Zd ZdZdddZdS )r   z Dialog to ask for medium change.Nc             C   sp   t jj| |t jjd td| }td| }| jd||f  | jt jt j	j
tdt j	j | jt j	j d S )N)r   typezCD/DVD '%s' is requiredzXPlease insert the above CD/DVD into the drive '%s' to install software packages from it.z<big><b>%s</b></big>

%s	C_ontinue)r   MessageDialogr%   ZMessageTypeINFOrj   rZ   r   r   r   CANCELr   set_default_response)r+   r   r   r   r^   r   r    r    r!   r%   V  s    
z AptMediumRequiredDialog.__init__)N)r8   r9   r:   r;   r%   r    r    r    r!   r   R  s   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )r   zODialog to confirm the changes that would be required by a
    transaction.
    Nc       	      C   sH  t jj| |d | jt jt jj | jtdt jj || _	|| _
t|tjrd| j  | jj| n
| j| | jd | jd | j jd t jjt jt jj}|jdd t j }|jd |jd t j }|jd |j|ddd |j|ddd t j | _| jj d | jjdd |j| jddd | j j|ddd t j!t"j#| _$t j%j&| j$| _'| j'j(d | j'j)d t j* | _+| j'j,| j+ t j- }| j+j|d | j+j.|| j/d t j0 }| j+j|d | j+j.|| j1d t j2 | _3| j3j4t j5j6t j5j6 | j3j7t j8j9 | j3j:| j' |j| j3ddd | j;t jj dS )	aH  Return an AptConfirmDialog instance.

        Keyword arguments:
        trans -- the transaction of which the dependencies should be shown
        cache -- an optional apt.cache.Cache() instance to provide more details
                 about packages
        parent -- set the dialog transient for the given Gtk.Window
        )r   r   Tr   r   r   FN)<r   r   r%   Z
add_buttonr   r   r   rj   r   cacher   
isinstancer   ZWindowr   ZwindowZset_transient_forr   r   r   r   r$   r   r   r<   r=   r)   r   r   r   r\   rn   r   Z	TreeStorer   r   	treestorer   Znew_with_modeltreeviewZset_headers_visibler   r   r   r   ZCellRendererPixbufr   render_package_iconr   render_package_descru   scrolledrz   r{   r}   rw   rx   ry   r~   r   )	r+   r   r   r   r   r   r   Z	cell_iconZ	cell_descr    r    r!   r%   j  s`    	











zAptConfirmDialog.__init__c          	   C   s  | j j  xttdtdtdtdtdtdtdgD ]f\}}| jj| r>| j jdd	| g}x<| jj| D ],}x&| j|D ]}| j j|t|g qW qtW q>W td
}t	| j dkr| j j
tjj }| jj  | jj| | jjd | jjt rtd}n| jjt r$td}nv| jjt r<td}n^| jjt rTtd}nF| jjt rltd}n.| jjt rtd}n| jjt rtd}t	|dk r| jd | jjtjjtjj n| jjdd n td}| jjdd | jj   | jj!r&|d7 }|tdt"j#| jj! 7 }| jj$dk rX|d7 }|tdt"j#| jj$ 7 }n0| jj$dkr|d7 }|tdt"j#| jj$ 7 }| j%j&d||f  dS )z2Show a message and the dependencies in the dialog.ZInstallZ	ReinstallZRemoveZPurgeZUpgradeZ	DowngradezSkip upgradeNz	<b>%s</b>z0Please take a look at the list of changes below.r   Fz'Additional software has to be installedz*Additional software has to be re-installedz%Additional software has to be removedz$Additional software has to be purgedz&Additional software has to be upgradedz(Additional software has to be downgradedzUpdates will be skippedr   i^  rR   zAdditional changes are required
z %sB will be downloaded in total.r   z %sB of disk space will be freed.z!%sB more disk space will be used.z<b><big>%s</big></b>

%s)'r   clear	enumeraterj   r   Zdependenciesr/   map_packagestrrK   Z
filter_newr   ZTreePathZ	new_firstr  Z
expand_allr   Zset_show_expandersZPKGS_INSTALLZPKGS_REINSTALLZPKGS_REMOVEZ
PKGS_PURGEZPKGS_UPGRADEZPKGS_DOWNGRADEZ	PKGS_KEEPr   r  rz   r{   r}   r|   r   Zcollapse_allZdownloadr   rk   Zspacern   rZ   )r+   indexr   ZpiterpkgobjectZfiltered_storer   r    r    r!   _show_changes  sn    

 









zAptConfirmDialog._show_changesc             C   s   |gS )a  Map a package to a different object type, e.g. applications
        and return a list of those.

        By default return the package itself inside a list.

        Override this method if you don't want to store package names
        in the treeview.
        r    )r+   r  r    r    r!   r    s    	zAptConfirmDialog.map_packagec             C   s4   |j |}|j dkr d|j_nd|j_d|j_dS )zData func for the Gtk.CellRendererPixbuf which shows the package.

        Override this method if you want to show custom icons for
        a package or map it to applications.
        r   FTzapplications-otherN)r   Z	get_depthr   rT   r'   )r+   r   r   r   rC   r   pathr    r    r!   r    s
    

z$AptConfirmDialog.render_package_iconc       
      C   s   |j |d}|sdS y|jddd \}}W n tk
rJ   |}d}Y nX y4|rjd||| j| jf }	nd|| j| jf }	W n2 ttfk
r   |rd||f }	nd| }	Y nX |jd	|	 dS )
zData func for the Gtk.CellRendererText which shows the package.

        Override this method if you want to show more information about
        a package or map it to applications.
        r   N=r#   z%s (%s)
<small>%s</small>z%s
<small>%s</small>z%s (%s)z%sr   )r   split
ValueErrorr   Zsummaryr   r   set_property)
r+   r   r   r   rC   r   r   pkg_nameZpkg_versionr^   r    r    r!   r    s&    
z$AptConfirmDialog.render_package_descc             C   s   | j   | j  tjj| S )N)r  r   r   r   r   )r+   r    r    r!   r     s    zAptConfirmDialog.run)NN)
r8   r9   r:   r;   r%   r  r  r  r  r   r    r    r    r!   r   d  s   
;Ac               @   s"   e Zd ZdZdddZdd ZdS )r   zSDialog to resolve conflicts between local and shipped
    configuration files.
    Nc             C   s   || _ || _td| }td}tjddd}|jtjjtjj |jtj	j
 t | _| jjdd |j| j tj| |td|||tdtjjtd	tjjfd
 | jtjj d S )NzLReplace your changes in '%s' with a later version of the configuration file?zRIf you don't know why the file is there already, it is usually safe to replace it.T)ZhexpandZvexpandr   rR   Z_ChangesZ_KeepZ_Replace)r   r   r   r   r   r   r   )	from_pathto_pathrj   r   ru   rz   r{   r}   rw   rx   ry   r   diffviewr   r~   r   r%   r   ZNOr   r   )r+   r  r  r   r   r   r  r    r    r!   r%   &  s&    z$AptConfigFileConflictDialog.__init__c             C   s$   | j   | jj| j| j tj| S )N)r   r  	show_diffr  r  r   r   )r+   r    r    r!   r   =  s    zAptConfigFileConflictDialog.run)N)r8   r9   r:   r;   r%   r   r    r    r    r!   r      s   
zp^@@ \-(?P<from_start>[0-9]+)(?:,(?P<from_context>[0-9]+))? \+(?P<to_start>[0-9]+)(?:,(?P<to_context>[0-9]+))? @@c               @   s$   e Zd ZdZdZdd Zdd ZdS )r   z'Shows the difference between two files.u   […]
c             C   s   t j | _t jj| | jd | jdd | jd | jj }t jj	d}|j
dd |j| t jj	d}|j
ddd	 |j| t jj	d
}|j
ddd	 |j| t jj	d}|j
ddd	 |j| d S )N)bufferZeditableFdefaultZMono)fontr~   z#8ae234)r  Z
backgroundremovez#ef2929numberz#eee)r   Z
TextBuffer
textbufferTextViewr%   r  Zset_cursor_visibleZget_tag_tableZTextTagr   Zset_propertiesr~   )r+   ZtagsZtag_defaultZtag_addZ
tag_removeZtag_numr    r    r!   r%   M  s(    





zDiffView.__init__c                s|  y<t |}|j }W dQ R X t |}|j }W dQ R X W n tk
rP   dS X  fdd}d} jj }xtj||ddD ]}	|	jdrtj	t
|	}
|
sqt|
jd}|d	kr|| jd
 q|	jds|	jdrqq|	jdr|d	7 }||t|d |||	d
 q|	jdrB|d	7 }||t|d |||	d q|	jdrdtt| }|||d |||	d qW dS )z&Show the difference between two files.Nc                s2   | j  } jj| |  jj| jj||  d S )N)Z
get_offsetr  insertZapply_tag_by_nameZget_iter_at_offset)rC   r^   tagoffset)r+   r    r!   insert_tagged_textq  s    z.DiffView.show_diff.<locals>.insert_tagged_textr   r   )Zlinetermz@@Z
from_startr   r  z---z+++r_   r  -r  +r~   )open	readlinesIOErrorr  Zget_start_iterdifflibZunified_diff
startswithrematchREGEX_RANGEr   groupELLIPSISr	  rK   )r+   r  r  fpZ
from_linesZto_linesr"  Zline_numberrC   liner+  Zspacesr    )r+   r!   r  d  s@    




zDiffView.show_diffN)r8   r9   r:   r;   r.  r%   r  r    r    r    r!   r   G  s   c               @   s   e Zd ZdZdddZdS )_DetailsExpanderMessageDialogz*
    Common base class for Apt*Dialog
    Nc       	   	   C   s   t j }|jt jjt jj |jt jj t j }|j	t j
j |j }|j| tj| |td||||d | j  |r|j| n| jjd d S )NZ_Details)r   r   r   r   r   r   F)r   ru   rz   r{   r}   rw   rx   ry   r  Zset_wrap_modeZWrapModeZWORDZ
get_bufferr~   r   r%   rj   r   Zinsert_at_cursorr   Zset_visible)	r+   r^   r   r   detailsr   r  Ztextviewr  r    r    r!   r%     s     
z&_DetailsExpanderMessageDialog.__init__)NN)r8   r9   r:   r;   r%   r    r    r    r!   r1    s   r1  c               @   s   e Zd ZdZdddZdS )r   zM
    Dialog for aptdaemon errors with details in an expandable text view
    Nc             C   s0   t |j}t|j}tj| ||tj|j| d S )N)Zget_error_string_from_enumcodeZget_error_description_from_enumr1  r%   r   ZSTOCK_DIALOG_ERRORr2  )r+   r   r   r^   r   r    r    r!   r%     s    

zAptErrorDialog.__init__)NN)r8   r9   r:   r;   r%   r    r    r    r!   r     s   )r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   );r;   
__author____all__r(  r   r   r   r*  ZgiZrequire_versionapt_pkgZgi.repositoryr   r   r   r   r   r   r   r   ZenumsZdeferr   Zdefer.utilsr   rj   r   rJ   Z	COLUMN_IDZCOLUMN_PACKAGEr$   r   r   r   r\   r	   r
   r`   r   ro   rm   r   r   r   r   r   rs   r   r   r   r   r   r   r   r,  r  r   r1  r   r    r    r    r!   <module>   sb         &Q!:C(g PR =#K