Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Net.Mail / AlternateView.cs
1 //
2 // System.Net.Mail.AlternateView.cs
3 //
4 // Author:
5 //      John Luke (john.luke@gmail.com)
6 //
7 // Copyright (C) John Luke, 2005
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Net.Mime;
33 using System.Text;
34
35 namespace System.Net.Mail {
36         public class AlternateView : AttachmentBase
37         {
38                 #region Fields
39                 Uri baseUri;
40                 LinkedResourceCollection linkedResources = new LinkedResourceCollection ();
41
42                 #endregion // Fields
43
44                 #region Constructors
45
46                 public AlternateView (string fileName) : base (fileName)
47                 {
48                         if (fileName == null)
49                                 throw new ArgumentNullException ();
50                 }
51
52                 public AlternateView (string fileName, ContentType contentType) : base (fileName, contentType)
53                 {
54                         if (fileName == null)
55                                 throw new ArgumentNullException ();
56                 }
57
58                 public AlternateView (string fileName, string mediaType) : base (fileName, mediaType)
59                 {
60                         if (fileName == null)
61                                 throw new ArgumentNullException ();
62                 }
63
64                 public AlternateView (Stream contentStream) : base (contentStream)
65                 {
66                 }
67                 
68                 public AlternateView (Stream contentStream, string mediaType) : base (contentStream, mediaType)
69                 {
70                 }
71                 
72                 public AlternateView (Stream contentStream, ContentType contentType) : base (contentStream, contentType)
73                 {
74                 }
75
76                 #endregion // Constructors
77
78                 #region Properties
79
80                 #endregion // Properties
81
82                 public Uri BaseUri {
83                         get { return baseUri; }
84                         set { baseUri = value; }
85                 }
86
87                 public LinkedResourceCollection LinkedResources {
88                         get { return linkedResources; }
89                 }
90                 
91                 #region Methods
92
93                 public static AlternateView CreateAlternateViewFromString (string content)
94                 {
95                         if (content == null)
96                                 throw new ArgumentNullException ();
97                         MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (content));
98                         AlternateView av = new AlternateView (ms);
99                         av.TransferEncoding = TransferEncoding.QuotedPrintable;
100                         return av;
101                 }
102
103                 public static AlternateView CreateAlternateViewFromString (string content, ContentType contentType)
104                 {
105                         if (content == null)
106                                 throw new ArgumentNullException ("content");
107                         Encoding enc = contentType.CharSet != null ? Encoding.GetEncoding (contentType.CharSet) : Encoding.UTF8;
108                         MemoryStream ms = new MemoryStream (enc.GetBytes (content));
109                         AlternateView av = new AlternateView (ms, contentType);
110                         av.TransferEncoding = TransferEncoding.QuotedPrintable;
111                         return av;
112                 }
113
114                 public static AlternateView CreateAlternateViewFromString (string content, Encoding encoding, string mediaType)
115                 {
116                         if (content == null)
117                                 throw new ArgumentNullException ("content");
118                         if (encoding == null)
119                                 encoding = Encoding.UTF8;
120                         MemoryStream ms = new MemoryStream (encoding.GetBytes (content));
121                         ContentType ct = new ContentType ();
122                         ct.MediaType = mediaType;
123                         ct.CharSet = encoding.HeaderName;
124                         AlternateView av = new AlternateView (ms, ct);
125                         av.TransferEncoding = TransferEncoding.QuotedPrintable;
126                         return av;
127                 }
128
129                 protected override void Dispose (bool disposing)
130                 {
131                         if (disposing)
132                                 foreach (LinkedResource lr in linkedResources)
133                                         lr.Dispose ();
134                         base.Dispose (disposing);
135                 }
136                 
137                 #endregion // Methods
138         }
139 }