[gmime] Implemented HeaderEnumerator



commit c1b3ac49cfa663be6d66bbd4703007d5591ccb53
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Sun Aug 14 21:46:00 2011 -0400

    Implemented HeaderEnumerator
    
    2011-08-14  Jeffrey Stedfast  <fejj gnome org>
    
    	* mono/HeaderEnumerator.cs: New IEnumerator class returned by
    	HeaderList.GetEnumerator(). Wraps HeaderIter, providing a more
    	C#-like experience.

 ChangeLog                |    6 +++
 mono/GMime.metadata      |   15 ++++++-
 mono/Header.cs           |   21 +++++++++
 mono/HeaderEnumerator.cs |  113 ++++++++++++++++++++++++++++++++++++++++++++++
 mono/HeaderList.custom   |   17 +++++++
 mono/Makefile.am         |    6 ++-
 6 files changed, 176 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7f20169..de1b4b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2011-08-14  Jeffrey Stedfast  <fejj gnome org>
 
+	* mono/HeaderEnumerator.cs: New IEnumerator class returned by
+	HeaderList.GetEnumerator(). Wraps HeaderIter, providing a more
+	C#-like experience.
+
+2011-08-14  Jeffrey Stedfast  <fejj gnome org>
+
 	* mono/GMime.metadata: Rename AppendHeader() to Append(),
 	PrependHeader() to Prepend(), RemoveHeader() to Remove(), etc.
 
diff --git a/mono/GMime.metadata b/mono/GMime.metadata
index 10d5968..3002c45 100644
--- a/mono/GMime.metadata
+++ b/mono/GMime.metadata
@@ -186,13 +186,26 @@
   <remove-node path="/api/namespace/struct[ cname='GMimeHeader']"/>
 
   <!-- HeaderList -->
+  <!-- We're going to implement the IEnumerable interface -->
+  <add-node path="/api/namespace/object[ cname='GMimeHeaderList']">
+    <implements>
+      <interface name="IEnumerable"/>
+    </implements>
+  </add-node>
   <remove-node path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ cname='g_mime_header_list_foreach']"/>
   <attr path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='SetStream']" name="hidden">true</attr>
   <attr path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='GetStream']" name="hidden">true</attr>
   <remove-node path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='Get']"/>
   <remove-node path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='Set']"/>
-  <attr path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='GetIter']/*/parameter[ name='iter']" name="pass_as">out</attr>
+  <remove-node path="/api/namespace/struct[ cname='GMimeHeaderList']/method[ name='GetIter']"/>
+  
+  <!-- HeaderIter -->
+  <!-- This should really be made to be internal, but I don't know how to do that... -->
   <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']/method[ name='SetValue']/return-type" name="type">void</attr>
+  <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']/method[ name='First']" name="name">MoveFirst</attr>
+  <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']/method[ name='Last']" name="name">MoveLast</attr>
+  <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']/method[ name='Next']" name="name">MoveNext</attr>
+  <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']/method[ name='Prev']" name="name">MovePrev</attr>
   <attr path="/api/namespace/struct[ cname='GMimeHeaderIter']" name="opaque">true</attr>
   <remove-node path="/api/namespace/struct[ cname='GMimeHeaderIter']/field[ cname='hdrlist']"/>
   <remove-node path="/api/namespace/struct[ cname='GMimeHeaderIter']/field[ cname='version']"/>
diff --git a/mono/Header.cs b/mono/Header.cs
new file mode 100644
index 0000000..702e603
--- /dev/null
+++ b/mono/Header.cs
@@ -0,0 +1,21 @@
+using System;
+
+namespace GMime {
+	public class Header {
+		public Header () { }
+		
+		public Header (string name, string value)
+		{
+			Value = value;
+			Name = name;
+		}
+		
+		public string Name {
+			get; set;
+		}
+		
+		public string Value {
+			get; set;
+		}
+	}
+}
diff --git a/mono/HeaderEnumerator.cs b/mono/HeaderEnumerator.cs
new file mode 100644
index 0000000..11431a6
--- /dev/null
+++ b/mono/HeaderEnumerator.cs
@@ -0,0 +1,113 @@
+using System;
+using System.Collections;
+
+namespace GMime {
+	public class HeaderEnumerator : IEnumerator, IDisposable {
+		bool premove = true;
+		HeaderList headers;
+		HeaderIter iter;
+		
+		public HeaderEnumerator (HeaderList headers)
+		{
+			if (headers == null)
+				throw new ArgumentNullException ("headers");
+			
+			iter = headers.GetIter ();
+			this.headers = headers;
+		}
+		
+		public Header Current {
+			get {
+				CheckDisposed ();
+				return new Header (iter.Name, iter.Value);
+			}
+		}
+		
+		object IEnumerator.Current {
+			get { return Current; }
+		}
+		
+		public string Name {
+			get {
+				CheckDisposed ();
+				return iter.Name;
+			}
+		}
+		
+		public string Value {
+			set {
+				CheckDisposed ();
+				iter.Value = value;
+			}
+			
+			get {
+				CheckDisposed ();
+				return iter.Value;
+			}
+		}
+		
+		public bool MoveFirst ()
+		{
+			CheckDisposed ();
+			
+			premove = false;
+			return iter.MoveFirst ();
+		}
+		
+		public bool MoveLast ()
+		{
+			CheckDisposed ();
+			
+			premove = false;
+			return iter.MoveLast ();
+		}
+		
+		public bool MoveNext ()
+		{
+			CheckDisposed ();
+			
+			if (premove) {
+				premove = false;
+				return iter.MoveFirst ();
+			}
+			
+			return iter.MoveNext ();
+		}
+		
+		public bool MovePrev ()
+		{
+			CheckDisposed ();
+			
+			if (premove)
+				return false;
+			
+			premove = false;
+			return iter.MovePrev ();
+		}
+		
+		public bool Remove ()
+		{
+			CheckDisposed ();
+			return iter.Remove ();
+		}
+		
+		public void Reset ()
+		{
+			CheckDisposed ();
+			iter.MoveFirst ();
+			premove = true;
+		}
+
+		void CheckDisposed ()
+		{
+			if (headers == null)
+				throw new ObjectDisposedException ("The HeaderEnumerator has been disposed.");
+		}
+		
+		public void Dispose ()
+		{
+			headers = null;
+			iter = null;
+		}
+	}
+}
diff --git a/mono/HeaderList.custom b/mono/HeaderList.custom
index 048a83d..9cea737 100644
--- a/mono/HeaderList.custom
+++ b/mono/HeaderList.custom
@@ -29,6 +29,23 @@
 			}
 		}
 		
+		[DllImport ("gmime")]
+		static extern bool g_mime_header_list_get_iter (IntPtr raw, IntPtr iter);
+		
+		internal HeaderIter GetIter ()
+		{
+			HeaderIter iter = new HeaderIter ();
+			
+			g_mime_header_list_get_iter (Handle, iter.Handle);
+			
+			return iter;
+		}
+		
+		public IEnumerator GetEnumerator ()
+		{
+			return new HeaderEnumerator (this);
+		}
+		
 		public StreamWrapper Stream {
 			get {
 				GMime.StreamMem mem_stream = new GMime.StreamMem ();
diff --git a/mono/Makefile.am b/mono/Makefile.am
index 15c8bff..c0ef720 100644
--- a/mono/Makefile.am
+++ b/mono/Makefile.am
@@ -16,10 +16,14 @@ pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = $(PACKAGE_SHARP).pc
 
 sources = 					\
-	StreamWrapper.cs
+	StreamWrapper.cs			\
+	HeaderEnumerator.cs			\
+	Header.cs
 
 build_sources =  				\
 	$(srcdir)/StreamWrapper.cs		\
+	$(srcdir)/HeaderEnumerator.cs		\
+	$(srcdir)/Header.cs			\
 	AssemblyInfo.cs
 
 customs = 					\



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