/IZWebFileManager/Components/DirectoryProvider.cs

http://izwebfilemanager.googlecode.com/ · C# · 171 lines · 133 code · 23 blank · 15 comment · 21 complexity · 5eb94f6d77c8f0addaf0af738d6f20a0 MD5 · raw file

  1. // Copyright (C) 2006 Igor Zelmanovich <izwebfilemanager@gmail.com>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using System.Web.UI.WebControls;
  20. using System.IO;
  21. using System.Web;
  22. using System.Collections;
  23. using System.Globalization;
  24. namespace IZ.WebFileManager.Components
  25. {
  26. internal sealed class DirectoryProvider
  27. {
  28. DirectoryInfo directory;
  29. SortMode sort;
  30. SortDirection sortDirection;
  31. Hashtable groups;
  32. internal DirectoryProvider (DirectoryInfo directory, SortMode sort, SortDirection sortDirection) {
  33. this.directory = directory;
  34. this.sort = sort;
  35. this.sortDirection = sortDirection;
  36. }
  37. public GroupInfo [] GetGroups () {
  38. groups = new Hashtable ();
  39. FileSystemInfo [] fsis = GetFileSystemInfos ();
  40. ArrayList groupInfos = new ArrayList ();
  41. switch (sort) {
  42. case SortMode.Name:
  43. Hashtable letters = new Hashtable ();
  44. foreach (FileSystemInfo fsi in fsis) {
  45. string l = fsi.Name.Substring (0, 1).ToUpper (CultureInfo.InvariantCulture);
  46. GroupInfo gi = (GroupInfo) letters [l];
  47. if (gi == null) {
  48. gi = new GroupInfo (l);
  49. letters [l] = gi;
  50. }
  51. ArrayList gfsis = (ArrayList) groups [gi];
  52. if (gfsis == null) {
  53. gfsis = new ArrayList ();
  54. groups [gi] = gfsis;
  55. groupInfos.Add (gi);
  56. }
  57. gfsis.Add (fsi);
  58. }
  59. GroupInfo [] gis = (GroupInfo []) groupInfos.ToArray (typeof (GroupInfo));
  60. Array.Sort<GroupInfo> (gis, new Comparison<GroupInfo> (CompareGroupInfos));
  61. return gis;
  62. case SortMode.Modified:
  63. break;
  64. case SortMode.Type:
  65. break;
  66. case SortMode.Size:
  67. break;
  68. }
  69. return (GroupInfo []) groupInfos.ToArray (typeof (GroupInfo));
  70. }
  71. public FileSystemInfo [] GetFileSystemInfos () {
  72. return GetFileSystemInfos (FileSystemInfosFilter.All);
  73. }
  74. public FileSystemInfo [] GetFileSystemInfos (FileSystemInfosFilter filter) {
  75. if (!directory.Exists)
  76. return new FileSystemInfo [0];
  77. FileSystemInfo [] dirs;
  78. switch (filter) {
  79. case FileSystemInfosFilter.Directories:
  80. dirs = directory.GetDirectories ();
  81. break;
  82. case FileSystemInfosFilter.Files:
  83. dirs = directory.GetFiles ();
  84. break;
  85. default:
  86. dirs = directory.GetFileSystemInfos ();
  87. break;
  88. }
  89. Array.Sort<FileSystemInfo> (dirs, new Comparison<FileSystemInfo> (CompareFileSystemInfos));
  90. return dirs;
  91. }
  92. public FileSystemInfo [] GetFileSystemInfos (GroupInfo group) {
  93. return (FileSystemInfo []) ((ArrayList) groups [group]).ToArray (typeof (FileSystemInfo));
  94. }
  95. int CompareGroupInfos (GroupInfo gi1, GroupInfo gi2) {
  96. int res = 0;
  97. switch (sort) {
  98. case SortMode.Name:
  99. res = String.Compare (gi1.Name, gi2.Name);
  100. break;
  101. }
  102. if (sortDirection == SortDirection.Descending)
  103. res = -res;
  104. return res;
  105. }
  106. int CompareFileSystemInfos (FileSystemInfo file1, FileSystemInfo file2) {
  107. int res = 0;
  108. FileInfo f1 = file1 as FileInfo;
  109. FileInfo f2 = file2 as FileInfo;
  110. switch (sort) {
  111. case SortMode.Name:
  112. res = String.Compare ((f1 == null ? "1" : "2") + file1.Name, (f2 == null ? "1" : "2") + file2.Name);
  113. break;
  114. case SortMode.Modified:
  115. res = String.Compare ((f1 == null ? "1" : "2") + file1.LastWriteTime.ToString ("s", null), (f2 == null ? "1" : "2") + file2.LastWriteTime.ToString ("s", null));
  116. break;
  117. case SortMode.Type:
  118. res = String.Compare (f1 != null ? f1.Extension.ToLower (CultureInfo.InvariantCulture) + file1.Name : String.Empty, f2 != null ? f2.Extension.ToLower (CultureInfo.InvariantCulture) + file2.Name : String.Empty);
  119. break;
  120. case SortMode.Size:
  121. long length1 = f1 != null ? f1.Length : -1;
  122. long length2 = f2 != null ? f2.Length : -1;
  123. res = Math.Sign (length1 - length2);
  124. break;
  125. }
  126. if (sortDirection == SortDirection.Descending)
  127. res = -res;
  128. return res;
  129. }
  130. public enum FileSystemInfosFilter
  131. {
  132. All,
  133. Directories,
  134. Files
  135. }
  136. }
  137. internal sealed class GroupInfo
  138. {
  139. private string name;
  140. public string Name {
  141. get { return name; }
  142. set { name = value; }
  143. }
  144. internal GroupInfo (string name) {
  145. this.name = name;
  146. }
  147. }
  148. }