o
    `                     @   s   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 dl	Z	d dl
mZ ddlmZ dd	lmZ d
d ZG dd deZdd Zdd Zdd ZdS )    N)GLib)Gio)Gtk)log   )vmmGObjectUI   )uiutilc               	   C   sv   t  } d}z%ttjjd }tj|dd dddd }|d| }t	
d| d}W |S  ty:   t	d	 Y |S w )
NFr   zorg.freedesktop.login1z/org/freedesktop/login1zorg.freedesktop.login1.Managerz(u)zFound login1 session=%sTzFailure talking to logind)osgetpidr   bus_get_syncBusTypeSYSTEM	DBusProxynew_syncGetSessionByPIDr   debug	Exception	exception)pidretbusmanagerout r   6/usr/share/virt-manager/virtManager/lib/connectauth.pydo_we_have_session   s"   r   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )_vmmConnectAuthc                 C   sf   t | dd || _| jtd | j| j| j	| j
| j
d | d| _| d| _|   d S )Nzconnectauth.uiconnectauthzAuthentication required)on_connectauth_cancel_clickedon_connectauth_ok_clickedon_entry1_activateon_entry2_activateentry1entry2)r   __init__credstopwin	set_title_builderconnect_signals
_cancel_cb_ok_cb	_entry_cbwidgetr#   r$   _init_ui)selfr&   r   r   r   r%   /   s   z_vmmConnectAuth.__init__c                 C   s   d S Nr   )r1   r   r   r   _cleanup?   s   z_vmmConnectAuth._cleanupc                 C   s   t | jd t | jd t| jD ]K\}}|\}}}}}|tjtjfv }|s.t	d| |d7 }| 
d|d  }	| 
d|d  }
t |	d |	| |
|  |
 |d  qd S )	NFzNo prompt for auth credtype=%sz: zlabel%sr   zentry%sTz entry)r	   set_grid_row_visibler#   r$   	enumerater&   libvirtVIR_CRED_PASSPHRASEVIR_CRED_NOECHOPROMPTRuntimeErrorr/   set_textset_visibilityget_accessibleset_name)r1   idxcredcredtypeprompt
_challenge
_defresult_resultnoecholabelentryr   r   r   r0   B   s$   
z_vmmConnectAuth._init_uic                 C   sd   | j   | j  }| j   |tjjkrdS | j | j	d d< | j
 r0| j
 | j	d d< dS )Nr      r   )r'   showrunhider   ResponseTypeOKr#   get_textr&   r$   get_visible)r1   resr   r   r   rK   W   s   



z_vmmConnectAuth.runc                 C      | j tjj d S r2   )r'   responser   rM   rN   r1   srcr   r   r   r-   d      z_vmmConnectAuth._ok_cbc                 C   rR   r2   )r'   rS   r   rM   CANCELrT   r   r   r   r,   f   rV   z_vmmConnectAuth._cancel_cbc                 C   s6   || j kr| j r| j  dS | jtjj dS )zg
        If entry 1 activated and entry2 visible, jump to entry 2.
        Otherwise, click OK
        N)	r#   r$   
is_visible
grab_focusr'   rS   r   rM   rN   rT   r   r   r   r.   i   s   
z_vmmConnectAuth._entry_cbN)
__name__
__module____qualname__r%   r3   r0   rK   r-   r,   r.   r   r   r   r   r   .   s    r   c                    s8   g   fdd}t || |  std  r d S )zE
    Thread safe wrapper for libvirt openAuth user/pass callback
    c                    sP   z|}t | }| }|  W n ty    td d}Y nw  | d S )NzError from creds dialogrH   )r   rK   cleanupr   r   r   append)r&   cbdata_conn	dialogobjr   retipcr   r   wrapperz   s   
zcreds_dialog.<locals>.wrapperg?r   )r   idle_addtimesleep)r&   r_   rd   r   rb   r   creds_dialogt   s   
rh   c           
      C   sT  | d}| d}d}d}|  rEtd|   td|r)|td7 }d}n@|  dkr>td	|r>|td
7 }d}n+|td7 }n$|  rP|td7 }n|r[|td7 }d}ntd|ri|td7 }d}td| 	  }|ry|d| 7 }|r|d| 7 }| d}|}|d7 }|d| 	  7 }||7 }td}t
dg d}	|	|||S )z)
    Format connection error message
    z 
 Tz connect_error: conn transport=%sznc: .* -- 'U'zMThe remote host requires a version of netcat/nc which supports the -U option.FsshaskpasszXConfigure SSH key access for the remote host, or install an SSH askpass package locally.z@Verify that the 'libvirtd' daemon is running on the remote host.zPVerify that:
 - A Xen host kernel was booted
 - The Xen service has been startedzCould not detect a local session: if you are running virt-manager over ssh -X or VNC, you may not be able to connect to libvirt as a regular user. Try running as root.zlibvirt-sockz-Verify that the 'libvirtd' daemon is running.z Unable to connect to libvirt %s.z

%s


zLibvirt URI is: %s

z*Virtual Machine Manager Connection FailureConnectError)msgdetailstitle)strip	is_remoter   r   get_uri_transportresearchr)   is_xenget_uricollections
namedtuple)
connerrmsgtbwarnconsolehintshow_errmsgro   rp   rq   rn   r   r   r   connect_error   sP   



r   c                 C   s   t tjdtjd tj d d}| jjrd}|r|rd S d}|s'|td7 }|r-d|vr9|r3|d	7 }|td
7 }|rE|d	7 }|td7 }|pHd S )NlibvirtdPATHz	/usr/sbin)pathFri   zThe libvirtd service does not appear to be installed. Install and run the libvirtd service to manage virtualization on this host.qemurm   zCould not detect a default hypervisor. Make sure the appropriate QEMU/KVM virtualization packages are installed to manage virtualization on this host.zJA virtualization connection can be manually added via File->Add Connection)	boolshutilwhichr
   environpathsepCLITestOptionsfake_no_libvirtdr)   )configtryurilibvirtd_installedro   r   r   r   setup_first_uri   s    "r   )ry   r
   ru   r   rf   gi.repositoryr   r   r   r6   virtinstr   	baseclassr   ri   r	   r   r   rh   r   r   r   r   r   r   <module>   s"   FA