Exemple #1
0
 private void toolStripMenuItem1_Click(object sender, EventArgs e)
 {
     using (var dlg = new SearchDlg())
     {
         dlg.SearchParameters = _searchParameters;
         if (dlg.ShowDialog() == DialogResult.OK)
         {
             _searchParameters = dlg.SearchParameters;
             EventCallbackSystem.InvokeCallback("HostsListUpdated", false);
         }
     }
 }
Exemple #2
0
 private void addNewHostToolStripMenuItem_Click(object sender, EventArgs e)
 {
     using (var dlg = new AddEditHostDlg())
     {
         if (dlg.ShowDialog() == DialogResult.OK)
         {
             if (dlg.LocalDomain)
             {
                 HostsDatabase.Add(true, dlg.DomainName, dlg.IPAddress, dlg.Description, dlg.HiddenDomain);
                 HostsDatabase.Save();
                 EventCallbackSystem.InvokeCallback("HostsListUpdated", true);
             }
             else
             {
                 _client.SendAddDomain(dlg.DomainName, dlg.IPAddress, dlg.Description, dlg.HiddenDomain);
             }
         }
     }
 }
Exemple #3
0
        private void LvLocalHosts_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvLocalHosts.SelectedItems.Count == 1)
            {
                var entry = lvLocalHosts.SelectedItems[0].Tag as HostEntry;

                using (var dlg = new AddEditHostDlg(entry, true))
                {
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        HostsDatabase.Remove(true, entry.Name);
                        HostsDatabase.Add(true, dlg.DomainName, dlg.IPAddress, dlg.Description, dlg.HiddenDomain);
                        HostsDatabase.Save();

                        EventCallbackSystem.InvokeCallback("HostsListUpdated", true);
                    }
                }
            }
        }
Exemple #4
0
        private void LvLocalHosts_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                LvLocalHosts_MouseDoubleClick(sender, null);
            }
            else if (e.KeyCode == Keys.Delete)
            {
                e.Handled = true;

                if (lvLocalHosts.SelectedItems.Count > 0)
                {
                    var str =
                        lvLocalHosts.SelectedItems.Count > 1 ?
                        "Are you sure you wish to delete " + lvLocalHosts.SelectedItems.Count + " local domains?\nThis action cannot be undone." :
                        "Are you sure you wish to delete this local domain?\nThis action cannot be undone.";

                    var res = MessageBox.Show(
                        str,
                        "Confirm remove local domain(s)",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning);

                    if (res == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvLocalHosts.SelectedItems)
                        {
                            // remove manually
                            HostsDatabase.Remove(true, (lvi.Tag as HostEntry).Name);
                        }

                        HostsDatabase.Save();
                        EventCallbackSystem.InvokeCallback("HostsListUpdated", true);
                    }
                }
            }
        }
Exemple #5
0
        private void RegisterEvents()
        {
            EventCallbackSystem.RegisterCallback <ClientAuthorizedEvent>("ClientAuthorized", () =>
            {
                Logger.Log(LogType.Debug, "Authorized to server.");
            });

            EventCallbackSystem.RegisterCallback <HostsListUpdatedEvent>("HostsListUpdated", (changed) =>
            {
                lvHosts.BeginUpdate();
                lvLocalHosts.BeginUpdate();
                lvHosts.Items.Clear();
                lvLocalHosts.Items.Clear();
                HostsDatabase.Lock();

                var lviList = new List <ListViewItem>(Math.Max(HostsDatabase.Hosts.Count(), HostsDatabase.LocalHosts.Count()));

                foreach (var host in HostsDatabase.Hosts)
                {
                    if ((Configuration.ShowHiddenHosts ||
                         !host.Hidden) &&
                        _searchParameters.Matches(host))
                    {
                        var lvi = new ListViewItem(host.Name);
                        lvi.SubItems.Add(host.IP);
                        lvi.SubItems.Add(host.Description);
                        lvi.Tag = host;
                        //lvHosts.Items.Add(lvi);
                        lviList.Add(lvi);
                    }
                }
                lvHosts.Items.AddRange(lviList.ToArray());
                if (lvHosts.Items.Count < HostsDatabase.Hosts.Count())
                {
                    gbHosts.Text = string.Format(
                        "Domains ({0} of {1})",
                        lvHosts.Items.Count.FormatNumber(),
                        HostsDatabase.Hosts.Count().FormatNumber());
                }
                else
                {
                    gbHosts.Text = string.Format("Domains ({0})", lvHosts.Items.Count.FormatNumber());
                }
                lviList.Clear();

                foreach (var host in HostsDatabase.LocalHosts)
                {
                    if ((Configuration.ShowHiddenHosts ||
                         !host.Hidden) &&
                        _searchParameters.Matches(host))
                    {
                        var lvi = new ListViewItem(host.Name);
                        lvi.SubItems.Add(host.IP);
                        lvi.SubItems.Add(host.Description);
                        lvi.Tag = host;
                        //lvLocalHosts.Items.Add(lvi);
                        lviList.Add(lvi);
                    }
                }
                lvLocalHosts.Items.AddRange(lviList.ToArray());
                if (lvLocalHosts.Items.Count < HostsDatabase.LocalHosts.Count())
                {
                    gbLocalHosts.Text = string.Format(
                        "Local domains ({0} of {1})",
                        lvLocalHosts.Items.Count.FormatNumber(),
                        HostsDatabase.LocalHosts.Count().FormatNumber());
                }
                else
                {
                    gbLocalHosts.Text = string.Format("Local domains ({0})", lvLocalHosts.Items.Count.FormatNumber());
                }

                HostsDatabase.Unlock();
                lvHosts.EndUpdate();
                lvLocalHosts.EndUpdate();
                //Logger.Log(LogType.Debug, "Hosts list updated.");

                UpdateInterface();

                if (changed)      // only synchronize if the hostsdatabase was literally changed - ignore searching
                {
                    _synchronizer.PostSynchronize();
                }
            });
        }
Exemple #6
0
        public ClientForm(HostsFileSynchronizer synchronizer, bool startInTray)
        {
            _synchronizer = synchronizer;
            _startInTray  = startInTray;
            InitializeComponent();
            Font = new Font(Configuration.FontFamily, Configuration.FontSize);

            lvHosts.Columns.AddRange(new ColumnHeader[]
            {
                new ColumnHeader()
                {
                    Text = "Name"
                },
                new ColumnHeader()
                {
                    Text = "IP"
                },
                new ColumnHeader()
                {
                    Text = "Description"
                }
            });
            lvLocalHosts.Columns.AddRange(new ColumnHeader[]
            {
                new ColumnHeader()
                {
                    Text = "Name"
                },
                new ColumnHeader()
                {
                    Text = "IP"
                },
                new ColumnHeader()
                {
                    Text = "Description"
                }
            });

            hiddenDomainsToolStripMenuItem.Checked = Configuration.ShowHiddenHosts;
            hiddenDomainsToolStripMenuItem.Click  += (sender, e) =>
            {
                Configuration.ShowHiddenHosts          = !Configuration.ShowHiddenHosts;
                hiddenDomainsToolStripMenuItem.Checked = Configuration.ShowHiddenHosts;
                EventCallbackSystem.InvokeCallback("HostsListUpdated", false);
            };

            RegisterEvents();

            do
            {
                if (!_client.Connect(Configuration.Address, Configuration.Port))
                {
                    var res = MessageBox.Show(
                        "Failed to connect to " + Configuration.Address + ":" + Configuration.Port +
                        "\nThe configuration window will now open.\nCanceling will terminate the application.",
                        "WHD",
                        MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Warning);

                    if (res != DialogResult.OK)
                    {
                        Environment.Exit(0);
                    }

                    using (var dlg = new ClientConfigurationDlg())
                    {
                        if (dlg.ShowDialog() != DialogResult.OK)
                        {
                            Environment.Exit(0);
                        }

                        Configuration.Address = dlg.Address;
                        Configuration.Port    = dlg.Port;
                        Configuration.Key     = dlg.Key;
                    }
                }
            }while (!_client.IsConnected);

            new Timer()
            {
                Interval = 100, Enabled = true
            }.Tick += (sender, e) =>
            {
                _client.Process();
                Logger.ProcessMessage((msg) =>
                {
                    AddLog(string.Format(
                               "[{0}][{1}] {2}",
                               msg.UtcDate.ToLocalTime().ToString("HH:mm:ss"),
                               Enum.GetName(typeof(LogType), msg.Type),
                               msg.Text));
                });
            };

            UpdateInterface();
            Resize += (sender, e) => UpdateInterface();

            lvHosts.KeyDown               += LvHosts_KeyDown;
            lvLocalHosts.KeyDown          += LvLocalHosts_KeyDown;
            lvHosts.MouseDoubleClick      += LvHosts_MouseDoubleClick;
            lvLocalHosts.MouseDoubleClick += LvLocalHosts_MouseDoubleClick;

            notifyIcon1.MouseDoubleClick += (sender, e) => Show();

            Shown       += ClientForm_Shown;
            FormClosing += ClientForm_FormClosing;

            startWithWindowsToolStripMenuItem.Checked = Configuration.StartWithWindows;

            lvHosts.ListViewItemSorter      = new HostsListViewSorter();
            lvLocalHosts.ListViewItemSorter = new HostsListViewSorter();

            lvHosts.ColumnClick      += (sender, e) => ColumnClickHandler(lvHosts, e.Column);
            lvLocalHosts.ColumnClick += (sender, e) => ColumnClickHandler(lvLocalHosts, e.Column);
        }