コード例 #1
0
ファイル: IOConnectionForm.cs プロジェクト: knut0815/KeePass2
        private void InitAdvancedTab()
        {
            IocProperties p = m_ioc.Properties;
            Dictionary <string, List <IocPropertyInfo> > dProps = GetPropertyInfos();

            const int d         = 7;
            int       hLabel    = m_lblUrl.Height;
            int       hTextBox  = m_tbUrl.Height;
            int       hComboBox = m_cmbCredSaveMode.Height;
            Font      f         = m_lblUrl.Font;

            Debug.Assert(m_pnlAdv.AutoScroll);
            m_pnlAdv.AutoScrollMargin = new Size(1, d);

            int wPanel = m_pnlAdv.ClientSize.Width - UIUtil.GetVScrollBarWidth() - 1;
            int wGroup = wPanel - (2 * d);
            int wCell  = (wPanel - (3 * d)) / 2;
            int xText  = d - 1;
            int xInput = d + wCell + d - 1;
            int xGroup = xText;

            if (this.RightToLeft == RightToLeft.Yes)
            {
                int iTemp = xText;
                xText  = xInput;
                xInput = iTemp;
            }

            int y   = 1;
            int iID = 0;

            m_pnlAdv.SuspendLayout();

            foreach (KeyValuePair <string, List <IocPropertyInfo> > kvp in dProps)
            {
                string strGroup = kvp.Key;
                y += d;

                Label lblGroup = new Label();
                lblGroup.Name = "cGroup" + iID.ToString(NumberFormatInfo.InvariantInfo);
                ++iID;
                lblGroup.AutoEllipsis = true;
                lblGroup.AutoSize     = false;
                lblGroup.Dock         = DockStyle.None;
                lblGroup.Location     = new Point(xGroup, y);
                lblGroup.Size         = new Size(wGroup, hLabel);
                lblGroup.Text         = strGroup;
                lblGroup.TextAlign    = ContentAlignment.MiddleLeft;
                // lblGroup.BackColor = Color.Red;
                FontUtil.AssignDefaultBold(lblGroup);

                m_pnlAdv.Controls.Add(lblGroup);
                y += hLabel;

                foreach (IocPropertyInfo pi in kvp.Value)
                {
                    string strName = pi.Name;
                    string strText = pi.DisplayName + ":";
                    Type   t       = pi.Type;
                    y += d;

                    int hText = hLabel;
                    int wRem  = TextRenderer.MeasureText(strText, f).Width;
                    while (wRem >= wCell)
                    {
                        hText += (hLabel + 1);
                        wRem  -= wCell;
                    }

                    Label lblText = new Label();
                    lblText.Name = "cText" + iID.ToString(NumberFormatInfo.InvariantInfo);
                    ++iID;
                    lblText.AutoEllipsis = true;
                    lblText.AutoSize     = false;
                    lblText.Dock         = DockStyle.None;
                    lblText.Size         = new Size(wCell, hText);
                    lblText.Text         = strText;
                    lblText.TextAlign    = ContentAlignment.MiddleLeft;
                    // lblText.BackColor = Color.Green;

                    Control cInput = null;
                    if ((t == typeof(string)) || (t == typeof(long)))
                    {
                        TextBox tb = new TextBox();
                        tb.Size = new Size(wCell, hTextBox);

                        tb.Text = (p.Get(strName) ?? string.Empty);

                        cInput = tb;
                    }
                    else if (t == typeof(bool))
                    {
                        ComboBox cmb = new ComboBox();
                        cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                        cmb.Size          = new Size(wCell, hComboBox);

                        cmb.Items.Add(KPRes.Auto);
                        cmb.Items.Add(KPRes.Yes);
                        cmb.Items.Add(KPRes.No);

                        bool?ob = p.GetBool(strName);
                        if (ob.HasValue)
                        {
                            cmb.SelectedIndex = (ob.Value ? 1 : 2);
                        }
                        else
                        {
                            cmb.SelectedIndex = 0;
                        }

                        cInput = cmb;
                    }
                    else
                    {
                        Debug.Assert(false); continue;
                    }

                    cInput.Dock = DockStyle.None;
                    cInput.Name = "cInput" + iID.ToString(NumberFormatInfo.InvariantInfo);
                    ++iID;

                    int hDiff = lblText.Height - cInput.Height;
                    if (hDiff >= 0)
                    {
                        lblText.Location = new Point(xText, y);
                        cInput.Location  = new Point(xInput, y + (hDiff / 2));

                        y += lblText.Height;
                    }
                    else
                    {
                        lblText.Location = new Point(xText, y - (hDiff / 2));
                        cInput.Location  = new Point(xInput, y);

                        y += cInput.Height;
                    }

                    m_pnlAdv.Controls.Add(lblText);
                    m_pnlAdv.Controls.Add(cInput);

                    m_lProps.Add(new KeyValuePair <IocPropertyInfo, Control>(
                                     pi, cInput));
                }
            }

            m_pnlAdv.ResumeLayout(true);
        }
コード例 #2
0
ファイル: IOConnection.cs プロジェクト: cappert/keepass2
		private static void PrepareWebAccess(IOConnectionInfo ioc)
		{
#if !KeePassUAP
			IocProperties p = ((ioc != null) ? ioc.Properties : null);
			if(p == null) { Debug.Assert(false); p = new IocProperties(); }

			try
			{
				if(m_bSslCertsAcceptInvalid)
					ServicePointManager.ServerCertificateValidationCallback =
						IOConnection.AcceptCertificate;
				else
					ServicePointManager.ServerCertificateValidationCallback = null;
			}
			catch(Exception) { Debug.Assert(false); }

			try
			{
				SecurityProtocolType spt = (SecurityProtocolType.Ssl3 |
					SecurityProtocolType.Tls);

				// The flags Tls11 and Tls12 in SecurityProtocolType have been
				// introduced in .NET 4.5 and must not be set when running under
				// older .NET versions (otherwise an exception is thrown)
				Type tSpt = typeof(SecurityProtocolType);
				string[] vSpt = Enum.GetNames(tSpt);
				foreach(string strSpt in vSpt)
				{
					if(strSpt.Equals("Tls11", StrUtil.CaseIgnoreCmp))
						spt |= (SecurityProtocolType)Enum.Parse(tSpt, "Tls11", true);
					else if(strSpt.Equals("Tls12", StrUtil.CaseIgnoreCmp))
						spt |= (SecurityProtocolType)Enum.Parse(tSpt, "Tls12", true);
				}

				ServicePointManager.SecurityProtocol = spt;
			}
			catch(Exception) { Debug.Assert(false); }

			try
			{
				bool bCurCont = ServicePointManager.Expect100Continue;
				if(!m_obDefaultExpect100Continue.HasValue)
				{
					Debug.Assert(bCurCont); // Default should be true
					m_obDefaultExpect100Continue = bCurCont;
				}

				bool bNewCont = m_obDefaultExpect100Continue.Value;
				bool? ob = p.GetBool(IocKnownProperties.Expect100Continue);
				if(ob.HasValue) bNewCont = ob.Value;

				if(bNewCont != bCurCont)
					ServicePointManager.Expect100Continue = bNewCont;
			}
			catch(Exception) { Debug.Assert(false); }
#endif
		}
コード例 #3
0
ファイル: IOConnectionForm.cs プロジェクト: knut0815/KeePass2
        private void OnBtnOK(object sender, EventArgs e)
        {
            string strUrl = m_tbUrl.Text;

            if (strUrl.IndexOf("://") < 0)
            {
                // m_ttInvalidUrl.Show(KPRes.InvalidUrl, m_tbUrl);
                MessageService.ShowWarning(strUrl, KPRes.InvalidUrl);
                this.DialogResult = DialogResult.None;
                return;
            }

            m_ioc.Path     = strUrl;
            m_ioc.UserName = m_tbUserName.Text;
            m_ioc.Password = m_tbPassword.TextEx.ReadString();

            if (m_cmbCredSaveMode.SelectedIndex == 1)
            {
                m_ioc.CredSaveMode = IOCredSaveMode.UserNameOnly;
            }
            else if (m_cmbCredSaveMode.SelectedIndex == 2)
            {
                m_ioc.CredSaveMode = IOCredSaveMode.SaveCred;
            }
            else
            {
                m_ioc.CredSaveMode = IOCredSaveMode.NoSave;
            }

            IocProperties p = m_ioc.Properties;

            foreach (KeyValuePair <IocPropertyInfo, Control> kvp in m_lProps)
            {
                IocPropertyInfo pi      = kvp.Key;
                string          strName = pi.Name;
                Type            t       = pi.Type;
                Control         c       = kvp.Value;

                try
                {
                    if (t == typeof(string))
                    {
                        TextBox tb = (c as TextBox);
                        if (tb != null)
                        {
                            p.Set(strName, tb.Text.Trim());
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else if (t == typeof(bool))
                    {
                        ComboBox cmb = (c as ComboBox);
                        if (cmb != null)
                        {
                            int iSel = cmb.SelectedIndex;
                            if (iSel == 0)
                            {
                                p.SetBool(strName, null);
                            }
                            else
                            {
                                p.SetBool(strName, (iSel == 1));
                            }
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else if (t == typeof(long))
                    {
                        TextBox tb = (c as TextBox);
                        if (tb != null)
                        {
                            string str = tb.Text.Trim();
                            if (str.Length == 0)
                            {
                                p.SetLong(strName, null);
                            }
                            else
                            {
                                // Validate and store number
                                long l = long.Parse(str, NumberFormatInfo.InvariantInfo);
                                p.SetLong(strName, l);
                            }
                        }
                        else
                        {
                            Debug.Assert(false);
                        }
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
                catch (Exception ex)
                {
                    string strMsg = KPRes.Field + @" '" + pi.DisplayName +
                                    @"':" + MessageService.NewLine + ex.Message;
                    // if(!VistaTaskDialog.ShowMessageBox(strMsg, KPRes.ValidationFailed,
                    //	PwDefs.ShortProductName, VtdIcon.Warning, this))
                    MessageService.ShowWarning(strMsg);

                    this.DialogResult = DialogResult.None;
                    return;
                }
            }

            if (m_bTestConnection && !m_bSave)
            {
                if (!TestConnectionEx())
                {
                    this.DialogResult = DialogResult.None;
                }
            }
        }
コード例 #4
0
ファイル: IOConnection.cs プロジェクト: cappert/keepass2
		internal static void ConfigureWebRequest(WebRequest request,
			IOConnectionInfo ioc)
		{
			if(request == null) { Debug.Assert(false); return; } // No throw

			IocProperties p = ((ioc != null) ? ioc.Properties : null);
			if(p == null) { Debug.Assert(false); p = new IocProperties(); }

			IHasIocProperties ihpReq = (request as IHasIocProperties);
			if(ihpReq != null)
			{
				IocProperties pEx = ihpReq.IOConnectionProperties;
				if(pEx != null) p.CopyTo(pEx);
				else ihpReq.IOConnectionProperties = p.CloneDeep();
			}

			if(IsHttpWebRequest(request))
			{
				// WebDAV support
#if !KeePassUAP
				request.PreAuthenticate = true; // Also auth GET
#endif
				if(string.Equals(request.Method, WebRequestMethods.Http.Post,
					StrUtil.CaseIgnoreCmp))
					request.Method = WebRequestMethods.Http.Put;

#if !KeePassUAP
				HttpWebRequest hwr = (request as HttpWebRequest);
				if(hwr != null)
				{
					string strUA = p.Get(IocKnownProperties.UserAgent);
					if(!string.IsNullOrEmpty(strUA)) hwr.UserAgent = strUA;
				}
				else { Debug.Assert(false); }
#endif
			}
#if !KeePassUAP
			else if(IsFtpWebRequest(request))
			{
				FtpWebRequest fwr = (request as FtpWebRequest);
				if(fwr != null)
				{
					bool? obPassive = p.GetBool(IocKnownProperties.Passive);
					if(obPassive.HasValue) fwr.UsePassive = obPassive.Value;
				}
				else { Debug.Assert(false); }
			}
#endif

#if !KeePassUAP
			// Not implemented and ignored in Mono < 2.10
			try
			{
				request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
			}
			catch(NotImplementedException) { }
			catch(Exception) { Debug.Assert(false); }
#endif

			try
			{
				IWebProxy prx;
				if(GetWebProxy(out prx)) request.Proxy = prx;
			}
			catch(Exception) { Debug.Assert(false); }

#if !KeePassUAP
			long? olTimeout = p.GetLong(IocKnownProperties.Timeout);
			if(olTimeout.HasValue && (olTimeout.Value >= 0))
				request.Timeout = (int)Math.Min(olTimeout.Value, (long)int.MaxValue);

			bool? ob = p.GetBool(IocKnownProperties.PreAuth);
			if(ob.HasValue) request.PreAuthenticate = ob.Value;
#endif
		}