o
    AVb*                     @   sj   d dl mZ d dlmZ ddlmZ G dd dejZG dd deZG d	d
 d
eZ	G dd de
ZdS )    )Gtk)log   )vmmAsyncJobc                   @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )_VMMenuTc                 C   s*   t j|  || _|| _|| _|   d S N)r   Menu__init___parent_current_vm_cb
_show_open_init_state)selfsrccurrent_vm_cb	show_open r   -/usr/share/virt-manager/virtManager/vmmenu.pyr	      s
   z_VMMenu.__init__c                    s>   t j|}||_ r fdd}|d| | |S )Nc                    s     }|r j|S d S r   )r   r
   )	_menuitem_vmcbr   r   r   _cb    s   z _VMMenu._add_action.<locals>._cbactivate)r   MenuItemnew_with_mnemonicvmm_widget_nameconnectadd)r   label
widgetnamer   itemr   r   r   r   _add_action   s   
z_VMMenu._add_actionc                 C      t  r   NotImplementedErrorr   r   r   r   r   )      z_VMMenu._init_statec                 C   r#   r   r$   )r   vmr   r   r   update_widget_states+   r'   z_VMMenu.update_widget_statesN)T)__name__
__module____qualname__r	   r"   r   r)   r   r   r   r   r      s
    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )VMShutdownMenuz<
    Shutdown submenu for reboot, forceoff, reset, etc.
    c                 C   s   |  tddtj |  tddtj |  tddtj |  tddtj | t	  |  td	d
tj
 |  d |   d S )N_Rebootreboot
_Shut DownshutdownzF_orce Resetresetz
_Force OffdestroySa_vesavezvmm-shutdown-menu)r"   _
VMActionUIr/   r1   r2   r3   r   r   SeparatorMenuItemr5   get_accessibleset_nameshow_allr&   r   r   r   r   3   s   zVMShutdownMenu._init_statec                 C   s   t |o| t |o| t |o| t |o| t |o"| d}|  D ]}t|dd }||v r=|||  q*d S )N)r/   r1   r2   r3   r5   r   )boolis_stoppableis_destroyableget_childrengetattrset_sensitive)r   r(   statemapchildnamer   r   r   r)   >   s   z#VMShutdownMenu.update_widget_statesN)r*   r+   r,   __doc__r   r)   r   r   r   r   r-   /   s    r-   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	VMActionMenuz9
    VM submenu for run, pause, shutdown, clone, etc
    c                 C   s   |  tddtj |  tddtj |  tddtj |  tddd }|t| j| j	 | 
t  |  td	d
tj |  tddtj |  tddtj | jrj| 
t  |  tddtj |  d |   d S )N_Runrun_PausesuspendR_esumeresumer0   r1   zClone...clonez
Migrate...migrate_Deletedelete_Openshowzvm-action-menu)r"   r6   r7   rH   rJ   rL   set_submenur-   r
   r   r   r   r8   rM   rN   rP   r   rR   r9   r:   r;   )r   sr   r   r   r   Q   s   zVMActionMenu._init_statec                 C   s   t |o| t |o| t |o| t |o| t |o"| t |o)| d}t |o4|  t |o;| d}|  D ])}t|dd }| rV| | ||v ra|	||  ||v rl|
||  qCd S )N)rH   r1   rJ   rL   rN   rM   )rJ   rL   r   )r<   
is_runabler=   	is_pausedis_cloneabler?   r@   get_submenur)   rA   set_visible)r   r(   rB   vismaprC   rD   r   r   r   r)   d   s(   	z!VMActionMenu.update_widget_statesc                 C   s0   |   D ]}t|dd dkr| | qd S )Nr   rH   )r?   r@   	get_child	set_label)r   textrC   r   r   r   change_run_text{   s
   zVMActionMenu.change_run_textN)r*   r+   r,   rE   r   r)   r^   r   r   r   r   rF   M   s
    rF   c                   @   s   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd ZdS )r7   zv
    Singleton object for handling VM actions, asking for confirmation,
    showing errors/progress dialogs, etc.
    c              
   C   sp   t d |s	d S z|  W n# ty2 } zt d | tdt|  W Y d }~d S d }~ww d| _d S )NzCancelling save jobzError cancelling save jobzError cancelling save job: %sT)	r   debug	abort_job	Exception	exceptionshow_warningr6   strjob_canceled)asyncjobr(   er   r   r   save_cancel   s   


zVMActionUI.save_cancelc              
      s    j j jj jjtd  dsd S d } r tj	f}fdd} fdd}t
|g |g tdtd j|d	}|  d S )
Nz#Are you sure you want to save '%s'?text1c                    s    j |  d d S )N)meter)r5   	get_meter)rf   )r(   r   r   r      s   zVMActionUI.save.<locals>.cbc                    s,   | d urt d|  }  jj| |d d S d S )NzError saving domain: %s)details)r6   errshow_err)errorrm   )r   r   r   	finish_cb   s   z"VMActionUI.save.<locals>.finish_cbzSaving Virtual Machinez&Saving virtual machine memory to disk )	cancel_cb)rn   chkbox_helperconfigget_confirm_poweroffset_confirm_poweroffr6   get_namesupports_domain_job_infor7   rh   r   topwinrH   )r   r(   
_cancel_cbr   rq   progWinr   r   r(   r   r5      s"   
zVMActionUI.savec                 C   Z   | j j| jj| jjtd|  tddsd S td|  t	
|jg | td d S )Nz-Are you sure you want to force poweroff '%s'?z[This will immediately poweroff the VM without shutting down the OS and may cause data loss.rj   text2zDestroying vm '%s'Error shutting down domain)rn   rs   rt   get_confirm_forcepoweroffset_confirm_forcepoweroffr6   rw   r   r_   r   simple_async_noshowr3   r|   r   r   r   r3         zVMActionUI.destroyc                 C   T   | j j| jj| jjtd|  dsd S td|  t	
|jg | td d S )Nz$Are you sure you want to pause '%s'?ri   zPausing vm '%s'zError pausing domain)rn   rs   rt   get_confirm_pauseset_confirm_pauser6   rw   r   r_   r   r   rJ   r|   r   r   r   rJ         zVMActionUI.suspendc                 C   s*   t d|  t|jg | td d S )NzUnpausing vm '%s'zError unpausing domain)r   r_   rw   r   r   rL   r6   r|   r   r   r   rL      s   zVMActionUI.resumec              	      sr   t d   r* fdd}td}td}tjjg  ||d|d d S td}tjg  | d S )	NzStarting vm '%s'c              
      s    j jtd|  |tdtjjtjjdd}|sd S z  t	
  W d S  tyF } z j tdt|  W Y d }~d S d }~ww )NzError restoring domain: %szjThe domain could not be restored. Would you like
to remove the saved state and perform a regular
start up?T)rm   r   dialog_typebuttonsmodalzError removing domain state: %s)rn   ro   r6   r   MessageTypeWARNINGButtonsTypeYES_NOremove_saved_imager7   rH   ra   rd   )rp   rm   resrg   r|   r   r   errorcb   s*   
zVMActionUI.run.<locals>.errorcbzRestoring Virtual Machinez*Restoring virtual machine memory from disk )r   zError starting domain)	r   r_   rw   has_managed_saver6   r   simple_asyncstartupr   )r   r(   r   titler]   
errorintror   r|   r   rH      s   
zVMActionUI.runc                 C   r   )Nz'Are you sure you want to poweroff '%s'?ri   zShutting down vm '%s'r   )rn   rs   rt   ru   rv   r6   rw   r   r_   r   r   r1   r|   r   r   r   r1      r   zVMActionUI.shutdownc                 C   r   )Nz%Are you sure you want to reboot '%s'?ri   zRebooting vm '%s'zError rebooting domain)rn   rs   rt   ru   rv   r6   rw   r   r_   r   r   r/   r|   r   r   r   r/     r   zVMActionUI.rebootc                 C   r}   )Nz*Are you sure you want to force reset '%s'?zXThis will immediately reset the VM without shutting down the OS and may cause data loss.r~   zResetting vm '%s'zError resetting domain)rn   rs   rt   r   r   r6   rw   r   r_   r   r   r2   r|   r   r   r   r2     r   zVMActionUI.resetc                 C      ddl m} || | d S )Nr   )vmmDeleteDialog)rP   r   show_instance)r   r(   r   r   r   r   rP        zVMActionUI.deletec                 C   r   )Nr   )vmmMigrateDialog)rN   r   r   )r   r(   r   r   r   r   rN   #  r   zVMActionUI.migratec                 C   r   )Nr   )
vmmCloneVM)rM   r   r   )r   r(   r   r   r   r   rM   (  r   zVMActionUI.clonec                 C   s    ddl m} || |  d S )Nr   )vmmVMWindow)vmwindowr   get_instancerR   )r   r(   r   r   r   r   rR   -  s   zVMActionUI.showN)r*   r+   r,   rE   staticmethodrh   r5   r3   rJ   rL   rH   r1   r/   r2   rP   rN   rM   rR   r   r   r   r   r7      s8    





&





r7   N)gi.repositoryr   virtinstr   rf   r   r   r   r-   rF   objectr7   r   r   r   r   <module>   s   4