U
    d@                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ dZdZdZedd	 d
d ZG dd dejZG dd dejjeZdS )    )GLibN)SourceEntry)DBusGMainLoop)SoftwarePropertieszcom.ubuntu.SoftwareProperties/T)Zset_as_defaultc                 C   s   t jdk r| dS | S d S )N3zutf-8)sysversionencode)string r   P/usr/lib/python3/dist-packages/softwareproperties/dbus/SoftwarePropertiesDBus.py_to_unicode(   s    

r   c                   @   s   e Zd ZdZdS )PermissionDeniedByPolicyz6com.ubuntu.SoftwareProperties.PermissionDeniedByPolicyN)__name__
__module____qualname___dbus_error_namer   r   r   r   r   .   s   r   c                       s  e Zd ZdNddZ fddZ fddZejje	d	d
dd Z
ejje	d	d
dd Zejje	d	d
dd Zejje	d	d
dd Zejje	d	d
dd Zejje	ddd	d	ddOddZejje	ddd	d	ddPddZejje	dddd	ddQddZejje	dddd	ddRdd Zejje	dddd	ddSd!d"Zejje	dddd	ddTd#d$Zejje	ddd	d	ddUd%d&Zejje	ddd	d	ddVd'd(Zejje	dddd	ddWd)d*Zejje	ddd+d,ddXd-d.Zejje	ddd	d	ddYd/d0Zejje	ddd	d	ddZd1d2Zd3d4 Zd5d6 Zejje	ddd,d	dd[d7d8Zejje	ddd9d	dd\d:d;Zejje	ddd9d	dd]d<d=Z ejje	ddd9d	dd^d>d?Z!ejje	dddd	dd_d@dAZ"ejje	dddd	dd`dBdCZ#ejje	dddd,ddadDdEZ$ejje	dddd,ddbdFdGZ%ejje	dddd,ddcdHdIZ&ejje	ddd	d,ddddJdKZ'dLdM Z(  Z)S )eSoftwarePropertiesDBusNr   c                 C   sT   t j| |||d d | _d | _tjjt|d}tjj| |t	 d| _
td d S )N)optionsdatadirrootdir)busTzwaiting for connections)r   __init__	dbus_infopolkitdbusserviceZBusNameDBUS_INTERFACE_NAMEObject	DBUS_PATHenforce_polkitloggingdebug)selfr   r   r   r   Zbus_namer   r   r   r   3   s    zSoftwarePropertiesDBus.__init__c                    s   t t|   |   d S N)superr   save_sourceslistSourcesListModifiedr$   	__class__r   r   r'   A   s    z'SoftwarePropertiesDBus.save_sourceslistc                    s   t t|   |   d S r%   )r&   r   write_configConfigModifiedr)   r*   r   r   r,   D   s    z#SoftwarePropertiesDBus.write_config )Zdbus_interfaceZ	signaturec                 C   s   t d dS )0 emit signal when the sources.list got modified zSourcesListModified signalNr"   r#   r)   r   r   r   r(   I   s    z*SoftwarePropertiesDBus.SourcesListModifiedc                 C   s   t d dS )r/   zConfigModified signalNr0   r)   r   r   r   r-   N   s    z%SoftwarePropertiesDBus.ConfigModifiedc                 C   s   t d dS )z, emit signal when the apt keys got modified zKeysModified signalNr0   r)   r   r   r   KeysModifiedS   s    z#SoftwarePropertiesDBus.KeysModifiedc                 C   s   t d dS )z6 emit signal when the policykit authentication failed zAuth signalNr0   r)   r   r   r   
AuthFailedX   s    z!SoftwarePropertiesDBus.AuthFailedc                 C   s   t d dS )z( emit signal when adding a cdrom failed zCdrom scan failed signalNr0   r)   r   r   r   CdromScanFailed]   s    z&SoftwarePropertiesDBus.CdromScanFailedsenderconn)Zsender_keywordZconnection_keywordZin_signatureZout_signaturec                 C   s   |  ||d |   d S Nz*com.ubuntu.softwareproperties.applychanges)_check_policykit_privilegeZrevertr$   r4   r5   r   r   r   Revertd   s      zSoftwarePropertiesDBus.Revertc                 C   s   |    d S r%   )Zreload_sourceslistr8   r   r   r   Reloadl   s    zSoftwarePropertiesDBus.Reloadsc                 C   s    |  ||d | t| d S r6   )r7   Zenable_child_sourcer   r$   templater4   r5   r   r   r   EnableChildSources   s      z(SoftwarePropertiesDBus.EnableChildSourcec                 C   s    |  ||d | t| d S r6   )r7   Zdisable_child_sourcer   r<   r   r   r   DisableChildSource{   s      z)SoftwarePropertiesDBus.DisableChildSourcec                 C   s    |  ||d | t| d S r6   )r7   Zenable_componentr   r$   Z	componentr4   r5   r   r   r   EnableComponent   s      z&SoftwarePropertiesDBus.EnableComponentc                 C   s    |  ||d | t| d S r6   )r7   Zdisable_componentr   r@   r   r   r   DisableComponent   s      z'SoftwarePropertiesDBus.DisableComponentc                 C   s   |  ||d |   d S r6   )r7   Zenable_source_code_sourcesr8   r   r   r   EnableSourceCodeSources   s      z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc                 C   s"   |  ||d |   |   d S r6   )r7   Zdisable_source_code_sourcesr'   r8   r   r   r   DisableSourceCodeSources   s      z/SoftwarePropertiesDBus.DisableSourceCodeSourcesc                 C   s    |  ||d | t| d S r6   )r7   Ztoggle_source_user   r$   sourcer4   r5   r   r   r   ToggleSourceUse   s      z&SoftwarePropertiesDBus.ToggleSourceUseZssbc                 C   s"   |  ||d | t|t|S r6   )r7   Zreplace_source_entryr   )r$   oldnewr4   r5   r   r   r   ReplaceSourceEntry   s       z)SoftwarePropertiesDBus.ReplaceSourceEntryc                 C   s    |  ||d | t| d S r6   )r7   Zchange_main_download_serverr   )r$   Zserverr4   r5   r   r   r   ChangeMainDownloadServer   s      z/SoftwarePropertiesDBus.ChangeMainDownloadServerc                 C   s   |  ||d |   d S r6   )r7   _add_cdrom_sourcer8   r   r   r   AddCdromSource   s      z%SoftwarePropertiesDBus.AddCdromSourcec                 C   sJ   t  }dddddddddddd|j g}t|}td	| j|| d
S )z  add a (already inserted) cdrom z	apt-cdromaddz-ozDebug::aptcdrom=1zDebug::identcdrom=1zacquire::cdrom::AutoDetect=1zacquire::cdrom::NoMount=1zDir::Etc::sourcelist=%si  N)tempfileZNamedTemporaryFilename
subprocessPopenr   Ztimeout_add_wait_for_cdrom_scan_finish)r$   tmpcmdpr   r   r   rM      s         
z(SoftwarePropertiesDBus._add_cdrom_sourcec                 C   sl   |  dkrdS |  }|dkr,|   dS d}t|jD ]}|}q:|dkrh| jjdt| |   dS )z= glib timeout helper to wait for the cdrom scanner to finish NTr   Fr.   )	Zpollr3   openrQ   Zsourceslistlistinsertr   Zset_modified_sourceslist)r$   rW   rU   reslinexr   r   r   rT      s    z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc                 C   s   |  ||d | | d S r6   )r7   Zset_popcon_pariticipation)r$   Zparticipatesr4   r5   r   r   r   SetPopconPariticipation   s      z.SoftwarePropertiesDBus.SetPopconPariticipationic                 C   s   |  ||d | | d S r6   )r7   Zset_update_automation_levelr$   stater4   r5   r   r   r   SetUpdateAutomationLevel   s      z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc                 C   s   |  ||d | | d S r6   )r7   Zset_release_upgrades_policyr`   r   r   r   SetReleaseUpgradesPolicy   s      z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc                 C   s    |  ||d | t| d S r6   )r7   Zset_update_intervalint)r$   Zdaysr4   r5   r   r   r   SetUpdateInterval  s      z(SoftwarePropertiesDBus.SetUpdateIntervalc                 C   s(   |  ||d | t| |   d S r6   )r7   Zadd_source_from_liner   r1   )r$   Z
sourceLiner4   r5   r   r   r   AddSourceFromLine  s      z(SoftwarePropertiesDBus.AddSourceFromLinec                 C   s    |  ||d | t| d S r6   )r7   Zremove_sourcer   rE   r   r   r   RemoveSource  s      z#SoftwarePropertiesDBus.RemoveSourcec                 C   s(   |  ||d | |}|r$|   |S r6   )r7   Zadd_keyr1   )r$   pathr4   r5   r[   r   r   r   AddKey   s      
zSoftwarePropertiesDBus.AddKeyc                 C   s(   |  ||d | |}|r$|   |S r6   )r7   Zadd_key_from_datar1   )r$   ZkeyDatar4   r5   r[   r   r   r   AddKeyFromData+  s      
z%SoftwarePropertiesDBus.AddKeyFromDatac                 C   s(   |  ||d | |}|r$|   |S r6   )r7   Z
remove_keyr1   )r$   Zkeyidr4   r5   r[   r   r   r   	RemoveKey6  s      
z SoftwarePropertiesDBus.RemoveKeyc                 C   s&   |  ||d |  }|r"|   |S r6   )r7   Zupdate_keysr1   )r$   r4   r5   r[   r   r   r   
UpdateKeysA  s      z!SoftwarePropertiesDBus.UpdateKeysc           	   
   C   s4  |dkr|dkrdS | j sdS | jdkr@t|dddd| _| j|}| jdkrrtt dddd| _z>| jjdd	tj	|d
dif|ddit
d
ddd\}}}W nL tjk
r } z,|jdkrd| _| ||| W Y S  W 5 d}~X Y nX |s0td||||t|f  |   t|dS )a  Verify that sender has a given PolicyKit privilege.

        sender is the sender's (private) D-BUS name, such as ":1:42"
        (sender_keyword in @dbus.service.methods). conn is
        the dbus.Connection object (connection_keyword in
        @dbus.service.methods). privilege is the PolicyKit privilege string.

        This method returns if the caller is privileged, and otherwise throws a
        PermissionDeniedByPolicy exception.
        Nzorg.freedesktop.DBusz/org/freedesktop/DBus/BusFzorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authorityz$org.freedesktop.PolicyKit1.Authorityzsystem-bus-namerQ      )Zvariant_levelr.   iX  )Ztimeoutz)org.freedesktop.DBus.Error.ServiceUnknownzW_check_polkit_privilege: sender %s on connection %s pid %i is not authorized for %s: %s)r!   r   r   Z	InterfaceZ
get_objectZGetConnectionUnixProcessIDr   Z	SystemBusZCheckAuthorizationStringZUInt32DBusExceptionr   Z_check_polkit_privileger"   r#   strr2   r   )	r$   r4   r5   Z	privilegepidZis_auth_Zdetailser   r   r   r7   M  sP    

 
     
z1SoftwarePropertiesDBus._check_policykit_privilege)NNr   )NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)NN)*r   r   r   r   r'   r,   r   r   signalr   r(   r-   r1   r2   r3   methodr9   r:   r>   r?   rA   rB   rC   rD   rG   rK   rL   rN   rM   rT   r^   rb   rc   re   rf   rg   ri   rj   rk   rl   r7   __classcell__r   r   r*   r   r   1   sT  





                                            	r   )Zgi.repositoryr   Zdbus.servicer   r"   rR   rP   r   Zaptsources.sourceslistr   Zdbus.mainloop.glibr   Z%softwareproperties.SoftwarePropertiesr   ZDBUS_BUS_NAMEr    r   r   ro   r   r   r   r   r   r   r   r   <module>   s   
