
    Kch                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlZ ej	        d           d dl
Z
d dlT d dlZd dlZ ej        ej        ej                    G d	 d
e j                  Z G d d          Z G d d          Z G d de          Zedk    r3 ed            G d d          Z e              ej                     dS dS )    N)GObject)GLib)Gdk)Gtkz1.9.60)*)domain	localedirc                   H    e Zd Z	 	 	 d
dZd Zd Zd Zd ZddZd Z	d	 Z
dS )_IPPConnectionThreadNc
                 P   t           j                            |            |                     d           || _        || _        || _        || _        |	| _        || _	        || _
        || _        t          j        d          | _        || _        d| _        t#          d| z             d S )NT   F+%s)	threadingThread__init__	setDaemon_queue_connhostport_encryption_reply_handler_error_handler_auth_handlerqueueQueue_auth_queueuser
_destroyed
debugprint)
selfmyqueueconnreply_handlererror_handlerauth_handlerr   r   r   
encryptions
             ,/usr/share/system-config-printer/asyncipp.pyr   z_IPPConnectionThread.__init__0   s     	!!4(((
		%++) ;q>>	EDL!!!!!    c                 4    t          j        d| z             d S Nz-%sdebugr    r!   s    r(   __del__z_IPPConnectionThread.__del__C       %$,'''''r)   c                 :    | j                             |           d S N)r   putr!   passwords     r(   set_auth_infoz"_IPPConnectionThread.set_auth_infoF   s    h'''''r)   c                 R   | j         t          j                    | _         | j        t          j                    | _        | j        t          j                    | _        | j        rt          j        | j                   nt          j	                    | _        t          j
        | j                   	 t          j        | j         | j        | j                  }|                     d            n.# t          $ r!}d }|                     |           Y d }~nd }~ww xY w	 t!          d           | j                                        | _        | j                                        }t!          dt+          |          z             || j                                         nY| j        r| j                                         d| _        |\  }}}}}}	|dk    r|| _        |dk    r|| _        |	dk    r|	| _        |dk    r|d         | _        t          j        | j                   t!          d| j        z             t          j
        | j                   	 t          j        | j         | j        | j                  }t!          d           | j                                         |                     d            nT# t          $ rG}t!          d	           | j                                         |                     |           Y d }~nd }~ww xY w	 t!          d
|z              ||g|R i |}
|t          j        j        j        k    r%|
i k    rt          j        t          j        d          t!          d           |                     |
           nK# t>          $ r>}t!          dt+          |          z             |                     |           Y d }~nd }~ww xY w| j                                         t!          d           | ` | `| `| `| `| `!~t          j
        d            d S )N)r   r   r'   TzAwaiting further instructionszNext task: %sFr   zSet user=%s; reconnecting...z...reconnectedz	...failedzCall %s z
...successz...failure (%s)zThread exiting)"r   cups	getServerr   getPortr   getEncryptionr   setUsergetUsersetPasswordCB2_auth
Connection_replyRuntimeError_errorr    r   emptyidlegetrepr	task_doner   r   r   r   adminGetServerSettings__call__IPPErrorIPP_NOT_AUTHORIZED	Exceptionr   r   )r!   r#   eitemfnargskwdsrhehahresults              r(   runz_IPPConnectionThread.runI   s?   9))DI9DI##144D9 	(L$)$$$$DITZ(((	?)-/3/?A A AD KK 	 	 	DKKOOOOOOOO	>	%7888)),,DI;??%%D$++5666|%%((( %%(((DI+/(RtRRU{{&(#U{{&(#U{{%'"Tzz G	di(((:TYFGGG#TZ000$?157;7GI I ID  0111K)),,,KK&&&&# $ $ $,,,K)),,,KKOOOOOOOO$
  IN+++T1D111D11?HHH||"mT-DbIIIL)))V$$$$      -a8999Q  K!!$$$}>	%@ 	$%%%JKT"""""sJ   1;C- -
D7DDA#J8 8
L	=LL	BN 
O4OOc                 H    d| _         | j                            d            d S )NT)r   r   r3   r.   s    r(   stopz_IPPConnectionThread.stop   s#    r)   c                       fd} j         dS t          j        ||            j                                        }|S )Nc                     t          j                                         | j                   n                    | j                   t          j                     dS NF)r   threads_enterr   r   threads_leave)promptr#   methodresourcer!   s    r(   prompt_authz/_IPPConnectionThread._auth.<locals>.prompt_auth   sb       |""FDJ7777""FDJIII   5r)   r8   )r   r   idle_addr   rG   )r!   r`   r#   ra   rb   rc   r5   s   ` ```  r(   r@   z_IPPConnectionThread._auth   si    	 	 	 	 	 	 	 	 %2{F+++#''**r)   c                 l      fd} j         s$ j        rt          j        | j        |           d S d S d S )Nc                     j         s7t          j                      | j        |           t          j                     dS r]   r   r   r^   r   r_   )handlerrW   r!   s     r(   
send_replyz/_IPPConnectionThread._reply.<locals>.send_reply   sD    ? %!$$$V,,,!$$$5r)   )r   r   r   rd   )r!   rW   ri   s   `  r(   rB   z_IPPConnectionThread._reply   si    	 	 	 	 	  	D4#6 	DM:t':FCCCCC	D 	D 	D 	Dr)   c                       fd} j         s; j        r6t          d j        z             t          j        | j        |           d S d S d S )Nc                     j         s7t          j                      | j        |           t          j                     dS r]   rg   )rh   excr!   s     r(   
send_errorz/_IPPConnectionThread._error.<locals>.send_error   sD    ? %!$$$S)))!$$$5r)   zAdd %s to idle)r   r   r    r   rd   )r!   rl   rm   s   `  r(   rD   z_IPPConnectionThread._error   s    	 	 	 	 	  	A4#6 	A(4+>>???M:t':C@@@@@	A 	A 	A 	Ar)   )NNNNNNN)NNN)__name__
__module____qualname__r   r/   r6   rX   rZ   r@   rB   rD    r)   r(   r   r   /   s        IM@D!" " " "&( ( (( ( (a# a# a#F     $	D 	D 	D
A 
A 
A 
A 
Ar)   r   c                   L    e Zd ZdZ	 	 	 ddZd Zd Zd Zd ZddZ	d	 Z
d
 ZdS )IPPConnectiona  
    This class starts a new thread to handle IPP operations.

    Each IPP operation method takes optional reply_handler,
    error_handler and auth_handler parameters.

    If an operation requires a password to proceed, the auth_handler
    function will be called.  The operation will continue once
    set_auth_info (in this class) is called.

    Once the operation has finished either reply_handler or
    error_handler will be called.
    Nc	                 j   t          d           || _        t          j                    | _        t	          | j        | |||||||	  	        | _        | j                                         t          t          j	        j
                  }	g }
t          t          j	                  D ]v}|d         dk    rt          t          j	        |          }t          |          |	k    r=t          | ||                     |                     |
                    |           w|
| _        t          d| z             d S )NzNew IPPConnectionr$   r%   r&   r   r   r   r'   r    r   )r    _parentr   r   r   threadstarttyper9   rA   getPrintersdirgetattrsetattr_make_bindingappendbindings)r!   r$   r%   r&   r   r   r   r'   parent
methodtyper   fnamerQ   s                r(   r   zIPPConnection.__init__   s(    	'((([^^
*DJ:G:G9E15Dt7AC C C 	4?677
$/** 	$ 	$EQx3$/511BRyyJ&&T5$"4"4b"9"9:::OOU#### EDL!!!!!r)   c                 4    t          j        d| z             d S r+   r,   r.   s    r(   r/   zIPPConnection.__del__  r0   r)   c                    t          d| z             | j        D ]}t          | |           | j                                        rDt          d           | j                                         t          j        d| j                   d S d S )NzDESTROY: %szStopping worker threadr   )	r    r   delattrrx   is_aliverZ   r   timeout_add_seconds_reap_thread)r!   bindings     r(   destroyzIPPConnection.destroy  s    MD()))} 	$ 	$GT7####;"" 	<0111K$a):;;;;;	< 	<r)   c                     | j         j        r| j                                         dS t	          d| j         z             dS )NFz Thread %s still processing tasksT)rx   rF   r   joinr    r.   s    r(   r   zIPPConnection._reap_thread  sA    ; 	JOO56DEEEtr)   c                 :    | j                             |           dS )z*Call this from your auth_handler function.N)rx   r6   r4   s     r(   r6   zIPPConnection.set_auth_info  s    !!8,,,,,r)   c                 f    t          d           | j                            d|fi ||df           d S )NzReconnect...TF)r    r   r3   )r!   r   r$   r%   s       r(   	reconnectzIPPConnection.reconnect!  sF    N###
w&u> 	? 	? 	? 	? 	?r)   c                       fdS )Nc                  &     j         g| R i |S r2   )_call_function)rR   rS   rQ   r!   s     r(   <lambda>z-IPPConnection._make_binding.<locals>.<lambda>'  s%    %8T%8"%Lt%L%L%Lt%L%L r)   rq   )r!   rQ   s   ``r(   r   zIPPConnection._make_binding&  s    LLLLLLr)   c                     dx}x}}d|v r|d         }|d= d|v r|d         }|d= d|v r|d         }|d= | j                             ||||||f           d S NFr$   r%   r&   )r   r3   )r!   rQ   rR   rS   r$   r%   r&   s          r(   r   zIPPConnection._call_function)  s    7<<<d"" 1M_%d"" 1M_%T!!/L^$
T4&|E 	F 	F 	F 	F 	Fr)   )NNNNNNNNNN)rn   ro   rp   __doc__r   r/   r   r   r6   r   r   r   rq   r)   r(   rs   rs      s          ;?@D*." " " "8( ( (< < <  - - -? ? ? ?
M M MF F F F Fr)   rs   c                   V    e Zd Z	 d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 )_IPPAuthOperationNc                    d| _         d| _        d| _        d| _        d| _        d| _        || _        || _        | j        j        | _	        || _
        || _        || _        || _        || _        t          d| z             d S )NFr8   r   )_auth_called_dialog_shown_use_password_cancel
_reconnect_reconnected_userr   try_as_root_try_as_root
_client_fn_client_args_client_kwds_client_reply_handler_client_error_handlerr    )r!   r$   r%   r#   r   rQ   rR   rS   s           r(   r   z_IPPAuthOperation.__init__A  s    !"!

 J2  %2"%2"EDL!!!!!r)   c                 4    t          j        d| z             d S r+   r,   r.   s    r(   r/   z_IPPAuthOperation.__del__T  r0   r)   c                     | ` | `| `| `| `| `d S r2   )r   r   r   r   r   r   r.   s    r(   _destroyz_IPPAuthOperation._destroyW  s*    JO&&&&r)   c                    | j         %t          d           |                     ||          S | j        r't          d| z             |                     |          S | j        rQd| _        d| _        t          | d| j        d           |                    | j        | j	        | j                   d S d}t          |          t          j        k    r|j        \  }}|t          j        k    s |t          j        k    s|t          j        k    r|t          j        k    }n|t          j        k    r|                     ||          S |                     |          S t          |          t          j        k    rO|j        \  }|t          j        k    s|t          j        k    r|t          j        k    }n*|                     |          S |                     |          S |rt          d| z             nt          d	| z             | j        rg| j        d
k    r\| j        j        j        d         dk    s|r?t          d           d
| _        |                    | j        | j	        | j                   d S | j        s|                     |          S |j        j        }|j        j        }t6          j                            ||           d| _        t          | d| j        d           |                    | j        | j	        | j                   d S )NzConnection/reconnection failedz,%s (_error_handler): canceled so chaining upFTz$ (_error_handler): reconnecting (as )...r$   r%   z%s (_error_handler): forbiddenz#%s (_error_handler): not authorizedrootr   /zAuthentication: Try as rootr   r   r8   )r   r    _reconnect_errorr   rD   r   r   r   r   _reconnect_replyrz   r9   rL   rR   rM   IPP_FORBIDDENIPP_AUTHENTICATION_CANCELEDIPP_SERVICE_UNAVAILABLE	HTTPErrorHTTP_UNAUTHORIZEDHTTP_FORBIDDENr   r   rx   r   r   r   authconnglobal_authinfocacheremove_auth_infor   )	r!   r#   rl   	forbiddenrO   msr   r   s	            r(   r%   z_IPPAuthOperation.error_handler_  sH   ?" 8999(($444< 	%FMNNN;;$$$? 	#DO $Dtzzz+ , , ,NNDJ*.*?*.*?  A A A F	::&&XFQT,,,T'''T555$"44		d222,,dC888{{C(((3ZZ4>))8DQT+++T((($"55		{{C(((;;$$$  	F84?@@@@=DEEE 	J&  Z#A&#--- 5666DJNNDJ*.*?*.*?  A A A F  	%;;$$$ {{%66T=A 	7 	C 	C 	CTT4:::' 	( 	( 	(
&*&;&*&; 	 	= 	= 	= 	= 	=r)   c           
         | j         dk    r| j        t          j                    | _        | j        ra|j        j        }|j        j        }t          j        	                    ||          }|r&|d         | j        k    r|d         | _
        d| _        ~n@|j        j        }|j        j        }t          j                            ||           d| _
        d| _         | j        r2t          d           d| _        |                    | j
                   d S d| _        |j        s|                    | j
                   d S | j        rt#          j        | j        j        ddt"          j        j        t"          j        j        t3          d          	          }|                    t3          d
                     |                                 |                                 d }	|j        r|j                                        }	|	t          j        |j                  }n-t3          d          |	z  }
t          j        |
|j                  }|                     d           | j        t          j                    | _        |                    ddg           |!                    d           |"                    d           |#                                 |$                    d| j%                   d| _        d S )NFr   r   r   Tr8   z%Supplying password after reconnectionzNot authorizedr   modaldestroy_with_parentmessage_typebuttonstextzThe password may be incorrect.)r   zAuthentication (%s))titler   usernameresponse)&r   r   r9   r>   rx   r   r   r   r   lookup_auth_infor   r   r   r    r6   prompt_allowedr   r   MessageDialogr   r   MessageTypeERRORButtonsTypeCLOSE_format_secondary_textrX   r   semanticcurrent_operation
AuthDialog
set_promptfield_grab_focusset_keep_aboveshow_allconnect_on_auth_dialog_response)r!   r`   r#   ra   rb   r   r   credsdopr   s              r(   r&   z_IPPAuthOperation.auth_handler  s   %%z!!\^^
z 	{'{' 5FFTMQ G S S Qx4:---21X*,0);#D;#D)::AE ; G G G!#D  	?@@@ %D 2333F!" 	 2333F  	!$**;)-4030E+.?+@()*:(;(;	= = =A
 ##Q'G%H%HIIIEEHHHIILLL= 	40033B:#DK888AA+,,r1E#5,0K9 9 9A 	
b:DJ	"b"""	J'''	$	

			:t<===!r)   c                     d| _         | j        j                            | j        | j        | j        | j        | j        | j	        f           d S r]   )
r   r   r   r3   r   r   r   r   r%   r&   r.   s    r(   submit_taskz_IPPAuthOperation.submit_task  sY    !
t0A#0#9  $1#02 	3 	3 	3 	3 	3r)   c                 D   |                                 \  }}|dk    r| j        }t          j                            ||f| j        j        j        | j        j        j                   || _	        |
                                 |t          j        j        k    s|t          j        j        k    rqd| _        | j                            d           t          j                            | j        j        j        | j        j        j                   t%          d           d S || j        k    r2|| _        | j                            |           t%          d           d S || _        || _        d| _        | j                            d           t%          d| j        z             d S )Nr8   r   TzAuth canceledzPassword supplied.zWill try as %s)get_auth_infor   r   r   cache_auth_infor   rx   r   r   _dialoghider   ResponseTypeCANCELDELETE_EVENTr   r6   r   r    r   r   )r!   dialogr   r   r5   s        r(   r   z*_IPPAuthOperation._on_auth_dialog_response  s   !//22x2:::D%558@8B<@J<M<R<@J<M<R 	6 	T 	T 	T (///(555DLJ$$b))))::ARAWAEARAW ; Y Y Y(((F4:!)DJ$$h///,---F
%
  "%%%$tz122222r)   c                 n    t          d| j        z             | j        |                                  d S d S )NzConnected as %s)r    r   r   r   r!   r#   rW   s      r(   r   z"_IPPAuthOperation._reconnect_reply  sB     	%
2333?& '&r)   c                    t          d| j        z             | j        j        s|                     |           d S d }|j        r|j                                        }|t          d          }nt          d          |z  }t          j	        | j        j
        ddt          j        j        t          j        j        |          }| j        t!          |          t"          k    rd}n:t!          |          t$          j        k    r|j        d         }nt+          |          }|                    t          d|z                       |                    t          j        t          j        j        t          d	          t          j        j                   |                    t          j        j                   |                    d
| j                   t          | d|d|d           |                                 d S )NzFailed to connect as %szCUPS server errorzCUPS server error (%s)Tr   z!service-error-service-unavailabler   z3There was an error during the CUPS operation: '%s'.Retryr   z. (_reconnect_error): presenting error dialog (z; )) r    r   r   r   rD   r   r   r   r   r   r   r   r   r   NONEr   rz   rC   r9   rL   rR   rH   r   add_buttonsSTOCK_CANCELr   r   OKset_default_responser   _on_retry_server_error_responseshow)r!   r#   rl   r   msgr   messages          r(   r   z"_IPPAuthOperation._reconnect_error%  s   -
:;;;z( 	KKF= 	40033B:'((CC,--2Cdj&7%)t,/O,A'*';$'	) ) ) ?"tSzz\'A'A9GG3ZZ4=((hqkGGCjjG	 $;=D$E "F "F 	G 	G 	G	s')9)@zz3#3#6	8 	8 	8	 0 3444			:tCDDDTT333) 	* 	* 	*						r)   c                    |                                  |t          j        j        k    r\t	          | d| j        j        j        d           | j                            | j        j        j        | j	        | j
                   d S t	          d| z             |                     t          j        dt          d                               d S )Nz': got retry response, reconnecting (as r   r   z%s: got cancel responser   zOperation canceled)r   r   r   r   r    r   rx   r   r   r   r   rD   r9   rL   r   )r!   r   r   s      r(   r   z1_IPPAuthOperation._on_retry_server_error_responseL  s    s'***tz05557 8 8 8J  $*"3"8040E040E ! G G G G G 1D8999KK1-A+B+BCCDDDDDr)   c                    t          | dt          |                     | j        rJt          | d| j                   |                     | j        |           |                                  d S t          d| z             d S )Nz (_error): handling z (_error): calling z(%s (_error): no client error handler set)r    rH   r   r   r   )r!   rl   s     r(   rD   z_IPPAuthOperation._errorX  s    $$$c


CDDD% 	Kt99; < < <&&
C888MMBTIJJJJJr)   )NNNNr   )rn   ro   rp   r   r/   r   r%   r&   r   r   r   r   r   rD   rq   r)   r(   r   r   @  s        6:" " " "&( ( (' ' 'Q= Q= Q=f?" ?" ?" ?"B3 3 33 3 3@     % % %N
E 
E 
EK K K K Kr)   r   c                   *    e Zd Z	 	 	 	 ddZd Zd ZdS )IPPAuthConnectionNTc           
      0   || _         |	| _        || _        |
| _        d }t          j                            ||          }|r|d         dk    s|r|d         }~t          |||           }t          	                    | ||j
        |j        ||||           d S )Nr   r   r   ru   )r   r   r   r   r   r   r   r   rs   r   r%   r&   )r!   r$   r%   r&   r   r   r'   r   r   r   r   r   r   r   s                 r(   r   zIPPAuthConnection.__init__f  s     ,& ->>TEI ? K K 	Qx6!![!Qx }dCCM.0.>-/_4%) 	 	M 	M 	M 	M 	Mr)   c                 H    d | _         t                              |            d S r2   )r   rs   r   r.   s    r(   r   zIPPAuthConnection.destroy~  s#    t$$$$$r)   c           	          dx}x}}d|v r|d         }|d= d|v r|d         }|d= d|v r|d         }|d= t          ||| | j        j        |||          }|                                 d S r   )r   rx   r   r   )r!   rQ   rR   rS   r$   r%   r&   r   s           r(   r   z IPPAuthConnection._call_function  s    7<<<d"" 1M_%d"" 1M_%T!!/L^$ }d $ 0"dDB B 	r)   )
NNNNNNNTTN)rn   ro   rp   r   r   r   rq   r)   r(   r   r   e  sV        :>FJ@DM M M M0% % %    r)   r   __main__Tc                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
UIc                    t          j                    }|                    d| j                   t           j                            d          }|                    d| j                   t          j                    }|                    |ddd           |	                    |           t           j                            d          | _
        | j
                            d| j                   | j
                            d           |                    | j
        ddd           d | _        |                                 d S )Nr   ConnectclickedFr   zGet Devices)r   Windowr   r   Buttonnew_with_labelconnect_clickedVBox
pack_startaddget_devices_buttonget_devicesset_sensitiver#   r   )r!   wbvboxs       r(   r   zUI.__init__  s    
AIIy$,///
))955AIIy$"67778;;DOOQua000EE4LLL&)j&?&?&O&OD##++Y8HIII#115999OOT4eUAFFFDIJJMMMMMr)   c                     	 | j                                          n# t          $ r Y nw xY wt          j                     d S r2   )r#   r   AttributeErrorr   	main_quit)r!   windows     r(   r   z
UI.destroy  sT    	!!$$$$!    Ms    
))c                     | j         r| j                                          t          | j        | j                  | _         d S )Nr   )r#   r   r   	connectedconnect_failedr!   buttons     r(   r
  zUI.connect_clicked  sG    y %	!!$$$)9=9LN N NDIIIr)   c                 x    t          dt          |          z             | j                            d           d S )NzSuccess: %sT)r    rH   r  r  r   s      r(   r  zUI.connected  s8    f5666#11488888r)   c                     t          dt          |          z             | j                            d           | j                                         d S )NzExc %sF)r    rH   r  r  r#   r   r!   r#   rl   s      r(   r  zUI.connect_failed  sL    4::-...#115999I     r)   c                     |                     d           t          d           | j                            | j        | j                   d S )NFzGetting devicesr   )r  r    r#   
getDevicesget_devices_replyget_devices_errorr  s     r(   r  zUI.get_devices  s^      %((()***I  0F040F ! H H H H Hr)   c                     || j         k    rt          d           d S t          dt          |          z             | j                            d           d S )NzIgnoring stale replyzGot devices: %sTr#   r    rH   r  r  r   s      r(   r"  zUI.get_devices_reply  sZ    ty  2333)D&MM9:::#11488888r)   c                     || j         k    rt          d           d S t          dt          |          z             | j                            d           d S )NzIgnoring stale errorzError getting devices: %sTr%  r  s      r(   r#  zUI.get_devices_error  sZ    ty  23333dCjj@AAA#11488888r)   N)rn   ro   rp   r   r   r
  r  r  r  r"  r#  rq   r)   r(   r  r    s        	 	 		 	 		N 	N 	N	9 	9 	9	! 	! 	!
	H 	H 	H	9 	9 	9	9 	9 	9 	9 	9r)   r  )r   configr9   gi.repositoryr   r   r   r   r   requirer   r-   gettextinstallPACKAGEr	   r   r   rs   r   r   rn   set_debuggingr  mainrq   r)   r(   <module>r/     s  *       ! ! ! ! ! !                    h           v~1A B B B BhA hA hA hA hA9+ hA hA hA\YF YF YF YF YF YF YF YFF`K `K `K `K `K `K `K `KJ	/ / / / / / / /b zM4<9 <9 <9 <9 <9 <9 <9 <9| BEEECHKKKKKE r)   