From 0806a066084bab7a8be580133c50b54e14a4431f Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Fri, 3 Jul 2020 16:56:52 -0700 Subject: [PATCH] Added drag & drop support for .mcrouter files. --- MainForm.Designer.cs | 3 +++ MainForm.cs | 49 +++++++++++++++++++++++++++++++++----------- MainForm.resx | 6 +++--- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 2fca923..812051f 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -562,11 +562,14 @@ // // mapPanel // + this.mapPanel.AllowDrop = true; resources.ApplyResources(this.mapPanel, "mapPanel"); this.mapPanel.BackColor = System.Drawing.SystemColors.ControlLightLight; this.mapPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.mapPanel.Controls.Add(this.noMapLabel); this.mapPanel.Name = "mapPanel"; + this.mapPanel.DragDrop += new System.Windows.Forms.DragEventHandler(this.mapPanel_DragDrop); + this.mapPanel.DragEnter += new System.Windows.Forms.DragEventHandler(this.mapPanel_DragEnter); // // noMapLabel // diff --git a/MainForm.cs b/MainForm.cs index f9b58b4..9546016 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -182,7 +182,7 @@ namespace MeshCentralRouter if (arg.Length > 6 && arg.Substring(0, 6).ToLower() == "-pass:") { passwordTextBox.Text = arg.Substring(6); argflags |= 4; } if (arg.Length > 8 && arg.Substring(0, 8).ToLower() == "-search:") { searchTextBox.Text = arg.Substring(8); } if (arg.Length > 11 && arg.Substring(0, 11).ToLower() == "mcrouter://") { authLoginUrl = new Uri(arg); } - if ((arg.Length > 1) && (arg[0] != '-') && (arg.ToLower().EndsWith(".mcrouter"))) { try { argflags |= loadMappingFile(File.ReadAllText(arg)); } catch (Exception) { } } + if ((arg.Length > 1) && (arg[0] != '-') && (arg.ToLower().EndsWith(".mcrouter"))) { try { argflags |= loadMappingFile(File.ReadAllText(arg), 1); } catch (Exception) { } } } autoLogin = (argflags == 7); @@ -1132,27 +1132,30 @@ namespace MeshCentralRouter { if (openMapFileDialog.ShowDialog(this) == DialogResult.OK) { - string text = null; - try { text = File.ReadAllText(openMapFileDialog.FileName); } catch (Exception) { } - if (text != null) { loadMappingFile(text); } + try { loadMappingFile(File.ReadAllText(openMapFileDialog.FileName), 2); } catch (Exception) { } } } - private int loadMappingFile(string data) + private int loadMappingFile(string data, int mode) { int argFlags = 3; Dictionary jsonAction = new Dictionary(); jsonAction = new JavaScriptSerializer().Deserialize>(data); if ((jsonAction == null) || (jsonAction["hostname"].GetType() != typeof(string)) || (jsonAction["username"].GetType() != typeof(string)) || (jsonAction["certhash"].GetType() != typeof(string))) return 0; - serverNameComboBox.Text = jsonAction["hostname"].ToString(); - userNameTextBox.Text = jsonAction["username"].ToString(); - if (jsonAction.ContainsKey("password")) { passwordTextBox.Text = jsonAction["password"].ToString(); argFlags |= 4; } - acceptableCertHash = jsonAction["certhash"].ToString(); - + if (mode == 1) + { + serverNameComboBox.Text = jsonAction["hostname"].ToString(); + userNameTextBox.Text = jsonAction["username"].ToString(); + if (jsonAction.ContainsKey("password")) { passwordTextBox.Text = jsonAction["password"].ToString(); argFlags |= 4; } + acceptableCertHash = jsonAction["certhash"].ToString(); + } if (jsonAction["mappings"] != null) { ArrayList mappings = (ArrayList)jsonAction["mappings"]; - if (mappings.Count > 0) { mappingsToSetup = mappings; } + if (mappings.Count > 0) { + mappingsToSetup = mappings; + if (mode == 2) { setupMappings(); } + } } return argFlags; } @@ -1187,7 +1190,13 @@ namespace MeshCentralRouter noMapLabel.Visible = false; // Launch any executable - if (x.ContainsKey("launch")) { try { System.Diagnostics.Process.Start((string)x["launch"]); } catch (Exception) { } } + if (x.ContainsKey("launch")) { + try { + string lanuchString = (string)x["launch"]; + lanuchString = lanuchString.Replace("{port}", x["localPort"].ToString()); + System.Diagnostics.Process.Start(lanuchString); + } catch (Exception) { } + } } mappingsToSetup = null; } @@ -1220,6 +1229,22 @@ namespace MeshCentralRouter } } + private void mapPanel_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop) == false) return; + string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); + if ((s.Length != 1) || (s[0].ToLower().EndsWith(".mcrouter") == false)) return; + e.Effect = DragDropEffects.All; + } + + private void mapPanel_DragDrop(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop) == false) return; + string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); + if ((s.Length != 1) || (s[0].ToLower().EndsWith(".mcrouter") == false)) return; + try { loadMappingFile(File.ReadAllText(s[0]), 2); } catch (Exception) { } + } + /* private delegate void displayMessageHandler(string msg, int buttons, string extra, int progress); private void displayMessage(string msg, int buttons = 0, string extra = "", int progress = 0) diff --git a/MainForm.resx b/MainForm.resx index 6ab69ca..0e5c25b 100644 --- a/MainForm.resx +++ b/MainForm.resx @@ -1336,7 +1336,7 @@ 2, 88 - 462, 52 + 458, 52 5 @@ -1375,7 +1375,7 @@ 2, 88 - 462, 52 + 458, 52 4 @@ -1465,7 +1465,7 @@ 2, 79 - 462, 52 + 458, 52 4