
`9Sc           @   s   d  d l  Z  d  d l Z  d  d l Z d  d l Td  d l Td  d l Td  d l m Z d  d l m Z d  d l	 Td g Z
 d e f d     YZ d S(   iN(   t   *(   t   gui(   t   txtt   HttpRequestDialogc           B   s   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   s  Dialog that makes it possible to download a file from the Internet using
    either GET or POST HTTP request.

    If 'setDestinationFilePath' is called prior to 'exec_', a temporary file is
    created during the download with name obtained by adding '.tmp' to the name
    of the destination file. If the download completes successfully, temporary
    file is renamed to the destination one, possibly overwriting the target.
    If the download fails or is aborted by the user, temporary file is deleted.

    If 'setDestinationFilePath' is not called, then no files are created during
    the download, and 'getDownloadData' should be used to obtain contents of
    the downloaded file instead.c   	      C   s=  t  j |  |  t |  |  _ | |  _ d |  _ d |  _ d |  _ d |  _	 t
 |   |  _ d |  _ t   } t   |  _ |  j j   |  j j |  | j |  j  t |  j j t j t j B  } t d |  |  _ |  j j t  | j |  j  t   |  _ |  j j t  | j |  j  t   |  _ |  j j   | j t  j! |  j   | j"   t# d  |  _$ |  j$ j% t  |  j$ j& j' |  j(  t)   } | j"   | j |  j$  | j* |  |  j+ |  |  j, |  |  j- t  j. d   |  j/ |  j0   t1 j2 @ |  j3 j' |  j4  t5 j6 d |  j7  d S(   st   Use 'requestData' different from 'None' to send a POST request, and
        the default value to send a GET request.t    s   (%s)...t   Cancelt   worldi    N(8   t   QDialogt   __init__t   QUrlt   urlt   requestDatat   Nonet   destinationFilePatht   tmpFilePatht   tmpFilet   downloadedDatat   QNetworkAccessManagert   networkAccessManagert   networkReplyt   QVBoxLayoutt   StatusLabelWidgett   statusLabelt   disableWordWrappingt   setProgresst	   addWidgett   unicodet   toStringt   RemoveUserInfot   RemoveQueryt   QLabelt   urlLabelt   setWordWrapt   Falset   progressLabelt   QProgressBart   progressBart   hidet   addItemR   t   UnhidableWidgetItemt
   addStretcht   QPushButtont   cancelButtont   setAutoDefaultt   clickedt   connectt   rejectt   QHBoxLayoutt	   addLayoutt	   setLayoutt   setWindowTitlet   setWindowIcont   loadIcont   setWindowFlagst   windowFlagst   Qtt   WindowContextHelpButtonHintt   rejectedt
   onRejectedt   QTimert
   singleShott   onInitComplete(	   t   selft   parentt   windowTitlet   progressMessageR
   R   t   layoutt	   urlStringt   buttonBoxLayout(    (    s    code\common\HttpRequestDialog.pyR   1   sP    							
	
c         C   s8   |  j  d k s t  | |  _ | d |  _ d |  _ d S(   s   Call this prior to 'exec_' to instruct the dialog to store
        downloaded data in the given file on disk instead of a local
        variable.s   .tmpN(   R   R   t   AssertionErrorR   R   R   (   R>   R   (    (    s    code\common\HttpRequestDialog.pyt   setDestinationFilePath   s    	c         C   s   |  j  S(   s   Return an 'str' object holding the contents of the downloaded data,
        or 'None' if the data have been stored in a file on disk instead of a
        local variable.(   R   (   R>   (    (    s    code\common\HttpRequestDialog.pyt   getDownloadedData   s    c         C   s6   |  j  d  k	 r( |  j  j   d  |  _  n  | j   d  S(   N(   R   R   t   deleteLatert   accept(   R>   t   event(    (    s    code\common\HttpRequestDialog.pyt
   closeEvent   s    c         C   s   |  j  d k	 r( |  j  j   d |  _  n  |  j d k	 rC d |  _ n  t j |  j   |  j j   Wd QX|  j j   d |  _ |  j	 j
 d  |  j j   t j d |  j  d S(   sm   Abort the currently running download process and initiate a new one
        with the same request paremeters.R   Ni    (   R   R   t   closeR   R   t   SignalBlockerR   t   abortRH   R"   t   setTextR$   R%   R;   R<   R=   (   R>   (    (    s    code\common\HttpRequestDialog.pyt   restartDownload   s    	c         C   s_  |  j  d k	 r |  j d k s$ t  t j j |  j   } x t r y' t j j |  sg t j	 |  n  PWq< t
 k
 r t j |  j   d d t j |  t j t j Bt j  } | t j k r t Sq< Xq< Wx t rZy t |  j  d  |  _ PWq t k
 rVt j |  j   d d t j |  j   t j t j Bt j  } | t j k rWt Sq Xq Wt S(   s   Create an empty file at 'self.tmpFilePath' and open it.

        Return 'True' if the operation succeeds and 'False' otherwise.s   Failed to save data to disks8   Failed to create directory for the destination file (%s)t   wbs7   Failed to open the temporary data file (%s) for writingN(   R   R   R   RE   t   ost   patht   dirnamet   Truet   isdirt   makedirst   OSErrort   QMessageBoxt   criticalt   parentWidgetR   t	   quotePatht   Retryt   AbortR!   t   opent   IOError(   R>   t
   tmpFileDirt   answer(    (    s    code\common\HttpRequestDialog.pyt   openTmpFile   s8    $		c         C   s   |  j  d k	 r |  j d k	 s$ t  x t r y |  j j |  PWq' t k
 r t j |  j	   d d t
 j |  j   t j t j Bt j  } | t j k r t Sq' Xq' Wt S(   so   Write the given data to 'self.tmpFile'.

        Return 'True' if the operation succeeds and 'False' otherwise.s   Failed to save data to disks,   Failed to write the temporary data file (%s)N(   R   R   R   RE   RU   t   writeR`   RY   RZ   R[   R   R\   R]   R^   R!   (   R>   t   dataRb   (    (    s    code\common\HttpRequestDialog.pyt   writeTmpFile   s    $	c         C   s   |  j  d k	 r |  j d k s$ t  x t r y t j |  j  |  j  PWq' t t	 f k
 r t
 j |  j   d d t j |  j  t
 j t
 j Bt
 j  } | t
 j k r t Sq' Xq' W|  j   t S(   s   Move the file at 'self.tmpFilePath' to its final location.

        Return 'True' if the operation succeeds and 'False' otherwise.s   Failed to save data to disksA   Failed to move the temporary data file to its final location (%s)N(   R   R   R   RE   RU   t   shutilt   copyfileR   R`   RX   RY   RZ   R[   R   R\   R]   R^   R!   t   deleteTmpFile(   R>   Rb   (    (    s    code\common\HttpRequestDialog.pyt   moveTmpFile  s    $	
c         C   s   |  j  d k	 r |  j d k s$ t  x t r y t j |  j   PWq' t k
 r t j	 |  j
   d d t j |  j   t j t j Bt j  } | t j k r Pq q' Xq' Wd S(   s&   Delete the file at 'self.tmpFilePath'.s!   Failed to clean up temporary datas-   Failed to delete the temporary data file (%s)N(   R   R   R   RE   RU   RR   t   removeRX   RY   RZ   R[   R   R\   R]   t   Ignore(   R>   Rb   (    (    s    code\common\HttpRequestDialog.pyRi   &  s    $	c         C   s   t  |  j  } |  j d  k r6 |  j j |  |  _ n4 | j t  j d  |  j j	 | t
 |  j   |  _ |  j j j |  j  |  j j j |  j  |  j j j |  j  d  S(   Ns!   application/x-www-form-urlencoded(   t   QNetworkRequestR
   R   R   R   t   getR   t	   setHeadert   ContentTypeHeadert   postt
   QByteArrayt   downloadProgressR-   t   onDownloadProgresst   errort   onDownloadErrort   finishedt   onDownloadFinished(   R>   t   request(    (    s    code\common\HttpRequestDialog.pyR=   =  s    c         C   sW  | | k o d k n r  d  S|  j  j   | d k ry |  j j d | | f  |  j  j d |  |  j  j |  n7 |  j j d |  |  j  j d d  |  j  j d  |  j d  k	 r |  j d  k r |  j	   s |  j
   d  Sn  t |  j j    } |  j d  k	 r!|  j | 7_ n2 |  j d  k	 s6t  |  j |  sS|  j
   d  Sd  S(   Ni    is   %d bytes of %ds   %d bytes(   R$   t   showR"   RO   t   setRanget   setValueR   R   R   Rc   R.   t   strR   t   readAllR   RE   Rf   (   R>   t   bytesReceivedt
   bytesTotalRe   (    (    s    code\common\HttpRequestDialog.pyRt   P  s,    

c         C   s[   t  j |  d |  j j   t  j t  j Bt  j  } | t  j k rM |  j   n
 |  j   d  S(   Ns   Internet connection error(   RY   RZ   R   t   errorStringR]   R^   RP   R.   (   R>   t   networkErrorCodeRb   (    (    s    code\common\HttpRequestDialog.pyRv   y  s    	c         C   sP   |  j  d  k	 rB |  j  j   d  |  _  |  j   sB |  j   d  Sn  |  j   d  S(   N(   R   R   RL   Rj   R.   RI   (   R>   (    (    s    code\common\HttpRequestDialog.pyRx     s    	
c         C   s\   |  j  d  k	 r2 |  j  j   d  |  _  |  j   n  t j |  j   |  j j   Wd  QXd  S(   N(   R   R   RL   Ri   R   RM   R   RN   (   R>   (    (    s    code\common\HttpRequestDialog.pyR:     s    	N(   t   __name__t
   __module__t   __doc__R   R   RF   RG   RK   RP   Rc   Rf   Rj   Ri   R=   Rt   Rv   Rx   R:   (    (    (    s    code\common\HttpRequestDialog.pyR   "   s   O			
		2		!			)		(   RR   t   os.pathRg   t   PyQt4.QtCoret   PyQt4.QtGuit   PyQt4.QtNetworkt   common.utilsR   R   t   common.StatusLabelWidgett   __all__R   R   (    (    (    s    code\common\HttpRequestDialog.pyt   <module>   s   



	