~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/webaccess/.svn/text-base/WebUtility.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com
20
 
 |
21
 
 | Author: Rob
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.IO;
26
 
using System.Xml;
27
 
using System.Net;
28
 
using System.Web.Services.Protocols;
29
 
using System.Resources;
30
 
 
31
 
namespace Novell.iFolderApp.Web
32
 
{
33
 
        /// <summary>
34
 
        /// iFolder Web Utilities
35
 
        /// </summary>
36
 
        public class WebUtility
37
 
        {
38
 
                /// <summary>
39
 
                /// Private Constructor
40
 
                /// </summary>
41
 
                private WebUtility()
42
 
                {
43
 
                }
44
 
 
45
 
                /// <summary>
46
 
                /// Get a Localized String
47
 
                /// </summary>
48
 
                /// <param name="key"></param>
49
 
                /// <param name="rm"></param>
50
 
                /// <returns></returns>
51
 
                public static string GetString(string key, ResourceManager rm)
52
 
                {
53
 
                        string result = null;
54
 
                        
55
 
                        if (rm != null)
56
 
                        {
57
 
                                result = rm.GetString(key);
58
 
                        }
59
 
 
60
 
                        if ((result == null) || (result.Length == 0))
61
 
                        {
62
 
                                result = key;
63
 
                        }
64
 
 
65
 
                        return result;
66
 
                }
67
 
 
68
 
                /// <summary>
69
 
                /// Get the File Name with the given Path
70
 
                /// </summary>
71
 
                /// <param name="path">The File Path String</param>
72
 
                /// <returns>The File Name</returns>
73
 
                public static string GetFileName(String path)
74
 
                {
75
 
                        string result = null;
76
 
                        
77
 
                        if (path != null)
78
 
                        {
79
 
                                result = Path.GetFileName(path);
80
 
 
81
 
                                // KLUDGE: this is a kludge for Mono because it no longer
82
 
                                // (after 1.1.6) recognizes the backslash as a directory
83
 
                                // seperator
84
 
                                int index = result.LastIndexOf('\\');
85
 
                                if (index != -1) result = result.Substring(index + 1);
86
 
                        }
87
 
 
88
 
                        return result;
89
 
                }
90
 
 
91
 
 
92
 
                /// <summary>
93
 
                /// Get the Exception Type
94
 
                /// </summary>
95
 
                /// <param name="e">The Exception</param>
96
 
                /// <returns>The Exception Type</returns>
97
 
                public static string GetExceptionType(Exception e)
98
 
                {
99
 
                        string type = e.GetType().Name;
100
 
 
101
 
                        if (e is SoapException)
102
 
                        {
103
 
                                type = WebUtility.GetSmartExceptionType(e as SoapException);
104
 
                        }
105
 
                        else if (e is WebException)
106
 
                        {
107
 
                                type = WebUtility.GetWebExceptionType(e as WebException);       
108
 
                        }
109
 
 
110
 
                        return type;
111
 
                }
112
 
 
113
 
                /// <summary>
114
 
                /// Get the SmartException Type
115
 
                /// </summary>
116
 
                /// <param name="e">The SoapException with Detail.</param>
117
 
                /// <returns>The Exception Type</returns>
118
 
                public static string GetSmartExceptionType(SoapException e)
119
 
                {
120
 
                        // exception type
121
 
                        string type = e.GetType().Name;
122
 
 
123
 
                        try
124
 
                        {
125
 
                                XmlNode node = e.Detail.SelectSingleNode("OriginalException");
126
 
 
127
 
                                if (node == null)
128
 
                                {
129
 
                                        // try inside the <detail> tags
130
 
                                        node = e.Detail.SelectSingleNode("*/OriginalException");
131
 
                                }
132
 
 
133
 
                                if (node != null)
134
 
                                {
135
 
                                        type = node.Attributes.GetNamedItem("type").Value;
136
 
                                        type = type.Substring(type.LastIndexOf(".") + 1);
137
 
                                }
138
 
                        }
139
 
                        catch { }
140
 
 
141
 
                        return type;
142
 
                }
143
 
 
144
 
                /// <summary>
145
 
                /// Get the WebException Type
146
 
                /// </summary>
147
 
                /// <param name="e">The WebException with Status Detail.</param>
148
 
                /// <returns>The Exception Type</returns>
149
 
                public static string GetWebExceptionType(WebException e)
150
 
                {
151
 
                        // exception type
152
 
                        string type = e.Status.ToString();
153
 
 
154
 
                        if (e.Status == WebExceptionStatus.ProtocolError)
155
 
                        {
156
 
                                HttpWebResponse response = e.Response as HttpWebResponse;
157
 
 
158
 
                                type = response.StatusDescription;
159
 
                        }
160
 
 
161
 
                        return type;
162
 
                }
163
 
                
164
 
                /// <summary>
165
 
                /// Size Format Index
166
 
                /// </summary>
167
 
                private enum Index 
168
 
                {
169
 
                        B = 0,
170
 
                        KB = 1,
171
 
                        MB = 2,
172
 
                        GB = 3
173
 
                }
174
 
 
175
 
                /// <summary>
176
 
                /// Format Size
177
 
                /// </summary>
178
 
                /// <param name="size"></param>
179
 
                /// <param name="rm"></param>
180
 
                /// <returns></returns>
181
 
                public static string FormatSize(long size, ResourceManager rm)
182
 
                {
183
 
                        const int K = 1024;
184
 
 
185
 
                        string modifier = "";
186
 
 
187
 
                        long temp;
188
 
                        int index = 0;
189
 
 
190
 
                        // adjust
191
 
                        while((index < (int)Index.GB) && ((temp = (size / K)) > 1))
192
 
                        {
193
 
                                ++index;
194
 
                                size = temp;
195
 
                        }
196
 
 
197
 
                        // modifier
198
 
                        switch((Index)index)
199
 
                        {
200
 
                                        // B
201
 
                                case Index.B:
202
 
                                        modifier = WebUtility.GetString("ENTRY.B", rm);
203
 
                                        break;
204
 
 
205
 
                                        // KB
206
 
                                case Index.KB:
207
 
                                        modifier = WebUtility.GetString("ENTRY.KB", rm);
208
 
                                        break;
209
 
 
210
 
                                        // MB
211
 
                                case Index.MB:
212
 
                                        modifier = WebUtility.GetString("ENTRY.MB", rm);
213
 
                                        break;
214
 
 
215
 
                                        // GB
216
 
                                case Index.GB:
217
 
                                        modifier = WebUtility.GetString("ENTRY.GB", rm);
218
 
                                        break;
219
 
                        }
220
 
 
221
 
                        return String.Format("{0:N0}&nbsp;{1}", size, modifier);
222
 
                }
223
 
 
224
 
                /// <summary>
225
 
                /// Format Rights
226
 
                /// </summary>
227
 
                /// <param name="rights"></param>
228
 
                /// <param name="rm"></param>
229
 
                /// <returns></returns>
230
 
                public static string FormatRights(Rights rights, ResourceManager rm)
231
 
                {
232
 
                        string result;
233
 
 
234
 
                        switch(rights)
235
 
                        {
236
 
                                        // Admin
237
 
                                case Rights.Admin:
238
 
                                        result = WebUtility.GetString("RIGHTS.ADMIN", rm);
239
 
                                        break;
240
 
 
241
 
                                        // Read Only
242
 
                                case Rights.ReadOnly:
243
 
                                        result = WebUtility.GetString("RIGHTS.READONLY", rm);
244
 
                                        break;
245
 
 
246
 
                                        // Read Write
247
 
                                case Rights.ReadWrite:
248
 
                                        result = WebUtility.GetString("RIGHTS.READWRITE", rm);
249
 
                                        break;
250
 
 
251
 
                                        // Deny
252
 
                                case Rights.Deny:
253
 
                                default:
254
 
                                        result = WebUtility.GetString("RIGHTS.DENY", rm);
255
 
                                        break;
256
 
                        }
257
 
 
258
 
                        return result;
259
 
                }
260
 
 
261
 
                /// <summary>
262
 
                /// Format Date
263
 
                /// </summary>
264
 
                /// <param name="date"></param>
265
 
                /// <param name="rm"></param>
266
 
                /// <returns></returns>
267
 
                public static string FormatDate(DateTime date, ResourceManager rm)
268
 
                {
269
 
                        string result = date.ToString("d MMM yyyy");
270
 
 
271
 
                        DateTime today = DateTime.Today;
272
 
 
273
 
                        if (date.Year == today.Year)
274
 
                        {
275
 
                                result = date.ToString("d MMM");
276
 
 
277
 
                                if (date.Month == today.Month)
278
 
                                {
279
 
                                        if (date.Day == today.Day)
280
 
                                        {
281
 
                                                result = WebUtility.GetString("TODAY", rm);
282
 
                                        }
283
 
                                        else if (date.Day == today.AddDays(-1).Day)
284
 
                                        {
285
 
                                                result = WebUtility.GetString("YESTERDAY", rm);
286
 
                                        }
287
 
                                }
288
 
                        }
289
 
 
290
 
                        return result.Replace(" ", "&nbsp;");
291
 
                }
292
 
 
293
 
                /// <summary>
294
 
                /// Format Date and Time
295
 
                /// </summary>
296
 
                /// <param name="date"></param>
297
 
                /// <param name="rm"></param>
298
 
                /// <returns></returns>
299
 
                public static string FormatDateTime(DateTime date, ResourceManager rm)
300
 
                {
301
 
                        string result = String.Format("{0} @ {1}",
302
 
                                WebUtility.FormatDate(date, rm),
303
 
                                date.ToString("h:mm tt"));
304
 
 
305
 
                        return result.Replace(" ", "&nbsp;");
306
 
                }
307
 
 
308
 
                /// <summary>
309
 
                /// Format Yes/No
310
 
                /// </summary>
311
 
                /// <param name="value"></param>
312
 
                /// <param name="rm"></param>
313
 
                /// <returns></returns>
314
 
                public static string FormatYesNo(bool value, ResourceManager rm)
315
 
                {
316
 
                        return value ? WebUtility.GetString("YES", rm) : WebUtility.GetString("NO", rm);
317
 
                }
318
 
 
319
 
                /// <summary>
320
 
                /// Format Change Type
321
 
                /// </summary>
322
 
                /// <param name="action"></param>
323
 
                /// <param name="rm"></param>
324
 
                /// <returns></returns>
325
 
                public static string FormatChangeAction(ChangeEntryAction action, ResourceManager rm)
326
 
                {
327
 
                        string result;
328
 
 
329
 
                        switch(action)
330
 
                        {
331
 
                                case ChangeEntryAction.Modify:
332
 
                                        result = WebUtility.GetString("CHANGE.MODIFY", rm);
333
 
                                        break;
334
 
 
335
 
                                case ChangeEntryAction.Add:
336
 
                                        result = WebUtility.GetString("CHANGE.ADD", rm);
337
 
                                        break;
338
 
 
339
 
                                case ChangeEntryAction.Delete:
340
 
                                        result = WebUtility.GetString("CHANGE.DELETE", rm);
341
 
                                        break;
342
 
 
343
 
                                case ChangeEntryAction.Unknown:
344
 
                                default:
345
 
                                        result = WebUtility.GetString("CHANGE.UNKNOWN", rm);
346
 
                                        break;
347
 
                        }
348
 
 
349
 
                        return result;
350
 
                }
351
 
        }
352
 
}