o
    {fL                     @   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rd 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 )Nd   )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st|| _| 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   ,
"*  ^

