
    G@d$S                        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%dS )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.N c                 P   t          t          |                               |           d| _        d| _        t                      | _        t                      | _        t          t          j
        t          j        z            | _        | j                            t          j
                  | _        | j                            t          j                  | _        |                     |           |                     d           | j                            |           t'                      }|                    | j                   |                    | j                   |                    d           |                    | j                   |                     |           | j        j                            | j                   | j        j                            | j                   | j        j                            | j                   |                                  dS )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        Plib/python3.11/site-packages/spyder/plugins/explorer/widgets/fileassociations.pyr$   zInputTextDialog.__init__#   s   ot$$--V-<<<	 XX
!*+;+>-=-D,E F F//0@0CDD!_334D4KLL 	E"""S!!!
5!!! $$$'''")))v 	&&t{333"**4;777!))$-888    c                 *   |                                                                  }t          |          }| j        r<| j                            |          }|r|                    d          }|o||k    }nd}| j                            |           dS )zValidate content.r   FN)textstripboolr%   matchgroupr-   
setEnabled)r;   rB   is_validrestext_matcheds        r?   r:   zInputTextDialog.validateE   s    yy{{  ""::9 	!)//$''C !"yy||#<(< !!(+++++r@   c                     || _         t          j        |t          j                  | _        t          t          |                    }| j                            |           dS )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   sM    Jubm44	$WU^^44	""9-----r@   c                 4    | j                                         S )z Return the text of the lineedit.)r(   rB   r;   s    r?   rB   zInputTextDialog.textY   s    }!!###r@   c                 b    | j                             |           |                                  dS )zSet the text of the lineedit.N)r(   r1   r:   )r;   rB   s     r?   set_textzInputTextDialog.set_text]   s)    d###r@   )Nr   r   )
__name__
__module____qualname____doc__r$   r:   rR   rB   rV   __classcell__r>   s   @r?   r   r       s        22           D, , ,. . .$ $ $      r@   r   c                   r     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.Nc                     t          t                                         |           t                       _        t                       _        t                       _        t                       _	        t          t          d                     _        t          t          j        t          j        z             _         j                            t          j                   _         j                            t          j                   _                             t          d                      j                            t          d                      j	                            t/          dd                     t1                      }|                     j                   |                     j                   |                     j	                   t5                      }|                     j                   |                     j                   |                    |           |                    d           |                     j                                        |            j        j                             j                     j        j!                             fd            j        j!                             j"                    j        j!                             j#                    j	        j$                             j%                    %                                  &                                 dS )	r_   r    z	Browse...ApplicationszType to filter by name      c                 ,                                     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   r2   r   	addLayoutr3   r4   r9   r6   filterr5   r7   r8   currentItemChanged_refreshsetup)r;   r!   r=   layout_browser>   s   `   r?   r$   zApplicationsDialog.__init__f   s    $''000??? XX
"HH$;;MM	(;88*+;+>-=-D,E F F//0@0CDD!_334D4KLL 	An--...++A.F,G,GHHH	eBmm,,, $$$)***#### 2333 1222'''")))v 	$,,T[999"**+B+B+B+BCCC&&t{333"**4;777	$,,T];;;

r@   c                 P   t          j        t          t          j                             | j                                         |t                      }n|}t          |d           D ]_}||         }t          |          }t          ||          }|                    |           ||_        | j                            |           `| j                            | j                            d          dz              t          j                     |                                  dS )zLoad installed applications.Nc                 *    |                                  S re   )lower)rg   s    r?   rh   z*ApplicationsDialog.setup.<locals>.<lambda>   s    aggii r@   )keyr   r"   )r   setOverrideCursorr	   r   
WaitCursorrk   clearr   sortedr   r   
setToolTipfpathaddItemr0   sizeHintForColumnrestoreOverrideCursorrr   )r;   applicationsappsappr~   iconitems          r?   rs   zApplicationsDialog.setup   s   &wr}'='=>>>	-//DDD$$7$7888 	$ 	$CIE'..D"4--DOOE"""DJId#### 		!!$)"="=a"@"@2"EFFF*,,,r@   c                 p    | j                             | j                                        dk               dS )(Refresh the status of buttons on widget.N)r-   rG   rk   
currentRowrT   s    r?   rr   zApplicationsDialog._refresh   s1    !!$)"6"6"8"8B">?????r@   c                    d}d}t           j        dk    r|5d}t          d          }t          d          }t          | |||          \  }}|r|                    d          rt
          j                            |          rt
          j                            |          	                    d          d         }t          | j                                                  D ]A}| j                            |          }||                                k    r||j        k    r nBd}n+t
          j        dk    r<|5d	}t          d
          }t          d          }t          | |||          \  }}|r|                    d          ot#          |          }	|                    d          ot#          |           }
|	s|
rt
          j                            |                                                              d          d         }t          | j                                                  D ]A}| j                            |          }||                                k    r||j        k    r nBd}n|5d}t          d          }t          d          }t          | |||          \  }}|r|                    d          rt#          |          rt)          |          }|d         }t          | j                                                  D ]A}| j                            |          }||                                k    r||j        k    r nBd}|ry|r| j                            |           n\|rZt-          |          }t/          ||          }||_        | j                            |           | j                            |           | j                                         |                                  dS )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   rB   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3.//+D%'JJ	r  //  BGMM%4H4H  g&&u--33F;;A> !2!233    C9>>#..Ddiikk))etz.A.ADW__} >??.//+D%'JJ	r $..00H\%5H5H >>*:;; 7#/#6#66  $g $'**511<<>>EEcJJ1MC$TY__%6%677 $ $#y~~c22$))++--%4:2E2E!E#}677.//+D%'JJ	r  55  ,u:M:M  6u==
 ( !2!233    C9>>#..Ddiikk))etz.A.AD 	/ /	((.... /+E22&tS11"
	!!$'''	((...	r@   c                    | j                                                                                                         }t	          | j                                                  D ]W}| j                            |          }|                    ||                                                                v           X| 	                                 dS )z.Filter the list of applications based on text.N)
rj   rB   rw   rC   r   rk   r   r   	setHiddenrr   )r;   rB   r   r   s       r?   rp   zApplicationsDialog.filter   s    $$&&,,..4466**++ 	< 	<C9>>#&&DNN4tyy{{'8'8':'::;;;;r@   c                 Z    | j                             t          d          |z              dS )z-Set the extension on the label of the dialog.z)Choose the application for files of type N)r'   r1   r   )r;   	extensions     r?   set_extensionz ApplicationsDialog.set_extension   s9    
1HII&' 	( 	( 	( 	( 	(r@   c                 N    | j                                         }|r|j        nd}|S )z3Return the selected application path to executable.r   )rk   currentItemr~   )r;   r   r   s      r?   application_pathz#ApplicationsDialog.application_path   s,     y$$&&!)tzzrr@   c                 h    | j                                         }|r|                                nd}|S )z%Return the selected application name.r   )rk   r   rB   )r;   r   rB   s      r?   application_namez#ApplicationsDialog.application_name  s2     y$$&&"*tyy{{{r@   re   )rW   rX   rY   rZ   r$   rs   rr   rf   rp   r   propertyr   r   r[   r\   s   @r?   r^   r^   c   s        EE) ) ) ) ) )V   ,@ @ @C C C CJ  ( ( (
   X   X    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}Nc                 \
   t          t          |                               |           i | _        d| _        d| _        t          j        | j                  | _	        t          t          d                    | _        | 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                      }|                    | j        d           t9                      }|                    | j                   |                    | j                   |                    | j                   |                                 t5                      }|                    | j        d           t9                      }|                    | j                   |                    | j                   |                    | j                   |                                 |                    |d           |                    |d           t9                      }|                    | j                   |                    | j                   |                    |           |                    | j                   |                    |           |                     |           | j        j         !                    | j"                   | j        j         !                    | j#                   | j        j         !                    | j$                   | j        j         !                    | j%                   | j        j         !                    | j&                   | j        j         !                    | j'                   | j        j(        !                    | j)                   | j        j*        !                    | j$                   | j        j(        !                    | j+                   | ,                                 | -                                 dS )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_inputrL   rM   _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   r2   r   
addStretchro   r4   r5   r6   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__  sX   $d++44F4CCC 
!%j!<==  @ A A
 


 	
t$$$ &q'7'7 8 8*}}%ahh//(855&qyy11"(+E)F)F"G"G!,&1!E((&;&;#)4Qx[[)A)A&)!M*:*:;; (MM##D$8!<<<$/MM!!++DO<<<!++D,>???!++D,<===!,,...)mm%%d&<a@@@&1mm##--d.IJJJ#--d.LMMM#--d.ABBB#..000##$=qAAA%%&A1EEE$$$.///*+++0111,---v 	''(<==="**4+BCCC (()>???#+33D4HIII&.66#	% 	% 	%#++D,HIII.66t7MNNN.66t7LMMM088$	& 	& 	&'')))))r@   c                    |                      d           | j        | j        | j        | j        | j        fD ]}|                    d           | j                                        }|r2| j        | j        | j        | j        fD ]}|                    d           | 	                                 |                      d           dS )r   FTN)
setUpdatesEnabledr   r   r   r   r   setDisabledr   r   r   )r;   widgetr   s      r?   rr   zFileAssociationsWidget._refresh`  s    u%%%)D,G'5t7JL 	% 	%F t$$$$#//11 	*-t/J94;KM * *""5))))  """t$$$$$r@   c                    t          | j                                                  D ]Z}| j                            |          }|                                                                |                                k    r nD[t          |          }| j                            |           | j                            |           | 	                                 dS )zAdd association helper.N)
r   r   r   r   rB   rC   r   r   r   rr   )r;   valuer   r   s       r?   _add_associationz'FileAssociationsWidget._add_associationp  s     -335566 	6 	6C',,S11Dyy{{  ""ekkmm33 4 #5))D ((... //555r@   c                 &   t          d          }t          | j                                                  D ]`}| j                            |          }|                                                    |d                                          }|r||k    r nat          |          }t          j
                            |          s$t          j
                            |          s||z  }t          ||          }| j                            |           | j                            |           t          j
                            |          sCt          j
                            |          s&|                    t          d                     dS dS dS )zAdd application helper.z (Application not found!)r   zApplication not found!N)r   r   r   r   r   rB   replacerC   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~  sq   :;;/557788 	8 	8C)..s33D 		++,>CCIIKKI 	X--'..DGNN5)) /RW]]5-A-A /.."422D"**4000"11$777u%% 	9u)=)= 	9OOA67788888	9 	9 	9 	9r@   c                 >   | j                                          t          | j                                                  D ]\  }}|                     |           | j                             d           |                                  |                                  dS )zUpdate extensions list.r   N)	r   r{   r|   r   itemsr   setCurrentRowr   r   )r;   r   r   s      r?   _update_extensionsz)FileAssociationsWidget._update_extensions  s    ""$$$"4:#3#3#5#566 	- 	-LIq!!),,,, 	**1---     """""r@   c                     t          | t          d          t          d          dz   dz   dz             | _        | j                            | j                   dS )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   rR   r   rT   s    r?   r   z1FileAssociationsWidget._create_association_dialog  sx    )&'' > ? ?34 77 @	@	

 

 

 	,,T-HIIIIIr@   c                 D    |i n|| _         |                                  dS )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+      <RRT
!!!!!r@   c                    |Td\  }}| j                             d           | j                                         r| j                                         }d}n+| j                            |          }|t          |          }}|r>|| j        vr7g | j        |<   |                     |           | 	                                 dS dS dS )zAdd extension file association.N)r   Fr   T)
r   rV   exec_rB   r&   rE   rD   r   r   check_data_changed)r;   r   rB   
ok_pressedrE   s        r?   r   z&FileAssociationsWidget.add_association  s     =(D*O$$R((($$&& "++--!
K%%e,,E$d5kk*D 	*4:%%#%
4 %%d+++'')))))		* 	*%%r@   c                     | j         rd| j        r_| j                             | j                   |                                  |                                  |                                  dS dS dS )z"Remove extension file association.N)r   current_extensionpopr   rr   r   rT   s    r?   r   z)FileAssociationsWidget.remove_association  sy    : 	*% *
t5666'')))'')))))	* 	** *r@   c                 x   | j         }| j                            |           | j                                        r| j                                        }||k    r| j                            | j                   }|| j        |<   |                                  |                                  t          | j
                                                  D ]P}| j
                            |          }|                                |k    r| j
                            |            nQ|                                  dS dS dS )z*Edit text of current selected association.N)r   r   rV   r   rB   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!;!;!=!=>>  C/44S99Dyy{{h..,;;DAAA / '')))))	* 	*##r@   c                    | j         r| j        t          |           | _        | j                            | j                    | j                                        rm| j        j        }| j        j        }| j        | j                                      ||f           | 	                    ||           | 
                                 dS dS dS )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)A&"001GHHH%++-- *1B.?
412998U:KLLL%%h666'')))))	* 	** *r@   c                 0   | j                                         }| j                            | j                  }|rY|dk    rU|                    |           |                                  |                                  |                                  dS dS dS )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  s    ,7799 677 	&kR''JJ{###""$$$$$&&&##%%%%%		& 	&''r@   c                 0   | j                                         }|dk    rv| j        | j                 }|                    |          }|                    d|           || j        | j        <   |                                  |                                  dS dS )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9"Z 67FJJ{++EMM!U###17DJt-.""$$$##%%%%% r@   c                 L   | j                                          | j                                        D ]B\  }}|                                | j        k    r |D ]\  }}|                     ||            nC| j                             d           |                                  dS )z4Update extensiosn list after additions or deletions.r   N)	r   r{   r   r   rC   r   r   r   rr   )r;   r   r   r   r   r~   s         r?   r   z(FileAssociationsWidget.update_extensions  s    $$&&&!%!1!1!3!3 	 	Iv  D$:::)/ ; ;%Xu))(E:::: ; 	,,Q///r@   c                 t    | j                                         }| j                            |dk               dS )z5Update application list after additions or deletions.r   N)r   r   r   rG   )r;   r   r   s      r?   r   z*FileAssociationsWidget.update_applications  s8    ,7799&&{a'788888r@   c                 D    | j                             | j                   dS )z4Check if data has changed and emit signal as needed.N)sig_data_changedemitr   rT   s    r?   r   z)FileAssociationsWidget.check_data_changed!  s!    ""4:.....r@   c                 d    | j                                         }|r|                                S dS )z+Return the current selected extension text.N)r   r   rB   )r;   r   s     r?   r   z(FileAssociationsWidget.current_extension%  s8     #//11 	99;;	 	r@   c                 4    | j                                         S )z*Return the current file associations data.)r   copyrT   s    r?   r   zFileAssociationsWidget.data,  s     z   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DAvd||I* I* I* I* I* I*V% % %   9 9 9.	# 	# 	#J J J
" 
" 
" 
" 
TVV* * * V*&* * ** * *&* * *& & && & &	 	 	 	9 9 9 9
/ / /   X ! ! X! ! ! ! !r@   r   )&rZ   r   rL   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     s    
 
			 				 



 ( ' ' ' ' ' 8 8 8 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 02 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 !           . . . . . .> > > > > > > > > >
@ @ @ @ @g @ @ @Ff f f f f f f fRc! c! c! c! c!W c! c! c! c! c!r@   