Perferences.cs :  » Network-Clients » iReaper » IReaper » Configurations » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » iReaper 
iReaper » IReaper » Configurations » Perferences.cs
/*
 * 
 * 
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IReaper.Properties;
using System.Net;
using IReaper.Net;
using System.Threading;


namespace IReaper.Configurations{
    public partial class Perferences : Form
    {
        Settings currentSettings;
        static Perferences()
        {
            Settings.Default.SettingChanging += FileMonitor.CheckRootPath;
        }

        public Perferences()
        {
            InitializeComponent();
            currentSettings = new Settings();
            currentSettings.Reload();
        }


        private void OK(object sender, EventArgs e)
        {
            if (this.Validate())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //load connection
            LoadConnections();
            //load proxy
            LoadProxy();
            //load general
            LoadGeneral();
            //load browser
            LoadBrowser();
            //Load Path
            LoadPath();
        }        

        protected override void OnClosed(EventArgs e)
        {
            if (this.DialogResult == DialogResult.OK)
            {
                CopySettings(currentSettings, Settings.Default);
                Settings.Default.Save();
            }
            base.OnClosed(e);
        }

        private static void CopySettings(Settings src, Settings des)
        {
            des.MaxThread = src.MaxThread;
            des.ProxyCredentials = src.ProxyCredentials;
            des.ProxyURI = src.ProxyURI;
            des.ProxySelector = src.ProxySelector;
            des.ConnectTimeout = src.ConnectTimeout;
            des.ReadTimeout = src.ReadTimeout;
            des.Retry = src.Retry;
            des.RetryInterval = src.RetryInterval;
            des.AutoUnzip = src.AutoUnzip;
            des.RootPath = src.RootPath;
            des.UseInternalWebBrowser = src.UseInternalWebBrowser;
            des.RemindAllNewCourse = src.RemindAllNewCourse;
            des.AutoRSSUpdate = src.AutoRSSUpdate;
            des.RssUpdateInterval = src.RssUpdateInterval;
            des.RemoveItemsOlderThan = src.RemoveItemsOlderThan;
            des.PathPolicy = src.PathPolicy;
        }

        #region loading
        private void LoadConnections()
        {
            //connect
            if (currentSettings.ConnectTimeout == System.Threading.Timeout.Infinite)
            {
                this.textBoxConnectTimeout.Text = "";
                this.textBoxConnectTimeout.Enabled = false;
                this.checkBoxConnectInfinite.Checked = true;
            }
            else
            {
                this.checkBoxConnectInfinite.Checked = false;
                this.textBoxConnectTimeout.Enabled = true;
                this.textBoxConnectTimeout.Text = (currentSettings.ConnectTimeout / 1000).ToString();
            
            }
            //read
            if (currentSettings.ReadTimeout == System.Threading.Timeout.Infinite)
            {
                this.textBoxReadTimeout.Text = "";
                this.textBoxReadTimeout.Enabled = false;
                this.checkBoxReadInfinite.Checked = true;
            }
            else
            {
                this.checkBoxReadInfinite.Checked = false;
                this.textBoxReadTimeout.Enabled = true;
                this.textBoxReadTimeout.Text = (currentSettings.ConnectTimeout / 1000).ToString();
            }
        }

        private void LoadProxy()
        {
            Net.ProxySelector selector = (Net.ProxySelector)currentSettings.ProxySelector;
            switch (selector)
            { 
                case IReaper.Net.ProxySelector.IE:
                    this.radioBtnIE.Checked = true;
                    break;
                case IReaper.Net.ProxySelector.None:
                    this.radioBtnDirect.Checked = true;
                    break;
                case IReaper.Net.ProxySelector.UserSpecified:
                    this.radioBtnHttp.Checked = true;
                    break;
            }
            //set the user specified
            string proxyUriString = currentSettings.ProxyURI;
            string proxyCredentialString = currentSettings.ProxyCredentials;
            //
            Uri uri;
            Uri.TryCreate(proxyUriString, UriKind.Absolute, out uri);
            if (uri == null)
                return;
            this.textBoxAddress.Text = uri.Host;
            this.textBoxPort.Text = uri.Port.ToString();
            //get the credential
            if (proxyCredentialString != null)
            {
                string[] spliter = proxyCredentialString.Split(':');
                if (spliter.Length == 2)
                {
                    this.textBoxUserName.Text = spliter[0];
                    this.textBoxPassword.Text = spliter[1];
                }
            }
        }

        private void LoadGeneral()
        {
            this.numericMaxThread.Value = currentSettings.MaxThread;
            this.numericRetry.Value = currentSettings.Retry;
            this.textBoxRetryInterval.Text = currentSettings.RetryInterval.ToString();
            this.textBoxPath.Text = currentSettings.RootPath;
            this.checkBoxAutoDecompress.Checked = currentSettings.AutoUnzip;
        }

        private void LoadBrowser()
        {
            this.radioButtonUseInternalBrowser.Checked = currentSettings.UseInternalWebBrowser;
            this.radioButtonUseExternalBrowser.Checked = !currentSettings.UseInternalWebBrowser;
        }


        private void LoadPath()
        {
            this.pathPolicyControl1.Policy = (IReaper.PathManager.PathPolicyType)currentSettings.PathPolicy;
        }
        #endregion


        private void infiniteChanged(object sender, EventArgs e)
        {
            if (sender == this.checkBoxConnectInfinite)
            {
                this.textBoxConnectTimeout.Enabled = !this.checkBoxConnectInfinite.Checked;
                if (this.checkBoxConnectInfinite.Checked)
                {
                    currentSettings.ConnectTimeout = Timeout.Infinite;
                }
            }
            else if (sender == this.checkBoxReadInfinite)
            {
                this.textBoxReadTimeout.Enabled = !this.checkBoxReadInfinite.Checked;
                if (this.checkBoxReadInfinite.Checked)
                {
                    currentSettings.ReadTimeout = Timeout.Infinite;
                }
            }
        }

        private void HttpChecked(object sender, EventArgs e)
        {
            this.panelProxySettings.Enabled = this.radioBtnHttp.Checked;
        }

        private void validateHttpProxy(object sender, CancelEventArgs e)
        {
            //
            if (this.radioBtnDirect.Checked)
            {
                currentSettings.ProxySelector = (short)ProxySelector.None;
            }
            //
            else if (this.radioBtnHttp.Checked) 
            {
                Uri uri = null;
                try
                {
                    //
                    uri = new Uri("http://" + this.textBoxAddress.Text + ":" +  int.Parse(this.textBoxPort.Text));
                }
                catch(Exception ex)
                {
                    this.errorProvider1.SetError(this.textBoxAddress, ex.Message);
                    e.Cancel = true;
                    return;
                }
                try
                {
                    //
                    if (this.textBoxPassword.Text.Length == 0 &&
                        this.textBoxUserName.Text.Length == 0)
                    {
                        currentSettings.ProxyCredentials = this.textBoxUserName + ":" + this.textBoxPassword.Text;
                    }
                    currentSettings.ProxyURI = uri.ToString();
                    currentSettings.ProxySelector = (short)ProxySelector.UserSpecified;
                }
                catch (Exception ex)
                {
                    this.errorProvider1.SetError(this.textBoxUserName,ex.Message);
                    e.Cancel = true;
                    return;
                }
            }
            else if (this.radioBtnIE.Checked)
            {
                currentSettings.ProxySelector = (short)ProxySelector.IE;
            }
            else
            {
                e.Cancel = true;
                this.errorProvider1.SetError(this.groupBoxProxy, Resources.NoProxyError);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValidateTimeout(object sender, CancelEventArgs e)
        {
            //validat connect
            if (this.checkBoxConnectInfinite.Checked)
                currentSettings.ConnectTimeout = Timeout.Infinite;
            else
            {
                try
                {
                    currentSettings.ConnectTimeout = int.Parse(this.textBoxConnectTimeout.Text) * 1000;
                }
                catch (Exception ex)
                {
                    this.errorProvider1.SetError(this.groupBoxTimeout, ex.Message);
                    e.Cancel = true;
                    return;
                }
            }

            //validate read
            if (this.checkBoxReadInfinite.Checked)
                currentSettings.ReadTimeout = Timeout.Infinite;
            else
            {
                try
                {
                    currentSettings.ReadTimeout = int.Parse(this.textBoxReadTimeout.Text) * 1000;
                }
                catch (Exception ex)
                {
                    this.errorProvider1.SetError(this.groupBoxTimeout, ex.Message);
                    e.Cancel = true;
                    return;
                }
            }
        }

        /// <summary>
        /// General
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValidateGeneral(object sender, CancelEventArgs e)
        {
            currentSettings.RootPath = this.textBoxPath.Text;
            currentSettings.MaxThread = (int)numericMaxThread.Value;
            currentSettings.AutoUnzip = this.checkBoxAutoDecompress.Checked;
            try
            {
                currentSettings.Retry = (uint)this.numericRetry.Value;
            }
            catch (Exception ex)
            {
                this.errorProvider1.SetError(this.numericRetry, ex.Message);
                e.Cancel = true;
                return;
            }
            try
            {
                currentSettings.RetryInterval = int.Parse(this.textBoxRetryInterval.Text);
            }
            catch (Exception ex)
            {
                this.errorProvider1.SetError(this.textBoxRetryInterval, ex.Message);
                e.Cancel = true;
                return;
            }
            
        }

        private void SelectFolder(object sender, EventArgs e)
        {
            if (DialogResult.OK == this.folderBrowserDialog1.ShowDialog())
            {
                this.textBoxPath.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValidateBrowser(object sender, CancelEventArgs e)
        {
            bool useInternal = this.radioButtonUseInternalBrowser.Checked;
            bool useExternal = this.radioButtonUseExternalBrowser.Checked;
            if (useInternal == useExternal)
            {
                MessageBox.Show(Resources.SetBrowserWarning);
                e.Cancel = true;
            }
            currentSettings.UseInternalWebBrowser = useInternal;
            return;
        }


        private void ValidatePathPolicy(object sender, CancelEventArgs e)
        {
            currentSettings.PathPolicy = (int)this.pathPolicyControl1.Policy;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.