private void FixBreadCrumbForCurrentFolder(ODItem folder)
        {
            var  breadcrumbs   = flowLayoutPanelBreadcrumb.Controls;
            bool existingCrumb = false;

            foreach (LinkLabel crumb in breadcrumbs)
            {
                if (crumb.Tag == folder)
                {
                    RemoveDeeperBreadcrumbs(crumb);
                    existingCrumb = true;
                    break;
                }
            }

            if (!existingCrumb)
            {
                LinkLabel label = new LinkLabel();
                label.Text         = "> " + folder.Name;
                label.LinkArea     = new LinkArea(2, folder.Name.Length);
                label.LinkClicked += linkLabelBreadcrumb_LinkClicked;
                label.AutoSize     = true;
                label.Tag          = folder;
                flowLayoutPanelBreadcrumb.Controls.Add(label);
            }
        }
        /// <summary>
        /// Fetches the files in a folder.
        /// </summary>
        public static async Task <IEnumerable <ODItem> > GetFiles(ODItem parentItem)
        {
            await EnsureConnection();

            List <ODItem> result = new List <ODItem>();
            var           page   = await parentItem.PagedChildrenCollectionAsync(Connection, ChildrenRetrievalOptions.Default);

            var list = from f in page.Collection
                       where f.File != null
                       select f;

            result.AddRange(list);

            while (page.MoreItemsAvailable())
            {
                list = from f in page.Collection
                       where f.File != null
                       select f;

                result.AddRange(list);

                page = await page.GetNextPage(Connection);
            }

            return(result);
        }
Exemplo n.º 3
0
        private async void renameSelectedItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var itemToRename = this.SelectedItem;

            FormInputDialog dialog = new FormInputDialog("Rename selected item", "New item name");

            dialog.InputText = itemToRename.Name;

            var result = dialog.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            ODItem changes = new ODItem {
                Name = dialog.InputText
            };

            try
            {
                var renamedItem = await Connection.PatchItemAsync(itemToRename.ItemReference(), changes);

                UpdateItemInFolderContents(itemToRename, renamedItem);
            }
            catch (ODException ex)
            {
                PresentOneDriveException(ex);
            }
        }
 public static async Task <bool> DeleteFile(ODItem itemToDelete, string itemName)
 {
     EnsureConnection();
     return
         (await
          Connection.DeleteItemAsync(itemToDelete.ItemReference(), ItemDeleteOptions.Default));
 }
        /// <summary>
        /// Creates (if necessary) and returns a Working folder.
        /// </summary>
        public static async Task <ODItem> SetWorkingFolder(string folderName)
        {
            await EnsureConnection();

            // Connect to OneDrive root.
            var rootFolder = await Connection.GetRootItemAsync(ItemRetrievalOptions.DefaultWithChildren);

            // Search working folder
            var existingItem = (from f in rootFolder.Children
                                where f.Name == folderName
                                select f).FirstOrDefault();

            if (existingItem != null)
            {
                if (existingItem.Folder == null)
                {
                    // There is already a file with this name.
                    throw new Exception("There is already a file with this name.");
                }
                else
                {
                    // The folder already exists.
                    WorkingFolder = existingItem;
                    return(WorkingFolder);
                }
            }

            var newFolder = await Connection.CreateFolderAsync(rootFolder.ItemReference(), folderName);

            WorkingFolder = newFolder;
            return(WorkingFolder);
        }
Exemplo n.º 6
0
        public async Task <ODItem> UploadFileStream()
        {
            long currentPosition = 0;

            ODItem uploadedItem = null;

            byte[] fragmentBuffer = new byte[UploadOptions.FragmentSize];
            while (currentPosition < DataSource.Length)
            {
                long endPosition = currentPosition + UploadOptions.FragmentSize;
                if (endPosition > DataSource.Length)
                {
                    endPosition = DataSource.Length;
                }
                await DataSource.ReadAsync(fragmentBuffer, 0, (int)Math.Max(0, endPosition - currentPosition));

                var response = await ExecuteUploadFragment(currentPosition, Math.Max(0, endPosition - 1), fragmentBuffer);

                if (response is ODUploadSession)
                {
                    UploadSession.ApplySessionDelta((ODUploadSession)response);
                }
                else if (response is ODItem)
                {
                    uploadedItem = (ODItem)response;
                }
                UpdateProgress(endPosition);
                currentPosition = endPosition;
            }

            return(uploadedItem);
        }
 public static async Task <ODItem> ModifyFile(ODItem parentItem, string itemName, Stream itemContentStream)
 {
     EnsureConnection();
     return
         (await
          Connection.PutNewFileToParentItemAsync(parentItem.ItemReference(), itemName, itemContentStream,
                                                 ItemUploadOptions.Default));
 }
        public static async Task <List <ODItem> > GetODItemsFromAppFolder()
        {
            EnsureConnection();
            AppFolder =
                await Connection.GetItemAsync(AppFolder.ItemReference(), ItemRetrievalOptions.DefaultWithChildren);

            var itemsToReturn = AppFolder.Children.ToList();

            return(itemsToReturn);
        }
 private static ODItem FindItem(string itemName, ODItem root)
 {
     if (root.Children != null)
     {
         var existingItem = (from f in root.Children
                             where f.Name == itemName
                             select f).FirstOrDefault();
         return(existingItem);
     }
     return(null);
 }
Exemplo n.º 10
0
        private Control CreateControlForChildObject(ODItem item)
        {
            OneDriveTile tile = new OneDriveTile {
                SourceItem = item, Connection = this.Connection
            };

            tile.Click       += ChildObject_Click;
            tile.DoubleClick += ChildObject_DoubleClick;
            tile.Name         = item.Id;
            return(tile);
        }
Exemplo n.º 11
0
 private void UpdateItemInFolderContents(ODItem originalItem, ODItem latestItem)
 {
     foreach (OneDriveTile tile in flowLayoutContents.Controls)
     {
         if (tile.SourceItem == originalItem)
         {
             tile.SourceItem = latestItem;
             tile.Name       = latestItem.Id;
         }
     }
 }
Exemplo n.º 12
0
        /// <summary>
        ///     Creates folder if none exists else returns the folder
        /// </summary>
        public static async Task <ODItem> GetOrCreateFolder(ODItem itemToGet, string name, ODItem rootItem)
        {
            if (itemToGet != null)
            {
                if (itemToGet.Folder == null)
                {
                    throw new Exception("A file with the same name as the specified app folder already exists.");
                }
                return(itemToGet);
            }
            var newFolder = await Connection.CreateFolderAsync(rootItem.ItemReference(), name);

            return(newFolder);
        }
Exemplo n.º 13
0
        private void linkLabelBreadcrumb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel link = (LinkLabel)sender;

            RemoveDeeperBreadcrumbs(link);

            ODItem item = link.Tag as ODItem;

            if (null == item)
            {
                Task t = LoadFolderFromId("root");
            }
            else
            {
                Task t = LoadFolderFromId(item.Id);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Checks whether the main app folder and the working folder exists and if it does returns the working folder
        /// else creates the folders and returns them.
        /// </summary>
        /// <returns>
        /// App Folder
        /// </returns>
        public static async Task GetAppFolder()
        {
            EnsureConnection();
            await GetRootFiles();

            var appsFolder = FindItem(AppsFolderName, RootFiles);

            if (appsFolder == null)
            {
                await GetOrCreateFolder(appsFolder, AppsFolderName, RootFiles);
                await GetRootFiles();
            }

            var appFolder = FindItem(AppFolderName, appsFolder);

            await GetOrCreateFolder(appFolder, AppFolderName, appsFolder);

            AppFolder = appFolder;
        }
Exemplo n.º 15
0
        private async Task DownloadAndSaveItem(ODItem item)
        {
            var dialog = new SaveFileDialog();

            dialog.FileName = item.Name;
            dialog.Filter   = "All Files (*.*)|*.*";
            var result = dialog.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            var stream = await item.GetContentStreamAsync(Connection, StreamDownloadOptions.Default);

            using (var outputStream = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create))
            {
                await stream.CopyToAsync(outputStream);
            }
        }
Exemplo n.º 16
0
        private async void ProcessFolder(ODItem folder)
        {
            if (null != folder)
            {
                this.CurrentFolder = folder;

                LoadProperties(folder);

                ODItemCollection pagedItemCollection = await folder.PagedChildrenCollectionAsync(Connection, ChildrenRetrievalOptions.DefaultWithThumbnails);

                LoadChildren(pagedItemCollection, false);

                while (pagedItemCollection.MoreItemsAvailable())
                {
                    pagedItemCollection = await pagedItemCollection.GetNextPage(Connection);

                    LoadChildren(pagedItemCollection, false);
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        ///     Checks whether the main app folder and the working folder exists and if it does returns the working folder
        ///     else creates the folders and returns them.
        /// </summary>
        /// <returns>
        ///     App Folder
        /// </returns>
        public static async Task GetAppFolder()
        {
            EnsureConnection();
            await GetRootFiles();

            var appsFolder = FindItem(AppsFolderName, RootFiles);

            if (appsFolder == null)
            {
                await GetOrCreateFolder(appsFolder, AppsFolderName, RootFiles);
                await GetRootFiles();
            }
            appsFolder =
                await Connection.GetItemAsync(appsFolder.ItemReference(), ItemRetrievalOptions.DefaultWithChildren);

            var appFolder = FindItem(AppFolderName, appsFolder);

            await GetOrCreateFolder(appFolder, AppFolderName, appsFolder);

            AppFolder = appFolder;
        }
Exemplo n.º 18
0
 public static FileSystemInfoContract ToFileSystemInfoContract(this ODItem item) => item.CanHaveChildren()
         ? new DirectoryInfoContract(item.Id, item.Name, item.CreatedDateTime, item.LastModifiedDateTime) as FileSystemInfoContract
         : new FileInfoContract(item.Id, item.Name, item.CreatedDateTime, item.LastModifiedDateTime, (FileSize)item.Size, item.File.Hashes.Sha1.ToLowerInvariant()) as FileSystemInfoContract;
Exemplo n.º 19
0
 private void RemoveItemFromFolderContents(ODItem itemToDelete)
 {
     flowLayoutContents.Controls.RemoveByKey(itemToDelete.Id);
 }
Exemplo n.º 20
0
 private void AddItemToFolderContents(ODItem obj)
 {
     flowLayoutContents.Controls.Add(CreateControlForChildObject(obj));
 }
Exemplo n.º 21
0
 private async void NavigateToItemWithChildren(ODItem folder)
 {
     FixBreadCrumbForCurrentFolder(folder);
     await LoadFolderFromId(folder.Id);
 }
Exemplo n.º 22
0
 private void LoadProperties(ODItem item)
 {
     SelectedItem = item;
     oneDriveObjectBrowser1.SelectedItem = item;
 }
        /// <summary>
        /// Deletes a file or a folder.
        /// </summary>
        public static async Task <bool> DeleteItem(ODItem item)
        {
            await EnsureConnection();

            return(await Connection.DeleteItemAsync(item.ItemReference(), ItemDeleteOptions.Default));
        }
        /// <summary>
        /// Saves a file in the specified folder.
        /// </summary>
        public static async Task <ODItem> SaveFile(ODItem parentItem, string filename, Stream fileContent)
        {
            await EnsureConnection();

            return(await Connection.PutNewFileToParentItemAsync(parentItem.ItemReference(), filename, fileContent, ItemUploadOptions.Default));
        }
        /// <summary>
        /// Downloads a file.
        /// </summary>
        public static async Task <Stream> DownloadFile(ODItem item)
        {
            await EnsureConnection();

            return(await item.GetContentStreamAsync(Connection, StreamDownloadOptions.Default));
        }