PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/IZWebFileManager/FileViewItem.cs

http://izwebfilemanager.googlecode.com/
C# | 155 lines | 120 code | 20 blank | 15 comment | 25 complexity | ebfa89be9040ad5130b4a998bb2f8a93 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.IO;
 21using System.Web;
 22using System.Globalization;
 23
 24namespace IZ.WebFileManager
 25{
 26	sealed class FileViewItem
 27	{
 28		string _thumbnailImage;
 29		string _smallImage;
 30		string _largeImage;
 31		string _size;
 32		string _type;
 33		string _modified;
 34		string _cliendID;
 35		readonly FileSystemInfo _fsi;
 36		readonly FileManagerControlBase _fileView;
 37		bool? _hidden;
 38
 39		public FileSystemInfo FileSystemInfo {
 40			get { return _fsi; }
 41		}
 42
 43		public bool IsDirectory {
 44			get { return _fsi is DirectoryInfo; }
 45		}
 46
 47		public string ClientID {
 48			get { return _cliendID; }
 49			set { _cliendID = value; }
 50		}
 51
 52		public bool CanBeRenamed {
 53			get { return true; }
 54		}
 55
 56		public string SmallImage {
 57			get {
 58				if (_smallImage == null)
 59					_smallImage = _fileView.Controller.GetItemSmallImage (_fsi);
 60				return _smallImage;
 61			}
 62		}
 63
 64		public string LargeImage {
 65			get {
 66				if (_largeImage == null)
 67					_largeImage = _fileView.Controller.GetItemLargeImage (_fsi);
 68				return _largeImage;
 69			}
 70		}
 71
 72		public string ThumbnailImage {
 73			get {
 74				if (_thumbnailImage == null)
 75					_thumbnailImage = _fileView.Controller.GetItemThumbnailImage (_fsi, _fileView.CurrentDirectory);
 76				return _thumbnailImage;
 77			}
 78		}
 79
 80		public string Info {
 81			get { return String.Empty; }
 82		}
 83
 84		public string Size {
 85			get {
 86				if (_size == null)
 87					_size = _fsi is DirectoryInfo ? "&nbsp;" : FileSizeToString (((FileInfo) _fsi).Length);
 88				return _size;
 89			}
 90		}
 91
 92		public string Type {
 93			get {
 94				if (_type == null)
 95					_type = GetItemType (_fsi);
 96				return _type;
 97			}
 98		}
 99
100		public string Modified {
101			get {
102				if (_modified == null)
103					_modified = _fsi.LastWriteTime.ToString ("g", null);
104				return _modified;
105			}
106		}
107
108		public string Name {
109			get { return _fsi.Name; }
110		}
111
112		public bool Hidden {
113			get {
114				if (!_hidden.HasValue) {
115					_hidden = (!String.IsNullOrEmpty (_fileView.HiddenFilesAndFoldersPrefix) && _fsi.Name.StartsWith (_fileView.HiddenFilesAndFoldersPrefix, StringComparison.InvariantCultureIgnoreCase));
116					if (!_hidden.Value && _fsi is FileInfo) {
117						string ext = _fsi.Extension.ToLower (CultureInfo.InvariantCulture).TrimStart ('.');
118						_hidden = _fileView.Controller.HiddenFilesArray.Contains (ext);
119					}
120				}
121				return _hidden.Value;
122			}
123		}
124
125		internal FileViewItem (FileSystemInfo fsi, FileManagerControlBase fileView) {
126			this._fsi = fsi;
127			this._fileView = fileView;
128		}
129
130		string GetItemType (FileSystemInfo fsi) {
131			if (fsi is DirectoryInfo)
132				return _fileView.Controller.GetResourceString ("File_Folder", "File Folder");
133			else {
134				FileInfo file = (FileInfo) fsi;
135				FileType ft = _fileView.Controller.GetFileType (file);
136				if (ft != null && ft.Name.Length > 0)
137					return ft.Name;
138				else {
139					return file.Extension.ToUpper (CultureInfo.InvariantCulture).TrimStart ('.') + " File";
140				}
141			}
142
143		}
144
145		static string FileSizeToString (long size) {
146			if (size < 1024)
147				return size.ToString (null, null) + " B";
148			else if (size < 1048576)
149				return (size / 1024).ToString (null, null) + " KB";
150			else
151				return (size / 1048576).ToString (null, null) + " MB";
152
153		}
154	}
155}