// clsUpgrade.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
//using devX;
using System.Windows.Forms;
using System.Threading;



namespace ZANZI.Upgrade
{
	/// <summary>
	/// Summary description for clsUpgrade.
	/// </summary>
	public class clsUpgrade
	{
		#region Variable declaration

		#endregion

		#region Structures

		#endregion

		#region Functions

		/// <summary>Main entry for clsUpgrade</summary>
		/// <returns>Takes no variables and returns nothing</returns>
		///<c>public clsUpgrade()</c>
		
		public clsUpgrade()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public bool CheckUpgrade(bool autostart,Thread tt)
		{
			try
			{
			 
				devX.AutoUpgrade  au; // = new ZANZI.Upgrade.AutoUpgrade();
			    if (System.Configuration.ConfigurationSettings.AppSettings != null)
			        au = devX.AutoUpgrade.Create(System.Configuration.ConfigurationSettings.AppSettings["ZANZI.Manifest"].ToString(),true);

			    if(au.IsUpgradeAvailable(false))
				{

					tt.Abort();
					Thread.Sleep(100);
					DialogResult dr =  MessageBox.Show("do you want to download new updates now?","New updates",MessageBoxButtons.YesNo );


					if(dr == DialogResult.Yes)
					{
						
						try
						{
							au.StartUpgradeStub();
						}

						catch (System.Net.WebException  ex)
						{

							//there is no connectivity
							GUIControlLogic.Error.clsExeption exx = new GUIControlLogic.Error.clsExeption(ex);
				
							MessageBox.Show("Cannot connect to the update server, check connection and firewall","No connection");
							return false;
	
						}

						Application.Exit();
					}
					else
					{
					return false;
					}

				}
				else
				{
					return false;
				}


			}

			catch (Exception  ex)
			{
				//The underlying connection was closed: Unable to connect to the remote server.    
				GUIControlLogic.Error.clsExeption exx = new GUIControlLogic.Error.clsExeption(ex);
				return false;

			}

			return true;

		}

		private void Terminate(object sender, EventArgs e)
		{
			//MessageBox.Show("The application will now restart");
			System.Windows.Forms.Application.Exit();
		}

		#endregion

		#region Menu handlers

		#endregion

		#region Custom Windows Form Designer generated code

		#endregion

		#region Events

		#endregion

	}
}
