Exemple #1
0
        public static void ContinueFolderOpenPicker(FolderPickerContinuationEventArgs args, FolderViewModel folderViewModel)
        {
            try
            {
                if (args == null || (args.ContinuationData["Operation"] as string) != "SelectDownloadFolder" || args.Folder == null)
                {
                    LogService.Log(MLogLevel.LOG_LEVEL_WARNING, "Error selecting the download destination folder");
                    ResetFolderPicker();
                    return;
                }

                if (!App.CloudDrive.IsUserOnline())
                {
                    return;
                }

                if (args.ContinuationData["NodeData"] != null)
                {
                    String        base64Handle = (String)args.ContinuationData["NodeData"];
                    NodeViewModel node;
                    if (App.LinkInformation.PublicNode != null && base64Handle.Equals(App.LinkInformation.PublicNode.getBase64Handle()))
                    {
                        node = NodeService.CreateNew(SdkService.MegaSdk, App.AppInformation, App.LinkInformation.PublicNode, ContainerType.PublicLink);
                        App.LinkInformation.Reset();
                    }
                    else
                    {
                        node = NodeService.CreateNew(folderViewModel.MegaSdk, App.AppInformation,
                                                     folderViewModel.MegaSdk.getNodeByBase64Handle(base64Handle), folderViewModel.Type);
                    }

                    if (node != null)
                    {
                        node.Download(TransfersService.MegaTransfers, args.Folder.Path);
                    }

                    ResetFolderPicker();
                    return;
                }

                folderViewModel.MultipleDownload(args.Folder.Path);
            }
            catch (Exception e)
            {
                LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Error preparing downloads", e);
                new CustomMessageDialog(AppMessages.AM_DownloadFailed_Title,
                                        AppMessages.AM_PrepareDownloadsFailed, App.AppInformation,
                                        MessageDialogButtons.Ok).ShowDialog();
            }
            finally
            {
                ResetFolderPicker();
            }
        }
        public static void ContinueFolderOpenPicker(FolderPickerContinuationEventArgs args, FolderViewModel folderViewModel)
        {
            if ((args.ContinuationData["Operation"] as string) != "SelectDownloadFolder" || args.Folder == null)
            {
                ResetFolderPicker();
                return;
            }

            if (!App.CloudDrive.IsUserOnline())
            {
                return;
            }

            if (args.ContinuationData["NodeData"] != null)
            {
                String        base64Handle = (String)args.ContinuationData["NodeData"];
                NodeViewModel node;
                if (App.LinkInformation.PublicNode != null && base64Handle.Equals(App.LinkInformation.PublicNode.getBase64Handle()))
                {
                    node = NodeService.CreateNew(App.MegaSdk, App.AppInformation, App.LinkInformation.PublicNode, ContainerType.PublicLink);
                    App.LinkInformation.Reset();
                }
                else
                {
                    node = NodeService.CreateNew(folderViewModel.MegaSdk, App.AppInformation,
                                                 folderViewModel.MegaSdk.getNodeByBase64Handle(base64Handle), folderViewModel.Type);
                }

                if (node != null)
                {
                    App.AppInformation.PickerOrAsyncDialogIsOpen = false;
                    node.Download(App.MegaTransfers, args.Folder.Path);
                }

                ResetFolderPicker();
                return;
            }

            App.AppInformation.PickerOrAsyncDialogIsOpen = false;

            folderViewModel.MultipleDownload(args.Folder.Path);

            ResetFolderPicker();
        }
        /// <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); }
        }
        /// <summary>
        /// Update the transfers list/queue.
        /// </summary>
        /// <param name="MegaTransfers">Transfers list/queue to update.</param>
        public static void UpdateMegaTransfersList(TransferQueu MegaTransfers)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MegaTransfers.Clear();
                MegaTransfers.Downloads.Clear();
                MegaTransfers.Uploads.Clear();
            });

            App.GlobalTransferListener.Transfers.Clear();

            // Get transfers and fill the transfers list again.
            var transfers    = App.MegaSdk.getTransfers();
            var numTransfers = transfers.size();

            for (int i = 0; i < numTransfers; i++)
            {
                var transfer = transfers.get(i);

                TransferObjectModel megaTransfer = null;
                if (transfer.getType() == MTransferType.TYPE_DOWNLOAD)
                {
                    // If is a public node
                    MNode node = transfer.getPublicMegaNode();
                    if (node == null) // If not
                    {
                        node = App.MegaSdk.getNodeByHandle(transfer.getNodeHandle());
                    }

                    if (node != null)
                    {
                        megaTransfer = new TransferObjectModel(App.MegaSdk,
                                                               NodeService.CreateNew(App.MegaSdk, App.AppInformation, node, ContainerType.CloudDrive),
                                                               TransferType.Download, transfer.getPath());
                    }
                }
                else
                {
                    megaTransfer = new TransferObjectModel(App.MegaSdk, App.MainPageViewModel.CloudDrive.FolderRootNode,
                                                           TransferType.Upload, transfer.getPath());
                }

                if (megaTransfer != null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        GetTransferAppData(transfer, megaTransfer);

                        megaTransfer.Transfer                      = transfer;
                        megaTransfer.Status                        = TransferStatus.Queued;
                        megaTransfer.CancelButtonState             = true;
                        megaTransfer.TransferButtonIcon            = new Uri("/Assets/Images/cancel transfers.Screen-WXGA.png", UriKind.Relative);
                        megaTransfer.TransferButtonForegroundColor = new SolidColorBrush(Colors.White);
                        megaTransfer.IsBusy                        = true;
                        megaTransfer.TotalBytes                    = transfer.getTotalBytes();
                        megaTransfer.TransferedBytes               = transfer.getTransferredBytes();
                        megaTransfer.TransferSpeed                 = transfer.getSpeed().ToStringAndSuffixPerSecond();

                        MegaTransfers.Add(megaTransfer);
                        App.GlobalTransferListener.Transfers.Add(megaTransfer);
                    });
                }
            }
        }