o
    \c!                     @   s   d dl m Z d dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZ d	Zd Zd
Zd	ZdZdZejdsVedG dd dZG dd deeeZ dS )    )gettextN)DictCallableAnyOptional)GLibGio)	Mechanism)AppletPlugin)PowerManagerPowerStateHandler)StatusIconVisibilityHandler            /dev/rfkillzHardware kill switch not foundc                   @   s&   e Zd ZdedededefddZdS )Switchidxswitch_typesofthardc                 C   s   || _ || _|| _|| _d S N)r   typer   r   )selfr   r   r   r    r   C/usr/lib/python3/dist-packages/blueman/plugins/applet/KillSwitch.py__init__   s   
zSwitch.__init__N)__name__
__module____qualname__intr   r   r   r   r   r      s    r   c                   @   s  e Zd ZU dZedZddgZdZdddZd	e	d
diZ
i Zeeef ed< dZdZd
Zd'ddZd'ddZdejdededdfddZdejdeddfddZdejdejde	fddZdejfddZ d ed!e	d"e!e	gdf ddfd#d$Z"de	fd%d&Z#dS )(
KillSwitchWalmiszSwitches Bluetooth killswitch status to match Bluetooth power state. Allows turning Bluetooth back on from an icon that shows its status; provided it isn't unplugged by the system, or physically.r   
StatusIconzsystem-shutdown-symboliczorg.blueman.plugins.killswitchN)schemapathcheckedF)r   default	_switchesTreturnc                 C   sh   d | _ ttjjdtjj| j| j| _	t
jdd}|d u r"tdt
|t
jt
jB t
jB | j| _d S )Nnet.connmanr   rzCould not access RF kill switch)_connman_proxyr   bus_watch_nameBusTypeSYSTEMBusNameWatcherFlagsNONE_on_connman_appeared_on_connman_vanished_connman_watch_idr   	IOChannelnew_fileImportErrorio_add_watchIO_INIO_ERRIO_HUPio_event_iom)r   channelr   r   r   on_load8   s   $zKillSwitch.on_loadc                 C   s,   t | j d | _| jrt| j d S d S r   )r   bus_unwatch_namer5   r-   r>   r   source_remover   r   r   r   	on_unloadC   s
   zKillSwitch.on_unload
connectionnameownerc              	   C   s6   t | d tjtjjtjjd dddd | _	d S )Nz	 appearedr+   z!/net/connman/technology/bluetoothznet.connman.Technology)
logginginfor   	DBusProxynew_for_bus_syncr/   r0   DBusProxyFlagsDO_NOT_AUTO_STARTr-   )r   rE   rF   rG   r   r   r   r3   I   s   
zKillSwitch._on_connman_appearedc                 C   s   t | d d | _d S )Nz	 vanished)rH   rI   r-   )r   rE   rF   r   r   r   r4   T   s   
zKillSwitch._on_connman_vanishedr?   	conditionc                 C   s  |t j@ s
|t j@ rdS | }t|t}t|tkr(t	dt|  dS t
d|\}}}}}	|tkr9dS |tkrPt||||	| j|< td|  n:|tkra| j|= td|  n)|tkr| j| j|ksu| j| j|	krt||||	| j|< td|  ndS d| _d| _| j D ]}
|  j|
jdkO  _|  j|
jd	ko|
jd	kM  _qtd
| j  | jjjj| j d | jjj  dS )NFzBad rfkill event size: TIBBBBzkillswitch registered zkillswitch removed zkillswitch changed r   r   zState: )delay_hiding)r   r;   r<   unix_get_fdosreadRFKILL_EVENT_SIZE_V1lenrH   warningstructunpackRFKILL_TYPE_BLUETOOTHRFKILL_OP_ADDr   r)   rI   RFKILL_OP_DELRFKILL_OP_CHANGEr   r   _enabled_hardblockedvaluesparentPluginsr$   query_visibilityr   update_power_state)r   r?   rN   fddatar   r   opr   r   sr   r   r   r=   X   s:   ( zKillSwitch.io_eventc                 C   s$   | j rtjjS | jrtjjS tjjS r   )r^   r   State
OFF_FORCEDr]   ONOFFrC   r   r   r   on_power_state_query   s
   zKillSwitch.on_power_state_query_statecbc                    s   t | dtdd f fdd}dtdd f fdd}| jr7t d|  | jjdd	tj|||d
 d S t d|  t	 j
d|||d
 d S )Nrm   r*   c                         d d S )NTr   rm   ro   r   r   reply      z9KillSwitch.on_power_state_change_requested.<locals>.replyc                     rp   )NFr   rq   rr   r   r   error   rt   z9KillSwitch.on_power_state_change_requested.<locals>.errorzUsing connman to set state: z(sv)Powered)result_handlererror_handlerzUsing mechanism to set state: z(b))rH   rI   r   r-   debugSetPropertyr   Variantnew_booleanr	   SetRfkillState)r   rm   rn   ro   rs   ru   r   rr   r   on_power_state_change_requested   s   

z*KillSwitch.on_power_state_change_requestedc                 C   s   | j  o| j S r   )r^   r]   rC   r   r   r   %on_query_force_status_icon_visibility   s   z0KillSwitch.on_query_force_status_icon_visibility)r*   N)$r   r   r    
__author__rm   __description____depends____icon____gsettings__bool__options__r)   r   r!   r   __annotations__r>   r]   r^   r@   rD   r   DBusConnectionstrr3   r4   r   r6   IOConditionr=   r   rh   rl   r   r~   r   r   r   r   r   r"   #   s,   
 


)$r"   )!r   rm   rR   typingr   r   r   r   gi.repositoryr   r   rW   rH   blueman.main.DBusProxiesr	   blueman.plugins.AppletPluginr
   #blueman.plugins.applet.PowerManagerr   r   !blueman.plugins.applet.StatusIconr   rY   rZ   r[   r\   RFKILL_OP_CHANGE_ALLrT   r&   existsr8   r   r"   r   r   r   r   <module>   s(    