/// <summary> /// Update the currently selected item by appending new text. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <param name="fileText">The updated text contents of the file.</param> /// <returns>A Boolean value that indicates whether the text file was successfully updated.</returns> internal async Task <bool> UpdateTextFileAsync(FileSystemItemViewModel _selectedFileObject, string fileText) { File file; byte[] byteArray; bool isSuccess = false; try { // Get a handle on the selected item. IItem myFile = _selectedFileObject.FileSystemItem; file = myFile as File; string updateTime = "\n\r\n\rLast update at " + DateTime.Now.ToLocalTime().ToString(); byteArray = Encoding.UTF8.GetBytes(fileText + updateTime); using (MemoryStream stream = new MemoryStream(byteArray)) { // Update the file. This results in a call to the service. await file.UploadAsync(stream); isSuccess = true; // We've updated the file. } } catch (ArgumentException) { isSuccess = false; } return(isSuccess); }
/// <summary> /// Performs a search of the default Documents folder. Displays the first page of results. /// </summary> /// <returns>A collection of information that describes files and folders.</returns> internal async Task <List <FileSystemItemViewModel> > GetMyFilesAsync() { var fileResults = new List <FileSystemItemViewModel>(); try { var restURL = string.Format("{0}me/drive/root/children", AuthenticationHelper.ResourceBetaUrl); string responseString = await AuthenticationHelper.GetJsonAsync(restURL); if (responseString != null) { var jsonresult = JObject.Parse(responseString); foreach (var item in jsonresult["value"]) { FileSystemItemViewModel fileItemModel = new FileSystemItemViewModel(); fileItemModel.Name = !string.IsNullOrEmpty(item["name"].ToString()) ? item["name"].ToString() : string.Empty; fileItemModel.LastModifiedBy = !string.IsNullOrEmpty(item["lastModifiedBy"]["user"]["displayName"].ToString()) ? item["lastModifiedBy"]["user"]["displayName"].ToString() : string.Empty; fileItemModel.LastModifiedDateTime = !string.IsNullOrEmpty(item["lastModifiedDateTime"].ToString()) ? DateTime.Parse(item["lastModifiedDateTime"].ToString()) : new DateTime(); fileItemModel.Id = !string.IsNullOrEmpty(item["id"].ToString()) ? item["id"].ToString() : string.Empty; fileItemModel.Folder = item["folder"] != null ? item["folder"].ToString() : string.Empty; fileResults.Add(fileItemModel); } } } catch (Exception el) { el.ToString(); } return(fileResults.OrderBy(e => e.Name).ToList()); }
/// <summary> /// Performs a search of the default Documents folder. Displays the first page of results. /// </summary> /// <returns>A collection of information that describes files and folders.</returns> internal async Task <List <FileSystemItemViewModel> > GetMyFilesAsync() { var fileResults = new List <FileSystemItemViewModel>(); try { var graphClient = await AuthenticationHelper.GetGraphServiceClientAsync(); var driveItems = await graphClient.Me.Drive.Root.Children.Request().GetAsync(); foreach (var item in driveItems) { FileSystemItemViewModel fileItemModel = new FileSystemItemViewModel(); fileItemModel.Name = item.Name; fileItemModel.LastModifiedBy = item.LastModifiedBy.User.DisplayName; fileItemModel.LastModifiedDateTime = item.LastModifiedDateTime.GetValueOrDefault(new DateTime()); fileItemModel.Id = item.Id; fileItemModel.Folder = item.Folder != null?item.Folder.ToString() : string.Empty; fileResults.Add(fileItemModel); } } catch (Exception el) { el.ToString(); } return(fileResults.OrderBy(e => e.Name).ToList()); }
/// <summary> /// Deletes the selected item or folder from the ListBox. /// </summary> /// <returns>A Boolean value that indicates whether the file or folder was successfully deleted.</returns> internal async Task <bool?> DeleteFileOrFolderAsync(FileSystemItemViewModel _selectedFileObject) { bool?isSuccess = false; try { // Gets the FileSystemItem that is selected in the bound ListBox control. IItem fileOrFolderToDelete = _selectedFileObject.FileSystemItem; // This results in a call to the service. await fileOrFolderToDelete.DeleteAsync(); isSuccess = true; } catch (Microsoft.Data.OData.ODataErrorException) { isSuccess = null; } catch (NullReferenceException) { isSuccess = null; } return(isSuccess); }
/// <summary> /// Downloads a file selected in the ListBox control. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <returns>A Stream of the downloaded file.</returns> internal async Task <Stream> DownloadFileAsync(FileSystemItemViewModel _selectedFileObject) { File file; Stream stream = null; try { // Get a handle on the selected item. IItem myFile = _selectedFileObject.FileSystemItem; file = myFile as File; // Download the file from the service. This results in call to the service. stream = await file.DownloadAsync(); } catch (NullReferenceException) { // Silently fail. A null stream will be handled higher up the stack. } return(stream); }
private static ContextMenu BuildNewContextMenu(FileSystemItemViewModel item) { var contextMenu = new ContextMenu(); contextMenu.Items.Add(new MenuItem { Header = "Open", CommandParameter = item, Command = new DelegateCommand <FileSystemItemViewModel>(i => i.Open()) }); var openWith = new MenuItem { Header = "Open With" }; openWith.Items.Add(new MenuItem { Header = "App 1..." }); contextMenu.Items.Add(openWith); // Move to trash contextMenu.Items.Add(new MenuItem { Header = "Move to Trash", CommandParameter = item, Command = new DelegateCommand <FileSystemItemViewModel>(i => i.MoveToTrash()) }); // Others contextMenu.Items.Add(new Separator()); contextMenu.Items.Add(new MenuItem { Header = "Get Info" }); contextMenu.Items.Add(new MenuItem { Header = "Rename" }); contextMenu.Items.Add(new MenuItem { Header = $"Compress \"{item.DisplayName}\"" }); contextMenu.Items.Add(new MenuItem { Header = "Duplicate" }); contextMenu.Items.Add(new MenuItem { Header = "Make Alias" }); return(contextMenu); }
/// <summary> /// Reads the contents of a text file and displays the results in a TextBox. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <returns>A Boolean value that indicates whether the text file was successfully read.</returns> internal async Task <object[]> ReadTextFileAsync(FileSystemItemViewModel _selectedFileObject) { string fileContents = string.Empty; object[] results = new object[] { fileContents, false }; try { // Get a handle on the selected item. IItem myFile = _selectedFileObject.FileSystemItem; // Check that the selected item is a text-based file. if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml")) { results[0] = string.Empty; results[1] = false; return(results); } File file = myFile as File; // Download the file contents as a string. This results in a call to the service. using (Stream stream = await file.DownloadAsync()) { using (StreamReader reader = new StreamReader(stream)) { results[0] = await reader.ReadToEndAsync(); results[1] = true; } } } catch (NullReferenceException) { results[1] = false; } catch (ArgumentException) { results[1] = false; } return(results); }
public static FolderViewModel CreateFolderViewModel(Folder folder) { List <FileSystemItemViewModel> fileSystemItems = new List <FileSystemItemViewModel>(); foreach (var item in folder.FileSystemItems) { FileSystemItemViewModel file = new FileSystemItemViewModel { Id = item.Id, Name = item.Name, FileSystemItemType = item.FileSystemItemType }; fileSystemItems.Add(file); } FolderViewModel folderViewModel = new FolderViewModel { ParentId = folder.ParentId, FullName = folder.FullName, NumberOfFiles = folder.NumberOfFiles, FileSystemItems = fileSystemItems.OrderBy(i => i.FileSystemItemType).ThenBy(i => i.Name).ToList() }; return(folderViewModel); }
private void PushPane(FileSystemItemViewModel item) { IFileSystemPane pane; switch (item) { case DirectoryViewModel directory: // Open directory listing pane var directoryPane = new DirectoryListingPane(directory); // Subscribe to events directoryPane.MouseDoubleClick += DirectoryListingPane_MouseDoubleClick; directoryPane.PreviewKeyDown += DirectoryListingPane_PreviewKeyDown; directoryPane.KeyDown += DirectoryListingPane_KeyDown; directoryPane.SelectionChanged += DirectoryListingPane_SelectionChanged; pane = directoryPane; // Add a breadcrumb if (_panes.Count > 0) { StackPanelBreadcrumbs.Children.Add(new TextBlock { Text = ">", Height = 18, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(4, 0, 4, 0) }); } var breadcrumbIcon = new Image { Source = directory.Icon, Width = 18, Height = 18, // Smaller than the icons in the title bar and directory listings VerticalAlignment = VerticalAlignment.Center }; StackPanelBreadcrumbs.Children.Add(breadcrumbIcon); var breadcrumbTextBlock = new TextBlock { Text = directory.DisplayName, // TODO: Use same ellipsis as in directory listing item Height = 18, Margin = new Thickness(4, 0, 0, 0), VerticalAlignment = VerticalAlignment.Center }; StackPanelBreadcrumbs.Children.Add(breadcrumbTextBlock); break; case FileViewModel file: // Open file preview pane pane = new FileInfoPane(file); break; default: throw new NotSupportedException($"{item.GetType()} doesn't have a corresponding Pane"); } // Add a grid splitter for resizing if this isn't the first pane if (_panes.Count > 0) { AddGridSplitter(width: 5); } // Add the pane GridMain.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(260, GridUnitType.Pixel) // Panes' widths are in pixels, but resizable }); Grid.SetColumn((UIElement)pane, GridMain.ColumnDefinitions.Count - 1); // Set column position in the main grid _panes.Add(pane); // Add to stack GridMain.Children.Add((UIElement)pane); // Add to main grid // Scroll to the end horizontally ScrollViewerMain.ScrollToRightEnd(); }