o
    ïØ{f¥L  ã                   @   sL  d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZ d dlmZmZmZmZmZmZmZmZ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!Z!d dl"Z"G dd„ deƒZ#G dd„ dej$ƒZ%G dd„ deeƒZ&e'dkr¤d dl(Z(e( )¡ Z*dZ+d dl,m-Z- e-e+e*ƒZ.e&e.dƒZ/e/ 0dgg g ¡ e 1¡  dS dS )é    )Úprint_function)ÚGtk)ÚclientÚerrors)Úinline_callbacks)ÚAptCancelButtonÚAptConfigFileConflictDialogÚAptDetailsExpanderÚAptMediumRequiredDialogÚAptProgressBar)	ÚEXIT_SUCCESSÚEXIT_FAILEDÚSTATUS_COMMITTINGÚSTATUS_DOWNLOADINGÚSTATUS_DOWNLOADING_REPOÚSTATUS_FINISHEDÚget_error_description_from_enumÚget_error_string_from_enumÚget_status_string_from_enum)ÚInstallBackend)ÚUnitySupport)ÚBuilderDialog)ÚgettextNc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚUpdateManagerExpanderaF  An AptDetailsExpander which can be used with multiple terminals.

       The default AptDetailsExpander will shrink/hide when its transaction
       finishes. But here we want to support "chaining" transactions. So we
       override the status-changed handler to only do that when we are
       running the final transaction.TFc                    s   t ƒ  ||¡ || _d S ©N)ÚsuperÚ__init__Úfinal)ÚselfÚtransactionÚterminalr   ©Ú	__class__© úO/usr/lib/python3/dist-packages/UpdateManager/backend/InstallBackendAptdaemon.pyr   -   s   
zUpdateManagerExpander.__init__c                 C   sÒ   |t tfv r|  d¡ | j ¡  | jr| j ¡  d S d S |tkrE| j ¡  | jr4| j ¡  |  d¡ d S | jrC|  	d¡ |  d¡ d S d S | jre|t
krg| j ¡  | jrY| j ¡  |  d¡ |  	d¡ d S d S d S ©NTF)r   r   Úset_sensitiveÚdownload_scrolledÚshowr    Úhider   r   Úset_expandedr   )r   ÚtransÚstatusr#   r#   r$   Ú_on_status_changed1   s,   

ÿ


þ


ûz(UpdateManagerExpander._on_status_changed)TF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   Ú__classcell__r#   r#   r!   r$   r   %   s    r   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
ÚAptStackedProgressBaraq   A GtkProgressBar which represents the state of many aptdaemon
    transactions.

    aptdaemon provides AptProgressBar for the state of *one* transaction to
    be represented in a progress bar. This widget creates one of those per
    containing transaction, and scales its progress to the given ratio, so
    one progress bar can show the state of many transactions.c                    s    d| _ g | _|| _tƒ  ¡  d S )Nr   )Úcurrent_max_progressÚprogress_barsÚunityr   r   )r   r6   r!   r#   r$   r   P   s   zAptStackedProgressBar.__init__c                 C   s~   d|  krdksJ ‚ J ‚t |ƒ}| j |¡ | j|_|  j|7  _| jdkr+d| _| j|_| d| j¡ | d| j¡ d S )Nr   é   znotify::fractionznotify::text)	r   r5   Úappendr4   ÚminÚmaxÚconnectÚ_update_progressÚ_update_text)r   r+   Úmax_progressÚprogressr#   r#   r$   Úadd_transactionW   s   
z%AptStackedProgressBar.add_transactionc                 C   s@   |j |j }|| ¡  }|j| }|  |¡ | j |d ¡ d S )Néd   )r:   r9   Úget_fractionÚset_fractionr6   Úset_progress)r   Úinner_progressÚdataÚdeltaÚposition_in_deltaÚnew_progressr#   r#   r$   r<   f   s
   

z&AptStackedProgressBar._update_progressc                 C   s   |   | ¡ ¡ d S r   )Úset_textÚget_text)r   rE   rF   r#   r#   r$   r=   m   s   z"AptStackedProgressBar._update_text)	r.   r/   r0   r1   r   r@   r<   r=   r2   r#   r#   r!   r$   r3   G   s    r3   c                   @   s²   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
d„ Z	edd„ ƒZ
dd„ Zedd„ ƒZedd„ ƒZdd„ Zdd„ Ze		d&dd„ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ ZdS )'ÚInstallBackendAptdaemonzCMakes use of aptdaemon to refresh the cache and to install updates.c                 C   sh   t  | ||¡ tj |jd¡}t | ||d¡ t ¡ | _t	ƒ | _
d | _d | _d | _d | _d | _d | _d S )Nzgtkbuilder/UpdateProgress.uiÚpane_update_progress)r   r   ÚosÚpathÚjoinÚdatadirr   r   Ú	AptClientr   r6   Ú_expanded_sizeÚbutton_cancelÚtrans_failed_msgÚprogressbarÚ_active_transactionÚ	_expander)r   Úwindow_mainÚactionÚui_pathr#   r#   r$   r   t   s   
ÿ
ÿ

z InstallBackendAptdaemon.__init__c                 C   s:   | j r| j  ¡ rz| j  ¡  W dS  ty   Y dS w dS r%   )rT   Úget_sensitiveÚclickedÚ	Exception)r   r#   r#   r$   Úclose„   s   üüzInstallBackendAptdaemon.closec              
   c   s‚   � z| j jdd�V }|  || jtdƒd¡V  W dS  tjy.   | j| jddddd� Y dS  ty@   | j| jddddd� ‚ w )zRefresh the package listT©Údeferu   Checking for updatesâ€¦FN©Ú
authorizedÚsuccessÚerror_stringÚ
error_desc)	r   Úupdate_cacheÚ_show_transactionÚACTION_UPDATEÚ_r   ÚNotAuthorizedErrorÚ_action_doner^   )r   r+   r#   r#   r$   Úupdate�   s$   €
ÿþþüzInstallBackendAptdaemon.updatec                 C   sP   t |jjƒ}t|jjƒ}| jrd}|d | j }nd }| j|dd|||d� d S )NTÚ
F©rc   rd   re   rf   Útrans_failed)r   ÚerrorÚcoder   rU   rl   )r   r+   rZ   re   rf   rp   r#   r#   r$   Ú_show_transaction_error¡   s   
üz/InstallBackendAptdaemon._show_transaction_errorc                 C   s$   |t kr|  ||¡ d S |  ¡  d S r   )r   rs   Ú_apt_update_oem)r   r+   r,   rZ   r#   r#   r$   Ú_update_next_package¯   s   z,InstallBackendAptdaemon._update_next_packagec              
   c   sN  � | j sJ ‚| j  ¡ }d|› d�}z0tj |¡r?| jj|d�V }| j r'| j}n| j}| j	|| j
tdƒd|d| j d�V  W d S W d S  tjyW   | j| j
ddd d d	� Y d S  tjyo } zt|ƒ| _W Y d }~d S d }~w tjy” } z| ¡ d
kr~‚ | j| j
ddd d d	� W Y d }~d S d }~w ty¦   | j| j
ddd d d	� ‚ w )Nz/etc/apt/sources.list.d/z.list)Úsources_listõ   Installing updatesâ€¦Tçš™™™™™¹?©Úon_finished_handlerÚprogress_bar_maxFrb   ú"org.freedesktop.DBus.Error.NoReply)Ú_oem_packages_to_updateÚpoprN   rO   Úexistsr   rg   ru   Ú_on_finishedrh   ÚACTION_PRE_INSTALLrj   Ú_len_oem_updatesr   rk   rl   ÚTransactionFailedÚstrrU   ÚdbusÚDBusExceptionÚget_dbus_namer^   )r   ÚelemÚsources_list_filer+   Úfinished_handlerÚer#   r#   r$   rt   µ   sV   €

ÿû÷þ€þ€þüz'InstallBackendAptdaemon._apt_update_oemc                 C   sJ   |t kr|  ||¡ d S |j\}}}}}}t|ƒ| _t|ƒ| _|  ¡  d S r   )r   rs   ÚpackagesÚsetr}   Úlenr‚   rt   )r   r+   r,   rZ   Úinstallrj   r#   r#   r$   Ú_update_oemÞ   s   

z#InstallBackendAptdaemon._update_oemc              
   c   sH  � t |ƒt |ƒB | _z4|s|s| j| jddd d d d� W d S |r<| jj|dd�V }| j|| jtdƒd| jdd�V  W d S W d S  t	j
yT   | j| jddd d d� Y d S  t	jyl } zt|ƒ| _W Y d }~d S d }~w tjy‘ } z| ¡ d	kr{‚ | j| jddd d d� W Y d }~d S d }~w ty£   | j| jddd d d� ‚ w )
NTro   r`   rw   rx   ry   Frb   r|   )r�   Úall_oem_packagesrl   r�   r   Úinstall_packagesrh   rj   r�   r   rk   rƒ   r„   rU   r…   r†   r‡   r^   )r   Úpkgs_install_oemÚpkgs_upgrade_oemr+   r‹   r#   r#   r$   Ú
commit_oemî   sZ   €ýÿûý
þ€þ€þüz"InstallBackendAptdaemon.commit_oemc           	   
   c   s  � z#g  } }}| j j||||||dd�V }|  || jtdƒd¡V  W dS  tjy:   | j| jddddd� Y dS  tjyR } zt	|ƒ| _
W Y d}~dS d}~w tjyw } z| ¡ dkra‚ | j| jddddd� W Y d}~dS d}~w ty‰   | j| jddddd� ‚ w )z)Commit a list of package adds and removesTr`   rw   FNrb   r|   )r   Úcommit_packagesrh   ÚACTION_INSTALLrj   r   rk   rl   rƒ   r„   rU   r…   r†   r‡   r^   )	r   Úpkgs_installÚpkgs_upgradeÚpkgs_removeÚ	reinstallÚpurgeÚ	downgrader+   r‹   r#   r#   r$   Úcommit  sB   €
þ
ÿþ€þ€þüzInstallBackendAptdaemon.commitc                 C   s   |  |¡ d S r   )Ú	set_label)r   r+   ÚdetailsÚlabel_detailsr#   r#   r$   Ú_on_details_changed3  s   z+InstallBackendAptdaemon._on_details_changedc                 C   sD   |  t|ƒ¡ |tkr| jr| jj ¡ r |  | j¡ d S d S d S d S r   )rŸ   r   r   rX   r    Úget_visibleÚ_resize_to_show_details)r   r+   r,   r¡   r#   r#   r$   r-   6  s   
ÿþz*InstallBackendAptdaemon._on_status_changedr7   Nc                 c   s–  � |d u r| j }| j |¡ | js#t| jƒ| _| j ¡  | j | j¡ | j 	||¡ | j
r4| j
 |¡ nt|ƒ| _
| j
 ¡  | j | j
¡ || jkrQ| j
 tj¡ |r�| js�t|ƒ| _| j d¡ | j d¡ | j ¡  | j d| j¡ | j | j¡ | j ¡  n| j |¡ || jk| j_n| jr—| j ¡  | d| j| j¡ | d| j| j¡ | d||¡ | d| j ¡ | d| j!¡ | "d¡V  | #¡ V  d S )	NTznotify::expandedzstatus-details-changedzstatus-changedÚfinishedzmedium-requiredzconfig-file-conflictÚgnome)$r€   Úlabel_headerrŸ   rV   r3   r6   r(   Úprogressbar_slotÚaddr@   rT   Úset_transactionr   Úbutton_cancel_slotri   r   Ú
STOCK_STOPrX   r   Úset_vexpandÚset_hexpandÚshow_allr;   Ú_on_expandedÚexpander_slotr�   r   Ú_expander_slotr)   r¢   r¡   r-   Ú_on_medium_requiredÚ_on_config_file_conflictÚset_debconf_frontendÚrun)r   r+   rZ   ÚheaderÚshow_detailsr{   rz   r#   r#   r$   rh   >  sT   €





ÿ

ÿ
ÿz)InstallBackendAptdaemon._show_transactionc                 C   s|   |  ¡ s|j ¡ | j ¡ f| _| j ¡  d S | jr7| j\}\}}||j ¡ kr.|  |¡ d S | j ||¡ d S |  |¡ d S r   )	Úget_expandedr    r£   rY   Úget_sizerS   Úend_user_resizabler¤   Úbegin_user_resizable)r   ÚexpanderÚparamÚterm_visibleÚstored_widthÚstored_heightr#   r#   r$   r°   t  s   ÿÿz$InstallBackendAptdaemon._on_expandedc           
      C   s�   |  ¡ rF| j ¡ \}}| ¡ j}| ¡ j}|j ¡ r5|j ¡ d }|j 	¡ d }|| | }|| | }	n|d }|d }	| j 
||	¡ dS 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
        éP   é   rA   éÈ   N)r¹   rY   rº   Úget_allocationÚwidthÚheightr    r£   Úget_char_widthÚget_char_heightr¼   )
r   r½   Ú	win_widthÚ
win_heightÚ	exp_widthÚ
exp_heightÚterminal_widthÚterminal_heightÚ	new_widthÚ
new_heightr#   r#   r$   r¤   ‰  s   	


ôz/InstallBackendAptdaemon._resize_to_show_detailsc                 C   sD   t ||| jƒ}| ¡ }| ¡  |tjjkr| |¡ d S | ¡  d S r   )	r
   rY   r¶   r)   r   ÚResponseTypeÚOKÚprovide_mediumÚcancel)r   r   ÚmediumÚdriveÚdialogÚresr#   r#   r$   r³      s   z+InstallBackendAptdaemon._on_medium_requiredc                 C   sJ   t ||| jƒ}| ¡ }| ¡  |tjjkr| |d¡ d S | |d¡ d S )NÚreplaceÚkeep)r   rY   r¶   r)   r   rÒ   ÚYESÚresolve_config_file_conflict)r   r   ÚoldÚnewrØ   rÙ   r#   r#   r$   r´   ©  s   z0InstallBackendAptdaemon._on_config_file_conflictc              
   C   sž   d }d }d}|t kr"t|jjƒ}t|jjƒ}| jr"d}|d | j }| j d¡ |tk}z| j	|d||||d� W d S  t
yN   | j	|d|||d� Y d S w )NFTrn   éÿÿÿÿro   rb   )r   r   rq   rr   r   rU   r6   rD   r   rl   Ú	TypeError)r   r+   r,   rZ   re   rf   rp   Ú
is_successr#   r#   r$   r€   ²  s.   ýþüz$InstallBackendAptdaemon._on_finished)r7   N)r.   r/   r0   r1   r   r_   r   rm   rs   ru   rt   r�   r•   rž   r¢   r-   rh   r°   r¤   r³   r´   r€   r#   r#   r#   r$   rL   q   s4    

(
'
þ5		rL   Ú__main__z/usr/share/update-manager)ÚUpdateManagerÚ2vcard)2Ú
__future__r   Úgi.repositoryr   Ú	aptdaemonr   r   ra   r   Úaptdaemon.gtk3widgetsr   r   r	   r
   r   Úaptdaemon.enumsr   r   r   r   r   r   r   r   r   ÚUpdateManager.backendr   ÚUpdateManager.UnitySupportr   ÚUpdateManager.Dialogsr   r   rj   r…   rN   r   ÚProgressBarr3   rL   r.   ÚmockÚMockÚoptionsÚdata_dirÚUpdateManager.UpdateManagerrä   ÚappÚbrž   Úmainr#   r#   r#   r$   Ú<module>   s6   ,
"*  ^

ö