
    KcfK                        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  ej        dd           d dlmZ  ej        dd           d dlm	Z	 d dl
Z
d dlT d dlT d dlZ ej        ej        ej        	           d
 Z ej        d            G d de	j                  Z G d d          Z e            Z G d d          Zedk    r ej                     d dlmZ  ed            eed                                          Z ed           e                     d            e! eej"        d                                                     dS dS )    N)GLibGdkz3.0)r   Gtk)r   )*)domain	localedirc                     | S N )xs    ,/usr/share/system-config-printer/authconn.py<lambda>r   #   s    q     z1.9.60c                       e Zd Z ed           ed           ed          dZddej        j        ej        ej	        j
        ej        ej	        j        fddfdZd Zd	 Zd
 Zd Zd ZdS )
AuthDialogz	Username:z	Password:zDomain:)usernamepasswordr   NFc           	         |t          d          }|ddg}t          j                            | ||||           || _        |                     t          j        j                   |                     d           | 	                    d           t          j
                            dd          }|                    d           t          j                    }|                    t          j        t          j        j                   |                    dd           |                    |ddd           t          j                            dd          }	t          j                    | _        |	                    | j        ddd           t-          |          }
t          j                    }|                    |
           |                    d	           |                    d           |                    d           g | _        t;          |
          D ]}||         }t          j        t          | j                            ||                    
          }|                    dd           |                     |d|dd           t          j!                    }|"                    |dk               |                     |d|dd           | j        #                    |           | j        |
dz
           $                    d           |	                    |ddd           |                    |	ddd           | j%                            |ddd           |r`t          j&        '                    t          d                    }|(                    d           |	                    |ddd           || _)        | j%        *                                 d S )NAuthenticationr   r      F   g        r      )labelg      ?   TzRemember password)+_r   Dialog__init__auth_info_requiredset_default_responseResponseTypeOKset_border_widthset_resizableHBoxnewImageset_from_stockSTOCK_DIALOG_AUTHENTICATIONIconSizeDIALOGset_alignment
pack_startVBoxLabelprompt_labellenGrid
insert_rowinsert_columnset_row_spacingset_column_spacingfield_entryrange
AUTH_FIELDgetattachEntryset_visibilityappendset_activates_defaultvboxCheckButtonnew_with_label
set_activeremember_checkboxshow_all)selftitleparentflagsbuttonsr   allow_rememberhboximager?   
num_fieldsgridifieldr   entrycbs                    r   r   zAuthDialog.__init__+   sw    =&''E%",j!9
T5&%AAA"4!!3#3#6777q!!!E"""x||UB''q!!!	c=!l1	3 	3 	3S#&&&ua000x||UB''ILL*E5!<<<,--
xzz
###1a   ###
## 	, 	,A&q)EIQt':':E5'I'I%J%JKKKEC(((KK1a+++ILLE  %:"5666KK1a+++##U++++a(>>EEEueQ///ueQ///	dE5!444 	(//3F1G1GHHBMM5!!!OORq111%'D"	r   c                     | j                             d|z   dz              | j                             d           | j                             dd           | j                             d           d S )N"<span weight="bold" size="larger">z</span>Tr   )r/   
set_markupset_use_markupr+   set_line_wraprE   prompts     r   
set_promptzAuthDialog.set_promptd   s}    $$&J&,'-/8'9 	: 	: 	:(($///''A...''.....r   c                     t          t          | j                            D ](}| j        |                             ||                    )d S r
   )r7   r0   r6   set_text)rE   	auth_inforO   s      r   set_auth_infozAuthDialog.set_auth_infok   sQ    T-..// 	8 	8AQ(()A,7777	8 	8r   c                 $    d | j         D             S )Nc                 6    g | ]}|                                 S r   )get_text).0r   s     r   
<listcomp>z,AuthDialog.get_auth_info.<locals>.<listcomp>p   s     888!

888r   )r6   rE   s    r   get_auth_infozAuthDialog.get_auth_infoo   s    88t'78888r   c                 X    	 | j                                         S # t          $ r Y dS w xY w)NF)rC   
get_activeAttributeErrorrd   s    r   get_remember_passwordz AuthDialog.get_remember_passwordr   s>    	)44777 	 	 	55	s    
))c                 x    | j                             |          }| j        |                                          d S r
   )r   indexr6   
grab_focus)rE   rP   rO   s      r   field_grab_focuszAuthDialog.field_grab_focusx   s8    #))511&&)))))r   )__name__
__module____qualname__N_r8   r   DialogFlagsMODALSTOCK_CANCELr    CANCELSTOCK_OKr!   r   rZ   r^   re   ri   rm   r   r   r   r   r   &   s        B{OOB{OO"Y--) )J #4-+S-=-D<)9)<>%)!&7 7 7 7r/ / /8 8 89 9 9  * * * * *r   r   c                   ,    e Zd Zd ZddZddZddZdS )_AuthInfoCachec                 ,    t                      | _        d S r
   )dictcredsrd   s    r   r   z_AuthInfoCache.__init__   s    VV


r   Nc                 &    |d}|| j         ||f<   d S Niw  )r{   )rE   datahostports       r   cache_auth_infoz_AuthInfoCache.cache_auth_info   s"    <D"&
D;r   c                 L    |d}	 | j         ||f         S # t          $ r Y d S w xY wr}   r{   KeyErrorrE   r   r   s      r   lookup_auth_infoz_AuthInfoCache.lookup_auth_info   sD    <D	:tDk** 	 	 	44	s    
##c                 F    |d}	 | j         ||f= d S # t          $ r Y d S w xY wr}   r   r   s      r   remove_auth_infoz_AuthInfoCache.remove_auth_info   sG    <D	
D;''' 	 	 	44	s   
 
  )NN)rn   ro   rp   r   r   r   r   r   r   r   rx   rx      s_          ' ' ' '        r   rx   c                       e Zd Z	 	 ddZd Zd Zd Zd Zd	 Zdd
Z	d Z
d Zd Zd Zd ZddZd Zd Zd Zd Zd Zd ZdS )
ConnectionNTFc                    |t          j        |           |t          j        |           |t          j        |           d| _        || _        || _        t          j                    | _        t          j	                    | _
        t          j                    | _        t          j                    | _        d| _        g | _        || _        t%          j                    | _        |                                  d S )N T)cups	setServersetPortsetEncryption_use_password_parent_try_as_rootgetUser	_use_user	getServer_servergetPort_portgetEncryption_encryption_prompt_allowed_operation_stack_lock	threadingEvent
_gui_event_connect)rE   rG   try_as_rootlockr   r   
encryptions          r   r   zConnection.__init__   s    ND!!!L$!
+++'~((\^^
-00# "
#/,,r   c                 b    t          | d|           | j                            |           d S )Nz: Operation += )
debugprintr   r=   )rE   	operations     r   _begin_operationzConnection._begin_operation   s8    TTT99=>>>$$i00000r   c                 \    t          d| z             | j                                         d S )Nz%s: Operation ended)r   r   poprd   s    r   _end_operationzConnection._end_operation   s1    )D0111!!$$$$$r   c                     | j         S r
   r   rd   s    r   _get_prompt_allowedzConnection._get_prompt_allowed   s    ##r   c                     || _         d S r
   r   )rE   alloweds     r   _set_prompt_allowedzConnection._set_prompt_allowed   s    &r   c                     || _         d S r
   )r   )rE   whethers     r   	_set_lockzConnection._set_lock   s    


r   c           	      
   t          j        | j                   |o2| j        d         dk    p
| j        dk    ot	          j                    dk    | _        | j        rt          j        }nt           j        } || j        | j	        | j
                  | _        | j        r| j                            | j                   | j        | _        t          d| j        z             t!          | j        j                  }t!          | j        j                  }t'          | j                  D ]]}|d         dk    rt)          | j        |          }t!          |          ||fvr8t+          | ||                     ||                     ^d S )Nr   /	localhost)r   r   r   zConnected as user %sr   )r   setUserr   r   osgetuid_use_pkcupspkr   r   r   _connection
set_parentr   _userr   typegetPrinters
addPrinterdirgetattrsetattr_make_binding)rE   allow_pkcreate_objectmethodtype_lambdamethodtype_realfnamefns          r   r   zConnection._connect   s   dn%%%  .aC/N4<;3N.ILLA- 	 < 	,"-MM OM(=t|157;7GI I I < 	6''555^
*TZ7888 $"2">?? 0 ;<<$*++ 	B 	BEQx3$*E22B99!2O DDDT5$"4"4eR"@"@AAAA	B 	Br   c                 @    t          | j        t          j                  S r
   )
isinstancer   r   r   rd   s    r   _using_polkitzConnection._using_polkit   s    4+V->???r   c                       fdS )Nc                  (     j         g| R i |S r
   )	_authloop)argskwdsr   r   rE   s     r   r   z*Connection._make_binding.<locals>.<lambda>   s%    ^T^UB%N%N%N%N%N%N r   r   )rE   r   r   s   ```r   r   zConnection._make_binding   s    NNNNNNNr   c                    d| _         |                                 s|                    dd            | j        }d}	 	 |                                 dk    rn|| j        k    rt          | j        |          }| j        }t          j        | j                    |j	        |i |}|dk    r%|i k    rt          j
        t          j        d          n<# t          j
        $ r}|j        \  }}	| j        r(|	dk    r"t          j
        dt          d                    | j        sU|t          j        k    s |t          j        k    s|t          j        k    r%|                     |t          j        k               n<| j        s|t          j        k    rt)          d	           t)          |	           | j        rN| j                                         t1          j        d
| j        |	           | j                                         n|                     |	           | j        t:          j        j        k    r)t)          d           d}| xj         d
z  c_         d| _         nZd| _         | j        r)| j!        s"t          j
        dt          d                    t)          |dtE          |	                      Y d }~nTd }~wt          j#        $ r?}|j        \  }
| j        s$|                     |
t          j$        k               n Y d }~nd }~ww xY w|S )Nr   	signatureFTadminGetServerSettingsr   pkcancelzOperation canceledzGot IPP_SERVICE_UNAVAILABLEr   zretrying operation...z: )%_passesr   r   r   _perform_authenticationr   r   r   r   __call__IPPErrorIPP_NOT_AUTHORIZEDr   r   r   _cancelIPP_FORBIDDENIPP_AUTHENTICATION_CANCELED_failedIPP_SERVICE_UNAVAILABLEr   r   r   clearr   timeout_add_ask_retry_server_errorwait_retry_responser   r    r!   _has_failed_cannot_authrepr	HTTPErrorHTTP_FORBIDDEN)rE   r   r   r   r   cretryresultemss              r   r   zConnection._authloop   s*   !!## 	(HH[$''':	9//22a77((( $"2E::B(Adn---$t4t44444||"mT-DbIII=      A< EAOO-A.B,C,CDDD| d.E)E)E)*d.@)@)@)*d.N)N)NLL!t'9"9:::: !t/K*K*K =>>>NNNz 9--000(!T-I1MMM,,////44a888+s/?/BBB"$;<<< $)+/(('+| ID,= I"mQ2F0G0GHHHAAAtQxxx 8999>   v| LL!t':":;;;; <;;;;k:	x s+   C A8C L!GJ77L	5LLc                 $   | j         rt          j                     	 t          d          | j        d         z  }n# t
          $ r t          d          }Y nw xY wt          j        | j        ddt          j	        j
        t          j        j        |          }|                    t          d          |z             |                    t          j        t          j        j        t          d          t          j        j                   |                    t          j        j                   | j         r0|                    d| j                   t          j                     d S |                                | _        |                                 d S )	NzCUPS server error (%s)r   zCUPS server errorT)transient_formodaldestroy_with_parentmessage_typerI   textz3There was an error during the CUPS operation: '%s'.Retryresponse)r   r   threads_enterr   r   
IndexErrorr   MessageDialogr   MessageTypeERRORButtonsTypeNONEformat_secondary_textadd_buttonsrt   r    ru   r!   r   connect_on_retry_server_error_responsethreads_leaverunr   destroy)rE   messagemsgds       r   r   z"Connection._ask_retry_server_error0  s{   : 	!   	)-..1Fq1IICC 	) 	) 	)'((CCC	) T\%)t,/O,A'*';$'	) ) ) 	
 $; "< "<>E"F 	G 	G 	G	s')9)@zz3#3#6	8 	8 	8	 0 3444: 	IIz4#GHHH     #$5588D IILLLLLs   : AAc                 n    || _         |                                 | j                                         d S r
   )r   r	  r   setrE   dialogr   s      r   r  z*Connection._on_retry_server_error_responseK  s4    'r   c                 "    d| _         || _        d S )NT)r   
_forbidden)rE   	forbiddens     r   r   zConnection._failedP  s    #r   c                 j    t          d           | j        s| j        rdS d| _        || _        | j        S )NzGot password callbackr   T)r   r   _auth_called_promptr   rX   s     r   _password_callbackzConnection._password_callbackT  sB    +,,,< 	4, 	2 !!r   c                    | xj         dz  c_         t                              | j        | j                  }|#|d         dk    s| j        r|\  | _        | _        ~t          d| j         z             | j         dk    rTd| _	        d| _
        d| _        d| _        d| _        d| _        t          j        | j                   t          d           dS t          d| j
        z             | j	        st          d	           dS d| _	        | j         d
k    r| j        r| j        dk    r|| j        d         dk    s| j
        rdt          d           d| _        d| _        	 |                     d           n-# t(          $ r  t          j        t          j        d          w xY wdS | j        st          d           d| _        dS | j        st          d           d| _        d| _        dS d| _        | j        rh| j        rM| j                                         t7          j        d| j                   | j                                         n|                                  | j        rM| j                                         t7          j        d| j                   | j                                         n|                                  | j        rt          d           dS t          j         | j                   t          d           	 |                     d           n-# t(          $ r  t          j        t          j        d          w xY wdS )Nr   r   r   r   rootzAuthentication pass: %dFz%Authentication: password callback setzForbidden: %sz$Authentication: Operation successfulr   r   zAuthentication: Try as root)r   z server-error-service-unavailablez%Authentication: prompting not allowedTzAuthentication: giving up	cancelledzAuthentication: Reconnect)!r   global_authinfocacher   r   r   r   r   r   r   r   r  r  r   r   _dialog_shownr   setPasswordCBr  r   r   RuntimeErrorr   r   r   r   r   r   r   r   _show_not_authorized_dialogr   #_perform_authentication_with_dialogr   )rE   r{   s     r   r   z"Connection._perform_authentication]  s   $55DLtz5ZZaF""d&7"7<4!3-<===<1  %D#DO %D DL %D!&D 7888?@@@1Odo5666 	 >???1 !<1! 
f$$aC''4?' 9:::!'$)!MMMEM2222# M M M-)E)KM M MM q# 	?@@@DL1  	3444DL $D1 "  	4z 4%%((( !T%EFFF$$''''00333: 	8O!!$$$a!IJJJO  ####44777< 	$$$2dn%%%/000	EMMEM**** 	E 	E 	E-!=!CE E E	E qs   E2 2*FL *Mc                 t   | j         rt          j                     t          j        | j        ddt          j        j        t          j        j	                  }|
                    t          d                     |                    dt          d          z   dz   t          d          z              | j         rX|                    d| j                   |                                 |                                 t          j                     d S |                                 |                                 d S )NT)r   r   r   r   rI   zNot authorizedrT   z	</span>

zThe password may be incorrect.r   )r   r   r   r   r   r   r   r   r  CLOSE	set_titler   rU   r  "_on_not_authorized_dialog_responserD   show_nowr  r  r	  )rE   r  s     r   r!  z&Connection._show_not_authorized_dialog  s   : 	!   T\%)t,/O,A'*'<> > > 	
Q'(()))	:())*,9:899: 	; 	; 	; : 	IIz4#JKKKJJMMMJJMMM     EEHHHIILLLLLr   c                 `    | j                                          |                                 d S r
   )r   r  r	  r  s      r   r&  z-Connection._on_not_authorized_dialog_response  s,    r   c                 
   | j         rt          j                     t          | j                  dk    rU	 t          d          | j        d         z  }n# t          $ r t          d          }Y nw xY wt          || j                  }nt          | j                  }|	                    d           |
                    ddg           |                    d           |                    d           |                                 |                                 d| _        | j         r0|                    d	| j                   t          j                     d S |                                }|                     ||           d S )
Nr   zAuthentication (%s)r   )rF   rG   )rG   r   r   Tr   )r   r   r   r0   r   r   r   r   r   rZ   r^   rm   set_keep_aboverD   r'  r  r  _on_authentication_responser  r  )rE   rF   r  r   s       r   r"  z.Connection._perform_authentication_with_dialog  s   : 	!    %&&**,011D4I!4LL , , ,*++, %#'<1 1 1AA 4<000A	b	"b"""	J'''	$	

	

!: 	;IIz4#CDDD     uuxxH,,a:::::s   A A.-A.c                    |                                 \  }| _        |dk    r|| _        t                              | j        | j        f| j        | j                   |                                 |t          j	        j
        k    s|t          j	        j        k    rd| _        | j        r| j                                         d S d S )Nr   r  T)re   r   r   r  r   r   r   r	  r   r    ru   DELETE_EVENTr   r   r   r  )rE   r  r   users       r   r+  z&Connection._on_authentication_response  s    %+%9%9%<%<"t!2::!DN,,t~/3/A/C37<37: 	- 	? 	? 	? 	(///(555DL: 	#O"""""	# 	#r   )NTFNNN)T)F)rn   ro   rp   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r   r!  r&  r"  r+  r   r   r   r   r      sX       <A37   .1 1 1% % %$ $ $' ' '  B B B B<@ @ @O O OD D DL  6  
$ $ $ $" " "d d dL  (  ; ; ;<# # # # #r   r   __main__)TimedOperationTr
   )r   	Connected)z/admin/conf/cupsd.confz/dev/stdout)#r   configr   r   gigi.repositoryr   require_versionr   r   r   errordialogsdebuggettextinstallPACKAGEr   rq   requirer   r   rx   r  r   rn   threads_inittimedopsr0  set_debuggingr  r   r   r   printgetFiler   r   r   <module>rA     sa  (        				        5%              5%             				          v~1A B B B B[ X   T* T* T* T* T* T* T* T*r       8 &~(( k# k# k# k# k# k# k# k#Z zC''''''M4
1115588AJKK	E..!) /0 0 0038 8 8 8 8 r   