o
    U7e@                     @   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)set_as_defaultc                 C   s   t jdk r
| dS | 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dOddZejje	dddd	ddOddZejje	dddd	ddOdd Zejje	dddd	ddOd!d"Zejje	dddd	ddOd#d$Zejje	ddd	d	ddOd%d&Zejje	ddd	d	ddOd'd(Zejje	dddd	ddOd)d*Zejje	ddd+d,ddOd-d.Zejje	ddd	d	ddOd/d0Zejje	ddd	d	ddOd1d2Zd3d4 Zd5d6 Zejje	ddd,d	ddOd7d8Zejje	ddd9d	ddOd:d;Zejje	ddd9d	ddOd<d=Z ejje	ddd9d	ddOd>d?Z!ejje	dddd	ddOd@dAZ"ejje	dddd	ddOdBdCZ#ejje	dddd,ddOdDdEZ$ejje	dddd,ddOdFdGZ%ejje	dddd,ddOdHdIZ&ejje	ddd	d,ddOdJdKZ'dLdM Z(  Z)S )P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serviceBusNameDBUS_INTERFACE_NAMEObject	DBUS_PATHenforce_polkitloggingdebug)selfr   r   r   r   bus_namer   r   r   r   3   s   zSoftwarePropertiesDBus.__init__c                       t t|   |   d S N)superr   save_sourceslistSourcesListModifiedr&   	__class__r   r   r+   A      z'SoftwarePropertiesDBus.save_sourceslistc                    r(   r)   )r*   r   write_configConfigModifiedr-   r.   r   r   r1   D   r0   z#SoftwarePropertiesDBus.write_config )dbus_interface	signaturec                 C      t d dS )0 emit signal when the sources.list got modified zSourcesListModified signalNr$   r%   r-   r   r   r   r,   I      z*SoftwarePropertiesDBus.SourcesListModifiedc                 C   r6   )r7   zConfigModified signalNr8   r-   r   r   r   r2   N   r9   z%SoftwarePropertiesDBus.ConfigModifiedc                 C   r6   )z, emit signal when the apt keys got modified zKeysModified signalNr8   r-   r   r   r   KeysModifiedS   r9   z#SoftwarePropertiesDBus.KeysModifiedc                 C   r6   )z6 emit signal when the policykit authentication failed zAuth signalNr8   r-   r   r   r   
AuthFailedX   r9   z!SoftwarePropertiesDBus.AuthFailedc                 C   r6   )z( emit signal when adding a cdrom failed zCdrom scan failed signalNr8   r-   r   r   r   CdromScanFailed]   r9   z&SoftwarePropertiesDBus.CdromScanFailedsenderconn)sender_keywordconnection_keywordin_signatureout_signaturec                 C      |  ||d |   d S Nz*com.ubuntu.softwareproperties.applychanges)_check_policykit_privilegerevertr&   r=   r>   r   r   r   Revertd      zSoftwarePropertiesDBus.Revertc                 C   s   |    d S r)   )reload_sourceslistrG   r   r   r   Reloadl   s   zSoftwarePropertiesDBus.Reloadsc                 C       |  ||d | t| d S rD   )rE   enable_child_sourcer   r&   templater=   r>   r   r   r   EnableChildSources      z(SoftwarePropertiesDBus.EnableChildSourcec                 C   rM   rD   )rE   disable_child_sourcer   rO   r   r   r   DisableChildSource{   rR   z)SoftwarePropertiesDBus.DisableChildSourcec                 C   rM   rD   )rE   enable_componentr   r&   	componentr=   r>   r   r   r   EnableComponent   rR   z&SoftwarePropertiesDBus.EnableComponentc                 C   rM   rD   )rE   disable_componentr   rV   r   r   r   DisableComponent   rR   z'SoftwarePropertiesDBus.DisableComponentc                 C   rC   rD   )rE   enable_source_code_sourcesrG   r   r   r   EnableSourceCodeSources   rI   z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc                 C   s"   |  ||d |   |   d S rD   )rE   disable_source_code_sourcesr+   rG   r   r   r   DisableSourceCodeSources   s
   z/SoftwarePropertiesDBus.DisableSourceCodeSourcesc                 C   rM   rD   )rE   toggle_source_user   r&   sourcer=   r>   r   r   r   ToggleSourceUse   rR   z&SoftwarePropertiesDBus.ToggleSourceUsessbc                 C   s"   |  ||d | t|t|S rD   )rE   replace_source_entryr   )r&   oldnewr=   r>   r   r   r   ReplaceSourceEntry   s   z)SoftwarePropertiesDBus.ReplaceSourceEntryc                 C   rM   rD   )rE   change_main_download_serverr   )r&   serverr=   r>   r   r   r   ChangeMainDownloadServer   rR   z/SoftwarePropertiesDBus.ChangeMainDownloadServerc                 C   rC   rD   )rE   _add_cdrom_sourcerG   r   r   r   AddCdromSource   rI   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)tempfileNamedTemporaryFilename
subprocessPopenr   timeout_add_wait_for_cdrom_scan_finish)r&   tmpcmdpr   r   r   rl      s   

z(SoftwarePropertiesDBus._add_cdrom_sourcec                 C   sl   |  du rdS |  }|dkr|   dS d}t|jD ]}|}q|dkr4| jjdt| |   dS )z= glib timeout helper to wait for the cdrom scanner to finish NTr   Fr3   )	pollr<   openrq   sourceslistlistinsertr   set_modified_sourceslist)r&   rx   rv   reslinexr   r   r   ru      s   z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc                 C      |  ||d | | d S rD   )rE   set_popcon_pariticipation)r&   participatesr=   r>   r   r   r   SetPopconPariticipation      z.SoftwarePropertiesDBus.SetPopconPariticipationic                 C   r   rD   )rE   set_update_automation_levelr&   stater=   r>   r   r   r   SetUpdateAutomationLevel   r   z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc                 C   r   rD   )rE   set_release_upgrades_policyr   r   r   r   SetReleaseUpgradesPolicy   r   z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc                 C   rM   rD   )rE   set_update_intervalint)r&   daysr=   r>   r   r   r   SetUpdateInterval  rR   z(SoftwarePropertiesDBus.SetUpdateIntervalc                 C   s(   |  ||d | t| |   d S rD   )rE   add_source_from_liner   r:   )r&   
sourceLiner=   r>   r   r   r   AddSourceFromLine  s
   z(SoftwarePropertiesDBus.AddSourceFromLinec                 C   rM   rD   )rE   remove_sourcer   r`   r   r   r   RemoveSource  rR   z#SoftwarePropertiesDBus.RemoveSourcec                 C   (   |  ||d | |}|r|   |S rD   )rE   add_keyr:   )r&   pathr=   r>   r   r   r   r   AddKey      
zSoftwarePropertiesDBus.AddKeyc                 C   r   rD   )rE   add_key_from_datar:   )r&   keyDatar=   r>   r   r   r   r   AddKeyFromData+  r   z%SoftwarePropertiesDBus.AddKeyFromDatac                 C   r   rD   )rE   
remove_keyr:   )r&   keyidr=   r>   r   r   r   r   	RemoveKey6  r   z SoftwarePropertiesDBus.RemoveKeyc                 C   s&   |  ||d |  }|r|   |S rD   )rE   update_keysr:   )r&   r=   r>   r   r   r   r   
UpdateKeysA  s   z!SoftwarePropertiesDBus.UpdateKeysc           	   
   C   s,  |du r
|du r
dS | j sdS | jdu r t|dddd| _| j|}| jdu r9t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 n# tjy{ } z|jdkrvd| _| |||W  Y d}~S  d}~ww |st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      )variant_levelr3   iX  )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   	Interface
get_objectGetConnectionUnixProcessIDr   	SystemBusCheckAuthorizationStringUInt32DBusExceptionr   _check_polkit_privileger$   r%   strr;   r   )	r&   r=   r>   	privilegepidis_auth_detailser   r   r   rE   M  sJ   



z1SoftwarePropertiesDBus._check_policykit_privilege)NNr   )NN)*r   r   r   r   r+   r1   r   r   signalr    r,   r2   r:   r;   r<   methodrH   rK   rQ   rT   rX   rZ   r\   r^   rb   rh   rk   rm   rl   ru   r   r   r   r   r   r   r   r   r   r   rE   __classcell__r   r   r.   r   r   1   s    





	r   )gi.repositoryr   dbus.servicer   r$   rr   ro   r	   aptsources.sourceslistr   dbus.mainloop.glibr   %softwareproperties.SoftwarePropertiesr   DBUS_BUS_NAMEr"   r    r   r   r   r   r!   r   r   r   r   r   <module>   s    
