public void LoadNodes() { this.ChildNodes.Clear(); MNodeList nodeList = this._megaSdk.getChildren(this.CurrentRootNode.GetBaseNode()); for (int i = 0; i < nodeList.size(); i++) { ChildNodes.Add(new NodeViewModel(this._megaSdk, nodeList.get(i))); } }
public void GetContactSharedFolders() { // User must be online to perform this operation if (!IsUserOnline()) { return; } // First cancel any other loading task that is busy CancelLoad(); // Create the option to cancel CreateLoadCancelOption(); OnUiThread(() => InShares.ChildNodes.Clear()); MNodeList inSharesList = MegaSdk.getInShares(MegaSdk.getContact(_selectedContact.Email)); Task.Factory.StartNew(() => { try { Deployment.Current.Dispatcher.BeginInvoke(() => { for (int i = 0; i < inSharesList.size(); i++) { // If the task has been cancelled, stop processing if (LoadingCancelToken.IsCancellationRequested) { LoadingCancelToken.ThrowIfCancellationRequested(); } // To avoid null values if (inSharesList.get(i) == null) { continue; } var _inSharedFolder = NodeService.CreateNew(this.MegaSdk, this.AppInformation, inSharesList.get(i), ContainerType.InShares, InShares.ChildNodes); _inSharedFolder.DefaultImagePathData = VisualResources.FolderTypePath_shared; InShares.ChildNodes.Add(_inSharedFolder); } OnPropertyChanged("NumberOfInSharedFolders"); OnPropertyChanged("NumberOfInSharedFoldersText"); }); } catch (OperationCanceledException) { // Do nothing. Just exit this background process because a cancellation exception has been thrown } }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current); }
private async Task RecursiveSaveForOffline(String sfoPath, NodeViewModel node) { if (!FolderService.FolderExists(sfoPath)) { FolderService.CreateFolder(sfoPath); } String newSfoPath = Path.Combine(sfoPath, node.Name); if (!FolderService.FolderExists(newSfoPath)) { FolderService.CreateFolder(newSfoPath); } if (!SavedForOffline.ExistsNodeByLocalPath(newSfoPath)) { SavedForOffline.Insert(node.OriginalMNode, true); } else { SavedForOffline.UpdateNode(node.OriginalMNode, true); } MNodeList childList = MegaSdk.getChildren(node.OriginalMNode); for (int i = 0; i < childList.size(); i++) { // To avoid pass null values to CreateNew if (childList.get(i) == null) { continue; } var childNode = NodeService.CreateNew(this.MegaSdk, this.AppInformation, childList.get(i), this.ParentContainerType); // If node creation failed for some reason, continue with the rest and leave this one if (childNode == null) { continue; } if (childNode.IsFolder) { await RecursiveSaveForOffline(newSfoPath, childNode); } else { await SaveFileForOffline(newSfoPath, childNode); } } }
/// <summary> /// Get the account used space by the incoming shared folders /// </summary> /// <returns>Used space by the incoming shared folders</returns> private static ulong GetIncomingSharesUsedSpace() { MNodeList inSharesList = SdkService.MegaSdk.getInShares(); int inSharesListSize = inSharesList.size(); ulong inSharesUsedSpace = 0; for (int i = 0; i < inSharesListSize; i++) { MNode inShare = inSharesList.get(i); if (inShare != null) { inSharesUsedSpace += SdkService.MegaSdk.getSize(inShare); } } return(inSharesUsedSpace); }
public void onNodesUpdate(MegaSDK api, MNodeList nodes) { // Exit methods when node list is incorrect if (nodes == null || nodes.size() < 1) { return; } // Retrieve the listsize for performance reasons and store local int listSize = nodes.size(); for (int i = 0; i < listSize; i++) { try { // Get the specific node that has an update. If null exit the method // and process no notification MNode megaNode = nodes.get(i); if (megaNode == null) { return; } // Incoming shared folder if (megaNode.isInShare()) { if (megaNode.isRemoved()) // REMOVED Scenario { OnInSharedFolderRemoved(megaNode); } else // ADDED/UPDATED scenarios { OnInSharedFolderAdded(megaNode); } } // Outgoing shared folder - ADDED/UPDATED scenarios else if (megaNode.isOutShare()) { OnOutSharedFolderAdded(megaNode); } else { // Outgoing shared folder - REMOVED Scenario if (megaNode.hasChanged((int)MNodeChangeType.CHANGE_TYPE_OUTSHARE)) { OnOutSharedFolderRemoved(megaNode); } // Normal node if (megaNode.isRemoved()) // REMOVED Scenario { OnNodeRemoved(megaNode); } else // ADDED/UPDATED scenarions { OnNodeAdded(megaNode); } } } catch (Exception) { /* Dummy catch, suppress possible exception */ } } }
public void onNodesUpdate(MegaSDK api, MNodeList nodes) { // exit methods when node list is incorrect if (nodes == null || nodes.size() < 1) { return; } try { // Retrieve the listsize for performance reasons and store local int listSize = nodes.size(); for (int i = 0; i < listSize; i++) { bool isProcessed = false; // Get the specific node that has an update. If null exit the method // and process no notification MNode megaNode = nodes.get(i); if (megaNode == null) { return; } // PROCESS THE FOLDERS LISTENERS if (megaNode.isRemoved()) { // REMOVED Scenario foreach (var folder in Folders) { IMegaNode nodeToRemoveFromView = folder.ChildNodes.FirstOrDefault( node => node.Base64Handle.Equals(megaNode.getBase64Handle())); // If node is found in current view, process the remove action if (nodeToRemoveFromView != null) { // Needed because we are in a foreach loop to prevent the use of the wrong // local variable in the dispatcher code. var currentFolder = folder; Deployment.Current.Dispatcher.BeginInvoke(() => { try { currentFolder.ChildNodes.Remove(nodeToRemoveFromView); ((FolderNodeViewModel)currentFolder.FolderRootNode).SetFolderInfo(); } catch (Exception) { // Dummy catch, surpress possible exception } }); isProcessed = true; break; } } if (!isProcessed) { // REMOVED in subfolder scenario MNode parentNode = api.getParentNode(megaNode); if (parentNode != null) { foreach (var folder in Folders) { IMegaNode nodeToUpdateInView = folder.ChildNodes.FirstOrDefault( node => node.Base64Handle.Equals(parentNode.getBase64Handle())); // If parent folder is found, process the update action if (nodeToUpdateInView != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { try { nodeToUpdateInView.Update(parentNode, folder.Type); var folderNode = nodeToUpdateInView as FolderNodeViewModel; if (folderNode != null) { folderNode.SetFolderInfo(); } } catch (Exception) { // Dummy catch, surpress possible exception } }); } } } } } // UPDATE / ADDED scenarions else { // UPDATE Scenario // PROCESS THE SINGLE NODE(S) LISTENER(S) (NodeDetailsPage live updates) foreach (var node in Nodes) { if (megaNode.getBase64Handle() == node.getNodeBase64Handle()) { Deployment.Current.Dispatcher.BeginInvoke(() => node.updateNode(megaNode)); } } // Used in different scenario's MNode parentNode = api.getParentNode(megaNode); foreach (var folder in Folders) { IMegaNode nodeToUpdateInView = folder.ChildNodes.FirstOrDefault( node => node.Base64Handle.Equals(megaNode.getBase64Handle())); // If node is found, process the update action if (nodeToUpdateInView != null) { bool isMoved = !folder.FolderRootNode.Base64Handle.Equals(parentNode.getBase64Handle()); // Is node is move to different folder. Remove from current folder view if (isMoved) { // Needed because we are in a foreach loop to prevent the use of the wrong // local variable in the dispatcher code. var currentFolder = folder; Deployment.Current.Dispatcher.BeginInvoke(() => { try { currentFolder.ChildNodes.Remove(nodeToUpdateInView); ((FolderNodeViewModel)currentFolder.FolderRootNode).SetFolderInfo(); UpdateFolders(currentFolder); } catch (Exception) { // Dummy catch, surpress possible exception } }); } // Node is updated with new data. Update node in current view else { Deployment.Current.Dispatcher.BeginInvoke(() => { try { nodeToUpdateInView.Update(megaNode, folder.Type); } catch (Exception) { // Dummy catch, surpress possible exception } }); isProcessed = true; break; } } } // ADDED scenario if (parentNode != null && !isProcessed) { foreach (var folder in Folders) { bool isAddedInFolder = folder.FolderRootNode.Base64Handle.Equals(parentNode.getBase64Handle()); // If node is added in current folder, process the add action if (isAddedInFolder) { // Retrieve the index from the SDK // Substract -1 to get a valid list index int insertIndex = api.getIndex(megaNode, UiService.GetSortOrder(parentNode.getBase64Handle(), parentNode.getName())) - 1; // If the insert position is higher than the ChilNodes size insert in the last position if (insertIndex >= folder.ChildNodes.Count()) { // Needed because we are in a foreach loop to prevent the use of the wrong // local variable in the dispatcher code. var currentFolder = folder; Deployment.Current.Dispatcher.BeginInvoke(() => { try { currentFolder.ChildNodes.Add(NodeService.CreateNew(api, _appInformation, megaNode, currentFolder.Type)); ((FolderNodeViewModel)currentFolder.FolderRootNode).SetFolderInfo(); UpdateFolders(currentFolder); } catch (Exception) { // Dummy catch, surpress possible exception } }); } // Insert the node at a specific position else { // Insert position can never be less then zero // Replace negative index with first possible index zero if (insertIndex < 0) { insertIndex = 0; } // Needed because we are in a foreach loop to prevent the use of the wrong // local variable in the dispatcher code. var currentFolder = folder; Deployment.Current.Dispatcher.BeginInvoke(() => { try { currentFolder.ChildNodes.Insert(insertIndex, NodeService.CreateNew(api, _appInformation, megaNode, currentFolder.Type)); ((FolderNodeViewModel)currentFolder.FolderRootNode).SetFolderInfo(); UpdateFolders(currentFolder); } catch (Exception) { // Dummy catch, surpress possible exception } }); } break; } // ADDED in subfolder scenario IMegaNode nodeToUpdateInView = folder.ChildNodes.FirstOrDefault( node => node.Base64Handle.Equals(parentNode.getBase64Handle())); if (nodeToUpdateInView != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { try { nodeToUpdateInView.Update(parentNode, folder.Type); var folderNode = nodeToUpdateInView as FolderNodeViewModel; if (folderNode != null) { folderNode.SetFolderInfo(); } } catch (Exception) { // Dummy catch, surpress possible exception } }); break; } // Unconditional scenarios // Move/delete/add actions in subfolders var localFolder = folder; Deployment.Current.Dispatcher.BeginInvoke(() => { try { UpdateFolders(localFolder); } catch (Exception) { // Dummy catch, surpress possible exception } }); } } } } } catch (Exception) { // Dummy catch, surpress possible exception } }
private async Task <bool> RecursiveDownloadFolder(String downloadPath, NodeViewModel folderNode) { if (String.IsNullOrWhiteSpace(folderNode.Name)) { await new CustomMessageDialog(AppMessages.DownloadNodeFailed_Title, AppMessages.AM_DownloadNodeFailedNoErrorCode, this.AppInformation).ShowDialogAsync(); return(false); } // Check for illegal characters in the folder name if (FolderService.HasIllegalChars(folderNode.Name)) { await new CustomMessageDialog(AppMessages.DownloadNodeFailed_Title, String.Format(AppMessages.InvalidFolderNameOrPath, folderNode.Name), this.AppInformation).ShowDialogAsync(); return(false); } try { String newDownloadPath = Path.Combine(downloadPath, folderNode.Name); if (!await CheckDownloadPath(newDownloadPath)) { return(false); } MNodeList childList = MegaSdk.getChildren(folderNode.OriginalMNode); bool result = true; // Default value in case that the folder is empty for (int i = 0; i < childList.size(); i++) { // To avoid pass null values to CreateNew if (childList.get(i) == null) { continue; } var childNode = NodeService.CreateNew(this.MegaSdk, this.AppInformation, childList.get(i), ContainerType.CloudDrive); // If node creation failed for some reason, continue with the rest and leave this one if (childNode == null) { continue; } bool partialResult; if (childNode.IsFolder) { partialResult = await RecursiveDownloadFolder(newDownloadPath, childNode); } else { partialResult = await DownloadFile(newDownloadPath, childNode); } // Only change the global result if the partial result indicates an error if (!partialResult) { result = partialResult; } } return(result); } catch (Exception e) { OnUiThread(() => { new CustomMessageDialog(AppMessages.DownloadNodeFailed_Title, String.Format(AppMessages.DownloadNodeFailed, e.Message), App.AppInformation).ShowDialog(); }); return(false); } }
/// <summary> /// Gets the incoming shared folders and fills the list. /// </summary> public void GetIncomingSharedFolders() { // User must be online to perform this operation if (!IsUserOnline()) { return; } // First cancel any other loading task that is busy CancelLoad(); // Create the option to cancel CreateLoadCancelOption(); InShares.SetProgressIndication(true); // Process is started so we can set the empty content template to loading already InShares.SetEmptyContentTemplate(true); MNodeList inSharesList = MegaSdk.getInShares(); if (inSharesList == null) { OnUiThread(() => { new CustomMessageDialog( AppMessages.AM_LoadFailed_Title, AppMessages.AM_LoadInSharesFailed, App.AppInformation, MessageDialogButtons.Ok).ShowDialog(); InShares.SetEmptyContentTemplate(false); }); return; } OnUiThread(() => InShares.ChildNodes.Clear()); Task.Factory.StartNew(() => { try { int inSharesListSize = inSharesList.size(); for (int i = 0; i < inSharesListSize; i++) { // If the task has been cancelled, stop processing if (LoadingCancelToken.IsCancellationRequested) { LoadingCancelToken.ThrowIfCancellationRequested(); } MNode inSharedFolder = inSharesList.get(i); if (inSharedFolder != null) { var _inSharedFolder = NodeService.CreateNew(this.MegaSdk, this.AppInformation, inSharedFolder, ContainerType.InShares, InShares.ChildNodes); if (_inSharedFolder != null) { _inSharedFolder.DefaultImagePathData = VisualResources.FolderTypePath_shared; OnUiThread(() => InShares.ChildNodes.Add(_inSharedFolder)); } } } OnUiThread(() => { OnPropertyChanged("HasInSharedFolders"); OnPropertyChanged("NumberOfInSharedFolders"); OnPropertyChanged("NumberOfInSharedFoldersText"); }); } catch (OperationCanceledException) { // Do nothing. Just exit this background process because a cancellation exception has been thrown } finally { // Show the user that processing the childnodes is done InShares.SetProgressIndication(false); // Set empty content to folder instead of loading view InShares.SetEmptyContentTemplate(false); } }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current); }
protected async void GetIncomingSharedItems(MUser contact = null) { // User must be online to perform this operation if (!await IsUserOnlineAsync()) { return; } // First cancel any other loading task that is busy CancelLoad(); // Create the option to cancel CreateLoadCancelOption(); // Process is started so we can set the empty content template to loading already SetEmptyContent(true); await OnUiThreadAsync(() => this.ItemCollection.Clear()); MNodeList inSharedItems = (contact != null) ? SdkService.MegaSdk.getInShares(contact) : SdkService.MegaSdk.getInShares(); await Task.Factory.StartNew(() => { try { var inSharedItemsListSize = inSharedItems.size(); for (int i = 0; i < inSharedItemsListSize; i++) { // If the task has been cancelled, stop processing if (LoadingCancelToken.IsCancellationRequested) { LoadingCancelToken.ThrowIfCancellationRequested(); } // To avoid null values if (inSharedItems.get(i) == null) { continue; } var node = NodeService.CreateNewSharedFolder(SdkService.MegaSdk, App.AppInformation, inSharedItems.get(i), this); // If node creation failed for some reason, continue with the rest and leave this one if (node == null) { continue; } OnUiThread(() => this.ItemCollection.Items.Add((IMegaSharedFolderNode)node)); } } catch (OperationCanceledException) { // Do nothing. Just exit this background process because a cancellation exception has been thrown } }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current); this.SortBy(this.CurrentOrder, this.ItemCollection.CurrentOrderDirection); OnItemCollectionChanged(this, EventArgs.Empty); SetEmptyContent(false); }
private void CreateChildren(MNodeList childList, int listSize) { // Set the parameters for the performance for the different view types of a folder int viewportItemCount, backgroundItemCount; InitializePerformanceParameters(out viewportItemCount, out backgroundItemCount); // We will not add nodes one by one in the dispatcher but in groups List <IMegaNode> helperList; try { helperList = new List <IMegaNode>(1024); } catch (ArgumentOutOfRangeException) { helperList = new List <IMegaNode>(); } for (int i = 0; i < listSize; i++) { // If the task has been cancelled, stop processing if (LoadingCancelToken.IsCancellationRequested) { LoadingCancelToken.ThrowIfCancellationRequested(); } // To avoid pass null values to CreateNew if (childList.get(i) == null) { continue; } var node = NodeService.CreateNew(this.MegaSdk, this.AppInformation, childList.get(i), this.Type, ChildNodes); // If node creation failed for some reason, continue with the rest and leave this one if (node == null) { continue; } // If the user is moving nodes, check if the node had been selected to move // and establish the corresponding display mode if (CurrentDisplayMode == DriveDisplayMode.CopyOrMoveItem) { // Check if it is the only focused node if ((FocusedNode != null) && (node.OriginalMNode.getBase64Handle() == FocusedNode.OriginalMNode.getBase64Handle())) { node.DisplayMode = NodeDisplayMode.SelectedForCopyOrMove; FocusedNode = node; } // Check if it is one of the multiple selected nodes IsSelectedNode(node); } helperList.Add(node); // First add the viewport items to show some data to the user will still loading if (i == viewportItemCount) { var waitHandleViewportNodes = new AutoResetEvent(false); Deployment.Current.Dispatcher.BeginInvoke(() => { // If the task has been cancelled, stop processing foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested)) { ChildNodes.Add(megaNode); } waitHandleViewportNodes.Set(); }); waitHandleViewportNodes.WaitOne(); helperList.Clear(); continue; } if (helperList.Count != backgroundItemCount || i <= viewportItemCount) { continue; } // Add the rest of the items in the background to the list var waitHandleBackgroundNodes = new AutoResetEvent(false); Deployment.Current.Dispatcher.BeginInvoke(() => { // If the task has been cancelled, stop processing foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested)) { ChildNodes.Add(megaNode); } waitHandleBackgroundNodes.Set(); }); waitHandleBackgroundNodes.WaitOne(); helperList.Clear(); } // Add any nodes that are left over var waitHandleRestNodes = new AutoResetEvent(false); Deployment.Current.Dispatcher.BeginInvoke(() => { // Show the user that processing the childnodes is done SetProgressIndication(false); // Set empty content to folder instead of loading view SetEmptyContentTemplate(false); // If the task has been cancelled, stop processing foreach (var megaNode in helperList.TakeWhile(megaNode => !LoadingCancelToken.IsCancellationRequested)) { ChildNodes.Add(megaNode); } waitHandleRestNodes.Set(); }); waitHandleRestNodes.WaitOne(); OnUiThread(() => OnPropertyChanged("HasChildNodesBinding")); }