
    Je!T                         d Z ddl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
mZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lm Z m!Z!m"Z"  G d
 de      Z# G d de      Z$ G d de      Z%y)zE
File associations widget for use in global and project preferences.
    N)getopenfilename)QRegExpQSizeQtSignalSlot)QCursorQRegExpValidator)QApplicationQDialogQDialogButtonBoxQHBoxLayoutQLabel	QLineEditQListWidgetQListWidgetItemQPushButtonQVBoxLayoutQWidget)_)is_text_file)get_application_iconget_installed_applicationsparse_linux_desktop_entryc                   <     e Zd ZdZd fd	Zd Zd Zd Zd Z xZ	S )InputTextDialog(Input text dialog with regex validation.c                 J   t         t        |   |       d| _        d| _        t               | _        t               | _        t        t        j                  t        j                  z        | _        | j                  j                  t        j                        | _        | j                  j                  t        j                        | _        | j!                  |       | j#                  d       | j                  j%                  |       t'               }|j)                  | j                         |j)                  | j                         |j+                  d       |j)                  | j                         | j-                  |       | j                  j.                  j1                  | j2                         | j                  j.                  j1                  | j4                         | j                  j6                  j1                  | j8                         | j9                          y)r   parentNi     )superr   __init___reg_regexr   labelr   lineeditr   OkCancel
button_boxbutton	button_okbutton_cancelsetWindowTitlesetMinimumWidthsetTextr   	addWidget
addSpacing	setLayoutclickedconnectacceptrejecttextChangedvalidate)selfr    titler&   layout	__class__s        R/usr/lib/python3/dist-packages/spyder/plugins/explorer/widgets/fileassociations.pyr#   zInputTextDialog.__init__#   s   ot-V-<	 X
!*+;+>+>-=-D-D,E F//0@0C0CD!__334D4K4KL 	E"S!

5! $'")v 	&&t{{3""**4;;7!!))$--8    c                    | j                         j                         }t        |      }| j                  r:| j                  j	                  |      }|r|j                  d      }|xr ||k(  }nd}| j                  j                  |       y)zValidate content.r   FN)textstripboolr$   matchgroupr,   
setEnabled)r:   rA   is_validrestext_matcheds        r>   r9   zInputTextDialog.validateE   sn    yy{  ":99))//$'C"yy|#<(< !!(+r?   c                     || _         t        j                  |t        j                        | _        t        t        |            }| j                  j                  |       y)z/Set the regular expression to validate content.N)	r%   recompile
IGNORECASEr$   r
   r   r'   setValidator)r:   regex	validators      r>   set_regex_validationz$InputTextDialog.set_regex_validationR   s?    JJubmm4	$WU^4	""9-r?   c                 6    | j                   j                         S )z Return the text of the lineedit.)r'   rA   r:   s    r>   rA   zInputTextDialog.textY   s    }}!!##r?   c                 Z    | j                   j                  |       | j                          y)zSet the text of the lineedit.N)r'   r0   r9   )r:   rA   s     r>   set_textzInputTextDialog.set_text]   s    d#r?   )N rV   )
__name__
__module____qualname____doc__r#   r9   rQ   rA   rU   __classcell__r=   s   @r>   r   r       s     2 D,.$r?   r   c                   f     e Zd ZdZd
 fd	Zd
dZd Zd
dZd Zd Z	e
d        Ze
d	        Z xZS )ApplicationsDialog;Dialog for selection of installed system/user applications.c                     t         t           |       t                _        t                _        t                _        t                _	        t        t        d             _        t        t        j                  t        j                  z         _         j                   j#                  t        j                         _         j                   j#                  t        j                         _         j)                  t        d              j                  j+                  t        d              j                  j-                  t/        dd             t1               }|j3                   j                         |j3                   j                         |j3                   j                         t5               }|j3                   j                         |j3                   j
                         |j7                  |       |j9                  d       |j3                   j                           j;                  |        j                  j<                  j?                   j@                          j                  jB                  j?                   fd        j$                  jB                  j?                   jD                          j&                  jB                  j?                   jF                          j                  jH                  j?                   jJ                          jK                           jM                          y)	r_   r   z	Browse...ApplicationszType to filter by name      c                 $    j                         S N)browse)xr:   s    r>   <lambda>z-ApplicationsDialog.__init__.<locals>.<lambda>   s    T[[] r?   N)'r"   r^   r#   r   r&   label_browser   edit_filterr   listr   r   button_browser   r(   r)   r*   r+   r,   r-   r.   setPlaceholderTextsetIconSizer   r   r1   r   	addLayoutr2   r3   r8   r5   filterr4   r6   r7   currentItemChanged_refreshsetup)r:   r    r<   layout_browser=   s   `   r>   r#   zApplicationsDialog.__init__f   sC    $00? X
"H$;M	(;8*+;+>+>-=-D-D,E F//0@0C0CD!__334D4K4KL 	An-.++A.F,GH		eBm, $))*## 2 23 1 12'")v 	$$,,T[[9""**+BC&&t{{3""**4;;7		$$,,T]];

r?   c                 L   t        j                  t        t        j                               | j
                  j                          |	 t               }n|}t        |d       D ]Q  }||   }t        |      }t        ||      }|j                  |       ||_        | j
                  j                  |       S | j
                  j                  | j
                  j!                  d      dz          t        j"                          | j%                          y# t        $ r g }Y w xY w)zLoad installed applications.Nc                 "    | j                         S re   )lower)rg   s    r>   rh   z*ApplicationsDialog.setup.<locals>.<lambda>   s    aggi r?   )keyr   r!   )r   setOverrideCursorr	   r   
WaitCursorrk   clearr   PermissionErrorsortedr   r   
setToolTipfpathaddItemr/   sizeHintForColumnrestoreOverrideCursorrr   )r:   applicationsappsappr   iconitems          r>   rs   zApplicationsDialog.setup   s    &&wr}}'=>		13  D$$78 	$CIE'.D"4-DOOE"DJIId#	$ 			!!$))"="=a"@2"EF**,! # s   

D D#"D#c                 p    | j                   j                  | j                  j                         dk7         y)(Refresh the status of buttons on widget.N)r,   rF   rk   
currentRowrS   s    r>   rr   zApplicationsDialog._refresh   s&    !!$))"6"6"8B">?r?   c                 P   d}d}t         j                  dk(  r|)d}t        d      }t        d      }t        | |||      \  }}|r|j	                  d      rt
        j                  j                  |      rt
        j                  j                  |      j                  d      d   }t        | j                  j                               D ]C  }| j                  j                  |      }||j                         k(  s2||j                  k(  sB n d}nt
        j                   dk(  r|)d	}t        d
      }t        d      }t        | |||      \  }}|r|j	                  d      xr t#        |      }	|j	                  d      xr t#        |       }
|	s|
rmt
        j                  j                  |      j%                         j'                  d      d   }t        | j                  j                               D ]B  }| j                  j                  |      }||j                         k(  s2||j                  k(  sB n d}n|)d}t        d      }t        d      }t        | |||      \  }}|r|j	                  d      rt#        |      rzt)        |      }|d   }t        | j                  j                               D ]B  }| j                  j                  |      }||j                         k(  s2||j                  k(  sB n d}|rt|r| j                  j+                  |       nV|rTt-        |      }t/        ||      }||_        | j                  j1                  |       | j                  j+                  |       | j                  j3                          | j5                          y)z;Prompt user to select an application not found on the list.Ndarwinz/Applications/zApplications (*.app)zSelect applicationz.appr   ntzC:\z Applications (*.exe *.bat *.com)z.bat)z.exez.com./zApplications (*.desktop)z.desktopname)sysplatformr   r   endswithospathisdirbasenamesplitrangerk   countr   rA   r   r   r   
capitalizersplitr   setCurrentItemr   r   r   setFocusrr   )r:   r   r   r   basedirfiltersr;   __rowcheck_1check_2
entry_datar   s                r>   rf   zApplicationsDialog.browse   s	   <<8#}*23./+D%'J	r/BGGMM%4Hgg&&u-33F;A> !23  C99>>#.Ddiik)etzz.A 
  DWW_} >?./+D%'J	r..0H\%5H >>*:; 7#/#66 g''**51<<>EEcJ1MC$TYY__%67 $#yy~~c2$))+-%4::2E!$
  $}67./+D%'J	r5,u:M6u=
 ( !23  C99>>#.Ddiik)etzz.A 
  D		((.+E2&tS1"
		!!$'		((.		r?   c                 t   | j                   j                         j                         j                         }t	        | j
                  j                               D ]L  }| j
                  j                  |      }|j                  ||j                         j                         v       N | j                          y)z.Filter the list of applications based on text.N)
rj   rA   rw   rB   r   rk   r   r   	setHiddenrr   )r:   rA   r   r   s       r>   rp   zApplicationsDialog.filter   s    $$&,,.446*+ 	<C99>>#&DNN4tyy{'8'8'::;	< 	r?   c                 R    | j                   j                  t        d      |z          y)z-Set the extension on the label of the dialog.z)Choose the application for files of type N)r&   r0   r   )r:   	extensions     r>   set_extensionz ApplicationsDialog.set_extension   s$    

1HI&' 	(r?   c                 ^    | j                   j                         }|r|j                  }|S d}|S )z3Return the selected application path to executable.rV   )rk   currentItemr   )r:   r   r   s      r>   application_pathz#ApplicationsDialog.application_path  s2     yy$$&!tzz (*r?   c                 f    | j                   j                         }|r|j                         }|S d}|S )z%Return the selected application name.rV   )rk   r   rA   )r:   r   rA   s      r>   application_namez#ApplicationsDialog.application_name
  s4     yy$$&"tyy{ )+r?   re   )rW   rX   rY   rZ   r#   rs   rr   rf   rp   r   propertyr   r   r[   r\   s   @r>   r^   r^   c   sP    E)V8@CJ(
    r?   r^   c                        e Zd ZdZdZ ee      Zd fd	Zd Z	d Z
d Zd Zd Zdd	Z e       dd
       Zd Zd Zd Zd Zd ZddZddZd Zed        Zed        Z xZS )FileAssociationsWidget:Widget to add applications association to file extensions.z7(?:(?:\*{1,1}|\w+)\.\w+)(?:,(?:\*{1,1}|\w+)\.\w+){0,20}c                 

   t         t        |   |       i | _        d| _        d| _        t        j                  | j                        | _	        t        t        d            | _        | j                  j                  d       t        t        d            | _        t               | _        t#        t        d            | _        t#        t        d            | _        t#        t        d            | _        t        t        d	            | _        t               | _        t#        t        d            | _        t#        t        d            | _        t#        t        d
            | _        t5               }|j7                  | j                   d       t9               }|j7                  | j$                         |j7                  | j&                         |j7                  | j(                         |j;                          t5               }|j7                  | j,                  d       t9               }|j7                  | j.                         |j7                  | j0                         |j7                  | j2                         |j;                          |j=                  |d       |j=                  |d       t9               }|j7                  | j                         |j7                  | j                         |j=                  |       |j7                  | j*                         |j=                  |       | j?                  |       | j$                  j@                  jC                  | jD                         | j&                  j@                  jC                  | jF                         | j(                  j@                  jC                  | jH                         | j.                  j@                  jC                  | jJ                         | j0                  j@                  jC                  | jL                         | j2                  j@                  jC                  | jN                         | j                   jP                  jC                  | jR                         | j                   jT                  jC                  | jH                         | j,                  jP                  jC                  | jV                         | jY                          | j[                          y)r   r   NzHere you can associate different external applications to open specific file extensions (e.g. .txt files with Notepad++ or .csv files with Excel).TzFile types:AddRemoveEditzAssociated applications:zSet default      ).r"   r   r#   _data_dlg_applications
_dlg_inputrK   rL   _EXTENSIONS_LIST_REGEXr%   r   r   r&   setWordWraplabel_extensionsr   list_extensionsr   
button_addbutton_removebutton_editlabel_applicationslist_applicationsbutton_add_applicationbutton_remove_applicationbutton_defaultr   r1   r   
addStretchro   r3   r4   r5   add_associationremove_associationedit_associationadd_applicationremove_applicationset_default_applicationcurrentRowChangedupdate_extensionsitemDoubleClickedupdate_applicationsrr   _create_association_dialog)r:   r    layout_extensionslayout_buttons_extensionslayout_applicationslayout_buttons_applicationsr<   r=   s          r>   r#   zFileAssociationsWidget.__init__  s   $d4F4C 
!%jj!<!<=  @ A


 	

t$ &q'7 8*}%ah/(85&qy1"(+E)F"G!,&1!E(&;#)4Qx[)A&)!M*:; (M##D$8$8!<$/M!!++DOO<!++D,>,>?!++D,<,<=!,,.)m%%d&<&<a@&1m##--d.I.IJ#--d.L.LM#--d.A.AB#..0##$=qA%%&A1E$../*+001,-v 	''(<(<=""**4+B+BC  (()>)>?##++33D4H4HI&&..66##	%##++D,H,HI..66t7M7MN..66t7L7LM0088$$	&'')r?   c                    | j                  d       | j                  | j                  | j                  | j                  | j
                  fD ]  }|j                  d        | j                  j                         }|rD| j                  | j                  | j                  | j                  fD ]  }|j                  d        | j                          | j                  d       y)r   FTN)
setUpdatesEnabledr   r   r   r   r   setDisabledr   r   r   )r:   widgetr   s      r>   rr   zFileAssociationsWidget._refreshf  s    u%))D,G,G''55t7J7JL 	%F t$	%
 ##//1--t/J/J994;K;KM *""5)* 	  "t$r?   c                    t        | j                  j                               D ]N  }| j                  j                  |      }|j	                         j                         |j                         k(  sN nB t        |      }| j                  j                  |       | j                  j                  |       | j                          y)zAdd association helper.N)
r   r   r   r   rA   rB   r   r   r   rr   )r:   valuer   r   s       r>   _add_associationz'FileAssociationsWidget._add_associationv  s     --3356 	6C'',,S1Dyy{  "ekkm3	6
 #5)D  ((.  //5r?   c                    t        d      }t        | j                  j                               D ]U  }| j                  j	                  |      }|j                         j                  |d      j                         }|sO||k(  sU n t        |      }t        j                  j                  |      s$t        j                  j                  |      s||z  }t        ||      }| j                  j                  |       | j                  j                  |       t        j                  j                  |      s;t        j                  j                  |      s|j!                  t        d             yyy)zAdd application helper.z (Application not found!)rV   zApplication not found!N)r   r   r   r   r   rA   replacerB   r   r   r   isfiler   r   r   r   r~   )r:   app_namer   app_not_found_textr   r   	item_textr   s           r>   _add_applicationz'FileAssociationsWidget._add_application  s   :;//5578 	8C))..s3D 		++,>CIIKI	X-	8 (.DGGNN5)RWW]]5-A.."42D""**40""11$7u%u)=OOA678 *>%r?   c                 &   | j                   j                          t        | j                  j	                               D ]  \  }}| j                  |        | j                   j                  d       | j                          | j                          y)zUpdate extensions list.r   N)	r   r{   r}   r   itemsr   setCurrentRowr   r   )r:   r   r   s      r>   _update_extensionsz)FileAssociationsWidget._update_extensions  su    ""$"4::#3#3#56 	-LIq!!),	- 	**1-   "r?   c                     t        | t        d      t        d      dz   dz   dz         | _        | j                  j                  | j                         y)z7Create input extension dialog and save it to for reuse.zFile associationz^Enter new file extension. You can add several values separated by commas.<br>Examples include:z<ul><li><code>*.txt</code></li>z"<li><code>*.json,*.csv</code></li>z+<li><code>*.json,README.md</code></li></ul>)r;   r&   N)r   r   r   rQ   r   rS   s    r>   r   z1FileAssociationsWidget._create_association_dialog  s_    )&' > ?34 77 @	@	

 	,,T-H-HIr?   c                 :    |i n|| _         | j                          y)z
        Load file associations data.

        Format {'*.ext': [['Application Name', '/path/to/app/executable']]}

        `/path/to/app/executable` is an executable app on mac and windows and
        a .desktop xdg file on linux.
        N)r   r   )r:   datas     r>   load_valuesz"FileAssociationsWidget.load_values  s      <RT
!r?   c                    |t        |t              rWd\  }}| j                  j                  d       | j                  j	                         rE| j                  j                         }d}n(| j                  j                  |      }|t        |      }}|r@|| j                  vr1g | j                  |<   | j                  |       | j                          yyy)zAdd extension file association.N)rV   FrV   T)
isinstancerC   r   rU   exec_rA   r%   rD   r   r   check_data_changed)r:   r   rA   
ok_pressedrD   s        r>   r   z&FileAssociationsWidget.add_association  s     =Jud3(D*OO$$R($$&++-!
KK%%e,E$d5k*D4::%#%

4 %%d+'') & r?   c                     | j                   rc| j                  rV| j                   j                  | j                         | j                          | j	                          | j                          yyy)z"Remove extension file association.N)r   current_extensionpopr   rr   r   rS   s    r>   r   z)FileAssociationsWidget.remove_association  sS    ::%%

t556'')'')	 & r?   c                 v   | j                   }| j                  j                  |       | j                  j                         r| j                  j	                         }||k7  r| j
                  j                  | j                         }|| j
                  |<   | j                          | j                          t        | j                  j                               D ]M  }| j                  j                  |      }|j	                         |k(  s2| j                  j                  |        n | j                          yyy)z*Edit text of current selected association.N)r   r   rU   r   rA   r   r   r   rr   r   r   r   r   r   r   )r:   old_textnew_textvaluesr   r   s         r>   r   z'FileAssociationsWidget.edit_association  s    ))  *??  "++-H8#(>(>?'-

8$'') !5!5!;!;!=> C//44S9Dyy{h.,,;;DA	
 '') $ #r?   c                    | j                   r| j                  t        |       | _        | j                  j                  | j                          | j                  j	                         ry| j                  j
                  }| j                  j                  }| j                  | j                      j                  ||f       | j                  ||       | j                          yyy)z)Remove application to selected extension.N)r   r   r^   r   r   r   r   r   appendr   r   )r:   r   r   s      r>   r   z&FileAssociationsWidget.add_application  s    !!%%-);D)A&""001G1GH%%++-11BB..??

4112998U:KL%%h6'') . "r?   c                    | j                   j                         }| j                  j                  | j                        }|rH|dk7  rB|j                  |       | j                          | j                          | j                          yyy)z+Remove application from selected extension.r   N)	r   r   r   getr   r   r   r   r   )r:   current_rowr   s      r>   r   z)FileAssociationsWidget.remove_application   so    ,,779 6 67kR'JJ{#""$$$&##%	 (6r?   c                 .   | j                   j                         }|dk7  rv| j                  | j                     }|j	                  |      }|j                  d|       || j                  | j                  <   | j                          | j                          yy)zW
        Set the selected item on the application list as default application.
        r   r   N)r   r   r   r   r   insertr   r   )r:   r   r   r   s       r>   r   z.FileAssociationsWidget.set_default_application
  s     ,,779"ZZ 6 67FJJ{+EMM!U#17DJJt--.""$##% r?   c                 H   | j                   j                          | j                  j                         D ]@  \  }}|j	                         | j
                  k(  s$|D ]  \  }}| j                  ||         n | j                   j                  d       | j                          y)z4Update extensiosn list after additions or deletions.r   N)	r   r{   r   r   rB   r   r   r   rr   )r:   r   r   r   r   r   s         r>   r   z(FileAssociationsWidget.update_extensions  s    $$&!%!1!1!3 	Iv D$:$::)/ ;%Xu))(E:;		
 	,,Q/r?   c                 t    | j                   j                         }| j                  j                  |dk7         y)z5Update application list after additions or deletions.r   N)r   r   r   rF   )r:   r   r   s      r>   r   z*FileAssociationsWidget.update_applications"  s/    ,,779&&{a'78r?   c                 N    | j                   j                  | j                         y)z4Check if data has changed and emit signal as needed.N)sig_data_changedemitr   rS   s    r>   r   z)FileAssociationsWidget.check_data_changed'  s    ""4::.r?   c                 \    | j                   j                         }|r|j                         S y)z+Return the current selected extension text.N)r   r   rA   )r:   r   s     r>   r   z(FileAssociationsWidget.current_extension+  s,     ##//199; r?   c                 6    | j                   j                         S )z*Return the current file associations data.)r   copyrS   s    r>   r   zFileAssociationsWidget.data2  s     zz  r?   re   )rW   rX   rY   rZ   r   r   dictr  r#   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[   r\   s   @r>   r   r     s    DAd|I*V% 9.	#J
" 
V* *&**&*&&	9
/   ! !r?   r   )&rZ   r   rK   r   qtpy.compatr   qtpy.QtCorer   r   r   r   r   
qtpy.QtGuir	   r
   qtpy.QtWidgetsr   r   r   r   r   r   r   r   r   r   r   spyder.config.baser   spyder.utils.encodingr   spyder.utils.programsr   r   r   r   r^   r    r?   r>   <module>r     su   
 
 	 
 ( 8 8 02 2 2 2
 ! .> >
@g @Fl l^c!W c!r?   