PageRenderTime 104ms CodeModel.GetById 50ms app.highlight 10ms RepoModel.GetById 40ms app.codeStats 0ms

/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
 17using System;
 18using System.Collections.Generic;
 19using System.Text;
 20using System.Web.UI.WebControls;
 21using System.IO;
 22using System.Web;
 23using System.Collections;
 24using System.Globalization;
 25
 26namespace IZ.WebFileManager.Components
 27{
 28	internal sealed class DirectoryProvider
 29	{
 30		DirectoryInfo directory;
 31		SortMode sort;
 32		SortDirection sortDirection;
 33		Hashtable groups;
 34
 35		internal DirectoryProvider (DirectoryInfo directory, SortMode sort, SortDirection sortDirection) {
 36
 37			this.directory = directory;
 38			this.sort = sort;
 39			this.sortDirection = sortDirection;
 40		}
 41
 42		public GroupInfo [] GetGroups () {
 43			groups = new Hashtable ();
 44			FileSystemInfo [] fsis = GetFileSystemInfos ();
 45
 46			ArrayList groupInfos = new ArrayList ();
 47
 48			switch (sort) {
 49			case SortMode.Name:
 50				Hashtable letters = new Hashtable ();
 51				foreach (FileSystemInfo fsi in fsis) {
 52					string l = fsi.Name.Substring (0, 1).ToUpper (CultureInfo.InvariantCulture);
 53					GroupInfo gi = (GroupInfo) letters [l];
 54					if (gi == null) {
 55						gi = new GroupInfo (l);
 56						letters [l] = gi;
 57					}
 58					ArrayList gfsis = (ArrayList) groups [gi];
 59					if (gfsis == null) {
 60						gfsis = new ArrayList ();
 61						groups [gi] = gfsis;
 62						groupInfos.Add (gi);
 63					}
 64					gfsis.Add (fsi);
 65				}
 66				GroupInfo [] gis = (GroupInfo []) groupInfos.ToArray (typeof (GroupInfo));
 67				Array.Sort<GroupInfo> (gis, new Comparison<GroupInfo> (CompareGroupInfos));
 68				return gis;
 69			case SortMode.Modified:
 70				break;
 71			case SortMode.Type:
 72				break;
 73			case SortMode.Size:
 74				break;
 75			}
 76
 77			return (GroupInfo []) groupInfos.ToArray (typeof (GroupInfo));
 78		}
 79
 80		public FileSystemInfo [] GetFileSystemInfos () {
 81			return GetFileSystemInfos (FileSystemInfosFilter.All);
 82		}
 83
 84		public FileSystemInfo [] GetFileSystemInfos (FileSystemInfosFilter filter) {
 85			if (!directory.Exists)
 86				return new FileSystemInfo [0];
 87
 88			FileSystemInfo [] dirs;
 89			switch (filter) {
 90			case FileSystemInfosFilter.Directories:
 91				dirs = directory.GetDirectories ();
 92				break;
 93			case FileSystemInfosFilter.Files:
 94				dirs = directory.GetFiles ();
 95				break;
 96			default:
 97				dirs = directory.GetFileSystemInfos ();
 98				break;
 99			}
100			Array.Sort<FileSystemInfo> (dirs, new Comparison<FileSystemInfo> (CompareFileSystemInfos));
101			return dirs;
102		}
103
104
105		public FileSystemInfo [] GetFileSystemInfos (GroupInfo group) {
106			return (FileSystemInfo []) ((ArrayList) groups [group]).ToArray (typeof (FileSystemInfo));
107		}
108
109		int CompareGroupInfos (GroupInfo gi1, GroupInfo gi2) {
110			int res = 0;
111			switch (sort) {
112			case SortMode.Name:
113				res = String.Compare (gi1.Name, gi2.Name);
114				break;
115			}
116
117			if (sortDirection == SortDirection.Descending)
118				res = -res;
119
120			return res;
121		}
122
123		int CompareFileSystemInfos (FileSystemInfo file1, FileSystemInfo file2) {
124			int res = 0;
125			FileInfo f1 = file1 as FileInfo;
126			FileInfo f2 = file2 as FileInfo;
127			switch (sort) {
128			case SortMode.Name:
129				res = String.Compare ((f1 == null ? "1" : "2") + file1.Name, (f2 == null ? "1" : "2") + file2.Name);
130				break;
131			case SortMode.Modified:
132				res = String.Compare ((f1 == null ? "1" : "2") + file1.LastWriteTime.ToString ("s", null), (f2 == null ? "1" : "2") + file2.LastWriteTime.ToString ("s", null));
133				break;
134			case SortMode.Type:
135				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);
136				break;
137			case SortMode.Size:
138				long length1 = f1 != null ? f1.Length : -1;
139				long length2 = f2 != null ? f2.Length : -1;
140				res = Math.Sign (length1 - length2);
141				break;
142			}
143
144			if (sortDirection == SortDirection.Descending)
145				res = -res;
146
147			return res;
148		}
149
150		public enum FileSystemInfosFilter
151		{
152			All,
153			Directories,
154			Files
155		}
156	}
157	internal sealed class GroupInfo
158	{
159		private string name;
160
161		public string Name {
162			get { return name; }
163			set { name = value; }
164		}
165
166		internal GroupInfo (string name) {
167			this.name = name;
168		}
169	}
170
171}