<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using KoenZomers.OneDrive.Api;
using KoenZomers.OneDrive.Api.Entities;

namespace KoenZomersKeePassOneDriveSync.Forms
{
     public partial class OneDriveFilePickerDialog : Form
    {
        /// <summary>
        /// Instance of the OneDrive API that can be used to communicate with the cloud service
        /// </summary>
        private readonly OneDriveApi _oneDriveApi;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the My files tab
        /// </summary>
        private OneDriveItem CurrentMyOneDriveItem;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the Shared With Me tab
        /// </summary>
        private OneDriveItem CurrentSharedWithMeOneDriveItem;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the active tab
        /// </summary>
        public OneDriveItem CurrentOneDriveItem
        {
            get { return FilesTabControl.SelectedIndex == 0 ? CurrentMyOneDriveItem : CurrentSharedWithMeOneDriveItem; }
        }

        /// <summary>
        /// Gets or the filename in the textbox on the screen
        /// </summary>
        public string FileName
        {
            get { return FileNameTextBox.Text; }
            set { FileNameTextBox.Text = value; }
        }

        /// <summary>
        /// Gets or sets the text to explain the purpose of the dialog
        /// </summary>
        public string ExplanationText
        {
            get { return ExplanationLabel.Text; }
            set { ExplanationLabel.Text = value; }
        }

        /// <summary>
        /// Gets or sets if the user can enter a filename which does not exist yet on their OneDrive
        /// </summary>
        public bool AllowEnteringNewFileName
        {
            get { return FileNameTextBox.Enabled; }
            set { FileNameTextBox.Enabled = value; }
        }

        public OneDriveFilePickerDialog(OneDriveApi oneDriveApi)
        {
            InitializeComponent();

            _oneDriveApi = oneDriveApi;

            var sharedWithMeDisabled = (oneDriveApi is OneDriveForBusinessO365Api);

            SharedWithMePicker.Visible = !sharedWithMeDisabled;
            SharedWithMeUpButton.Visible = !sharedWithMeDisabled;
            SharedWithMeNotAvailableLabel.Visible = sharedWithMeDisabled;
        }

        /// <summary>
        /// Loads the folders in the "My files" view
        /// </summary>
        /// <param name="parentItemId">Folder to display the child folders and items of. If not provided or NULL, the root folder will be used.</param>
         public async Task LoadFolderItems(string parentItemId = null)
         {
             CloudLocationPicker.Items.Clear();

            // Check if there is a parent folder to display its children of
             OneDriveItemCollection itemCollection;
             if (parentItemId == null)
             {
                // No parent folder, get the items under the root
                itemCollection = await _oneDriveApi.GetDriveRootChildren();
                CloudLocationPath.Text = "/drive/root:";
                UpButton.Enabled = false;
                GoToRootToolStripMenuItem.Enabled = false;
                CurrentMyOneDriveItem = await _oneDriveApi.GetDriveRoot();
             }
             else
             {
                // Parent folder provided, get its children
                itemCollection = await _oneDriveApi.GetChildrenByFolderId(parentItemId);
                CurrentMyOneDriveItem = await _oneDriveApi.GetItemById(parentItemId);
                GoToRootToolStripMenuItem.Enabled = true;
                UpButton.Enabled = CurrentMyOneDriveItem.ParentReference != null;
                UpButton.Tag = CurrentMyOneDriveItem.ParentReference != null ? CurrentMyOneDriveItem.ParentReference.Id : null;
                CloudLocationPath.Text = CurrentMyOneDriveItem.ParentReference != null ? CurrentMyOneDriveItem.ParentReference.Path + "/" + CurrentMyOneDriveItem.Name : "";
             }

             foreach (var listViewItem in itemCollection.Collection.Select(oneDriveItem => new ListViewItem
             {
                 Text = oneDriveItem.Name,
                 Tag = oneDriveItem.RemoteItem != null ? oneDriveItem.RemoteItem.Id : oneDriveItem.Id,
                 ImageKey = oneDriveItem.Folder != null ? "Folder" : oneDriveItem.RemoteItem != null ? "RemoteFolder" : "File"
             }))
             {
                 CloudLocationPicker.Items.Add(listViewItem);
             }

            // Define if the OK button should be enabled
            FileNameTextBox_TextChanged(null, null);
        }

        /// <summary>
        /// Loads the items shared with the user in the "Shared with me" overview
        /// </summary>
        /// <param name="parentItem">OneDriveItem to display the child folders and items of. If not provided or NULL, the root folder will be used.</param>
        public async Task LoadSharedWithMeItems(OneDriveItem parentItem = null)
        {
            SharedWithMePicker.Items.Clear();

            if (parentItem == null)
            {
                // Get the root of the shared with me items
                var itemCollection = await _oneDriveApi.GetSharedWithMe();
                SharedWithMeUpButton.Enabled = false;
                CurrentSharedWithMeOneDriveItem = null;
                GoToSharedWithMeRootTtoolStripMenuItem.Enabled = false;
                SharedWithMePath.Text = string.Empty;

                foreach (var listViewItem in itemCollection.Collection.OrderBy(i => i.Name).OrderBy(i => i.Folder == null).OrderBy(i => i.RemoteItem.Folder == null).Select(oneDriveItem => new ListViewItem
                {
                    Text = oneDriveItem.Name,
                    Tag = oneDriveItem,
                    ImageKey = (oneDriveItem.RemoteItem != null && oneDriveItem.RemoteItem.Folder != null) || oneDriveItem.Folder != null ? "RemoteFolder" : "File"
                }))
                {
                    SharedWithMePicker.Items.Add(listViewItem);
                }
            }
            else
            {
                // Parent folder provided, get its children
                var itemCollection = await _oneDriveApi.GetAllChildrenFromDriveByFolderId(parentItem.RemoteItem != null ? parentItem.RemoteItem.ParentReference.DriveId : parentItem.ParentReference.DriveId, parentItem.RemoteItem != null ? parentItem.RemoteItem.ParentReference != null ? string.IsNullOrEmpty(parentItem.RemoteItem.ParentReference.Id) ? parentItem.RemoteItem.Id : parentItem.RemoteItem.ParentReference.Id : parentItem.RemoteItem.Id : parentItem.Id);

                SharedWithMeUpButton.Tag = parentItem.Name == "root" || parentItem.ParentReference.Id == null || (parentItem.ParentReference.Path != null && parentItem.ParentReference.Path.EndsWith("root:")) ? null : await _oneDriveApi.GetItemFromDriveById(parentItem.ParentReference.Id, parentItem.ParentReference.DriveId);
                CurrentSharedWithMeOneDriveItem = parentItem;
                GoToSharedWithMeRootTtoolStripMenuItem.Enabled = true;
                SharedWithMeUpButton.Enabled = CurrentSharedWithMeOneDriveItem.ParentReference != null;

                if (CurrentSharedWithMeOneDriveItem.ParentReference.Path != null)
                {
                    var rootLocation = CurrentSharedWithMeOneDriveItem.ParentReference.Path.IndexOf("root:");
                    SharedWithMePath.Text = ((rootLocation == -1 ? CurrentSharedWithMeOneDriveItem.ParentReference.Path : CurrentSharedWithMeOneDriveItem.ParentReference.Path.Remove(0, rootLocation + 5).TrimStart(new[] {'/'})) + "/" + CurrentSharedWithMeOneDriveItem.Name).TrimStart(new[] { '/' });
                }
                else
                {
                    SharedWithMePath.Text = CurrentSharedWithMeOneDriveItem.Name;
                }

                foreach (var listViewItem in itemCollection.Select(oneDriveItem => new ListViewItem
                {
                    Text = oneDriveItem.Name,
                    Tag = oneDriveItem,<commit_msg>Add more checking in LoadSharedWithMeItems to handle null ParentReference<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using KoenZomers.OneDrive.Api;
using KoenZomers.OneDrive.Api.Entities;

namespace KoenZomersKeePassOneDriveSync.Forms
{
     public partial class OneDriveFilePickerDialog : Form
    {
        /// <summary>
        /// Instance of the OneDrive API that can be used to communicate with the cloud service
        /// </summary>
        private readonly OneDriveApi _oneDriveApi;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the My files tab
        /// </summary>
        private OneDriveItem CurrentMyOneDriveItem;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the Shared With Me tab
        /// </summary>
        private OneDriveItem CurrentSharedWithMeOneDriveItem;

        /// <summary>
        /// Reference to the currently displayed folder on OneDrive for the active tab
        /// </summary>
        public OneDriveItem CurrentOneDriveItem
        {
            get { return FilesTabControl.SelectedIndex == 0 ? CurrentMyOneDriveItem : CurrentSharedWithMeOneDriveItem; }
        }

        /// <summary>
        /// Gets or the filename in the textbox on the screen
        /// </summary>
        public string FileName
        {
            get { return FileNameTextBox.Text; }
            set { FileNameTextBox.Text = value; }
        }

        /// <summary>
        /// Gets or sets the text to explain the purpose of the dialog
        /// </summary>
        public string ExplanationText
        {
            get { return ExplanationLabel.Text; }
            set { ExplanationLabel.Text = value; }
        }

        /// <summary>
        /// Gets or sets if the user can enter a filename which does not exist yet on their OneDrive
        /// </summary>
        public bool AllowEnteringNewFileName
        {
            get { return FileNameTextBox.Enabled; }
            set { FileNameTextBox.Enabled = value; }
        }

        public OneDriveFilePickerDialog(OneDriveApi oneDriveApi)
        {
            InitializeComponent();

            _oneDriveApi = oneDriveApi;

            var sharedWithMeDisabled = (oneDriveApi is OneDriveForBusinessO365Api);

            SharedWithMePicker.Visible = !sharedWithMeDisabled;
            SharedWithMeUpButton.Visible = !sharedWithMeDisabled;
            SharedWithMeNotAvailableLabel.Visible = sharedWithMeDisabled;
        }

        /// <summary>
        /// Loads the folders in the "My files" view
        /// </summary>
        /// <param name="parentItemId">Folder to display the child folders and items of. If not provided or NULL, the root folder will be used.</param>
         public async Task LoadFolderItems(string parentItemId = null)
         {
             CloudLocationPicker.Items.Clear();

            // Check if there is a parent folder to display its children of
             OneDriveItemCollection itemCollection;
             if (parentItemId == null)
             {
                // No parent folder, get the items under the root
                itemCollection = await _oneDriveApi.GetDriveRootChildren();
                CloudLocationPath.Text = "/drive/root:";
                UpButton.Enabled = false;
                GoToRootToolStripMenuItem.Enabled = false;
                CurrentMyOneDriveItem = await _oneDriveApi.GetDriveRoot();
             }
             else
             {
                // Parent folder provided, get its children
                itemCollection = await _oneDriveApi.GetChildrenByFolderId(parentItemId);
                CurrentMyOneDriveItem = await _oneDriveApi.GetItemById(parentItemId);
                GoToRootToolStripMenuItem.Enabled = true;
                UpButton.Enabled = CurrentMyOneDriveItem.ParentReference != null;
                UpButton.Tag = CurrentMyOneDriveItem.ParentReference != null ? CurrentMyOneDriveItem.ParentReference.Id : null;
                CloudLocationPath.Text = CurrentMyOneDriveItem.ParentReference != null ? CurrentMyOneDriveItem.ParentReference.Path + "/" + CurrentMyOneDriveItem.Name : "";
             }

             foreach (var listViewItem in itemCollection.Collection.Select(oneDriveItem => new ListViewItem
             {
                 Text = oneDriveItem.Name,
                 Tag = oneDriveItem.RemoteItem != null ? oneDriveItem.RemoteItem.Id : oneDriveItem.Id,
                 ImageKey = oneDriveItem.Folder != null ? "Folder" : oneDriveItem.RemoteItem != null ? "RemoteFolder" : "File"
             }))
             {
                 CloudLocationPicker.Items.Add(listViewItem);
             }

            // Define if the OK button should be enabled
            FileNameTextBox_TextChanged(null, null);
        }

        /// <summary>
        /// Loads the items shared with the user in the "Shared with me" overview
        /// </summary>
        /// <param name="parentItem">OneDriveItem to display the child folders and items of. If not provided or NULL, the root folder will be used.</param>
        public async Task LoadSharedWithMeItems(OneDriveItem parentItem = null)
        {
            SharedWithMePicker.Items.Clear();

            if (parentItem == null)
            {
                // Get the root of the shared with me items
                var itemCollection = await _oneDriveApi.GetSharedWithMe();
                SharedWithMeUpButton.Enabled = false;
                CurrentSharedWithMeOneDriveItem = null;
                GoToSharedWithMeRootTtoolStripMenuItem.Enabled = false;
                SharedWithMePath.Text = string.Empty;

                foreach (var listViewItem in itemCollection.Collection.OrderBy(i => i.Name).OrderBy(i => i.Folder == null).OrderBy(i => i.RemoteItem.Folder == null).Select(oneDriveItem => new ListViewItem
                {
                    Text = oneDriveItem.Name,
                    Tag = oneDriveItem,
                    ImageKey = (oneDriveItem.RemoteItem != null && oneDriveItem.RemoteItem.Folder != null) || oneDriveItem.Folder != null ? "RemoteFolder" : "File"
                }))
                {
                    SharedWithMePicker.Items.Add(listViewItem);
                }
            }
            else
            {
                // Parent folder provided, get its children
                var itemCollection = await _oneDriveApi.GetAllChildrenFromDriveByFolderId(parentItem.RemoteItem != null ? parentItem.RemoteItem.ParentReference.DriveId : parentItem.ParentReference.DriveId, parentItem.RemoteItem != null ? parentItem.RemoteItem.ParentReference != null ? string.IsNullOrEmpty(parentItem.RemoteItem.ParentReference.Id) ? parentItem.RemoteItem.Id : parentItem.RemoteItem.ParentReference.Id : parentItem.RemoteItem.Id : parentItem.Id);

                SharedWithMeUpButton.Tag = parentItem.Name == "root" || parentItem.ParentReference == null || parentItem.ParentReference.Id == null || (parentItem.ParentReference.Path != null && parentItem.ParentReference.Path.EndsWith("root:")) ? null : await _oneDriveApi.GetItemFromDriveById(parentItem.ParentReference.Id, parentItem.ParentReference.DriveId);
                CurrentSharedWithMeOneDriveItem = parentItem;
                GoToSharedWithMeRootTtoolStripMenuItem.Enabled = true;
                SharedWithMeUpButton.Enabled = CurrentSharedWithMeOneDriveItem.ParentReference != null;

                if (CurrentSharedWithMeOneDriveItem.ParentReference != null && CurrentSharedWithMeOneDriveItem.ParentReference.Path != null)
                {
                    var rootLocation = CurrentSharedWithMeOneDriveItem.ParentReference.Path.IndexOf("root:");
                    SharedWithMePath.Text = ((rootLocation == -1 ? CurrentSharedWithMeOneDriveItem.ParentReference.Path : CurrentSharedWithMeOneDriveItem.ParentReference.Path.Remove(0, rootLocation + 5).TrimStart(new[] {'/'})) + "/" + CurrentSharedWithMeOneDriveItem.Name).TrimStart(new[] { '/' });
                }
                else
                {
                    SharedWithMePath.Text = CurrentSharedWithMeOneDriveItem.Name;
                }

                foreach (var listViewItem in itemCollection.Select(oneDriveItem => new ListViewItem
                {
                    Text = oneDriveItem.Name,
                    Tag = oneDriveItem,
{'fraction_non_alphanumeric': 0.08304830483048305, 'fraction_numerical': 0.0008400840084008401, 'mean_word_length': 2.099497861260926, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '138108', 'n_tokens_mistral': 4288, 'n_tokens_neox': 3960, 'n_words': 991}