⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmprotocols.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using GPCore.GibphoneControls;
using System.Collections;
using GPCore.Protocols;

namespace GPCore.Forms
{
    /// <summary>
    /// A form that displays the user's accounts and allows the addition,
    /// modification and removal thereof.
    /// </summary>
    internal partial class frmProtocols : Form
    {
        /// <summary>
        /// Creates a new <see cref="frmProtocols"/>.
        /// </summary>
        public frmProtocols()
        {
            InitializeComponent();
            Core.onProtocolAdded += new Core.ProtocolAddedEventDelegate(Core_onProtocolAdded);
        }

        void Core_onProtocolAdded(bool AutoLoad, string password, ProtocolEventArgs e)
        {
            Protocol p = Core.GetProtocol(e.Username, e.Type.FullName);

            ListViewItem li = new ListViewItem();
            listView.Items.Add(li);

            CheckBox b = new CheckBox();
            b.Tag = li;
            b.Checked = p.Connected;
            b.CheckedChanged += new EventHandler(onlinechecked);

            listView.AddEmbeddedControl(b, 0, li.Index);
            b = new CheckBox();
            b.Tag = li;
            b.Checked = AutoLoad;
            b.CheckedChanged += new EventHandler(autochecked);

            listView.AddEmbeddedControl(b, 1, li.Index);

            li.SubItems.Add(new ListViewItem.ListViewSubItem());
            li.SubItems.Add(e.Username);
            li.SubItems.Add(p.ToString());
            li.Tag = p;
            Sql.ExecuteNonQuery("insert into protocols (Username, Pass, ProtocolType,Autoload) values (?,?,?,?)", p.Username, password, e.Type.FullName, (AutoLoad ? 1 : 0));
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddProtocol f = new frmAddProtocol();
            f.Owner = this;
            f.Show();
        }

        private void onlinechecked(object sender, EventArgs e)
        {
            CheckBox b = sender as CheckBox;
            ListViewItem li = b.Tag as ListViewItem;
            Protocol p = li.Tag as Protocol;
//            if (!loaded
            if (b.Checked && p is IAuthenticate)
            {
                IAuthenticate a = p as IAuthenticate;
                if (!a.PasswordSet)
                    a.Password = PasswordBox.Show("Please Enter the password for\n" + Core.ProtocolToString(p), "Password").Text;
                a.SignOn();
                
            }
            else if ( p is IAuthenticate)
            {
                if (p.Connected) (p as IAuthenticate).SignOff();
            }
        }

        private void autochecked(object sender, EventArgs e)
        {
            CheckBox b = sender as CheckBox;
            ListViewItem li = b.Tag as ListViewItem;
            Protocol p = li.Tag as Protocol;
            Sql.ExecuteNonQuery("update protocols set Autoload = ? where Username = ? and ProtocolType = ?", (b.Checked ? 1 : 0), p.Username, p.GetType().FullName);
        }

        private void frmAccounts_Load(object sender, EventArgs e)
        {
            foreach (Protocol p in Core.Protocols)
            {
                //XmlElement elem = XMLHelper.GetElement(p);
                //if (elem == null) continue;
                ListViewItem li = new ListViewItem();
                li.Tag = p;
                listView.Items.Add(li);

                CheckBox b = new CheckBox();
                listView.AddEmbeddedControl(b, 0, li.Index);
                b.Checked = p.Connected;
                b.CheckedChanged += new EventHandler(onlinechecked);
                b.Tag = li;
                b = new CheckBox();
                listView.AddEmbeddedControl(b, 1, li.Index);
                object o = Sql.ExecuteScalar("select Autoload from Protocols where Username = ? and Protocoltype = ?", p.Username, p.GetType().FullName);
                b.Checked = (o == null ? false : ((int)o) == 1);
                b.CheckedChanged += new EventHandler(autochecked);
                b.Tag = li;
                li.SubItems.Add(new ListViewItem.ListViewSubItem());
                li.SubItems.Add(p.Username);
                li.SubItems.Add(p.ToString());
            }

            if (listView.Items.Count > 0)
                listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //TODO: Remove everything from the contacts file

            foreach (ListViewItem li in listView.SelectedItems)
            {
                Core.DestroyInstance(li.Tag as Protocol);
                listView.Items.Remove(li);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented in this version");
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -