3
NZ=                 @   s   d dl Z d dlZd dlZd dlZd dlT d dlmZmZ d dlm	Z	 d dl
T ddlT d dlmZmZmZ d dlT d dlT d dlT ddlmZ d ZdZG dd	 d	eZdS )
    N)*)utilsmagic)cups   )
to_unicodeto_string_utf8from_unicode_to_str)MimeTypesDialogc               @   s   e Zd Ze Ze Ze Zdd Zdd Zdd Z	dd Z
d	d
 Zdd Zdd Zdd Zd7ddZdd Zdd Zdd Zd8ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd9d5d6Zd4S ):	FileTablec             C   sf   t j| | || _| j  g | _t| _d | _d | _t	j
 | _t | _| jj  | jj  | jj| _d S )N)QWidget__init__parentinitUi	file_listFILETABLE_TYPE_PRINTtypselected_filenamefax_add_callbackr   ZgetAllowableMIMETypesallowable_mime_typesZUserSettingsuser_settingsloaddebugworking_dir)selfr    r   !/usr/share/hplip/ui5/filetable.pyr   8   s    


zFileTable.__init__c             C   s  t | | _| jjd t| | _| jjd | jj| jdddd t| | _| jjd | jj| jdddd t| | _| jjd | jj| jdddd t| | _	| j	jd | jj| j	dd	dd t| | _
| j
jd
 | jj| j
dddd tddtjtj}| jj|dddd t| | _| jjd | jj| jdddd | jj| jd | jjttdd | jjj| j | jjttdd | jj| jd | jjj| j | j	j| jd | j	jttdd | j	jj| j | j
j| jd | j
jttdd | j
jj| j | jj| jd | jjttdd | jjj| j | jjtj | jj d j| j! | jd| jd| jdg| _"| jj#d  | jj$j| j% d S )!N
gridlayoutr   r   r      AddFileButtonRemoveFileButtonMoveFileUpButton   MoveFileDownButton   [         ShowTypesButton   zAdd...Zlist_add16x16list_removeZRemovezMove UpZupz	Move DownZdownzShow Valid Types...Z	mimetypeszconst QPoint &NameTypezFolder/PathF)&ZQGridLayoutr   ZsetObjectNameZQTableWidgetr   Z	addWidgetZQPushButtonr   r    r!   r#   ZQSpacerItemZQSizePolicyZ	ExpandingZMinimumZaddItemr(   ZsetText_FileTable__trsetIconQIconload_pixmapclickedZconnectAddFileButton_clickedRemoveFileButton_clickedMoveFileUpButton_clickedMoveFileDownButton_clickedShowTypesButton_clickedZsetContextMenuPolicyQtZCustomContextMenuZcustomContextMenuRequested$FileTable_customContextMenuRequestedheadersZsetSortingEnabledZitemSelectionChangedFileTable_itemSelectionChanged)r   Z
spacerItemr   r   r   r   K   sT    






zFileTable.initUic             C   s   t jj|r|| _d S )N)ospathexistsr   )r   dr   r   r   setWorkingDir{   s    zFileTable.setWorkingDirc             C   s8   | j r2tjj| j d d | _| j| j_| jj  | jS )Nr   )r   r<   r=   pathnamer   r   Zsave)r   r   r   r   getWorkingDir   s
    

zFileTable.getWorkingDirc             C   sL   || _ | j tkrH| jd| jd| jdg| _tj rH| jj| jd d S )Nr,   r-   ZPagesZFile)r   FILETABLE_TYPE_FAXr.   r:   logis_debugappend)r   tr   r   r   setType   s
    
zFileTable.setTypec             C   s
   || _ d S )N)r   )r   callbackr   r   r   setFaxCallback   s    zFileTable.setFaxCallbackc             C   s
   t | jS )N)lenr   )r   r   r   r   
isNotEmpty   s    zFileTable.isNotEmptyc             C   s   | j  | _| j  d S )N)currentFilenamer   setUpDownButtons)r   r   r   r   r;      s    
z(FileTable.FileTable_itemSelectionChangedTc             C   s  | j j  | j jt| j | j jd | jr| jj  tj	t
tj zd }| j jt| j | j j| j tjtjB }xt| jD ]\}}|\}}}}	}
d}| jtkr|	rt|	}qttjj|}nttjj|}|jtjt| |j| | jd k	r| j|kr|}| j j||| |d7 }t|}|j| | j j||| |d7 }| jtkrttjj|}|j| | j j||| |d7 }| jtkr|
dk rt| j d}ntt|
}|j| | j j||| |d7 }| jtkrt!j" rt|}|j| | j j||| qW | j j#  |d krF| j j$dd}|j%d | j j&| W d tj'  X | j(j)d | j(j*t+t,dd | j-  n0| j.j  | j(j)d | j-  |r| j/j0j  d S )Nr   r   z	(unknown)Tr+   r*   F)1r   clearZsetRowCountrK   r   ZsetColumnCount	isNotEmptemitZQApplicationZsetOverrideCursorZQCursorr8   Z
WaitCursorr:   ZsetHorizontalHeaderLabelsZItemIsSelectableZItemIsEnabled	enumerater   rC   ZQTableWidgetItemr<   r=   basenameZsetDataUserRoler   ZsetFlagsr   ZsetItemr   dirnamer.   rD   rE   ZresizeColumnsToContentsitemZsetSelectedZsetCurrentItemZrestoreOverrideCursorr    
setEnabledr/   r0   r1   rN   isEmptyr   r2   )r   Zshow_add_file_if_emptyZselectedflagsrowffilename	mime_typemime_type_desctitle	num_pagescolir   r   r   updateUi   sx    
















zFileTable.updateUic             C   s   | j r|| jj }t| j dkr>|t| j d kr>| jjd n| jjd t| j dkrn|dkrn| jjd q| jjd n| jjd | jjd d S )Nr   TFr   )r   r   
currentRowrK   r#   rW   r!   )r   rb   r   r   r   rN      s    
 zFileTable.setUpDownButtonsc             C   s   | j tkr| jd}nbd}y,tjdtjtjd}|j \}}t|}W n& tk
rZ   Y n   t	j
  Y nX | jd}tj| || j| jd}dd |d	 D }|r| j| | j tkr| jd
 d S )NzSelect File(s) to Print Z
getenforce)stdoutstderrzSelect File(s) to SendzAll files (*)c             S   s   g | ]}t |qS r   )r   ).0r[   r   r   r   
<listcomp>&  s    z3FileTable.AddFileButton_clicked.<locals>.<listcomp>r   F)r   r   r.   
subprocessPopenPIPEZcommunicater   OSErrorrD   Z	exceptionZQFileDialogZgetOpenFileNamesr   addFileListrc   )r   sstatperrfilesr   r   r   r3     s$    



zFileTable.AddFileButton_clickedc             C   s   x|D ]}| j | qW d S )N)addFileFromUI)r   r   r[   r   r   r   rn   /  s    
zFileTable.addFileListre   r   c             C   s  t jjt jj|}tjd|  t jj|rt j|t jrt	j
|}|}tjd||f  yt| d }W nP tk
r   | jtkrt| | jd| jd nt| | jd| jd Y n*X | jtkr| j||||| n
| j| nt| | jd| | jd d S )NzTrying to add file: %szFile type of file %s: %sr   a(  <b>You are trying to add a file  that cannot be directly printed with this utility.</b><p>To print this file, use the print command in the application that created it.<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly printed from this utility.zHP Device Managera]  <b>You are trying to add a file  that cannot be directly faxed with this utility.</b><p>To fax this file, use the print command in the application that created it (using the appropriate fax print queue).<p>Note: Click <i>Show Valid Types...</i> to view a list of compatible file types that can be directly added to the fax file list in this utility.z~<b>Unable to add file '%s' to file list (file not found or insufficient permissions).</b><p>Check the file name and try again.)r<   r=   abspath
expanduserrD   r   r>   accessR_OKr   r]   MIME_TYPES_DESCKeyErrorr   r   Z	FailureUIr.   addFiler   )r   r[   r_   r`   r]   r^   r   r   r   rt   4  s&    


zFileTable.addFileFromUIc             C   sD   t jd|||||f  | jj|||||f | j  | jj  d S )NzAdding file %s (%s,%s,%s,%d))rD   r   r   rF   rc   fileListChangedrQ   )r   r[   r]   r^   r_   r`   r   r   r   r{   N  s    zFileTable.addFilec             C   s0   | j j| j j d}|d kr d S t|jtjS )Nr   )r   rV   rd   Z	value_strdatar8   rT   )r   rb   r   r   r   rM   V  s    zFileTable.currentFilenamec             C   s   | j  }|d krd S | j|S )N)rM   
removeFile)r   r\   r   r   r   r4   ^  s    z"FileTable.RemoveFileButton_clickedc       	      C   s`   | j d d  }d}xH|D ]@\}}}}}|t|krP| j |= | jj  | jd P |d7 }qW d S )Nr   Fr   )r   r   r|   rQ   rc   )	r   r\   tempindexr[   r]   r^   r_   r`   r   r   r   r~   f  s    

zFileTable.removeFilec       	      C   s\   | j d d  }d}xD|D ]<\}}}}}||krL| j |= | jj  | jd P |d7 }qW d S )Nr   Fr   )r   r|   rQ   rc   )	r   r]   r   r   r\   mr^   r_   r`   r   r   r   removeFileByMIMETypes  s    

zFileTable.removeFileByMIMETypec             C   s*   x$| j D ]\}}}}}||krdS qW dS )NTF)r   )r   r]   r\   r   r^   r_   r`   r   r   r   isMIMETypeInList  s    zFileTable.isMIMETypeInListc             C   s:   i }x| j D ]}tj|d||< qW t|| }|j  d S )NUnknownn/a)r   r   )r   ry   getr
   Zexec_)r   xaZdlgr   r   r   r7     s
    
z!FileTable.ShowTypesButton_clickedc             C   s2   | j  }|d krd S tj| j|| j | j  d S )N)rM   r   Zlist_move_upr   _FileTable__compareFilenamesrc   )r   r\   r   r   r   r5     s
    z"FileTable.MoveFileUpButton_clickedc             C   s2   | j  }|d krd S tj| j|| j | j  d S )N)rM   r   Zlist_move_downr   r   rc   )r   r\   r   r   r   r6     s
    z$FileTable.MoveFileDownButton_clickedc             C   s   |d |kS )Nr   r   )r   r   br   r   r   Z__compareFilenames  s    zFileTable.__compareFilenamesc             C   s   t | d S )N)print)r   rq   r   r   r   r9     s    z.FileTable.FileTable_customContextMenuRequestedNc             C   s   t jd||S )Nr   )ZqApp	translate)r   ro   cr   r   r   Z__tr  s    zFileTable.__tr)T)re   r   )N) __name__
__module____qualname__Z
pyqtSignalr|   rX   rP   r   r   r@   rB   rH   rJ   rL   r;   rc   rN   r3   rn   rt   r{   rM   r4   r~   r   r   r7   r5   r6   r   r9   r.   r   r   r   r   r   2   s8   0

^
			r   )sysos.pathr<   rj   Zbase.gbaser   r   Zprntr   Z
base.codesZui_utilsZbase.sixextr   r   r	   ZPyQt5.QtCoreZPyQt5.QtGuiZPyQt5.QtWidgetsZmimetypesdialogr
   r   rC   r   r   r   r   r   r   <module>   s    