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