/// <summary>
 /// Gets a list of all form fields
 /// </summary>
 /// <returns></returns>
 public Field[] GetFields()
 {
     ElementList nl = SelectElements(typeof(Field));
     Field[] fields = new Field[nl.Count];
     int i = 0;
     foreach (Element e in nl)
     {
         fields[i] = (Field)e;
         i++;
     }
     return fields;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="field"></param>
 public Field AddField(Field field)
 {
     AddChild(field);
     return field;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <returns></returns>
 public Field AddField()
 {
     Field f = new Field();
     AddChild(f);
     return f;
 }
        /// <summary>
        /// 设置聊天室的参数
        /// </summary>
        public void SetConfiguration()
        {
            IQ IqSetRquest = new IQ(IqType.set);
            IqSetRquest.Namespace = null;
            IqSetRquest.Id = CSS.IM.XMPP.Id.GetNextId();
            IqSetRquest.To = this.MJid;

            CSS.IM.XMPP.protocol.Base.Query query = new CSS.IM.XMPP.protocol.Base.Query();
            query.Namespace = CSS.IM.XMPP.Uri.MUC_OWNER;

            Data query_x = new Data(XDataFormType.submit);

            Field field1 = new Field();
            field1.Var = "FORM_TYPE";
            field1.Type = FieldType.Hidden;
            field1.AddValue(Features.FEAT_MUC_ROOMCONFIG);
            query_x.AddField(field1);

            Field field2 = new Field();
            field2.Var = "muc#roomconfig_roomname";
            field2.Type = FieldType.Text_Single;
            field2.AddValue(txt_name.Texts.Trim());
            query_x.AddField(field2);

            Field field3 = new Field();
            field3.Var = "muc#roomconfig_roomdesc";
            field3.Type = FieldType.Text_Single;
            field3.AddValue(txt_tm.Texts.Trim());
            query_x.AddField(field3);

            Field field4 = new Field();
            field4.Var = "muc#roomconfig_persistentroom";
            field4.Type = FieldType.Boolean;
            field4.AddValue(txt_gd.Checked == true ? "1" : "0");
            query_x.AddField(field4);

            Field field5 = new Field();
            field5.Var = "muc#roomconfig_passwordprotectedroom";
            field5.Type = FieldType.Boolean;
            field5.AddValue(txt_sy.Checked == true ? "1" : "0");
            query_x.AddField(field5);

            Field field6 = new Field();
            field6.Var = "muc#roomconfig_roomsecret";
            field6.Type = FieldType.Text_Private;
            field6.AddValue(txt_pswd2.Texts.ToString());
            query_x.AddField(field6);

            Field field7 = new Field();
            field7.Var = "muc#roomconfig_roomowners";
            field7.Type = FieldType.Jid_Multi;
            field7.AddValue(XmppCon.MyJID.ToString());
            query_x.AddField(field7);

            query.AddChild(query_x);
            IqSetRquest.AddChild(query);
            XmppCon.IqGrabber.SendIq(IqSetRquest, new IqCB(SetConfigurationCell), null, true);
        }
        private void btn_submit_Click(object sender, EventArgs e)
        {
            //fields.RemoveTag("title");
            //fields.RemoveTag("instructions");

            //selectValue = fields.GetField("muc#roomconfig_maxusers").GetValue();
            //Option[] options = fields.GetField("muc#roomconfig_maxusers").GetOptions();
            //string[] cbb_roomconfig_maxusersValue = new string[options.Length];

            //chb_moderator

            //chb_participant

            //chb_visitor

            //for (int i = 0; i < options.Length; i++)
            //{
            //    cbb_roomconfig_maxusersValue[i] = options[i].GetValue();
            //}

            //cbb_roomconfig_maxusers.Items = cbb_roomconfig_maxusersValue;
            //cbb_roomconfig_maxusers.SelectItem = selectValue;

            fields.GetField("muc#roomconfig_roomsecret").SetValue(chb_roomconfig_roomsecret.Texts);//设置密码

            IQ iqSendSet = new IQ();
            iqSendSet.Namespace = null;
            iqSendSet.Id = CSS.IM.XMPP.Id.GetNextId();
            iqSendSet.To = to_jid;
            iqSendSet.Type = IqType.set;
            Query query = new Query();
            query.Namespace = CSS.IM.XMPP.Uri.MUC_OWNER;

            Data datas = new Data();
            datas.Namespace = CSS.IM.XMPP.Uri.X_DATA;
            datas.Type = XDataFormType.submit;
            //1
            Field FORM_TYPE = new Field();
            FORM_TYPE.Type = FieldType.Hidden;
            FORM_TYPE.Var = "FORM_TYPE";
            FORM_TYPE.SetValue(@"http://jabber.org/protocol/muc#roomconfig");
            datas.AddField(FORM_TYPE);
            //2
            Field roomconfig_roomname = new Field();
            roomconfig_roomname.Type = FieldType.Text_Single;
            roomconfig_roomname.Var = "muc#roomconfig_roomname";
            roomconfig_roomname.SetValue(txt_roomconfig_roomname.Texts.ToString());
            datas.AddField(roomconfig_roomname);
            //3
            Field roomconfig_roomdesc = new Field();
            roomconfig_roomdesc.Type = FieldType.Text_Single;
            roomconfig_roomdesc.Var = "muc#roomconfig_roomdesc";
            roomconfig_roomdesc.SetValue(txt_roomconfig_roomdesc.Texts.ToString());
            datas.AddField(roomconfig_roomdesc);
            //4
            Field roomconfig_changesubject = new Field();
            roomconfig_changesubject.Type = FieldType.Boolean;
            roomconfig_changesubject.Var = "muc#roomconfig_changesubject";
            roomconfig_changesubject.SetValueBool(chb_roomconfig_changesubject.Checked);
            datas.AddField(roomconfig_changesubject);
            //5
            Field roomconfig_maxusers = new Field();
            roomconfig_maxusers.Type = FieldType.List_Single;
            roomconfig_maxusers.Var = "muc#roomconfig_maxusers";
            roomconfig_maxusers.SetValue(cbb_roomconfig_maxusers.SelectItem.ToString());
            datas.AddField(roomconfig_maxusers);
            //6
            Field roomconfig_publicroom = new Field();
            roomconfig_publicroom.Type = FieldType.Boolean;
            roomconfig_publicroom.Var = "muc#roomconfig_publicroom";
            roomconfig_publicroom.SetValueBool(chb_roomconfig_publicroom.Checked);
            datas.AddField(roomconfig_publicroom);
            //7
            Field roomconfig_persistentroom = new Field();
            roomconfig_persistentroom.Type = FieldType.Boolean;
            roomconfig_persistentroom.Var = "muc#roomconfig_persistentroom";
            roomconfig_persistentroom.SetValueBool(chb_roomconfig_persistentroom.Checked);
            datas.AddField(roomconfig_persistentroom);
            //8
            Field roomconfig_moderatedroom = new Field();
            roomconfig_moderatedroom.Type = FieldType.Boolean;
            roomconfig_moderatedroom.Var = "muc#roomconfig_moderatedroom";
            roomconfig_moderatedroom.SetValueBool(chb_roomconfig_moderatedroom.Checked);
            datas.AddField(roomconfig_moderatedroom);
            //9
            Field roomconfig_membersonly = new Field();
            roomconfig_membersonly.Type = FieldType.Boolean;
            roomconfig_membersonly.Var = "muc#roomconfig_membersonly";
            roomconfig_membersonly.SetValueBool(chb_roomconfig_membersonly.Checked);
            datas.AddField(roomconfig_membersonly);
            //10
            Field roomconfig_allowinvites = new Field();
            roomconfig_allowinvites.Type = FieldType.Boolean;
            roomconfig_allowinvites.Var = "muc#roomconfig_allowinvites";
            roomconfig_allowinvites.SetValueBool(chb_roomconfig_allowinvites.Checked);
            datas.AddField(roomconfig_allowinvites);
            //11
            Field roomconfig_passwordprotectedroom = new Field();
            roomconfig_passwordprotectedroom.Type = FieldType.Boolean;
            roomconfig_passwordprotectedroom.Var = "muc#roomconfig_passwordprotectedroom";
            roomconfig_allowinvites.SetValueBool(chb_roomconfig_passwordprotectedroom.Checked);
            datas.AddField(roomconfig_passwordprotectedroom);
            //12
            Field roomconfig_whois = new Field();
            roomconfig_whois.Type = FieldType.Text_Single;
            roomconfig_whois.Var = "muc#roomconfig_whois";
            roomconfig_whois.SetValue(cbb_roomconfig_whois.SelectItem.ToString());
            datas.AddField(roomconfig_whois);
            //13
            Field roomconfig_enablelogging = new Field();
            roomconfig_enablelogging.Type = FieldType.Boolean;
            roomconfig_enablelogging.Var = "muc#roomconfig_enablelogging";
            roomconfig_enablelogging.SetValueBool(chb_roomconfig_enablelogging.Checked);
            datas.AddField(roomconfig_enablelogging);
            //14
            Field roomconfig_reservednick = new Field();
            roomconfig_reservednick.Type = FieldType.Boolean;
            roomconfig_reservednick.Var = "x-muc#roomconfig_reservednick";
            roomconfig_reservednick.SetValueBool(chb_roomconfig_reservednick.Checked);
            datas.AddField(roomconfig_reservednick);
            //15
            Field roomconfig_canchangenick = new Field();
            roomconfig_canchangenick.Type = FieldType.Boolean;
            roomconfig_canchangenick.Var = "x-muc#roomconfig_canchangenick";
            roomconfig_canchangenick.SetValueBool(chb_roomconfig_canchangenick.Checked);
            datas.AddField(roomconfig_canchangenick);
            //16
            Field roomconfig_registration = new Field();
            roomconfig_registration.Type = FieldType.Boolean;
            roomconfig_registration.Var = "x-muc#roomconfig_registration";
            roomconfig_registration.SetValueBool(chb_roomconfig_registration.Checked);
            datas.AddField(roomconfig_registration);

            Field roomconfig_roomadmins = new Field();
            roomconfig_roomadmins.Type = FieldType.Jid_Multi;
            roomconfig_roomadmins.Var = "muc#roomconfig_roomadmins";

            string[] itemsToStrings = new string[list_roomconfig_roomadmins.Items.Count];
            for (int i = 0; i <  list_roomconfig_roomadmins.Items.Count; i++)
            {
                itemsToStrings[i] =  list_roomconfig_roomadmins.Items[i].Text;
            }

            roomconfig_roomadmins.SetValues(itemsToStrings);
            datas.AddField(roomconfig_roomadmins);

            Field roomconfig_roomowners = new Field();
            roomconfig_roomowners.Type = FieldType.Jid_Multi;
            roomconfig_roomowners.Var = "muc#roomconfig_roomowners";

            if (list_roomconfig_roomowners.Items.Count>0)
            {
                itemsToStrings = new string[list_roomconfig_roomowners.Items.Count];
                for (int i = 0; i < list_roomconfig_roomowners.Items.Count; i++)
                {
                    itemsToStrings[i] = list_roomconfig_roomowners.Items[i].Text;
                }
            }
            else
            {
                string[] selectValues = fields.GetField("muc#roomconfig_roomadmins").GetValues();
                itemsToStrings = new string[selectValues.Length];

                for (int i = 0; i < selectValues.Length; i++)
                {
                    itemsToStrings[i] = selectValues[i];
                }
            }

            roomconfig_roomowners.SetValues(itemsToStrings);
            datas.AddField(roomconfig_roomowners);

            foreach (Field item in fields.GetFields())
            {
                datas.AddField(item);
            }

            query.AddChild(datas);
            iqSendSet.AddChild(query);

            System.Diagnostics.Debug.WriteLine("发送的数据:\n" + iqSendSet.ToString());
            XMPPConn.IqGrabber.SendIq(iqSendSet,new IqCB(SendSetingCell),null,true);
        }
        private void btn_find_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //<field type="boolean" var="Email"><value>1</value></field>
            //<field type="boolean" var="Name"><value>1</value></field>
            //<field type="boolean" var="Username"><value>1</value></field>
            //<field type="text-single" var="search"><value>*</value></field>
            //<field type="hidden" var="FORM_TYPE"><value>jabber:iq:search</value></field>

            Data data = new Data(XDataFormType.submit);
            Field fEmail = new Field(FieldType.Boolean);
            fEmail.SetValueBool(true);
            fEmail.Var = "Email";
            data.AddField(fEmail);

            Field fName = new Field(FieldType.Boolean);
            fName.SetValueBool(true);
            fName.Var = "Name";
            data.AddField(fName);

            Field fUsername = new Field(FieldType.Boolean);
            fUsername.SetValueBool(true);
            fUsername.Var = "Username";
            data.AddField(fUsername);

            Field fWhare = new Field(FieldType.Text_Single);
            if (cmb_findtype.Items[cmb_findtype.SelectIndex].ToString() != "全部")
            {
                fWhare.AddValues(new String[] { txt_userName.Texts.Trim().ToString() });
            }
            else
            {
                fWhare.AddValues(new String[] { "*" });
            }

            fWhare.Var = "search";
            data.AddField(fWhare);

            Field fsearch = new Field(FieldType.Hidden);

            fsearch.AddValue("jabber:iq:search");
            fsearch.Var = "FORM_TYPE";

            data.AddField(fsearch);

            IQ siq = siq = new SearchIq();
            ((SearchIq)siq).Query.Data = data;

            //directory

            if (Util.Services.Finds.Count==0)
            {
                 MsgBox.Show(this, "CSS&IM", "服务器没有注册Findes请与管理员联系!", MessageBoxButtons.OK);
                 return;
            }

            if (Util.Services.Finds[0]!=null)
            {
                try
                {
                    siq.To = Util.Services.Finds[0];
                    siq.Type = IqType.set;
                    m_XmppCon.IqGrabber.SendIq(siq, new IqCB(OnSearchResult), null, true);
                }
                catch (Exception)
                {

                }

            }
        }