o
    Oeb@                     @   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 )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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                       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   r.   D   r-   z#SoftwarePropertiesDBus.write_config )Zdbus_interfaceZ	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   r1   )r2   zConfigModified signalNr3   r*   r   r   r   r/   N   r4   z%SoftwarePropertiesDBus.ConfigModifiedc                 C   r1   )z, emit signal when the apt keys got modified zKeysModified signalNr3   r*   r   r   r   KeysModifiedS   r4   z#SoftwarePropertiesDBus.KeysModifiedc                 C   r1   )z6 emit signal when the policykit authentication failed zAuth signalNr3   r*   r   r   r   
AuthFailedX   r4   z!SoftwarePropertiesDBus.AuthFailedc                 C   r1   )z( emit signal when adding a cdrom failed zCdrom scan failed signalNr3   r*   r   r   r   CdromScanFailed]   r4   z&SoftwarePropertiesDBus.CdromScanFailedsenderconn)Zsender_keywordZconnection_keywordZin_signatureZout_signaturec                 C      |  ||d |   d S Nz*com.ubuntu.softwareproperties.applychanges)_check_policykit_privilegeZrevertr$   r8   r9   r   r   r   Revertd      zSoftwarePropertiesDBus.Revertc                 C   s   |    d S r&   )Zreload_sourceslistr=   r   r   r   Reloadl   s   zSoftwarePropertiesDBus.Reloadsc                 C       |  ||d | t| d S r;   )r<   Zenable_child_sourcer   r$   templater8   r9   r   r   r   EnableChildSources      z(SoftwarePropertiesDBus.EnableChildSourcec                 C   rB   r;   )r<   Zdisable_child_sourcer   rC   r   r   r   DisableChildSource{   rF   z)SoftwarePropertiesDBus.DisableChildSourcec                 C   rB   r;   )r<   Zenable_componentr   r$   Z	componentr8   r9   r   r   r   EnableComponent   rF   z&SoftwarePropertiesDBus.EnableComponentc                 C   rB   r;   )r<   Zdisable_componentr   rH   r   r   r   DisableComponent   rF   z'SoftwarePropertiesDBus.DisableComponentc                 C   r:   r;   )r<   Zenable_source_code_sourcesr=   r   r   r   EnableSourceCodeSources   r?   z.SoftwarePropertiesDBus.EnableSourceCodeSourcesc                 C   s"   |  ||d |   |   d S r;   )r<   Zdisable_source_code_sourcesr(   r=   r   r   r   DisableSourceCodeSources   s
   z/SoftwarePropertiesDBus.DisableSourceCodeSourcesc                 C   rB   r;   )r<   Ztoggle_source_user   r$   sourcer8   r9   r   r   r   ToggleSourceUse   rF   z&SoftwarePropertiesDBus.ToggleSourceUsessbc                 C   s"   |  ||d | t|t|S r;   )r<   Zreplace_source_entryr   )r$   oldnewr8   r9   r   r   r   ReplaceSourceEntry   s   z)SoftwarePropertiesDBus.ReplaceSourceEntryc                 C   rB   r;   )r<   Zchange_main_download_serverr   )r$   Zserverr8   r9   r   r   r   ChangeMainDownloadServer   rF   z/SoftwarePropertiesDBus.ChangeMainDownloadServerc                 C   r:   r;   )r<   _add_cdrom_sourcer=   r   r   r   AddCdromSource   r?   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   rV      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   Fr0   )	Zpollr7   openrZ   Zsourceslistlistinsertr   Zset_modified_sourceslist)r$   r`   r^   reslinexr   r   r   r]      s   z2SoftwarePropertiesDBus._wait_for_cdrom_scan_finishc                 C      |  ||d | | d S r;   )r<   Zset_popcon_pariticipation)r$   Zparticipatesr8   r9   r   r   r   SetPopconPariticipation      z.SoftwarePropertiesDBus.SetPopconPariticipationic                 C   rg   r;   )r<   Zset_update_automation_levelr$   stater8   r9   r   r   r   SetUpdateAutomationLevel   ri   z/SoftwarePropertiesDBus.SetUpdateAutomationLevelc                 C   rg   r;   )r<   Zset_release_upgrades_policyrk   r   r   r   SetReleaseUpgradesPolicy   ri   z/SoftwarePropertiesDBus.SetReleaseUpgradesPolicyc                 C   rB   r;   )r<   Zset_update_intervalint)r$   Zdaysr8   r9   r   r   r   SetUpdateInterval  rF   z(SoftwarePropertiesDBus.SetUpdateIntervalc                 C   s(   |  ||d | t| |   d S r;   )r<   Zadd_source_from_liner   r5   )r$   Z
sourceLiner8   r9   r   r   r   AddSourceFromLine  s
   z(SoftwarePropertiesDBus.AddSourceFromLinec                 C   rB   r;   )r<   Zremove_sourcer   rM   r   r   r   RemoveSource  rF   z#SoftwarePropertiesDBus.RemoveSourcec                 C   (   |  ||d | |}|r|   |S r;   )r<   Zadd_keyr5   )r$   pathr8   r9   rd   r   r   r   AddKey      
zSoftwarePropertiesDBus.AddKeyc                 C   rs   r;   )r<   Zadd_key_from_datar5   )r$   ZkeyDatar8   r9   rd   r   r   r   AddKeyFromData+  rv   z%SoftwarePropertiesDBus.AddKeyFromDatac                 C   rs   r;   )r<   Z
remove_keyr5   )r$   Zkeyidr8   r9   rd   r   r   r   	RemoveKey6  rv   z SoftwarePropertiesDBus.RemoveKeyc                 C   s&   |  ||d |  }|r|   |S r;   )r<   Zupdate_keysr5   )r$   r8   r9   rd   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-namerZ      )Zvariant_levelr0   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#   strr6   r   )	r$   r8   r9   Z	privilegepidZis_auth_Zdetailser   r   r   r<   M  sJ   



z1SoftwarePropertiesDBus._check_policykit_privilege)NNr   )NN)*r   r   r   r   r(   r.   r   r   signalr   r)   r/   r5   r6   r7   methodr>   r@   rE   rG   rI   rJ   rK   rL   rO   rT   rU   rW   rV   r]   rh   rm   rn   rp   rq   rr   ru   rw   rx   ry   r<   __classcell__r   r   r+   r   r   1   s    





	r   )Zgi.repositoryr   Zdbus.servicer   r"   r[   rY   r   Zaptsources.sourceslistr   Zdbus.mainloop.glibr   Z%softwareproperties.SoftwarePropertiesr   ZDBUS_BUS_NAMEr    r   r   r|   r   r   r   r   r   r   r   r   <module>   s    
