コード例 #1
0
        /// <summary>
        /// Create a <see cref="TransferObjectModel"/> from a <see cref="MTransfer"/>.
        /// </summary>
        /// <param name="transfer"></param>
        /// <returns>The new <see cref="TransferObjectModel"/></returns>
        public static TransferObjectModel CreateTransferObjectModel(MTransfer transfer)
        {
            if (transfer == null)
            {
                return(null);
            }

            try
            {
                TransferObjectModel megaTransfer = null;

                switch (transfer.getType())
                {
                case MTransferType.TYPE_DOWNLOAD:
                    MNode node = transfer.getPublicMegaNode() ??                               // If is a public node
                                 SdkService.MegaSdk.getNodeByHandle(transfer.getNodeHandle()); // If not

                    if (node == null)
                    {
                        return(null);
                    }

                    megaTransfer = new TransferObjectModel(SdkService.MegaSdk,
                                                           NodeService.CreateNew(SdkService.MegaSdk, App.AppInformation, node, ContainerType.CloudDrive),
                                                           MTransferType.TYPE_DOWNLOAD, transfer.getPath());
                    break;

                case MTransferType.TYPE_UPLOAD:
                    var parentNode = SdkService.MegaSdk.getNodeByHandle(transfer.getParentHandle());

                    if (parentNode == null)
                    {
                        return(null);
                    }

                    megaTransfer = new TransferObjectModel(SdkService.MegaSdk,
                                                           NodeService.CreateNew(SdkService.MegaSdk, App.AppInformation, parentNode, ContainerType.CloudDrive),
                                                           MTransferType.TYPE_UPLOAD, transfer.getPath());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (megaTransfer != null)
                {
                    GetTransferAppData(transfer, megaTransfer);

                    megaTransfer.Transfer          = transfer;
                    megaTransfer.TransferState     = transfer.getState();
                    megaTransfer.TransferPriority  = transfer.getPriority();
                    megaTransfer.IsBusy            = false;
                    megaTransfer.TotalBytes        = transfer.getTotalBytes();
                    megaTransfer.TransferedBytes   = transfer.getTransferredBytes();
                    megaTransfer.TransferSpeed     = string.Empty;
                    megaTransfer.TransferMeanSpeed = 0;

                    megaTransfer.TransferState = !SdkService.MegaSdk.areTransfersPaused((int)transfer.getType())
                        ? MTransferState.STATE_QUEUED : MTransferState.STATE_PAUSED;
                }

                return(megaTransfer);
            }
            catch (Exception) { return(null); }
        }