* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PrintingPermission.cs
1 //
2 // System.Drawing.PrintingPermission.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Herve Poussineau (hpoussineau@fr.st)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Security;
34 using System.Security.Permissions;
35
36 namespace System.Drawing.Printing {
37
38         [Serializable]
39         public sealed class PrintingPermission : CodeAccessPermission, IUnrestrictedPermission {
40
41                 private const int version = 1;
42
43                 private PrintingPermissionLevel _Level;
44                 
45                 public PrintingPermission (PermissionState state) 
46                 {
47                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
48                                 _Level = PrintingPermissionLevel.AllPrinting;
49                         else
50                                 _Level = PrintingPermissionLevel.NoPrinting;
51                 }
52
53                 public PrintingPermission (PrintingPermissionLevel printingLevel) 
54                 {
55                         Level = printingLevel;
56                 }
57                 
58                 // properties
59
60                 public PrintingPermissionLevel Level{
61                         get { return _Level; }
62                         set {
63                                 if (!Enum.IsDefined (typeof (PrintingPermissionLevel), value)) {
64                                         string msg = Locale.GetText ("Invalid enum {0}");
65                                         throw new ArgumentException (String.Format (msg, value), "Level");
66                                 }
67                                  _Level = value;
68                         }
69                 }
70
71                 // methods
72
73                 public override IPermission Copy ()
74                 {
75                         return new PrintingPermission (this.Level);
76                 }
77                 
78                 public override void FromXml (SecurityElement esd)
79                 {
80                         CheckSecurityElement (esd, "esd", version, version);
81                         // Note: we do not (yet) care about the return value 
82                         // as we only accept version 1 (min/max values)
83
84                         if (IsUnrestricted (esd))
85                                 _Level = PrintingPermissionLevel.AllPrinting;
86                         else {
87                                 string level = esd.Attribute ("Level");
88                                 if (level != null) {
89                                         _Level = (PrintingPermissionLevel) Enum.Parse (
90                                                 typeof (PrintingPermissionLevel), level);
91                                 }
92                                 else
93                                         _Level = PrintingPermissionLevel.NoPrinting;
94                         }
95                 }
96                 
97                 public override IPermission Intersect (IPermission target)
98                 {
99                         PrintingPermission pp = Cast (target);
100                         if ((pp == null) || IsEmpty () || pp.IsEmpty ())
101                                 return null;
102
103                         PrintingPermissionLevel level = (_Level <= pp.Level) ? _Level : pp.Level;
104                         return new PrintingPermission (level);
105                 }
106                 
107                 public override bool IsSubsetOf (IPermission target)
108                 {
109                         PrintingPermission pp = Cast (target);
110                         if (pp == null)
111                                 return IsEmpty ();
112                         
113                         return (_Level <= pp.Level);
114                 }
115                 
116                 public bool IsUnrestricted ()
117                 {
118                         return (_Level == PrintingPermissionLevel.AllPrinting);
119                 }
120                 
121                 public override SecurityElement ToXml ()
122                 {
123                         SecurityElement se = Element (version);
124                         if (IsUnrestricted ())
125                                 se.AddAttribute ("Unrestricted", "true");
126                         else
127                                 se.AddAttribute ("Level", _Level.ToString ());
128                         return se;
129                 }
130                 
131                 public override IPermission Union (IPermission target)
132                 {
133                         PrintingPermission pp = Cast (target);
134                         if (pp == null)
135                                 return new PrintingPermission (_Level);
136                         if (IsUnrestricted () || pp.IsUnrestricted ())
137                                 return new PrintingPermission (PermissionState.Unrestricted);
138                         if (IsEmpty () && pp.IsEmpty ())
139                                 return null;
140
141                         PrintingPermissionLevel level = (_Level > pp.Level) ? _Level : pp.Level;
142                         return new PrintingPermission (level);
143                 }
144
145                 // Internal helpers methods
146
147                 private bool IsEmpty ()
148                 {
149                         return (_Level == PrintingPermissionLevel.NoPrinting);
150                 }
151
152                 private PrintingPermission Cast (IPermission target)
153                 {
154                         if (target == null)
155                                 return null;
156
157                         PrintingPermission pp = (target as PrintingPermission);
158                         if (pp == null) {
159                                 ThrowInvalidPermission (target, typeof (PrintingPermission));
160                         }
161
162                         return pp;
163                 }
164
165                 // NOTE: The following static methods should be moved out to a (static?) class 
166                 // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
167
168                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
169                 internal SecurityElement Element (int version) 
170                 {
171                         SecurityElement se = new SecurityElement ("IPermission");
172                         Type type = this.GetType ();
173                         se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
174                         se.AddAttribute ("version", version.ToString ());
175                         return se;
176                 }
177
178                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
179                 {
180                         string msg;
181                         switch (state) {
182                         case PermissionState.None:
183                                 break;
184                         case PermissionState.Unrestricted:
185                                 if (!allowUnrestricted) {
186                                         msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
187                                         throw new ArgumentException (msg, "state");
188                                 }
189                                 break;
190                         default:
191                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
192                                 throw new ArgumentException (msg, "state");
193                         }
194                         return state;
195                 }
196
197                 // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
198                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
199                 {
200                         if (se == null)
201                                 throw new ArgumentNullException (parameterName);
202
203                         if (se.Attribute ("class") == null) {
204                                 string msg = Locale.GetText ("Missing 'class' attribute.");
205                                 throw new ArgumentException (msg, parameterName);
206                         }
207
208                         // we assume minimum version if no version number is supplied
209                         int version = minimumVersion;
210                         string v = se.Attribute ("version");
211                         if (v != null) {
212                                 try {
213                                         version = Int32.Parse (v);
214                                 }
215                                 catch (Exception e) {
216                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
217                                         msg = String.Format (msg, v);
218                                         throw new ArgumentException (msg, parameterName, e);
219                                 }
220                         }
221
222                         if ((version < minimumVersion) || (version > maximumVersion)) {
223                                 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
224                                 msg = String.Format (msg, version, minimumVersion, maximumVersion);
225                                 throw new ArgumentException (msg, parameterName);
226                         }
227                         return version;
228                 }
229
230                 // must be called after CheckSecurityElement (i.e. se != null)
231                 internal static bool IsUnrestricted (SecurityElement se) 
232                 {
233                         string value = se.Attribute ("Unrestricted");
234                         if (value == null)
235                                 return false;
236                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
237                 }
238
239                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
240                 {
241                         string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
242                         msg = String.Format (msg, target.GetType (), expected);
243                         throw new ArgumentException (msg, "target");
244                 }
245         }
246 }