Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using BSE.Platten.BO;
using System.Globalization;
using BSE.Platten.Common.Properties;

namespace BSE.Platten.Common
{
    /// <summary>
    /// Represents a panel in a StatusStrip control that checks the availability of the database host.
    /// </summary>
    public partial class CToolStripStatusLabelCheckDB : ToolStripStatusLabel
    {
        #region Events
        /// <summary>
        /// Occurs when the HostAvailability has changed.
        /// </summary>
        public event EventHandler<HostAvailableEventArgs> HostAvailabilityChanged;

        #endregion

        #region FieldsPrivate

        private System.Windows.Forms.Timer m_timerCheckDb;
        private bool m_bHostIsAvailable;

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the environment object for the application
        /// </summary>
        public CEnvironment Environment
        {
            get;
            set;
        }
        /// <summary>
        /// Gets or sets the DatabaseHostAvailability object for the application
        /// </summary>
        public DatabaseHostAvailability DatabaseHostAvailability
        {
            get;
            set;
        }
        #endregion

        #region MethodsPublic
        /// <summary>
        /// Initializes a new instance of the CToolStripStatusLabelCheckDB class.
        /// </summary>
        public CToolStripStatusLabelCheckDB()
        {
            InitializeComponent();
            this.Image = Properties.Resources.Network;
            this.Enabled = false;
            this.m_timerCheckDb = new System.Windows.Forms.Timer();
            this.m_timerCheckDb.Interval = 10000;
            this.m_timerCheckDb.Tick += new System.EventHandler(this.TimerCheckDbTick);
        }
        /// <summary>
        /// Initializes a new instance of the CToolStripStatusLabelCheckDB class.
        /// </summary>
        /// <param name="environment">The <see cref="CEnvironment"/> object.</param>
        public CToolStripStatusLabelCheckDB(CEnvironment environment)
            : this()
        {
            if (environment == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException, "environment"));
            }
            this.Environment = environment;
        }
        /// <summary>
        /// Checks whether the database host is available.
        /// </summary>
        /// <param name="environment">The <see cref="CEnvironment"/> object.</param>
        /// <returns>True, if the database is available else false.</returns>
        public bool IsHostAvailable(CEnvironment environment)
        {
            this.Environment = environment;
            if (this.m_timerCheckDb.Enabled == false)
            {
                this.m_timerCheckDb.Enabled = true;
            }
            DatabaseHostAvailability databaseHostAvailability = GetDatabaseHostAvailability(this.Environment);
            if (databaseHostAvailability != null)
            {
                this.DatabaseHostAvailability = databaseHostAvailability;
                this.m_bHostIsAvailable = databaseHostAvailability.IsAvailable;
                SetLabelProperties(databaseHostAvailability);
                return this.m_bHostIsAvailable;
            }
            return false;
        }
        /// <summary>
        /// Gets a <see cref="DatabaseHostAvailability"/> objects with the availability properties for the database.
        /// </summary>
        /// <param name="environment">The <see cref="CEnvironment"/> object.</param>
        /// <returns>The <see cref="DatabaseHostAvailability"/> object with the database properties</returns>
        public static DatabaseHostAvailability GetDatabaseHostAvailability(CEnvironment environment)
        {
            DatabaseHostAvailability databaseHostAvailability = null;
            CUserGrant userGrant = null;
            if (environment != null)
            {
                string strConnection = environment.GetConnectionString();
                CHostBusinessObject hostBusinessObject = new CHostBusinessObject(strConnection);
                bool bHostIsAvailable = hostBusinessObject.IsHostAvailable();
                if (bHostIsAvailable)
                {
                    CBSEAdminBusinessObject adminBusinessObject = new CBSEAdminBusinessObject(strConnection);
                    userGrant = adminBusinessObject.GetUserGrant(environment.UserName);
                }

                databaseHostAvailability = new DatabaseHostAvailability
                {
                    IsAvailable = bHostIsAvailable,
                    UserGrant = userGrant
                };
            }
            return databaseHostAvailability;
        }

        #endregion

        #region MethodsPrivate

        private void TimerCheckDbTick(object sender, System.EventArgs e)
        {
            using (CheckDataBaseHostThread checkDataBaseHostThread = new CheckDataBaseHostThread(
                this.Environment))
            {
                checkDataBaseHostThread.HostChecked += new EventHandler<HostAvailableEventArgs>(CheckDataBaseHostChecked);
            }
        }

        private void CheckDataBaseHostChecked(object sender, HostAvailableEventArgs e)
        {
            using (CheckDataBaseHostThread checkDataBaseHostThread = sender as CheckDataBaseHostThread)
            {
                if (checkDataBaseHostThread != null)
                {
                    checkDataBaseHostThread.HostChecked -= new EventHandler<HostAvailableEventArgs>(CheckDataBaseHostChecked);
                }
            }
            DatabaseHostAvailability databaseHostAvailability = e.DatabaseHostAvailability;
            if (databaseHostAvailability != null)
            {
                bool bHostIsAvailable = databaseHostAvailability.IsAvailable;
                string strConnectionString = this.Environment.GetConnectionString();
                if (bHostIsAvailable.Equals(this.m_bHostIsAvailable) == false)
                {
                    this.m_bHostIsAvailable = bHostIsAvailable;
                    SetLabelProperties(databaseHostAvailability);
                    if (this.HostAvailabilityChanged != null)
                    {
                        this.HostAvailabilityChanged(this, e);
                    }
                }
            }
        }

        private void SetLabelProperties(DatabaseHostAvailability databaseHostAvailability)
        {
            if (databaseHostAvailability != null)
            {
                this.Enabled = databaseHostAvailability.IsAvailable;
                this.Environment.UserGrant = databaseHostAvailability.UserGrant;
                string strText = String.Format(CultureInfo.CurrentUICulture,
                        Properties.Resources.IDS_ToolStripStatusLabelCheckDBHostAndPort,
                        this.Environment.GetDataBaseHost(),
                        this.Environment.GetDataBasePort());
                this.Text = strText;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions