diff --git a/Sunfish/Sunfish/Configurator/ConfigurationElement.cs b/Sunfish/Sunfish/Configurator/ConfigurationElement.cs new file mode 100644 index 0000000..c87cc29 --- /dev/null +++ b/Sunfish/Sunfish/Configurator/ConfigurationElement.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Configurator +{ + public abstract class ConfigurationElement + { + protected ConfigurationElement(string id) + { + Id = id; + } + + public string Id { get; } + public string Label { get; set; } + public string Tooltip { get; set; } + } +} diff --git a/Sunfish/Sunfish/Configurator/ConfigurationMessage.cs b/Sunfish/Sunfish/Configurator/ConfigurationMessage.cs new file mode 100644 index 0000000..a218aa3 --- /dev/null +++ b/Sunfish/Sunfish/Configurator/ConfigurationMessage.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Configurator +{ + class ConfigurationMessage:ConfigurationElement + { + public enum MessageType + { + INFO,WARNING,ERROR + } + + public ConfigurationMessage(MessageType t,string message) : base(null) + { + Type = t; + Message = message; + } + + public MessageType Type { get; } + public string Message { get; } + } +} diff --git a/Sunfish/Sunfish/Configurator/ConfigurationScreen.cs b/Sunfish/Sunfish/Configurator/ConfigurationScreen.cs new file mode 100644 index 0000000..0f64b6d --- /dev/null +++ b/Sunfish/Sunfish/Configurator/ConfigurationScreen.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Configurator +{ + public class ConfigurationScreen + { + + public delegate void AdvancedEditing(SunfishServiceConfiguration ssc); + //public delegate void AdvancedWebEditing(SunfishServiceConfiguration ssc, ); + + public ConfigurationElement[] Elements; + + public AdvancedEditing Advanced { get; set; } + //public AdvancedWebEditing AdvancedWeb{get;set;} + } +} diff --git a/Sunfish/Sunfish/Configurator/ConfigurationString.cs b/Sunfish/Sunfish/Configurator/ConfigurationString.cs new file mode 100644 index 0000000..de7bf6c --- /dev/null +++ b/Sunfish/Sunfish/Configurator/ConfigurationString.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Configurator +{ + public class ConfigurationString:ConfigurationElement + { + public ConfigurationString(string id) : base(id) + { + } + + public string DefaultValue { get; set; } + public bool IsPassword { get; set; } + public bool IsDirectoiryPath { get; set; } + public bool IsFilePath { get; set; } + } +} diff --git a/Sunfish/Sunfish/Configurator/SunfishServiceConfigurator.cs b/Sunfish/Sunfish/Configurator/SunfishServiceConfigurator.cs new file mode 100644 index 0000000..e381f7d --- /dev/null +++ b/Sunfish/Sunfish/Configurator/SunfishServiceConfigurator.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Configurator +{ + public abstract class SunfishServiceConfigurator + { + internal static SunfishServiceConfigurator GetConfiguratorForService(Type t) + { + DefineConfigurator cfgr = t.GetCustomAttribute(); + if (cfgr == null) + return null; + return cfgr.Instantiate(); + } + + internal protected abstract ConfigurationScreen GetConfigurationScreen(); + } +} diff --git a/Sunfish/Sunfish/DefineConfigurator.cs b/Sunfish/Sunfish/DefineConfigurator.cs new file mode 100644 index 0000000..a9115c9 --- /dev/null +++ b/Sunfish/Sunfish/DefineConfigurator.cs @@ -0,0 +1,26 @@ +using DolphinWebXplorer2.Configurator; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2 +{ + [AttributeUsage(AttributeTargets.Class)] + class DefineConfigurator : Attribute + { + public DefineConfigurator(Type configuratorType) + { + ConfiguratorType = configuratorType; + } + + public SunfishServiceConfigurator Instantiate() + { + Type t = ConfiguratorType; + return (SunfishServiceConfigurator)t.GetConstructor(new Type[0]).Invoke(new object[0]); + } + + public Type ConfiguratorType { get; } + } +} diff --git a/Sunfish/Sunfish/Extensions.cs b/Sunfish/Sunfish/Extensions.cs new file mode 100644 index 0000000..20b1769 --- /dev/null +++ b/Sunfish/Sunfish/Extensions.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DolphinWebXplorer2 +{ + public static class Extensions + { + public static void Show(this Exception ex) + { + MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } +} diff --git a/Sunfish/Sunfish/FServiceConf.Designer.cs b/Sunfish/Sunfish/FServiceConf.Designer.cs index 00b91a6..032a703 100644 --- a/Sunfish/Sunfish/FServiceConf.Designer.cs +++ b/Sunfish/Sunfish/FServiceConf.Designer.cs @@ -35,14 +35,18 @@ this.tbName = new System.Windows.Forms.TextBox(); this.cbActive = new System.Windows.Forms.CheckBox(); this.label1 = new System.Windows.Forms.Label(); - this.textBox1 = new System.Windows.Forms.TextBox(); + this.tbLocation = new System.Windows.Forms.TextBox(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); + this.btOk = new System.Windows.Forms.Button(); + this.btCancel = new System.Windows.Forms.Button(); + this.pScreen = new System.Windows.Forms.Panel(); + this.btAdv = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lbType // this.lbType.AutoSize = true; - this.lbType.Location = new System.Drawing.Point(12, 9); + this.lbType.Location = new System.Drawing.Point(11, 9); this.lbType.Name = "lbType"; this.lbType.Size = new System.Drawing.Size(47, 20); this.lbType.TabIndex = 0; @@ -50,13 +54,16 @@ // // cbType // + this.cbType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.cbType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbType.FormattingEnabled = true; this.cbType.Location = new System.Drawing.Point(25, 32); this.cbType.Name = "cbType"; - this.cbType.Size = new System.Drawing.Size(353, 28); + this.cbType.Size = new System.Drawing.Size(451, 28); this.cbType.TabIndex = 1; this.toolTip1.SetToolTip(this.cbType, "Type of service."); + this.cbType.SelectedIndexChanged += new System.EventHandler(this.cbType_SelectedIndexChanged); // // lbName // @@ -73,7 +80,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.tbName.Location = new System.Drawing.Point(25, 100); this.tbName.Name = "tbName"; - this.tbName.Size = new System.Drawing.Size(269, 26); + this.tbName.Size = new System.Drawing.Size(370, 26); this.tbName.TabIndex = 3; this.toolTip1.SetToolTip(this.tbName, "Name of the service.\r\n(only for management)"); // @@ -81,7 +88,7 @@ // this.cbActive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.cbActive.AutoSize = true; - this.cbActive.Location = new System.Drawing.Point(300, 102); + this.cbActive.Location = new System.Drawing.Point(401, 102); this.cbActive.Name = "cbActive"; this.cbActive.Size = new System.Drawing.Size(78, 24); this.cbActive.TabIndex = 4; @@ -97,20 +104,70 @@ this.label1.TabIndex = 5; this.label1.Text = "Location:"; // - // textBox1 + // tbLocation // - this.textBox1.Location = new System.Drawing.Point(25, 166); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(353, 26); - this.textBox1.TabIndex = 6; - this.toolTip1.SetToolTip(this.textBox1, "Document root on the server:\r\nThis works as a virtual directory in the url."); + this.tbLocation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.tbLocation.Location = new System.Drawing.Point(25, 166); + this.tbLocation.Name = "tbLocation"; + this.tbLocation.Size = new System.Drawing.Size(451, 26); + this.tbLocation.TabIndex = 6; + this.toolTip1.SetToolTip(this.tbLocation, "Document root on the server:\r\nThis works as a virtual directory in the url."); + // + // btOk + // + this.btOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btOk.Location = new System.Drawing.Point(282, 217); + this.btOk.Name = "btOk"; + this.btOk.Size = new System.Drawing.Size(113, 38); + this.btOk.TabIndex = 7; + this.btOk.Text = "Ok"; + this.btOk.UseVisualStyleBackColor = true; + this.btOk.Click += new System.EventHandler(this.button1_Click); + // + // btCancel + // + this.btCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.btCancel.Location = new System.Drawing.Point(408, 217); + this.btCancel.Name = "btCancel"; + this.btCancel.Size = new System.Drawing.Size(82, 38); + this.btCancel.TabIndex = 8; + this.btCancel.Text = "Cancel"; + this.btCancel.UseVisualStyleBackColor = true; + // + // pScreen + // + this.pScreen.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pScreen.Location = new System.Drawing.Point(12, 210); + this.pScreen.Name = "pScreen"; + this.pScreen.Size = new System.Drawing.Size(478, 1); + this.pScreen.TabIndex = 9; + // + // btAdv + // + this.btAdv.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.btAdv.Location = new System.Drawing.Point(12, 217); + this.btAdv.Name = "btAdv"; + this.btAdv.Size = new System.Drawing.Size(196, 38); + this.btAdv.TabIndex = 10; + this.btAdv.Text = "Advanced Settings"; + this.btAdv.UseVisualStyleBackColor = true; // // FServiceConf // + this.AcceptButton = this.btOk; this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(401, 408); - this.Controls.Add(this.textBox1); + this.CancelButton = this.btCancel; + this.ClientSize = new System.Drawing.Size(502, 267); + this.Controls.Add(this.btAdv); + this.Controls.Add(this.pScreen); + this.Controls.Add(this.btCancel); + this.Controls.Add(this.btOk); + this.Controls.Add(this.tbLocation); this.Controls.Add(this.label1); this.Controls.Add(this.cbActive); this.Controls.Add(this.tbName); @@ -122,6 +179,7 @@ this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Service"; + this.Load += new System.EventHandler(this.FServiceConf_Load); this.ResumeLayout(false); this.PerformLayout(); @@ -136,6 +194,10 @@ private System.Windows.Forms.CheckBox cbActive; private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.Label label1; - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox tbLocation; + private System.Windows.Forms.Button btOk; + private System.Windows.Forms.Button btCancel; + private System.Windows.Forms.Panel pScreen; + private System.Windows.Forms.Button btAdv; } } \ No newline at end of file diff --git a/Sunfish/Sunfish/FServiceConf.cs b/Sunfish/Sunfish/FServiceConf.cs index 49bb880..c87ad9f 100644 --- a/Sunfish/Sunfish/FServiceConf.cs +++ b/Sunfish/Sunfish/FServiceConf.cs @@ -1,4 +1,5 @@ -using System; +using DolphinWebXplorer2.Configurator; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +13,145 @@ { public partial class FServiceConf : Form { - public FServiceConf() + private SunfishServiceConfiguration ssc; + private int panelOffset; + + private FServiceConf() { InitializeComponent(); + panelOffset = ClientSize.Height - pScreen.Top; + } + + public static bool Execute(SunfishServiceConfiguration ssc) + { + using (FServiceConf f = new FServiceConf()) + { + f.ssc = ssc; + f.LoadData(); + return f.ShowDialog() == DialogResult.OK; + } + } + + private void LoadData() + { + cbType.SelectedItem = ssc.Type; + cbActive.Checked = ssc.Enabled; + tbName.Text = ssc.Name; + tbLocation.Text = ssc.Name; + LoadScreen(); + } + + private void SaveData() + { + ssc.Type = cbType.SelectedItem == null ? null : cbType.SelectedItem.ToString(); + ssc.Enabled = cbActive.Checked; + ssc.Name = tbName.Text; + ssc.Location = tbLocation.Text; + // TODO Save from dynamic screen + } + + private void AddElementLabel(ConfigurationElement ce, Panel p, ref int y) + { + if (string.IsNullOrEmpty(ce.Label)) + return; + y += 3; + Label l = new Label(); + l.Text = ce.Label; + l.Top = y; + l.AutoSize = true; + y += l.Height + 3; + p.Controls.Add(l); + } + + private void FinishElement(Control c, ConfigurationElement ce, Panel p, ref int y) + { + if (!string.IsNullOrEmpty(ce.Tooltip)) + toolTip1.SetToolTip(c, ce.Tooltip); + c.Top = y; + y += c.Height + 3; + c.Tag = ce; + p.Controls.Add(c); + } + + private void AddElementMessage(ConfigurationElement ce, Panel p, ref int y) + { + AddElementLabel(ce, p, ref y); + ConfigurationMessage cm = (ConfigurationMessage)ce; + Label c = new Label(); + c.Text = cm.Message; + c.BorderStyle = BorderStyle.FixedSingle; + switch (cm.Type) + { + case ConfigurationMessage.MessageType.ERROR: + c.BackColor = Color.FromArgb(255, 200, 200); + break; + case ConfigurationMessage.MessageType.INFO: + c.BackColor = Color.FromArgb(255, 255, 255); + break; + case ConfigurationMessage.MessageType.WARNING: + c.BackColor = Color.WhiteSmoke; + break; + } + c.MaximumSize = new Size(p.ClientSize.Width - 28, int.MaxValue); + c.MinimumSize = new Size(c.MaximumSize.Width, 0); + c.Left = 14; + c.AutoSize = true; + FinishElement(c, ce, p, ref y); + } + + private void AddElementString(ConfigurationElement ce, Panel p, ref int y) + { + AddElementLabel(ce, p, ref y); + ConfigurationString cs = (ConfigurationString)ce; + TextBox c = new TextBox(); + c.Text = ssc.GetConf(ce.Id, cs.DefaultValue); + c.Left = 14; + c.Width = p.ClientSize.Width - 28; + if (cs.IsPassword) + c.PasswordChar = '*'; + //TODO: Handle isDirectory and IsFile properties + FinishElement(c, ce, p, ref y); + } + + private void LoadScreen() + { + // Remove all + pScreen.Controls.Clear(); + string type = cbType.SelectedItem == null ? null : cbType.SelectedItem.ToString(); + if (string.IsNullOrEmpty(type)) + { + ClientSize = new Size(ClientSize.Width, pScreen.Top + panelOffset); + btAdv.Visible = false; + return; + } + ConfigurationScreen cs = SunfishService.GetConfigurator(type).GetConfigurationScreen(); + btAdv.Visible = cs.Advanced != null; + // Create new + int y = 0; + foreach (ConfigurationElement ce in cs.Elements) + { + if (ce is ConfigurationMessage) + AddElementMessage(ce, pScreen, ref y); + else if (ce is ConfigurationString) + AddElementString(ce, pScreen, ref y); + } + ClientSize = new Size(ClientSize.Width, y + pScreen.Top + panelOffset); + } + + private void FServiceConf_Load(object sender, EventArgs e) + { + cbType.Items.AddRange(SunfishService.GetTypes()); + } + + private void button1_Click(object sender, EventArgs e) + { + SaveData(); + DialogResult = DialogResult.OK; + } + + private void cbType_SelectedIndexChanged(object sender, EventArgs e) + { + LoadScreen(); } } } diff --git a/Sunfish/Sunfish/Form1.cs b/Sunfish/Sunfish/Form1.cs index 979d2fb..1d0273b 100644 --- a/Sunfish/Sunfish/Form1.cs +++ b/Sunfish/Sunfish/Form1.cs @@ -34,8 +34,8 @@ nudPort.Value = Sunfish.Port; cbActive.Checked = Sunfish.Active; lbPaths.Items.Clear(); - foreach (WShared sh in WebXplorer.Shares) - lbPaths.Items.Add(sh); + foreach (SunfishService s in Sunfish.Services) + lbPaths.Items.Add(s); Enabled = true; } @@ -67,48 +67,69 @@ nudPort.Enabled = !cbActive.Checked; if (!Enabled) return; - if (cbActive.Checked) - WebXplorer.Start(); - else - WebXplorer.Stop(); - cbActive.Checked = WebXplorer.Active; + try + { + Sunfish.Active = cbActive.Checked; + } + catch (Exception ex) + { + ex.Show(); + } + cbActive.Checked = Sunfish.Active; } private void nudPort_ValueChanged(object sender, EventArgs e) { if (!Enabled) return; - WebXplorer.Port = (int)nudPort.Value; + Sunfish.Port = (int)nudPort.Value; + cbActive.Checked = Sunfish.Active; } private void btAdd_Click(object sender, EventArgs e) { - WShared sh; - string clip = Clipboard.GetText(); - if ((Path.DirectorySeparatorChar == '/' ? clip.Length > 0 && clip[0] == '/' : clip.Length > 2 && clip[1] == ':' && clip[2] == '\\') - && (Directory.Exists(clip) || File.Exists(clip))) + SunfishServiceConfiguration ssc = new SunfishServiceConfiguration(); + if (FServiceConf.Execute(ssc)) { - if (File.Exists(clip)) - clip = Path.GetDirectoryName(clip); - sh = new WShared(Path.GetFileName(clip), clip); + try + { + lbPaths.Items.Add(Sunfish.AddService(ssc)); + Sunfish.Save(); + } + catch (Exception ex) + { + ex.Show(); + } } - else - sh = new WShared("NewShared", @"C:\"); - sh.Enabled = true; - if (FShared.Execute(sh)) - { - WebXplorer.Add(sh); - lbPaths.Items.Add(sh); - } + //WShared sh; + //string clip = Clipboard.GetText(); + //if ((Path.DirectorySeparatorChar == '/' ? clip.Length > 0 && clip[0] == '/' : clip.Length > 2 && clip[1] == ':' && clip[2] == '\\') + // && (Directory.Exists(clip) || File.Exists(clip))) + //{ + // if (File.Exists(clip)) + // clip = Path.GetDirectoryName(clip); + // sh = new WShared(Path.GetFileName(clip), clip); + //} + //else + // sh = new WShared("NewShared", @"C:\"); + //sh.Enabled = true; + //if (FShared.Execute(sh)) + //{ + // WebXplorer.Add(sh); + // lbPaths.Items.Add(sh); + //} } private void clbPaths_DoubleClick(object sender, EventArgs e) { - WShared sh = (WShared)lbPaths.SelectedItem; - if (sh == null) + SunfishService s = (SunfishService)lbPaths.SelectedItem; + if (s == null) return; - if (FShared.Execute(sh)) + if (FServiceConf.Execute(s.Configuration)) + { + //TODO: How to update service according to configuration changes? lbPaths.Update(); + } } private void lbPaths_DrawItem(object sender, DrawItemEventArgs e) @@ -117,18 +138,18 @@ e.DrawFocusRectangle(); if (e.Index < 0) return; - WShared sh = (WShared)lbPaths.Items[e.Index]; + SunfishService s = (SunfishService)lbPaths.Items[e.Index]; Graphics g = e.Graphics; int y = e.Bounds.Top + 2; using (Brush itembrush = new SolidBrush(e.ForeColor)) { - g.DrawImage(sh.Enabled ? Resources.foldericon : Resources.foldericond, 1, y, 24, 24); - g.DrawString(sh.Name, lbPaths.Font, itembrush, 28, y); - g.DrawString(sh.Path, smallfont, itembrushgray, 33, y + 12); - if (sh.AllowSubfolders) - g.DrawImage(Resources.osubf, 16, y + 13); - if (sh.AllowUpload) - g.DrawImage(Resources.ouplo, 3, y + 13); + g.DrawImage(s.Enabled ? Resources.foldericon : Resources.foldericond, 1, y, 24, 24); + g.DrawString(s.Configuration.Name, lbPaths.Font, itembrush, 28, y); + g.DrawString(s.Configuration.Location, smallfont, itembrushgray, 33, y + 12); + //if (sh.AllowSubfolders) + // g.DrawImage(Resources.osubf, 16, y + 13); + //if (sh.AllowUpload) + // g.DrawImage(Resources.ouplo, 3, y + 13); } } @@ -151,7 +172,7 @@ private void btShowIp_Click(object sender, EventArgs e) { - StringBuilder sb = new StringBuilder(WebXplorer.Active ? "A" : "ina"); + StringBuilder sb = new StringBuilder(Sunfish.Active ? "A" : "Ina"); sb.Append("ctive.\r\n"); foreach (IpInfo ip in ListInterfacesIPs()) { @@ -166,24 +187,24 @@ } sb.Append("\r\nSunfish "); sb.Append(Program.VERSION); - sb.Append(" (C) XWolfOverride@gmail.com 2007-2015\r\nEasy folder shares"); + sb.Append(" (C) XWolfOverride@gmail.com 2007-2020\r\nEasy folder shares"); MessageBox.Show(sb.ToString(), "Sunfish information", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void cmsItem_Opening(object sender, CancelEventArgs e) { - WShared sh = lbPaths.SelectedItem as WShared; - editarToolStripMenuItem.Enabled = sh != null; - borrarToolStripMenuItem.Enabled = sh != null; - toolStripSeparator1.Visible = sh != null; + SunfishService s = lbPaths.SelectedItem as SunfishService; + editarToolStripMenuItem.Enabled = s != null; + borrarToolStripMenuItem.Enabled = s != null; + toolStripSeparator1.Visible = s != null; while (cmsItem.Items.Count > 4) cmsItem.Items.RemoveAt(cmsItem.Items.Count - 1); - if (sh == null) + if (s == null) return; foreach (IpInfo ip in ListInterfacesIPs()) { ToolStripItem tsi = cmsItem.Items.Add("Copy url for " + ip.Address + " (" + ip.InterfaceName + ", " + ip.InterfaceType + ")"); - tsi.Tag = "http://" + ip.Address + ":" + WebXplorer.Port + "/" + sh.Name + "/"; + tsi.Tag = "http://" + ip.Address + ":" + WebXplorer.Port + "/" + s.Configuration.Location + "/"; tsi.Click += tsi_Click; } } diff --git a/Sunfish/Sunfish/Services/AdminService.cs b/Sunfish/Sunfish/Services/AdminService.cs index 6e82aa7..0dfca17 100644 --- a/Sunfish/Sunfish/Services/AdminService.cs +++ b/Sunfish/Sunfish/Services/AdminService.cs @@ -1,5 +1,4 @@ -using DolphinWebXplorer2.Services.UI; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,22 +6,21 @@ namespace DolphinWebXplorer2.Services { + [DefineConfigurator(typeof(AdminServiceConfigurator))] class AdminService : SunfishService { + private const string CFG_ADMIN_PWD = "adminPwd"; public AdminService(SunfishServiceConfiguration ssc) : base(ssc) { } - protected override ConfigurationScreen GetConfigurationScreen() + protected override void Start() { - return new ConfigurationScreen() - { - elements = new ConfigurationElement[] - { - new ConfigurationMessage(ConfigurationMessage.MessageType.ERROR,"Error loading or initializing this service") - } - }; + } + + protected override void Stop() + { } public override string Description => "Allow remote configuration of Sunfish server"; diff --git a/Sunfish/Sunfish/Services/AdminServiceConfigurator.cs b/Sunfish/Sunfish/Services/AdminServiceConfigurator.cs new file mode 100644 index 0000000..9761040 --- /dev/null +++ b/Sunfish/Sunfish/Services/AdminServiceConfigurator.cs @@ -0,0 +1,30 @@ +using DolphinWebXplorer2.Configurator; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Services +{ + class AdminServiceConfigurator : SunfishServiceConfigurator + { + internal const string CFG_ADMIN_PWD = "adminPwd"; + + internal protected override ConfigurationScreen GetConfigurationScreen() + { + return new ConfigurationScreen() + { + Elements = new ConfigurationElement[] + { + new ConfigurationString(CFG_ADMIN_PWD) + { + Tooltip="Password for the administration panel.\n Leave it blank for no password access (not recommended)", + IsPassword=true, + Label="Admin password: (blank for none)" + } + } + }; + } + } +} diff --git a/Sunfish/Sunfish/Services/ErrorService.cs b/Sunfish/Sunfish/Services/ErrorService.cs index 74cb50e..765c338 100644 --- a/Sunfish/Sunfish/Services/ErrorService.cs +++ b/Sunfish/Sunfish/Services/ErrorService.cs @@ -1,5 +1,4 @@ -using DolphinWebXplorer2.Services.UI; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,24 +6,21 @@ namespace DolphinWebXplorer2.Services { + [DefineConfigurator(typeof(ErrorServiceConfigurator))] class ErrorService : SunfishService { public ErrorService(SunfishServiceConfiguration ssc) : base(ssc) { } - protected override ConfigurationScreen GetConfigurationScreen() + protected override void Start() { - return new ConfigurationScreen() - { - elements = new ConfigurationElement[] - { - new ConfigurationMessage(ConfigurationMessage.MessageType.ERROR,"Error loading or initializing this service") - } - }; + } + + protected override void Stop() + { } public override string Description => null; - } } diff --git a/Sunfish/Sunfish/Services/ErrorServiceConfigurator.cs b/Sunfish/Sunfish/Services/ErrorServiceConfigurator.cs new file mode 100644 index 0000000..65bbd9d --- /dev/null +++ b/Sunfish/Sunfish/Services/ErrorServiceConfigurator.cs @@ -0,0 +1,23 @@ +using DolphinWebXplorer2.Configurator; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Services +{ + class ErrorServiceConfigurator : SunfishServiceConfigurator + { + internal protected override ConfigurationScreen GetConfigurationScreen() + { + return new ConfigurationScreen() + { + Elements = new ConfigurationElement[] + { + new ConfigurationMessage(ConfigurationMessage.MessageType.ERROR,"Error loading or initializing this service") + } + }; + } + } +} diff --git a/Sunfish/Sunfish/Services/UI/ConfigurationElement.cs b/Sunfish/Sunfish/Services/UI/ConfigurationElement.cs deleted file mode 100644 index 0586fd0..0000000 --- a/Sunfish/Sunfish/Services/UI/ConfigurationElement.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DolphinWebXplorer2.Services.UI -{ - public abstract class ConfigurationElement - { - protected ConfigurationElement(string id) - { - Id = id; - } - - public string Id { get; } - public string Label { get; set; } - } -} diff --git a/Sunfish/Sunfish/Services/UI/ConfigurationMessage.cs b/Sunfish/Sunfish/Services/UI/ConfigurationMessage.cs deleted file mode 100644 index 31ed9c8..0000000 --- a/Sunfish/Sunfish/Services/UI/ConfigurationMessage.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DolphinWebXplorer2.Services.UI -{ - class ConfigurationMessage:ConfigurationElement - { - public enum MessageType - { - INFO,WARNING,ERROR - } - - public ConfigurationMessage(MessageType t,string message) : base(null) - { - Type = t; - Message = message; - } - - public MessageType Type { get; } - public string Message { get; } - } -} diff --git a/Sunfish/Sunfish/Services/UI/ConfigurationScreen.cs b/Sunfish/Sunfish/Services/UI/ConfigurationScreen.cs deleted file mode 100644 index 0024fad..0000000 --- a/Sunfish/Sunfish/Services/UI/ConfigurationScreen.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DolphinWebXplorer2.Services.UI -{ - public class ConfigurationScreen - { - public ConfigurationElement[] elements; - } -} diff --git a/Sunfish/Sunfish/Services/UI/ConfigurationString.cs b/Sunfish/Sunfish/Services/UI/ConfigurationString.cs deleted file mode 100644 index fc3e7a9..0000000 --- a/Sunfish/Sunfish/Services/UI/ConfigurationString.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DolphinWebXplorer2.Services.UI -{ - public class ConfigurationString:ConfigurationElement - { - public ConfigurationString(string id,string defvalue) : base(id) - { - DefaultValue = defvalue; - } - - public string DefaultValue { get; } - } -} diff --git a/Sunfish/Sunfish/Services/WebService.cs b/Sunfish/Sunfish/Services/WebService.cs index 5b1b843..701ec05 100644 --- a/Sunfish/Sunfish/Services/WebService.cs +++ b/Sunfish/Sunfish/Services/WebService.cs @@ -1,5 +1,4 @@ -using DolphinWebXplorer2.Services.UI; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,21 +6,19 @@ namespace DolphinWebXplorer2.Services { + [DefineConfigurator(typeof(WebServiceConfigurator))] class WebService : SunfishService { public WebService(SunfishServiceConfiguration ssc) : base(ssc) { } - protected override ConfigurationScreen GetConfigurationScreen() + protected override void Start() { - return new ConfigurationScreen() - { - elements = new ConfigurationElement[] - { - new ConfigurationMessage(ConfigurationMessage.MessageType.ERROR,"Error loading or initializing this service") - } - }; + } + + protected override void Stop() + { } public override string Description => "For Webpages or file sharing"; diff --git a/Sunfish/Sunfish/Services/WebServiceConfigurator.cs b/Sunfish/Sunfish/Services/WebServiceConfigurator.cs new file mode 100644 index 0000000..3c07ca0 --- /dev/null +++ b/Sunfish/Sunfish/Services/WebServiceConfigurator.cs @@ -0,0 +1,44 @@ +using DolphinWebXplorer2.Configurator; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DolphinWebXplorer2.Services +{ + class WebServiceConfigurator : SunfishServiceConfigurator + { + internal const string CFG_PATH = "path"; + internal const string CFG_SHARE = "folderShare"; + internal const string CFG_NAVIGATION = "allowSubfolderNavigation"; + internal const string CFG_UPLOAD = "allowUploads"; + internal const string CFG_RENAME = "allowRenaming"; + internal const string CFG_DELETE = "allowDeletion"; + internal const string CFG_EXECUTE = "allowServerExecution"; + + private void AdvancedEditing(SunfishServiceConfiguration ssc) + { + + } + + internal protected override ConfigurationScreen GetConfigurationScreen() + { + return new ConfigurationScreen() + { + Elements = new ConfigurationElement[] + { + new ConfigurationString(CFG_PATH) + { + Tooltip = "Folder, zip or web-file path of site document root", + Label = "Web path", + IsDirectoiryPath = true, + IsFilePath = true, + } + }, + Advanced = AdvancedEditing + }; + } + + } +} diff --git a/Sunfish/Sunfish/Sunfish.cs b/Sunfish/Sunfish/Sunfish.cs index 65e5e21..c2c843d 100644 --- a/Sunfish/Sunfish/Sunfish.cs +++ b/Sunfish/Sunfish/Sunfish.cs @@ -35,7 +35,7 @@ conf = JsonNet.Deserialize(json); if (conf.Services == null) conf.Services = new List(); - foreach(SunfishServiceConfiguration ssc in conf.Services) + foreach (SunfishServiceConfiguration ssc in conf.Services) { srvs.Add(SunfishService.Instance(ssc)); } @@ -52,8 +52,8 @@ { if (conf.Active == act) return; - conf.Active = act; // INIT SERVER HERE + conf.Active = act; } private static void SetPort(int port) @@ -64,6 +64,13 @@ Active = act; } + public static SunfishService AddService(SunfishServiceConfiguration ssc) + { + SunfishService s; + srvs.Add(s = SunfishService.Instance(ssc)); + return s; + } + public static bool Active { get => conf.Active; set => SetActive(value); } public static int Port { get => conf.Port; set => SetPort(value); } public static SunfishService[] Services => srvs.ToArray(); diff --git a/Sunfish/Sunfish/Sunfish.csproj b/Sunfish/Sunfish/Sunfish.csproj index a20b5cb..3d1392c 100644 --- a/Sunfish/Sunfish/Sunfish.csproj +++ b/Sunfish/Sunfish/Sunfish.csproj @@ -75,6 +75,7 @@ + Form @@ -138,12 +139,17 @@ True + + + + - - - - + + + + + diff --git a/Sunfish/Sunfish/SunfishService.cs b/Sunfish/Sunfish/SunfishService.cs index 7a9bb89..b5b94b2 100644 --- a/Sunfish/Sunfish/SunfishService.cs +++ b/Sunfish/Sunfish/SunfishService.cs @@ -1,5 +1,5 @@ -using DolphinWebXplorer2.Services; -using DolphinWebXplorer2.Services.UI; +using DolphinWebXplorer2.Configurator; +using DolphinWebXplorer2.Services; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +9,7 @@ namespace DolphinWebXplorer2 { - abstract class SunfishService + public abstract class SunfishService { #region Static / Type management @@ -37,14 +37,20 @@ } } - public static SunfishService Instance(SunfishServiceConfiguration ssc) + private static Type GetServiceTypeOf(string type) { Type stype = null; foreach (Type t in ServiceTypes) - if (t.Name == ssc.Name) + if (t.Name == type) stype = t; if (stype == null) stype = typeof(ErrorService); + return stype; + } + + internal static SunfishService Instance(SunfishServiceConfiguration ssc) + { + Type stype = GetServiceTypeOf(ssc.Type); try { return (SunfishService)stype.GetConstructor(new Type[] { typeof(SunfishServiceConfiguration) }).Invoke(new Object[] { ssc }); @@ -52,19 +58,50 @@ catch { return new ErrorService(ssc); } } + public static SunfishServiceConfigurator GetConfigurator(string type) + { + return SunfishServiceConfigurator.GetConfiguratorForService(GetServiceTypeOf(type)); + } + + public static string[] GetTypes() + { + List lresult = new List(); + foreach (Type t in ServiceTypes) + lresult.Add(t.Name); + return lresult.ToArray(); + } + #endregion - protected SunfishServiceConfiguration ssc; + private SunfishServiceConfigurator configurator; public SunfishService(SunfishServiceConfiguration ssc) { Configuration = ssc; + configurator = SunfishServiceConfigurator.GetConfiguratorForService(GetType()); } - protected abstract ConfigurationScreen GetConfigurationScreen(); + protected abstract void Start(); + protected abstract void Stop(); + + #region GET/SET + + private void SetEnabled(bool enabled) + { + if (Enabled == enabled) + return; + if (enabled) + Start(); + else + Stop(); + Configuration.Enabled = enabled; + } + + #endregion public SunfishServiceConfiguration Configuration { get; } public abstract string Description { get; } - public ConfigurationScreen ConfigurationScreen => GetConfigurationScreen(); + internal protected SunfishServiceConfigurator Configurator => configurator; + public bool Enabled { get => Configuration.Enabled; set => SetEnabled(value); } } } diff --git a/Sunfish/Sunfish/SunfishServiceConfiguration.cs b/Sunfish/Sunfish/SunfishServiceConfiguration.cs index bbbaf56..7e654f7 100644 --- a/Sunfish/Sunfish/SunfishServiceConfiguration.cs +++ b/Sunfish/Sunfish/SunfishServiceConfiguration.cs @@ -6,12 +6,20 @@ namespace DolphinWebXplorer2 { - class SunfishServiceConfiguration + public class SunfishServiceConfiguration { + public string GetConf(string key, string def = null) + { + string value; + if (Settings.TryGetValue(key, out value)) + return value; + return def; + } + public string Type { get; set; } public string Name { get; set; } public bool Enabled { get; set; } - public bool BasePath { get; set; } - public Dictionary conf { get; set; } = new Dictionary(); + public string Location { get; set; } + public Dictionary Settings { get; set; } = new Dictionary(); } }