[gmime] Implemented signer/recipient collections



commit 8cd26b5133552dc36d6936a7b8710c9fb4106eae
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Wed Mar 16 19:27:55 2011 -0400

    Implemented signer/recipient collections
    
    2011-03-16  Jeffrey Stedfast  <fejj novell com>
    
    	* mono/CryptoRecipientCollection.cs:
    	* mono/SignerCollection.cs: New collections for Signers and
    	CryptoRecipients.
    
    	* mono/SignatureValidity.custom:
    	* mono/DecryptionResult.custom: Don't return IEnumerators for
    	signers/recipients, return the appropriate Collections instead.

 mono/CryptoRecipientCollection.cs |  189 +++++++++++++++++++++++++++++++++++++
 mono/SignerCollection.cs          |  189 +++++++++++++++++++++++++++++++++++++
 2 files changed, 378 insertions(+), 0 deletions(-)
---
diff --git a/mono/CryptoRecipientCollection.cs b/mono/CryptoRecipientCollection.cs
new file mode 100644
index 0000000..7695925
--- /dev/null
+++ b/mono/CryptoRecipientCollection.cs
@@ -0,0 +1,189 @@
+using System;
+using System.Collections;
+using System.Runtime.InteropServices;
+
+namespace GMime {
+	public class CryptoRecipientCollection : IList {
+		CryptoRecipient[] recipients;
+		
+		internal CryptoRecipientCollection (DecryptionResult result)
+		{
+			CryptoRecipient recipient;
+			int count = 0;
+			int i = 0;
+			
+			recipient = GetFirstRecipient (result);
+			while (recipient != null) {
+				recipient = recipient.Next ();
+				count++;
+			}
+			
+			recipients = new CryptoRecipient [count];
+			recipient = GetFirstRecipient (result);
+			while (recipient != null) {
+				recipients[i++] = recipient;
+				recipient = recipient.Next ();
+			}
+		}
+		
+		public int Count {
+			get { return recipients.Length; }
+		}
+		
+		public bool IsFixedSize {
+			get { return true; }
+		}
+		
+		public bool IsReadOnly {
+			get { return true; }
+		}
+		
+		public bool IsSynchronized {
+			get { return false; }
+		}
+		
+		public object SyncRoot {
+			get { return this; }
+		}
+		
+		int IList.Add (object value)
+		{
+			throw new InvalidOperationException ("Cannot add recipients to a CryptoRecipientCollection.");
+		}
+		
+		public void Clear ()
+		{
+			throw new InvalidOperationException ("Cannot clear a CryptoRecipientCollection.");
+		}
+		
+		public bool Contains (CryptoRecipient recipient)
+		{
+			if (recipient == null)
+				return false;
+			
+			for (int i = 0; i < Count; i++) {
+				if (recipients[i] == recipient)
+					return true;
+			}
+			
+			return false;
+		}
+		
+		bool IList.Contains (object value)
+		{
+			return Contains (value as CryptoRecipient);
+		}
+		
+		public void CopyTo (Array array, int index)
+		{
+			if (array == null)
+				throw new ArgumentNullException ("array");
+			
+			if (index < 0)
+				throw new ArgumentOutOfRangeException ("index");
+			
+			for (int i = 0; i < Count; i++)
+				array.SetValue (((IList) this)[i], index + i);
+		}
+		
+		public IEnumerator GetEnumerator ()
+		{
+			return new CryptoRecipientIterator (this);
+		}
+		
+		public int IndexOf (CryptoRecipient recipient)
+		{
+			if (recipient == null)
+				return -1;
+			
+			for (int i = 0; i < Count; i++) {
+				if (recipients[i] == recipient)
+					return i;
+			}
+			
+			return -1;
+		}
+		
+		int IList.IndexOf (object value)
+		{
+			return IndexOf (value as CryptoRecipient);
+		}
+		
+		void IList.Insert (int index, object value)
+		{
+			throw new InvalidOperationException ("Cannot insert recipients into a CryptoRecipientCollection.");
+		}
+		
+		void IList.Remove (object value)
+		{
+			throw new InvalidOperationException ("Cannot remove recipients from a CryptoRecipientCollection.");
+		}
+		
+		void IList.RemoveAt (int index)
+		{
+			throw new InvalidOperationException ("Cannot remove recipients from a CryptoRecipientCollection.");
+		}
+		
+		public CryptoRecipient this[int index] {
+			get {
+				if (index > Count)
+					throw new ArgumentOutOfRangeException ("index");
+				
+				return recipients[index];
+			}
+			
+			set {
+				throw new InvalidOperationException ("Cannot set recipients in a CryptoRecipientCollection.");
+			}
+		}
+		
+		object IList.this[int index] {
+			get {
+				return this[index];
+			}
+			
+			set {
+				throw new InvalidOperationException ("Cannot set recipients in a CryptoRecipientCollection.");
+			}
+		}
+		
+		[DllImport("gmime")]
+		static extern IntPtr g_mime_decryption_result_get_recipients(IntPtr raw);
+		
+		static CryptoRecipient GetFirstRecipient (DecryptionResult valididty)
+		{
+			IntPtr rv = g_mime_decryption_result_get_recipients (valididty.Handle);
+			
+			if (rv == IntPtr.Zero)
+				return null;
+			
+			return (CryptoRecipient) GLib.Opaque.GetOpaque (rv, typeof (CryptoRecipient), false);
+		}
+		
+		internal class CryptoRecipientIterator : IEnumerator {
+			CryptoRecipientCollection recipients;
+			int index = -1;
+			
+			public CryptoRecipientIterator (CryptoRecipientCollection recipients)
+			{
+				this.recipients = recipients;
+			}
+			
+			public object Current {
+				get { return index >= 0 ? recipients[index] : null; }
+			}
+			
+			public void Reset ()
+			{
+				index = -1;
+			}
+			
+			public bool MoveNext ()
+			{
+				index++;
+				
+				return index < recipients.Count;
+			}
+		}
+	}
+}
diff --git a/mono/SignerCollection.cs b/mono/SignerCollection.cs
new file mode 100644
index 0000000..3f67e79
--- /dev/null
+++ b/mono/SignerCollection.cs
@@ -0,0 +1,189 @@
+using System;
+using System.Collections;
+using System.Runtime.InteropServices;
+
+namespace GMime {
+	public class SignerCollection : IList {
+		Signer[] signers;
+		
+		internal SignerCollection (SignatureValidity validity)
+		{
+			Signer signer;
+			int count = 0;
+			int i = 0;
+			
+			signer = GetFirstSigner (validity);
+			while (signer != null) {
+				signer = signer.Next ();
+				count++;
+			}
+			
+			signers = new Signer [count];
+			signer = GetFirstSigner (validity);
+			while (signer != null) {
+				signers[i++] = signer;
+				signer = signer.Next ();
+			}
+		}
+		
+		public int Count {
+			get { return signers.Length; }
+		}
+		
+		public bool IsFixedSize {
+			get { return true; }
+		}
+		
+		public bool IsReadOnly {
+			get { return true; }
+		}
+		
+		public bool IsSynchronized {
+			get { return false; }
+		}
+		
+		public object SyncRoot {
+			get { return this; }
+		}
+		
+		int IList.Add (object value)
+		{
+			throw new InvalidOperationException ("Cannot add signers to a SignerCollection.");
+		}
+		
+		public void Clear ()
+		{
+			throw new InvalidOperationException ("Cannot clear a SignerCollection.");
+		}
+		
+		public bool Contains (Signer signer)
+		{
+			if (signer == null)
+				return false;
+			
+			for (int i = 0; i < Count; i++) {
+				if (signers[i] == signer)
+					return true;
+			}
+			
+			return false;
+		}
+		
+		bool IList.Contains (object value)
+		{
+			return Contains (value as Signer);
+		}
+		
+		public void CopyTo (Array array, int index)
+		{
+			if (array == null)
+				throw new ArgumentNullException ("array");
+			
+			if (index < 0)
+				throw new ArgumentOutOfRangeException ("index");
+			
+			for (int i = 0; i < Count; i++)
+				array.SetValue (((IList) this)[i], index + i);
+		}
+		
+		public IEnumerator GetEnumerator ()
+		{
+			return new SignerIterator (this);
+		}
+		
+		public int IndexOf (Signer signer)
+		{
+			if (signer == null)
+				return -1;
+			
+			for (int i = 0; i < Count; i++) {
+				if (signers[i] == signer)
+					return i;
+			}
+			
+			return -1;
+		}
+		
+		int IList.IndexOf (object value)
+		{
+			return IndexOf (value as Signer);
+		}
+		
+		void IList.Insert (int index, object value)
+		{
+			throw new InvalidOperationException ("Cannot insert signers into a SignerCollection.");
+		}
+		
+		void IList.Remove (object value)
+		{
+			throw new InvalidOperationException ("Cannot remove signers from a SignerCollection.");
+		}
+		
+		void IList.RemoveAt (int index)
+		{
+			throw new InvalidOperationException ("Cannot remove signers from a SignerCollection.");
+		}
+		
+		public Signer this[int index] {
+			get {
+				if (index > Count)
+					throw new ArgumentOutOfRangeException ("index");
+				
+				return signers[index];
+			}
+			
+			set {
+				throw new InvalidOperationException ("Cannot set signers in a SignerCollection.");
+			}
+		}
+		
+		object IList.this[int index] {
+			get {
+				return this[index];
+			}
+			
+			set {
+				throw new InvalidOperationException ("Cannot set signers in a SignerCollection.");
+			}
+		}
+		
+		[DllImport("gmime")]
+		static extern IntPtr g_mime_signature_validity_get_signers(IntPtr raw);
+		
+		static Signer GetFirstSigner (SignatureValidity valididty)
+		{
+			IntPtr rv = g_mime_signature_validity_get_signers (valididty.Handle);
+			
+			if (rv == IntPtr.Zero)
+				return null;
+			
+			return (Signer) GLib.Opaque.GetOpaque (rv, typeof (Signer), false);
+		}
+		
+		internal class SignerIterator : IEnumerator {
+			SignerCollection signers;
+			int index = -1;
+			
+			public SignerIterator (SignerCollection signers)
+			{
+				this.signers = signers;
+			}
+			
+			public object Current {
+				get { return index >= 0 ? signers[index] : null; }
+			}
+			
+			public void Reset ()
+			{
+				index = -1;
+			}
+			
+			public bool MoveNext ()
+			{
+				index++;
+				
+				return index < signers.Count;
+			}
+		}
+	}
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]