private void textBox1_TextChanged(object sender, EventArgs e) { if (_nodeCache == null) { _nodeCache = new TreeNode[FileTree.Nodes.Count]; FileTree.Nodes.CopyTo(_nodeCache, 0); } bool addAll = string.IsNullOrEmpty(textBox1.Text); FileTree.BeginUpdate(); FileTree.Nodes.Clear(); foreach (TreeNode root in _nodeCache) { TreeNode n = (TreeNode)root.Clone(); n.Nodes.Clear(); foreach (TreeNode leaf in root.Nodes) { if (addAll || leaf.Text.Contains(textBox1.Text, StringComparison.OrdinalIgnoreCase)) { n.Nodes.Add(leaf); } } FileTree.Nodes.Add(n); } FileTree.ExpandAll(); FileTree.EndUpdate(); }
// Update the hierarchy on the left with the files and folders from the selected path, along with security info private void UpdateFileTree() { StatusLabel.Text = ""; FileTree.Nodes.Clear(); if ((FolderPath.Text.Trim() != "") && (Directory.Exists(FolderPath.Text))) { LoadDirectory(FolderPath.Text); FileTree.ExpandAll(); StatusLabel.Text = "Ready."; } else { StatusLabel.Text = "Invalid path."; } }
/* Expand/collapse all nodes in the tree */ private void expandAllDirectoriesToolStripMenuItem_Click(object sender, EventArgs e) { FileTree.ExpandAll(); }
void connect() { string url = serverLocationTextBox.Text; if (!url.StartsWith("http")) { url = "https://" + url; } if (!url.EndsWith(BasePath)) { url += BasePath; } string host = new Uri(url).Host; while (true) { try { if (SelectedCredentials != null) { TokenClient client = new TokenClient(TokenEndpoint, "resourceownerclient", SelectedCredentials.ClientSecret); TokenResponse response = client.RequestResourceOwnerPasswordAsync(SelectedCredentials.Username, SelectedCredentials.Password, SelectedCredentials.ClientScope).Result; if (response.IsError) { if (MessageBox.Show(this, "Username or password are not correct!", "Error", MessageBoxButtons.RetryCancel) == DialogResult.Cancel) { return; } } else { _accessToken = response.AccessToken; _httpClient.SetBearerToken(_accessToken); _httpClient.DefaultRequestHeaders.Remove("Accept"); _httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=minimal"); break; } } var loginForm = new LoginForm() { StartPosition = FormStartPosition.CenterParent }; if (SelectedCredentials != null) { loginForm.usernameTextBox.Text = SelectedCredentials.Username; loginForm.passwordTextBox.Text = SelectedCredentials.Password; loginForm.identityServerTextBox.Text = SelectedCredentials.IdentityServer.Replace(host, "<HostURL>"); loginForm.clientScopeTextBox.Text = SelectedCredentials.ClientScope; loginForm.clientSecretTextBox.Text = SelectedCredentials.ClientSecret; } if (loginForm.ShowDialog(this) == DialogResult.Cancel) { return; } SelectedCredentials = new Credentials { Username = loginForm.usernameTextBox.Text, Password = loginForm.passwordTextBox.Text, IdentityServer = loginForm.identityServerTextBox.Text.Replace("<HostURL>", host), ClientScope = loginForm.clientScopeTextBox.Text, ClientSecret = loginForm.clientSecretTextBox.Text }; if (!SelectedCredentials.IdentityServer.StartsWith("http")) { SelectedCredentials.IdentityServer = "https://" + SelectedCredentials.IdentityServer; } } catch (Exception ex) { if (MessageBox.Show(this, "Failed to connect to identity server:\r\n" + ex.ToString(), "Error", MessageBoxButtons.RetryCancel) == DialogResult.Cancel) { return; } SelectedCredentials = null; } } FileTree.Nodes.Clear(); FileTree.Nodes.Add("host", host, 0); GetFolders(); FileTree.ExpandAll(); serverLocationTextBox.Text = url; serverLocationTextBox.ReadOnly = true; connectButton.Text = "Disconnect"; }
async void GetFolders() { string host = FileTree.Nodes[0].Text; if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; try { FileTree.Nodes[0].Text += " (loading...)"; UseWaitCursor = true; JObject jobject = await GetJsonFromEndpoint("/folders", cancellationToken); JArray folders = jobject["value"] as JArray; _nodeById = new Dictionary <string, TreeNode>(); foreach (JObject folder in folders) { var path = folder.Property("path").Value.ToString(); var id = folder.Property("id").Value.ToString(); JToken parentIdProperty; if (!folder.TryGetValue("parentId", out parentIdProperty) || (parentIdProperty as JValue).Value == null) { _nodeById[id] = FileTree.TopNode.Nodes.Add(id, System.IO.Path.GetFileName(path), 1); } else { var parentId = (parentIdProperty as JValue).Value.ToString(); var parentNode = _nodeById[parentId]; _nodeById[id] = parentNode.Nodes.Add(id, System.IO.Path.GetFileName(path), 1); } _nodeById[id].Tag = "folder"; } FileTree.Nodes[0].Text = host; FileTree.ExpandAll(); } catch (TaskCanceledException) { if (!cancellationToken.IsCancellationRequested) { Program.HandleException(new TimeoutException("UNIFI API call timed out")); } } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } FileTree.Nodes[0].Text = host + " (error)"; Program.HandleException(ex); disconnect(); } finally { UseWaitCursor = false; } }