o
    à;ïj�   ã                   @   s   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 dlm	Z	 d dl
mZ d dlZd dlZd dlmZ d dlmZmZmZmZ d dlmZ d d	lmZmZmZmZ d d
lmZmZ d dl m!Z! e  "e#¡Z$G dd„ deƒZ%G dd„ deƒZ&G dd„ deƒZ'G dd„ dej(ƒZ)dd„ Z*dS )é    N)Ú	b64encode)Úglob)Úmkstemp)Úurlparse)Úurl2pathname)Úreactor)ÚClientFactoryÚFactoryÚProtocolÚconnectionDone)Údecode_bytesÚ	is_magnetÚis_urlÚwindows_check)ÚConfigManagerÚget_config_dir)Úclientc                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚIPCProtocolServerc                 C   ó   d S ©N© ©Úselfr   r   úd/home/hd29/karthikselvaraju/venvs/deluge/lib/python3.10/site-packages/deluge/ui/gtk3/ipcinterface.pyÚ__init__    ó   zIPCProtocolServer.__init__c                 C   s<   t dƒ}tj|dd�}|r|d rt d¡ ¡  t|ƒ d S )Núgtk3ui.confT)Údecode_utf8Úfocus_main_window_on_addÚ
MainWindow)r   ÚrencodeÚloadsÚ	componentÚgetÚpresentÚprocess_args)r   ÚdataÚconfigr   r   r   ÚdataReceived#   s
   zIPCProtocolServer.dataReceivedN)Ú__name__Ú
__module__Ú__qualname__r   r)   r   r   r   r   r      s    r   c                   @   s(   e Zd Zdd„ Zdd„ Zefdd„ZdS )ÚIPCProtocolClientc                 C   r   r   r   r   r   r   r   r   ,   r   zIPCProtocolClient.__init__c                 C   s$   | j  t | jj¡¡ | j  ¡  d S r   )Ú	transportÚwriter!   ÚdumpsÚfactoryÚargsÚloseConnectionr   r   r   r   ÚconnectionMade/   s   z IPCProtocolClient.connectionMadec                 C   s   t  ¡  d| j_d S )NT)r   Ústopr1   )r   Úreasonr   r   r   ÚconnectionLost3   s   z IPCProtocolClient.connectionLostN)r*   r+   r,   r   r4   r   r7   r   r   r   r   r-   +   s    r-   c                   @   s    e Zd ZeZdd„ Zdd„ ZdS )ÚIPCClientFactoryc                 C   s
   d| _ d S )NF)r5   r   r   r   r   r   ;   s   
zIPCClientFactory.__init__c                 C   s   t  d¡ t ¡  d S )Nz&Connection to running instance failed.)ÚlogÚwarningr   r5   )r   Ú	connectorr6   r   r   r   ÚclientConnectionFailed>   s   
z'IPCClientFactory.clientConnectionFailedN)r*   r+   r,   r-   Úprotocolr   r<   r   r   r   r   r8   8   s    r8   c                   @   r   )ÚIPCInterfacec              
      s6  t j | d¡ d | _tdƒ}tj |¡st |¡ tj 	|d¡‰t
ƒ r³dd l}dd l}dd l}| d dd¡| _| ¡ |jkrztƒ | _t| j_dd l}| dd¡}t || j¡| _tˆd	ƒ�}| t|ƒ¡ W d   ƒ n1 sow   Y  t|ƒ d S tˆƒ�}t| ¡ ƒ}W d   ƒ n1 s�w   Y  t ƒ | _|| j_!t"| j_t #d
|| j¡ t $¡  t% &d¡ d S t'tj 	|d¡ƒ}	|	D ]}
t (|
¡ q¾ˆd ‰ t) *dˆ ¡ tj +ˆ ¡�r‡ ‡fdd„}zt ,tt -ˆ ¡ƒd¡ W n t.y÷   |ƒ  Y nw |	�rt) /d¡ |ƒ  ztƒ | _t| j_tj0ˆ| jdd�| _W n} t1j2j3j4�y” } zmt) 5d¡ t6ƒ | _|| j_!tj7ˆ| jdd� t $¡  | jj8�rVt) 5d¡ ddl9m:} |ƒ  t% &d¡ n,|	�ret) 3d|¡ t% &d¡ n$t) /d|¡ t;d|d� t <t%j=d t%j=¡ W Y d }~d S W Y d }~d S W Y d }~d S d }~ww t|ƒ d S )Nr>   Úipcz
deluge-gtkr   FÚdelugei N  iÿÿ  Úwz	127.0.0.1z	restart.*z.lockzChecking if lockfile exists: %sc               
      sX   t  d¡ zt ˆ ¡ t ˆ¡ W d S  ty+ }  zt  d| ¡ W Y d } ~ d S d } ~ ww )NzDelete stale lockfile.zFailed to delete lockfile: %s)r9   ÚdebugÚosÚremoveÚOSErrorÚerror)Úex©ÚlockfileÚsocketr   r   Údelete_lockfiles   s   

€ÿz.IPCInterface.__init__.<locals>.delete_lockfilezFFound running PID but it is not a Deluge process, removing lockfile...T)ÚwantPIDzCDeluge is already running! Sending arguments to running instance...)ÚcheckPIDz,Success sending arguments to running Deluge.)Únotify_startup_completezDeluge restart failed: %sé   zRestarting Deluge... (%s)zrestart.)ÚprefixÚdir)>r#   Ú	Componentr   Úlistenerr   rC   ÚpathÚexistsÚmakedirsÚjoinr   Úwin32apiÚ
win32eventÚwinerrorÚCreateMutexÚmutexÚGetLastErrorÚERROR_ALREADY_EXISTSr	   r1   r   r=   ÚrandomÚ	randranger   Ú	listenTCPÚopenr/   Ústrr&   ÚintÚreadliner   r2   r-   Ú
connectTCPÚrunÚsysÚexitr   rD   r9   rB   ÚlexistsÚkillÚreadlinkrE   r:   Ú
listenUNIXÚtwistedÚinternetrF   ÚCannotListenErrorÚinfor8   ÚconnectUNIXr5   Úgi.repository.GdkrN   r   ÚexecvÚargv)r   r2   Úipc_dirrX   rY   rZ   r_   ÚportÚ_fileÚrestart_tempfileÚfrK   rG   rN   r   rH   r   r   D   s–   
ÿ
ÿ
ÿÿÿ

"÷€ïzIPCInterface.__init__c                 C   s.   t ƒ rdd l}| | j¡ | jr| j ¡ S d S )Nr   )r   rX   ÚCloseHandler\   rS   ÚstopListening)r   rX   r   r   r   Úshutdown£   s   
ÿzIPCInterface.shutdownN)r*   r+   r,   r   r}   r   r   r   r   r>   C   s    _r>   c              	   C   sà  t | ƒ} t d| ¡ t ¡ st d¡ t d¡ | ¡ dS tdƒ}| D ]Ê}| 	¡ s*q#t d|¡ t
|ƒrYt d|¡ |d rQt d	¡ |¡ t d	¡ |d
 ¡ q#tj |d¡ q#t|ƒrƒt d|¡ |d r{t d	¡ |g¡ t d	¡ |d
 ¡ q#tj |i ¡ q#t d|¡ t|ƒjdkr—tt|ƒjƒ}tj t|ƒ¡}tj |¡s¬t d|¡ q#|d rÄt d	¡ |g¡ t d	¡ |d
 ¡ q#t|dƒ�}t| ¡ ƒ}W d  ƒ n1 sÚw   Y  tj tj  |¡d |d¡ q#dS )z0Process arguments sent to already running Delugez&Processing args from other process: %sz(Not connected to host.. Adding to queue.ÚQueuedTorrentsNr   zarg: %sz2Attempting to add url (%s) from external source...Úinteractive_addÚAddTorrentDialogÚfocus_add_dialogz5Attempting to add magnet (%s) from external source...z3Attempting to add file (%s) from external source...ÚfilezNo such file: %sÚrbéÿÿÿÿ)!Úlistr9   rB   r   Ú	connectedr#   r$   Úadd_to_queuer   Ústripr   Úadd_from_urlÚshowÚcoreÚadd_torrent_urlr   Úadd_from_magnetsÚadd_torrent_magnetr   Úschemer   rT   rC   Úabspathr   rU   rF   Úadd_from_filesrb   r   ÚreadÚadd_torrent_fileÚsplit)r2   r(   ÚargrT   rx   Úfiledumpr   r   r   r&   ¬   sL   
ÿÛr&   )+ÚloggingrC   rh   Úbase64r   r   Útempfiler   Úurllib.parser   Úurllib.requestr   r!   Útwisted.internet.errorrn   Útwisted.internetr   Útwisted.internet.protocolr   r	   r
   r   Údeluge.componentr#   Údeluge.commonr   r   r   r   Údeluge.configmanagerr   r   Údeluge.ui.clientr   Ú	getLoggerr*   r9   r   r-   r8   rR   r>   r&   r   r   r   r   Ú<module>   s,   
i