a
    {b                     @   s   d Z ddlmZmZmZmZmZ ddlmZm	Z	 ddlm
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G d	d
 d
eZG dd deZedkree d  dS )a  
This class provides a Background Transfer Service for iOS. It requires iOS 8 or
later.

Note that downloading from non-https sources requires modifying the Info.plist
file.

http://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi

Note that on iOS 9 and above, the 'NSExceptionAllowsInsecureHTTPLoads' key
also seems to be required and set to true to allow HTTP downloads.
    )	autoclassprotocolobjc_strselectorobjc_b)
make_dylib
load_dylib)load_frameworkINCLUDE)AppLabel)Logger)ObjectPropertyc                   @   s   e Zd ZdZe Zdd ZdS )TestAppz) Our test app for background downloading c                 C   s
   t ddS )Nz-Background Transfer Demo.
For iOS 8 or later.)textr   self r   /Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/../../../share/pyobjus-examples/background_transfer.pybuild   s    zTestApp.buildN)__name__
__module____qualname____doc__r   bg_transferr   r   r   r   r   r      s   r   c                       sx   e Zd ZdZedZ fddZdd Zdd Ze	d	d
d Z
e	d	dd Ze	d	dd Ze	ddd Z  ZS )BackgroundTransferz=
    Main worker class for handling background transfers
    zKivy Background Transferc                    s\   t t|   ttj td}|| j| _	td}|
| j	| d | _d | _| d d S )NZNSURLSessionConfigurationZNSURLSessionz-http://kivy.org/logos/kivy-logo-black-256.png)superr   __init__r	   r
   Z
Foundationr   Z-backgroundSessionConfigurationWithIdentifier_
identifierconfigZ0sessionWithConfiguration_delegate_delegateQueue_sessiontaskdownload_file)r   Z	ns_configr!   	__class__r   r   r   &   s    
zBackgroundTransfer.__init__c                 C   s2   t d}|t|}| j|| _| j  dS )z= Download the specified file in place it in the destination. NSURLN)r   ZURLWithString_r   r!   ZdownloadTaskWithURL_r"   resume)r   urlr&   Zoc_urlr   r   r   r#   9   s    z BackgroundTransfer.download_filec                 C   s   | j   dS )a   Close the session. This is required to prevent memory leaks after
        all the downloads have completed.

        https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instm/NSURLSession/downloadTaskWithURL:
        N)r!   ZfinishTasksAndInvalidater   r   r   r   close_sessionB   s    z BackgroundTransfer.close_sessionZNSURLSessionDownloadDelegatec                 G   s   t d| d S )Nzbackground_transfer.py: Protocol method URLSession_downloadTask_didWriteData_totalBytesWritten_totalBytesExpectedToWrite_ with {0}r   infoformatr   argsr   r   r   QURLSession_downloadTask_didWriteData_totalBytesWritten_totalBytesExpectedToWrite_J   s
    zdBackgroundTransfer.URLSession_downloadTask_didWriteData_totalBytesWritten_totalBytesExpectedToWrite_c                 G   sB   t d| t|dkr6|d }t d|j |   d S )Nzcbackground_transfer.py: Protocol method URLSession_downloadTask_didFinishDownloadingToURL_ with {0}   zJDownloaded file is {0}.
You need to move this before the function returns.)r   r+   r,   lenZfileSystemRepresentationr)   )r   r.   Zns_urlr   r   r   2URLSession_downloadTask_didFinishDownloadingToURL_Q   s    zEBackgroundTransfer.URLSession_downloadTask_didFinishDownloadingToURL_c                 G   s   t d| d S )Nznbackground_transfer.py: Protocol method URLSession_downloadTask_didResumeAtOffset_expectedTotalBytes_ with {0}r*   r-   r   r   r   =URLSession_downloadTask_didResumeAtOffset_expectedTotalBytes_^   s
    zPBackgroundTransfer.URLSession_downloadTask_didResumeAtOffset_expectedTotalBytes_ZNSURLSessionTaskDelegatec                 G   sH   t d| t|dkrD|d }|durDt d|   dS )z
        Although not technically part of the required delegate class, this
        delegate catches errors preventing the main delegate from functioning.
        zUbackground_transfer.py: Protocol method URLSession_task_didCompleteWithError_with {0}r0   Nzbackground_transfer: Error {})r   r+   r,   r1   descriptionZcString)r   r.   Zns_errr   r   r   %URLSession_task_didCompleteWithError_f   s    
z8BackgroundTransfer.URLSession_task_didCompleteWithError_)r   r   r   r   r   r   r   r#   r)   r   r/   r2   r3   r5   __classcell__r   r   r$   r   r       s   	


r   __main__)r   N)r   Zpyobjusr   r   r   r   r   Zpyobjus.dylib_managerr   r   r	   r
   Zkivy.appr   Zkivy.uix.labelr   Zkivy.loggerr   Zkivy.propertiesr   r   objectr   r   runr   r   r   r   <module>   s   W