1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
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.
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.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
22
|***************************************************************************/
28
using System.Web.Services.Protocols;
29
using System.Resources;
31
namespace Novell.iFolderApp.Web
34
/// iFolder Web Utilities
36
public class WebUtility
39
/// Private Constructor
46
/// Get a Localized String
48
/// <param name="key"></param>
49
/// <param name="rm"></param>
50
/// <returns></returns>
51
public static string GetString(string key, ResourceManager rm)
57
result = rm.GetString(key);
60
if ((result == null) || (result.Length == 0))
69
/// Get the File Name with the given Path
71
/// <param name="path">The File Path String</param>
72
/// <returns>The File Name</returns>
73
public static string GetFileName(String path)
79
result = Path.GetFileName(path);
81
// KLUDGE: this is a kludge for Mono because it no longer
82
// (after 1.1.6) recognizes the backslash as a directory
84
int index = result.LastIndexOf('\\');
85
if (index != -1) result = result.Substring(index + 1);
93
/// Get the Exception Type
95
/// <param name="e">The Exception</param>
96
/// <returns>The Exception Type</returns>
97
public static string GetExceptionType(Exception e)
99
string type = e.GetType().Name;
101
if (e is SoapException)
103
type = WebUtility.GetSmartExceptionType(e as SoapException);
105
else if (e is WebException)
107
type = WebUtility.GetWebExceptionType(e as WebException);
114
/// Get the SmartException Type
116
/// <param name="e">The SoapException with Detail.</param>
117
/// <returns>The Exception Type</returns>
118
public static string GetSmartExceptionType(SoapException e)
121
string type = e.GetType().Name;
125
XmlNode node = e.Detail.SelectSingleNode("OriginalException");
129
// try inside the <detail> tags
130
node = e.Detail.SelectSingleNode("*/OriginalException");
135
type = node.Attributes.GetNamedItem("type").Value;
136
type = type.Substring(type.LastIndexOf(".") + 1);
145
/// Get the WebException Type
147
/// <param name="e">The WebException with Status Detail.</param>
148
/// <returns>The Exception Type</returns>
149
public static string GetWebExceptionType(WebException e)
152
string type = e.Status.ToString();
154
if (e.Status == WebExceptionStatus.ProtocolError)
156
HttpWebResponse response = e.Response as HttpWebResponse;
158
type = response.StatusDescription;
165
/// Size Format Index
178
/// <param name="size"></param>
179
/// <param name="rm"></param>
180
/// <returns></returns>
181
public static string FormatSize(long size, ResourceManager rm)
185
string modifier = "";
191
while((index < (int)Index.GB) && ((temp = (size / K)) > 1))
202
modifier = WebUtility.GetString("ENTRY.B", rm);
207
modifier = WebUtility.GetString("ENTRY.KB", rm);
212
modifier = WebUtility.GetString("ENTRY.MB", rm);
217
modifier = WebUtility.GetString("ENTRY.GB", rm);
221
return String.Format("{0:N0} {1}", size, modifier);
227
/// <param name="rights"></param>
228
/// <param name="rm"></param>
229
/// <returns></returns>
230
public static string FormatRights(Rights rights, ResourceManager rm)
238
result = WebUtility.GetString("RIGHTS.ADMIN", rm);
242
case Rights.ReadOnly:
243
result = WebUtility.GetString("RIGHTS.READONLY", rm);
247
case Rights.ReadWrite:
248
result = WebUtility.GetString("RIGHTS.READWRITE", rm);
254
result = WebUtility.GetString("RIGHTS.DENY", rm);
264
/// <param name="date"></param>
265
/// <param name="rm"></param>
266
/// <returns></returns>
267
public static string FormatDate(DateTime date, ResourceManager rm)
269
string result = date.ToString("d MMM yyyy");
271
DateTime today = DateTime.Today;
273
if (date.Year == today.Year)
275
result = date.ToString("d MMM");
277
if (date.Month == today.Month)
279
if (date.Day == today.Day)
281
result = WebUtility.GetString("TODAY", rm);
283
else if (date.Day == today.AddDays(-1).Day)
285
result = WebUtility.GetString("YESTERDAY", rm);
290
return result.Replace(" ", " ");
294
/// Format Date and Time
296
/// <param name="date"></param>
297
/// <param name="rm"></param>
298
/// <returns></returns>
299
public static string FormatDateTime(DateTime date, ResourceManager rm)
301
string result = String.Format("{0} @ {1}",
302
WebUtility.FormatDate(date, rm),
303
date.ToString("h:mm tt"));
305
return result.Replace(" ", " ");
311
/// <param name="value"></param>
312
/// <param name="rm"></param>
313
/// <returns></returns>
314
public static string FormatYesNo(bool value, ResourceManager rm)
316
return value ? WebUtility.GetString("YES", rm) : WebUtility.GetString("NO", rm);
320
/// Format Change Type
322
/// <param name="action"></param>
323
/// <param name="rm"></param>
324
/// <returns></returns>
325
public static string FormatChangeAction(ChangeEntryAction action, ResourceManager rm)
331
case ChangeEntryAction.Modify:
332
result = WebUtility.GetString("CHANGE.MODIFY", rm);
335
case ChangeEntryAction.Add:
336
result = WebUtility.GetString("CHANGE.ADD", rm);
339
case ChangeEntryAction.Delete:
340
result = WebUtility.GetString("CHANGE.DELETE", rm);
343
case ChangeEntryAction.Unknown:
345
result = WebUtility.GetString("CHANGE.UNKNOWN", rm);