Example #1
0
        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem selectedItem = GetSelectedEntry();

            if (selectedItem == null)
            {
                return;
            }

            String     hostname = (String)selectedItem.Tag;
            HostsEntry entry    = HostsFileManager.FindEntry(hostname);

            if (entry == null)
            {
                return;
            }

            EditDialog dialog = new EditDialog(entry);

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                MarkUnsavedChanges();
                this.Refresh(false);
            }
        }
Example #2
0
        public EditDialog(HostsEntry entry = null)
        {
            InitializeComponent();

            this.entry = entry;

            if (this.entry == null)
            {
                this.Text = "Add new entry";
            }
            else
            {
                this.textBox1.Text = entry.Host;
                this.textBox2.Text = entry.Address;
            }
        }
Example #3
0
        public EditDialog(HostsEntry entry = null)
        {
            InitializeComponent();

            this.entry = entry;

            if (this.entry == null)
            {
                this.Text = "Add new entry";
            }
            else
            {
                this.textBox1.Text = entry.Host;
                this.textBox2.Text = entry.Address;
            }
        }
Example #4
0
        private void toggleEnabledToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem selectedItem = GetSelectedEntry();

            if (selectedItem == null)
            {
                return;
            }

            String     hostname = (String)selectedItem.Tag;
            HostsEntry entry    = HostsFileManager.FindEntry(hostname);

            if (entry == null)
            {
                return;
            }

            entry.Enabled = !entry.Enabled;
            MarkUnsavedChanges();
        }
Example #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress parsedIp = IPAddress.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Please enter a valid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (textBox1.TextLength == 0)
            {
                MessageBox.Show("Please fill in a hostname.", "Missing hostname", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            bool createNew = false;

            if (entry == null)
            {
                createNew = true;

                entry = new HostsEntry();
                entry.Enabled = true;
            }

            entry.Host = textBox1.Text;
            entry.Address = textBox2.Text;

            if (createNew)
            {
                HostsFileManager.Entries.Add(entry);
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Example #6
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress parsedIp = IPAddress.Parse(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Please enter a valid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (textBox1.TextLength == 0)
            {
                MessageBox.Show("Please fill in a hostname.", "Missing hostname", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            bool createNew = false;

            if (entry == null)
            {
                createNew = true;

                entry         = new HostsEntry();
                entry.Enabled = true;
            }

            entry.Host    = textBox1.Text;
            entry.Address = textBox2.Text;

            if (createNew)
            {
                HostsFileManager.Entries.Add(entry);
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Example #7
0
        public static string[] GenerateHostsLines()
        {
            List <string> outLines  = new List <string>();
            List <string> seenHosts = new List <string>();

            var raw = File.ReadAllLines(Filename);

            foreach (string line in raw)
            {
                bool isDisabledEntry = line.StartsWith(DISABLED_INDICATOR);

                if (line.StartsWith("#") && !isDisabledEntry)
                {
                    outLines.Add(line);
                    continue;
                }

                string aLine = line;

                if (isDisabledEntry)
                {
                    aLine = line.Substring(DISABLED_INDICATOR.Length).Trim();
                }

                string[] parts = aLine
                                 .Replace('\t', ' ')
                                 .Trim()
                                 .Split(' ');

                if (parts.Length == 2)
                {
                    String address = parts[0];
                    String host    = parts[1];

                    if (seenHosts.Contains(host))
                    {
                        continue;
                    }

                    seenHosts.Add(host);

                    HostsEntry customEntry = FindEntry(host);

                    // If we do not have a mutation for this line it has been deleted in the editor (or added after saving)
                    if (customEntry == null)
                    {
                        continue;
                    }
                    // If we DO have an entry, it might have been mutated by the editor, so provide an alternate version of this line
                    else
                    {
                        StringBuilder customLine = new StringBuilder();

                        if (!customEntry.Enabled)
                        {
                            customLine.Append(DISABLED_INDICATOR);
                        }

                        customLine.Append(customEntry.Address);
                        customLine.Append(" ");
                        customLine.Append(customEntry.Host);

                        outLines.Add(customLine.ToString());
                    }
                }
                else
                {
                    outLines.Add(line);
                }
            }

            foreach (HostsEntry customEntry in Entries)
            {
                if (seenHosts.Contains(customEntry.Host))
                {
                    continue;
                }

                StringBuilder customLine = new StringBuilder();

                if (!customEntry.Enabled)
                {
                    customLine.Append(DISABLED_INDICATOR);
                }

                customLine.Append(customEntry.Address);
                customLine.Append(" ");
                customLine.Append(customEntry.Host);

                outLines.Add(customLine.ToString());
            }

            return(outLines.ToArray());
        }