
    Hfh                        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 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(  r+ ed        G d d      Z e         ej0                          yy)    N)GObject)GLib)Gdk)Gtkz1.9.60)*)domain	localedirc                   F    e Zd Z	 	 	 d
dZd Zd Zd Zd ZddZd Z	d	 Z
y)_IPPConnectionThreadNc
                 @   t         j                  j                  |        | j                  d       || _        || _        || _        || _        |	| _        || _	        || _
        || _        t        j                  d      | _        || _        d| _        t#        d| z         y )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         y Nz-%sdebugr    r!   s    r(   __del__z_IPPConnectionThread.__del__C       %$,'r)   c                 :    | j                   j                  |       y N)r   putr!   passwords     r(   set_auth_infoz"_IPPConnectionThread.set_auth_infoF   s    h'r)   c                    | 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
                        }| j                  d        	 t!        d       | j"                  j%                         | _        | j"                  j)                         }t!        dt+        |      z         || j"                  j-                          n| j.                  r| j"                  j-                          d| _        |\  }}}}}}	|dk7  r|| _        |dk7  r|| _        |	dk7  r|	| _        |dk(  r|d   | _        t        j                  | j                         t!        d| j                  z         t        j                  | j                         	 t        j                  | j                   | j                  | j
                        }t!        d       | j"                  j-                          | j                  d        	 t!        d
|z          ||g|i |}
|t        j                  j6                  j8                  k(  r)|
i k(  r$t        j:                  t        j<                  d      t!        d       | j                  |
       | j"                  j-                          Kt!        d       | ` | `| `| `| `| `!~t        j                  d        y # t        $ r}d }| j                  |       Y d }~d }~ww xY w# t        $ rA}t!        d	       | j"                  j-                          | j                  |       Y d }~Bd }~ww xY w# t>        $ r2}t!        dt+        |      z         | j                  |       Y d }~d }~ww xY w)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   se   99)DI99DI##114D99LL$))$DITZZ(	??)-/3/?/?AD KK
 78)),DI;;??%D$+56|%%(%%(DI+/(RtRRU{&(#U{&(#U{%'"Tz G	dii(:TYYFG##TZZ0$??157;7G7GID  01KK)),KK&  IN+T1D1D1??HHH|"mmT-D-DbIIL)V$
 KK!!$} @ 	$%JKT"[  	DKKOO	V $ $,KK)),KKOO$&   -a89Q sK   AN A,O  BP 	N=N88N= 	P
	6PP
	Q(QQc                 H    d| _         | j                  j                  d        y )NT)r   r   r3   r.   s    r(   stopz_IPPConnectionThread.stop   s    r)   c                       fd} j                   yt        j                  ||        j                  j	                         }|S )Nc                     t        j                          j                  | j                         nj                  | j                         t        j                          yNF)r   threads_enterr   r   threads_leave)promptr#   methodresourcer!   s    r(   prompt_authz/_IPPConnectionThread._auth.<locals>.prompt_auth   sO     |""FDJJ7""FDJJI r)   r8   )r   r   idle_addr   rG   )r!   r`   r#   ra   rb   rc   r5   s   ` ```  r(   r@   z_IPPConnectionThread._auth   s@    	 %{F+##''*r)   c                       fd} j                   s/ j                  r"t        j                  | j                  |       y y y )Nc                     j                   s;t        j                           | j                  |       t        j                          yr]   r   r   r^   r   r_   )handlerrW   r!   s     r(   
send_replyz/_IPPConnectionThread._reply.<locals>.send_reply   s3    ??!!$V,!!$r)   )r   r   r   rd   )r!   rW   ri   s   `  r(   rB   z_IPPConnectionThread._reply   s6    	 4#6#6MM:t':':FC $7r)   c                       fd} j                   sG j                  r:t        d j                  z         t        j                  | j                  |       y y y )Nc                     j                   s;t        j                           | j                  |       t        j                          yr]   rg   )rh   excr!   s     r(   
send_errorz/_IPPConnectionThread._error.<locals>.send_error   s3    ??!!$S)!!$r)   zAdd %s to idle)r   r   r    r   rd   )r!   rl   rm   s   `  r(   rD   z_IPPConnectionThread._error   sJ    	 4#6#6(4+>+>>?MM:t':':C@ $7r)   )NNNNNNN)NNN)__name__
__module____qualname__r   r/   r6   rX   rZ   r@   rB   rD    r)   r(   r   r   /   s7    IM@D!"&((a#F$	D
Ar)   r   c                   J    e Zd ZdZ	 	 	 ddZd Zd Zd Zd ZddZ	d	 Z
d
 Zy)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	                 B   t        d       || _        t        j                         | _        t	        | j                  | |||||||	      | _        | j
                  j                          t        t        j                  j                        }	g }
t        t        j                        D ]a  }|d   dk(  rt        t        j                  |      }t        |      |	k7  r5t        | || j                  |             |
j                  |       c |
| _        t        d| z         y )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     	'([[^
*DJJ:G:G9E15Dt7AC 	4??667
$//* 	$EQx3$//51BRyJ&T5$"4"4b"9:OOU#	$ !EDL!r)   c                 4    t        j                  d| z         y r+   r,   r.   s    r(   r/   zIPPConnection.__del__  r0   r)   c                    t        d| z         | j                  D ]  }t        | |        | j                  j	                         rFt        d       | j                  j                          t        j                  d| j                         y y )NzDESTROY: %szStopping worker threadr   )	r    r   delattrrx   is_aliverZ   r   timeout_add_seconds_reap_thread)r!   bindings     r(   destroyzIPPConnection.destroy  sr    MD()}} 	$GT7#	$ ;;"01KK$$a):):; #r)   c                     | j                   j                  r| j                  j                          yt	        d| j                   z         y)NFz Thread %s still processing tasksT)rx   rF   r   joinr    r.   s    r(   r   zIPPConnection._reap_thread  s5    ;;JJOO6DEr)   c                 :    | j                   j                  |       y)z*Call this from your auth_handler function.N)rx   r6   r4   s     r(   r6   zIPPConnection.set_auth_info  s    !!8,r)   c                 ^    t        d       | j                  j                  d|fi ||df       y )NzReconnect...TF)r    r   r3   )r!   r   r$   r%   s       r(   	reconnectzIPPConnection.reconnect!  s.    N#

w&u> 	?r)   c                       fdS )Nc                  0     j                   g| i |S r2   )_call_function)rR   rS   rQ   r!   s     r(   <lambda>z-IPPConnection._make_binding.<locals>.<lambda>'  s    %8T%8%8"%Lt%Lt%L r)   rq   )r!   rQ   s   ``r(   r   zIPPConnection._make_binding&  s	    LLr)   c                     dx}x}}d|v r|d   }|d= d|v r|d   }|d= d|v r|d   }|d= | j                   j                  ||||||f       y 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r)   )NNNNNNNNNN)rn   ro   rp   __doc__r   r/   r   r   r6   r   r   r   rq   r)   r(   rs   rs      s<     ;?@D*."8(<-?
MFr)   rs   c                   T    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y)_IPPAuthOperationNc                    d| _         d| _        d| _        d| _        d| _        d| _        || _        || _        | j                  j                  | _	        || _
        || _        || _        || _        || _        t        d| z         y )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    !"!

 JJ22  %2"%2"EDL!r)   c                 4    t        j                  d| z         y r+   r,   r.   s    r(   r/   z_IPPAuthOperation.__del__T  r0   r)   c                     | ` | `| `| `| `| `y r2   )r   r   r   r   r   r   r.   s    r(   _destroyz_IPPAuthOperation._destroyW  s$    JO&&r)   c                    | j                   t        d       | j                  ||      S | j                  rt        d| z         | j	                  |      S | j
                  r\d| _        d| _        t        | d| j                  d       |j                  | j                  | j                  | j                         y 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| j                  ||      S | j	                  |      S t        |      t        j$                  k(  rY|j                  \  }|t        j&                  k(  s|t        j(                  k(  r|t        j(                  k(  }n"| j	                  |      S | j	                  |      S |rt        d| z         nt        d	| z         | j*                  r|| j                  d
k7  rm| j,                  j.                  j0                  d   dk(  s|rEt        d       d
| _        |j                  | j                  | j                  | j                         y | j2                  s| j	                  |      S |j.                  j0                  }|j.                  j4                  }t6        j8                  j;                  ||       d| _        t        | d| j                  d       |j                  | j                  | j                  | j                         y )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_  s   ??" 89(($44<<FMN;;$$??#DO $Dtzz+ ,NNDJJ*.*?*?*.*?*?  A 	:&XXFQT,,,T'''T555$"4"44	d222,,dC88{{C((3Z4>>)88DQT+++T((($"5"55	{{C((;;$$ 84?@=DEJJ& ZZ##A&#- 56DJNNDJJ*.*?*?*.*?*?  A   ;;$$ {{{{%%66T=A 	7 	C4::' 	(

&*&;&;&*&;&; 	 	=r)   c           
         | j                   dk(  r| j                  t        j                         | _        | j                  r|j                  j
                  }|j                  j                  }t        j                  j                  ||      }|ry|d   | j                  k(  r|d   | _
        d| _        ~nT|j                  j
                  }|j                  j                  }t        j                  j                  ||       d| _
        d| _         | j                  r.t        d       d| _        |j                  | j                         y d| _        |j                  s|j                  | j                         y | j                   rt#        j$                  | j&                  j(                  ddt"        j*                  j,                  t"        j.                  j0                  t3        d      	      }|j5                  t3        d
             |j7                          |j9                          d }	|j:                  r|j:                  j=                         }	|	!t        j>                  |j(                        }n/t3        d      |	z  }
t        j>                  |
|j(                        }|jA                  d       | j                  t        j                         | _        |j                  ddg       |jC                  d       |jE                  d       |jG                          |jI                  d| jJ                         d| _        y )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   %zz!!\\^
zz{{''{{'' 55FFTMQ G SQx4::--21X*,0);;##D;;##D))::AE ; G!#D ?@ %D 2 23!"" 2 23 !!$***;*;)-4030E0E+.??+@+@()*:(;	=A
 ##Q'G%HIEEHIIL==003B:##DKK8A+,r1E##5,0KK9A 	
b::DJ	"b"	J'	$	

			:t<<=!r)   c                     d| _         | j                  j                  j                  | j                  | j
                  | j                  | j                  | j                  | j                  f       y r]   )
r   r   r   r3   r   r   r   r   r%   r&   r.   s    r(   submit_taskz_IPPAuthOperation.submit_task  s[    !

t0A0A#00#99  $11#002 	3r)   c                    |j                         \  }}|dk(  r| j                  }t        j                  j	                  ||f| j
                  j                  j                  | j
                  j                  j                         || _	        |j                          |t        j                  j                  k(  s|t        j                  j                  k(  rd| _        | j
                  j!                  d       t        j                  j#                  | j
                  j                  j                  | j
                  j                  j                         t%        d       y || j                  k(  r.|| _        | j
                  j!                  |       t%        d       y || _        || _        d| _        | j
                  j!                  d       t%        d| j                  z         y )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  sy   !//2x2:::D%%558@8B<@JJ<M<M<R<R<@JJ<M<M<R<R 	6 	T ((///((555DLJJ$$b)))::ARARAWAWAEARARAWAW ; Y(4::!)DJJ$$h/,-
%

  "%$tzz12r)   c                 n    t        d| j                  z         | j                  | j                          y y )NzConnected as %s)r    r   r   r   r!   r#   rW   s      r(   r   z"_IPPAuthOperation._reconnect_reply  s1     	%

23??& 'r)   c                 <   t        d| j                  z         | j                  j                  s| j	                  |       y d }|j
                  r|j
                  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}n7t!        |      t$        j&                  k(  r|j(                  d   }nt+        |      }|j-                  t        d|z               |j/                  t        j0                  t        j2                  j4                  t        d	      t        j2                  j6                         |j9                  t        j2                  j6                         |j;                  d
| j<                         t        | d|d|d       |j?                          y )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   -

:;zz((KK==003B:'(C,-2Cdjj&7&7%)t,/OO,A,A'*';';$'	) ??"tSz\'A9G3Z4==(hhqkGCjG	 $;=D$E "F 	G	s'')9)9)@)@z3#3#3#6#6	8	 0 0 3 34			:tCCD3) 	*		r)   c                    |j                          |t        j                  j                  k(  rt	        | d| j
                  j                  j                  d       | j
                  j                  | j
                  j                  j                  | j                  | j                         y t	        d| z         | j                  t        j                  dt        d                   y )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''***tzz00557 8JJ  $**"3"3"8"8040E0E040E0E ! G 1D89KK1-A+BCDr)   c                     t        | dt        |             | j                  rGt        | d| j                         | j                  | j                  |       | j	                          y t        d| z         y )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%%t99; <&&

C8MMBTIJr)   )NNNNr   )rn   ro   rp   r   r/   r   r%   r&   r   r   r   r   r   rD   rq   r)   r(   r   r   @  sC    6:"&('Q=f?"B33@ %N
EKr)   r   c                   (    e Zd Z	 	 	 	 ddZd Zd Zy)IPPAuthConnectionNc           
      "   || _         |	| _        || _        |
| _        d }t        j
                  j                  ||      }|r|d   dk7  s|r|d   }~t        |||       }t        j                  | ||j                  |j                  ||||       y )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Qx6![Qx }dCM.0.>.>-/__4%) 	 	Mr)   c                 <    d | _         t        j                  |        y 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                  |||      }|j                          y 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 0"dDB 	r)   )
NNNNNNNTTN)rn   ro   rp   r   r   r   rq   r)   r(   r   r   e  s    :>FJ@D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
y	)
UIc                    t        j                         }|j                  d| j                         t         j                  j                  d      }|j                  d| j                         t        j                         }|j                  |ddd       |j                  |       t         j                  j                  d      | _
        | j                  j                  d| j                         | j                  j                  d       |j                  | j                  ddd       d | _        |j                          y )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5AIIy$"6"6788;DOOQua0EE4L&)jj&?&?&OD###++Y8H8HI##1159OOT44eUAFDIJJMr)   c                     	 | j                   j                          t        j                          y # t        $ r Y  w xY wr2   )r#   r   AttributeErrorr   	main_quit)r!   windows     r(   r   z
UI.destroy  s7    		!!$ MM " s   1 	==c                     | j                   r| j                   j                          t        | j                  | j                        | _         y )Nr   )r#   r   r   	connectedconnect_failedr!   buttons     r(   r
  zUI.connect_clicked  s5    yy		!!$)9=9L9LNDIr)   c                 h    t        dt        |      z         | j                  j                  d       y )NzSuccess: %sT)r    rH   r  r  r   s      r(   r  zUI.connected  s'    f56##1148r)   c                     t        dt        |      z         | j                  j                  d       | j                  j                          y )NzExc %sF)r    rH   r  r  r#   r   r!   r#   rl   s      r(   r  zUI.connect_failed  s7    4:-.##1159II r)   c                     |j                  d       t        d       | j                  j                  | j                  | j
                         y )NFzGetting devicesr   )r  r    r#   
getDevicesget_devices_replyget_devices_errorr  s     r(   r  zUI.get_devices  sB      %()*II  0F0F040F0F ! Hr)   c                     || j                   k7  rt        d       y t        dt        |      z         | j                  j	                  d       y )NzIgnoring stale replyzGot devices: %sTr#   r    rH   r  r  r   s      r(   r"  zUI.get_devices_reply  s@    tyy 23)D&M9:##1148r)   c                     || j                   k7  rt        d       y t        dt        |      z         | j                  j	                  d       y )NzIgnoring stale errorzError getting devices: %sTr%  r  s      r(   r#  zUI.get_devices_error  s@    tyy 233dCj@A##1148r)   N)rn   ro   rp   r   r   r
  r  r  r  r"  r#  rq   r)   r(   r  r    s,    			N	9	!
	H	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~~1A1A BhA9++ hA\YF YFF`K `KJ	/ /b z4<9 <9| ECHHKE r)   