www.pudn.com > lphant-v1.00-src.zip > uploadsListView.cs


#region Copyright (c)2003 Juanjo < http://lphant.sourceforge.net > 
/* 
* This file is part of eLePhant 
* Copyright (C)2003 Juanjo < j_u_a_n_j_o@users.sourceforge.net / http://lphant.sourceforge.net > 
*  
* 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., 675 Mass Ave, Cambridge, MA 02139, USA. 
*/ 
#endregion 
 
using System; 
using System.ComponentModel; 
using System.Collections; 
using System.Diagnostics; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using eLePhant.eDonkey; 
namespace eLePhant.Client 
{ 
	///  
	/// Summary description for uploadsListView. 
	///  
	public class uploadsListView : eLePhantListView 
	{ 
		///  
		/// Required designer variable. 
		///  
		private CkernelGateway krnGateway; 
		private ContextMenu ContextMenuUploads; 
		private ArrayList itemsToRemove; 
		private ColumnSorter TheColumnSorter; 
		private System.ComponentModel.Container components = null; 
		private string[] m_States; 
		 
		public void Initilize(CkernelGateway in_krnGateway) 
		{ 
			Name = "uploadsList"; 
			krnGateway=in_krnGateway; 
			Columns.Add("Name",90,false); 
			Columns.Add("File Name",190,false); 
			Columns.Add("Speed",60,false); 
			Columns.Add("Uploaded",60,false); 
			Columns.Add("Downloaded",60,false); 
			Columns.Add("Status",60,false); 
			Columns.Add("Progress",120,true); 
			Columns.Add("Software",60,false); 
			Columns.Add("Version",60,false); 
			krnGateway.OnNewUpload+=new ClientEvent(m_InvokeOnNewUpload); 
			krnGateway.OnRefreshUploads+=new RefreshEvent(m_InvokeRefreshList); 
 
			ContextMenuUploads = new ContextMenu(); 
			ContextMenuUploads.MenuItems.Add("Send message",new EventHandler(OnSendMessage)); 
			ContextMenuUploads.MenuItems.Add("View files",new EventHandler(OnViewFiles)); 
			ContextMenuUploads.MenuItems.Add("Add to friends",new EventHandler(OnAddFriend)); 
			ContextMenuUploads.Popup+=new System.EventHandler(myContextMenuPopup); 
 
			ContextMenu=ContextMenuUploads; 
			TheColumnSorter=new ColumnSorter(); 
			this.ClickHeader+=new HeaderEventHandler(miColumnClick); 
			this.DoubleClick+=new EventHandler(OnSendMessage); 
			m_DefaultWidths=new int[] {90,190,60,60,60,60,190,60,60}; 
			m_LoadWidths(); 
			m_Globalize(); 
		} 
		 
		private void m_Globalize() 
		{ 
			Columns[0].Text=eLePhantForm.Globalization["LBL_NAME"]; 
			Columns[1].Text=eLePhantForm.Globalization["LBL_FILENAME"]; 
			Columns[2].Text=eLePhantForm.Globalization["LBL_SPEED"]; 
			Columns[3].Text=eLePhantForm.Globalization["LBL_UPLOADED"]; 
			Columns[4].Text=eLePhantForm.Globalization["LBL_DOWNLOADED"]; 
			Columns[5].Text=eLePhantForm.Globalization["LBL_STATUS"]; 
			Columns[6].Text=eLePhantForm.Globalization["LBL_PROGRESS"]; 
			Columns[7].Text=eLePhantForm.Globalization["LBL_SOFTWARE"]; 
			Columns[8].Text=eLePhantForm.Globalization["LBL_VERSION"]; 
			ContextMenuUploads.MenuItems[0].Text=eLePhantForm.Globalization["LBL_SENDMESSAGE"]; 
			ContextMenuUploads.MenuItems[1].Text=eLePhantForm.Globalization["LBL_VIEWFILES"]; 
			ContextMenuUploads.MenuItems[2].Text=eLePhantForm.Globalization["LBL_ADDFRIEND"]; 
			m_States=new string[] { 
									  eLePhantForm.Globalization["LBL_NONE"], 
									  eLePhantForm.Globalization["LBL_PENDING"], 
									  eLePhantForm.Globalization["LBL_ONQUEUE"], 
									  "", 
									  eLePhantForm.Globalization["LBL_UPLOADING"], 
									  "","","", 
									  eLePhantForm.Globalization["LBL_CONNECTING_LOWID"], 
									  eLePhantForm.Globalization["LBL_CONNECTING"] 
									}; 
		} 
	 
		public void Globalize() 
		{ 
			m_Globalize(); 
			this.Refresh(); 
		} 
 
		private void myContextMenuPopup(object sender, System.EventArgs e) 
		{											 
			ContextMenuUploads.MenuItems[0].DefaultItem=true; 
			if (SelectedItems.Count==0) 
			{ 
				ContextMenuUploads.MenuItems[0].Enabled=false; 
				ContextMenuUploads.MenuItems[1].Enabled=false; 
				ContextMenuUploads.MenuItems[2].Enabled=false; 
			} 
			if (SelectedItems.Count==1) 
			{								 
				ContextMenuUploads.MenuItems[0].Enabled=true; 
				ContextMenuUploads.MenuItems[1].Enabled=true; 
				ContextMenuUploads.MenuItems[2].Enabled=true;	 
			} 
 
			if (SelectedItems.Count>1) 
			{ 
				ContextMenuUploads.MenuItems[0].Enabled=false; 
				ContextMenuUploads.MenuItems[1].Enabled=false; 
				ContextMenuUploads.MenuItems[2].Enabled=false;	 
			} 
		} 
 
		private void miColumnClick(object sender, HeaderEventArgs e) 
		{ 
			TheColumnSorter.CurrentColumn = e.ColumnIndex; 
			TheColumnSorter.sortOrder=this.sortOrder; 
			this.ListViewItemSorter = TheColumnSorter;	 
			this.Sort(); 
			this.ListViewItemSorter = null;			 
		} 
		private void OnSendMessage(object sender, System.EventArgs e) 
		{ 
			if (this.SelectedItems.Count<=0) return; 
			InterfaceClient source=(InterfaceClient)SelectedItems[0].Tag; 
			krnGateway.StartChatSession(source); 
		} 
 
		private void OnViewFiles(object sender, System.EventArgs e) 
		{ 
			if (this.SelectedItems.Count<=0) return; 
			InterfaceClient source=(InterfaceClient)SelectedItems[0].Tag; 
			krnGateway.RequestSharedList(source.ID,source.Port,source.ServerIP,source.UserHash); 
		} 
		private void OnAddFriend(object sender, System.EventArgs e) 
		{ 
			if (this.SelectedItems.Count<=0) return; 
			InterfaceClient source=(InterfaceClient)SelectedItems[0].Tag; 
			krnGateway.AddFriend(source); 
		} 
		protected override void OnDrawItem(int item,int column,Graphics g,Rectangle rec) 
		{ 
			InterfaceClient source=(InterfaceClient)(Items[item].Tag); 
			 
			Color b=Color.Red; 
			Color end; 
			LinearGradientBrush chunkBrush; 
 
			int xpos = rec.Left; 
			int ChunkLength; 
			 
			if (source.UpFileChunks==null)  
			{ 
				//draw empty rect 
				g.FillRectangle(new SolidBrush(this.BackColor),rec); 
				return; 
			} 
 
			for (int i=0;im_States.Length) return "Unknown"; 
			return m_States[status]; 
		} 
		 
		private void ClientToItem(InterfaceClient client,ListViewItem itemUpload) 
		{ 
			if (client==null) return; 
			if (itemUpload.SubItems[0].Text!=client.Name)  
				itemUpload.SubItems[0].Text=client.Name; 
			if (itemUpload.SubItems[1].Text!=client.UpFileName)  
				itemUpload.SubItems[1].Text=client.UpFileName; 
			if (itemUpload.SubItems[2].Text!=SpeedToString(client.UploadSpeed))  
				itemUpload.SubItems[2].Text=SpeedToString(client.UploadSpeed); 
			if (itemUpload.SubItems[3].Text!=SizeToString(client.UploadedBytes))  
				itemUpload.SubItems[3].Text=SizeToString(client.UploadedBytes); 
			if (itemUpload.SubItems[4].Text!=SizeToString(client.DownloadedBytes))  
				itemUpload.SubItems[4].Text=SizeToString(client.DownloadedBytes); 
			if (itemUpload.SubItems[5].Text!=StatusToString(client.UploadState))  
				itemUpload.SubItems[5].Text=StatusToString(client.UploadState); 
			if (itemUpload.SubItems[6].Text!="")  
				itemUpload.SubItems[6].Text=""; //progress 
			if (itemUpload.SubItems[7].Text!=client.Software)  
				itemUpload.SubItems[7].Text=client.Software; 
//			if (client.Software=="eMule") 
//			{ 
//				if (itemUpload.SubItems[8].Text!=client.Version.ToString("X"))  
//					itemUpload.SubItems[8].Text=client.Version.ToString("X"); 
//			} 
//			else 
//			{ 
			float div=100F; 
			if (client.Software=="eDonkeyHybrid") div=1000F;	 
			if (itemUpload.SubItems[8].Text!=(client.Version/div).ToString("0.00"))  
					itemUpload.SubItems[8].Text=(client.Version/div).ToString("0.00"); 
//			} 
			itemUpload.Tag=client; 
		} 
		private void m_InvokeRefreshList(CkernelGateway in_krnGateway) 
		{ 
			this.BeginInvoke(new RefreshEvent(m_RefreshList),new object[] {in_krnGateway}); 
		} 
		private void m_RefreshList(CkernelGateway in_krnGateway) 
		{ 
			int nItem=0; 
			itemsToRemove.Clear(); 
			while (nItem 
			/// Required for Windows.Forms Class Composition Designer support 
			///  
			itemsToRemove=new ArrayList(); 
			container.Add(this); 
			InitializeComponent(); 
			View = System.Windows.Forms.View.Details; 
		} 
 
		public uploadsListView() 
		{ 
			///  
			/// Required for Windows.Forms Class Composition Designer support 
			///  
			InitializeComponent(); 
			View = System.Windows.Forms.View.Details;			 
		} 
 
		#region Component Designer generated code 
		///  
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		///  
		private void InitializeComponent() 
		{ 
			components = new System.ComponentModel.Container(); 
		} 
		#endregion 
		public class ColumnSorter : IComparer 
		{ 
			public int CurrentColumn = -1; 
			public int sortOrder; 
			public int Compare(object x, object y) 
			{ 
				if ((x==null)||(y==null)) return 0; 
				ListViewItem rowA = (ListViewItem)x; 
				ListViewItem rowB = (ListViewItem)y; 
				switch(CurrentColumn) 
				{ 
					case 2: 
						if ((((InterfaceClient)rowA.Tag).UploadSpeed)>(((InterfaceClient)rowB.Tag).UploadSpeed)) 
								return sortOrder; 
							else 
								return -sortOrder; 
					case 3: 
						if ((((InterfaceClient)rowA.Tag).UploadedBytes)>(((InterfaceClient)rowB.Tag).UploadedBytes)) 
							return sortOrder; 
						else 
							return -sortOrder;	 
					 
					case 4: 
						if ((((InterfaceClient)rowA.Tag).DownloadedBytes)>(((InterfaceClient)rowB.Tag).DownloadedBytes)) 
								return sortOrder; 
							else 
								return -sortOrder; 
					case 6: 
						int a=0; 
						int b=0; 
						if (((InterfaceClient)rowA.Tag).UpFileChunks==null) return -sortOrder; 
						if (((InterfaceClient)rowB.Tag).UpFileChunks==null) return sortOrder; 
						foreach (byte ap in ((InterfaceClient)rowA.Tag).UpFileChunks) 
						{ 
							a+=ap; 
						} 
						foreach (byte bp in ((InterfaceClient)rowB.Tag).UpFileChunks) 
						{ 
							b+=bp; 
						} 
						if (a>b) 
							return sortOrder; 
						else 
							return -sortOrder;	 
					case 8: 
						if ((((InterfaceClient)rowA.Tag).Version)>(((InterfaceClient)rowB.Tag).Version)) 
								return sortOrder; 
							else 
								return -sortOrder;					 
					default: 
						return sortOrder*String.Compare(rowA.SubItems[CurrentColumn].Text, 
								rowB.SubItems[CurrentColumn].Text); 
 
				} 
				  
			} 
 
 
			public ColumnSorter() 
			{ 
 
			} 
 
		} 
	 
	} 
}