/IZWebFileManager/FileManagerControlBase.cs
C# | 655 lines | 537 code | 100 blank | 18 comment | 45 complexity | 67024a525a4f8160e4b20694bbe06c7b 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.Web; 22using System.Web.UI; 23using System.Globalization; 24using System.IO; 25using System.Diagnostics.CodeAnalysis; 26using System.Resources; 27using System.ComponentModel; 28using System.Collections.Specialized; 29using System.Drawing; 30using System.Drawing.Design; 31 32namespace IZ.WebFileManager 33{ 34 [PersistChildren (false)] 35 [ParseChildren (true)] 36 public abstract partial class FileManagerControlBase : WebControl, INamingContainer 37 { 38 #region Constructors 39 40 public FileManagerControlBase () { } 41 public FileManagerControlBase (FileManagerController controller) { 42 if (controller == null) 43 throw new ArgumentNullException ("controller"); 44 45 _controller = controller; 46 } 47 48 #endregion 49 50 #region Fields 51 52 FileManagerController _controller; 53 static readonly Unit _defaultWidth = Unit.Pixel (600); 54 static readonly Unit _defaultHeight = Unit.Pixel (400); 55 static readonly Unit _defaultBorderWidth = Unit.Pixel (1); 56 static readonly BorderStyle _defaultBorderStyle = BorderStyle.Solid; 57 static readonly Color _defaultBorderColor = Color.FromArgb (0xACA899); 58 static readonly Color _defaultBackColor = Color.White; 59 static readonly Color _defaultForeColor = Color.Black; 60 61 #endregion 62 63 #region Properties 64 65 [Themeable (false)] 66 [Localizable (false)] 67 [DefaultValue (false)] 68 [Category ("Behavior")] 69 public bool AllowOverwrite { 70 get { return Controller.AllowOverwrite; } 71 set { Controller.AllowOverwrite = value; } 72 } 73 74 [Themeable (false)] 75 [Localizable (false)] 76 [DefaultValue (true)] 77 [Category ("Behavior")] 78 public bool AllowUpload { 79 get { return Controller.AllowUpload; } 80 set { Controller.AllowUpload = value; } 81 } 82 83 /// <summary> 84 /// When is set true, Delete, Move and Rename are not allowed, Default value it true. 85 /// </summary> 86 [Themeable (false)] 87 [Localizable (false)] 88 [DefaultValue (true)] 89 [Category ("Behavior")] 90 public bool AllowDelete { 91 get { return Controller.AllowDelete; } 92 set { Controller.AllowDelete = value; } 93 } 94 95 [Themeable (false)] 96 [Localizable (false)] 97 [DefaultValue (false)] 98 [Category ("Behavior")] 99 public bool ReadOnly { 100 get { return Controller.ReadOnly; } 101 set { Controller.ReadOnly = value; } 102 } 103 104 [Themeable (false)] 105 [Localizable (false)] 106 [DefaultValue (false)] 107 [Category ("Behavior")] 108 public bool DownloadOnDoubleClick { 109 get { return Controller.DownloadOnDoubleClick; } 110 set { Controller.DownloadOnDoubleClick = value; } 111 } 112 113 [Browsable (false)] 114 public FileManagerController Controller { 115 get { 116 EnsureChildControls (); 117 return _controller; 118 } 119 } 120 121 [DefaultValue ("600px")] 122 public override Unit Width { 123 get { 124 if (ControlStyleCreated) 125 return ControlStyle.Width; 126 return _defaultWidth; 127 } 128 set { base.Width = value; } 129 } 130 131 [DefaultValue ("400px")] 132 public override Unit Height { 133 get { 134 if (ControlStyleCreated) 135 return ControlStyle.Height; 136 return _defaultHeight; 137 } 138 set { base.Height = value; } 139 } 140 141 [DefaultValue ("1px")] 142 public override Unit BorderWidth { 143 get { 144 if (ControlStyleCreated) 145 return ControlStyle.BorderWidth; 146 return _defaultBorderWidth; 147 } 148 set { base.BorderWidth = value; } 149 } 150 151 [DefaultValue ("0xACA899")] 152 public override Color BorderColor { 153 get { 154 if (ControlStyleCreated) 155 return ControlStyle.BorderColor; 156 return _defaultBorderColor; 157 } 158 set { base.BorderColor = value; } 159 } 160 161 [DefaultValue (BorderStyle.Solid)] 162 public override BorderStyle BorderStyle { 163 get { 164 if (ControlStyleCreated) 165 return ControlStyle.BorderStyle; 166 return _defaultBorderStyle; 167 } 168 set { base.BorderStyle = value; } 169 } 170 171 [DefaultValue ("White")] 172 public override Color BackColor { 173 get { 174 if (ControlStyleCreated) 175 return ControlStyle.BackColor; 176 return _defaultBackColor; 177 } 178 set { base.BackColor = value; } 179 } 180 181 [DefaultValue ("Black")] 182 public override Color ForeColor { 183 get { 184 if (ControlStyleCreated) 185 return ControlStyle.ForeColor; 186 return _defaultForeColor; 187 } 188 set { base.ForeColor = value; } 189 } 190 191 #endregion 192 193 #region InitControllerEvents 194 195 private void InitControllerEvents (FileManagerController controller) { 196 controller.ExecuteCommand += new EventHandler<ExecuteCommandEventArgs> (controller_ExecuteCommand); 197 controller.FileUploaded += new EventHandler<UploadFileEventArgs> (controller_FileUploaded); 198 controller.FileUploading += new EventHandler<UploadFileCancelEventArgs> (controller_FileUploading); 199 controller.ItemRenamed += new EventHandler<RenameEventArgs> (controller_ItemRenamed); 200 controller.ItemRenaming += new EventHandler<RenameCancelEventArgs> (controller_ItemRenaming); 201 controller.NewDocumentCreated += new EventHandler<NewDocumentEventArgs> (controller_NewDocumentCreated); 202 controller.NewDocumentCreating += new EventHandler<NewDocumentCancelEventArgs> (controller_NewDocumentCreating); 203 controller.NewFolderCreated += new EventHandler<NewFolderEventArgs> (controller_NewFolderCreated); 204 controller.NewFolderCreating += new EventHandler<NewFolderCancelEventArgs> (controller_NewFolderCreating); 205 controller.SelectedItemsActionComplete += new EventHandler<SelectedItemsActionEventArgs> (controller_SelectedItemsActionComplete); 206 controller.SelectedItemsAction += new EventHandler<SelectedItemsActionCancelEventArgs> (controller_SelectedItemsAction); 207 controller.FileDownload += controller_FileDownload; 208 } 209 210 private void controller_FileDownload(object sender, DownloadFileCancelEventArgs e) 211 { 212 if (FileDownload != null) 213 FileDownload(this, e); 214 } 215 216 void controller_SelectedItemsAction (object sender, SelectedItemsActionCancelEventArgs e) { 217 if (SelectedItemsAction != null) 218 SelectedItemsAction (this, e); 219 } 220 221 void controller_SelectedItemsActionComplete (object sender, SelectedItemsActionEventArgs e) { 222 if (SelectedItemsActionComplete != null) 223 SelectedItemsActionComplete (this, e); 224 } 225 226 void controller_NewFolderCreating (object sender, NewFolderCancelEventArgs e) { 227 if (NewFolderCreating != null) 228 NewFolderCreating (this, e); 229 } 230 231 void controller_NewFolderCreated (object sender, NewFolderEventArgs e) { 232 if (NewFolderCreated != null) 233 NewFolderCreated (this, e); 234 } 235 236 void controller_NewDocumentCreating (object sender, NewDocumentCancelEventArgs e) { 237 if (NewDocumentCreating != null) 238 NewDocumentCreating (this, e); 239 } 240 241 void controller_NewDocumentCreated (object sender, NewDocumentEventArgs e) { 242 if (NewDocumentCreated != null) 243 NewDocumentCreated (this, e); 244 } 245 246 void controller_ItemRenaming (object sender, RenameCancelEventArgs e) { 247 if (ItemRenaming != null) 248 ItemRenaming (this, e); 249 } 250 251 void controller_ItemRenamed (object sender, RenameEventArgs e) { 252 if (ItemRenamed != null) 253 ItemRenamed (this, e); 254 } 255 256 void controller_FileUploading (object sender, UploadFileCancelEventArgs e) { 257 if (FileUploading != null) 258 FileUploading (this, e); 259 } 260 261 void controller_FileUploaded (object sender, UploadFileEventArgs e) { 262 if (FileUploaded != null) 263 FileUploaded (this, e); 264 } 265 266 void controller_ExecuteCommand (object sender, ExecuteCommandEventArgs e) { 267 if (ExecuteCommand != null) 268 ExecuteCommand (this, e); 269 } 270 #endregion 271 272 273 public virtual FileManagerItemInfo [] SelectedItems { get { return null; } } 274 public virtual FileManagerItemInfo CurrentDirectory { get { return null; } } 275 276 protected override void OnInit (EventArgs e) { 277 base.OnInit (e); 278 279 RegisterComponent (); 280 281 } 282 283 protected override HtmlTextWriterTag TagKey { 284 get { return HtmlTextWriterTag.Div; } 285 } 286 287 protected virtual void RegisterComponent () { 288 Controller.RegisterComponent (this); 289 } 290 291 protected override void CreateChildControls () { 292 base.CreateChildControls (); 293 if (_controller == null) { 294 _controller = new FileManagerController (); 295 _controller.ID = "Controller"; 296 InitControllerEvents (_controller); 297 Controls.Add (_controller); 298 } 299 } 300 301 #region Methods 302 303 internal void RegisterHiddenField (string key, string value) { 304 string id = ClientID + "_" + key; 305 Page.ClientScript.RegisterHiddenField (id, value); 306 } 307 308 internal string GetValueFromHiddenField (string key) { 309 return Page.Request.Form [ClientID + "_" + key]; 310 } 311 312 protected internal virtual string RenderContents () { 313 return null; 314 } 315 316 internal virtual FileManagerItemInfo ResolveFileManagerItemInfo (string path) { return null; } 317 318 internal virtual FileManagerItemInfo GetCurrentDirectory () { return null; } 319 320 protected override void AddAttributesToRender (HtmlTextWriter writer) { 321 base.AddAttributesToRender (writer); 322 if (!ControlStyleCreated) 323 CreateControlStyle ().AddAttributesToRender (writer); 324 } 325 326 protected override Style CreateControlStyle () { 327 Style style = new Style (); 328 style.Font.Names = new string [] { "Tahoma", "Verdana", "Geneva", "Arial", "Helvetica", "sans-serif" }; 329 style.Font.Size = FontUnit.Parse ("11px", null); 330 style.BorderStyle = _defaultBorderStyle; 331 style.BorderWidth = _defaultBorderWidth; 332 style.BorderColor = _defaultBorderColor; 333 style.BackColor = _defaultBackColor; 334 style.ForeColor = _defaultForeColor; 335 style.Width = _defaultWidth; 336 style.Height = _defaultHeight; 337 return style; 338 } 339 340 protected override void LoadViewState (object savedState) { 341 if (savedState == null) 342 return; 343 344 object [] state = (object []) savedState; 345 346 base.LoadViewState (state [0]); 347 if (state [1] != null) 348 ((IStateManager) ControlStyle).LoadViewState (state [1]); 349 } 350 351 protected override object SaveViewState () { 352 object [] state = new object [2]; 353 state [0] = base.SaveViewState (); 354 if (ControlStyleCreated) 355 state [1] = ((IStateManager) ControlStyle).SaveViewState (); 356 357 if (state [0] != null || state [1] != null) 358 return state; 359 360 return null; 361 } 362 363 protected string GetResourceString (string name, string defaultValue) { 364 return Controller.GetResourceString (name, defaultValue); 365 } 366 367 #endregion 368 #region FileManagerController Members 369 370 [DefaultValue ("")] 371 [Category ("Action")] 372 [Themeable (false)] 373 [Localizable (false)] 374 public string ClientOpenItemFunction { 375 get { return Controller.ClientOpenItemFunction; } 376 set { Controller.ClientOpenItemFunction = value; } 377 } 378 379 [DefaultValue (null)] 380 [Category ("Appearance")] 381 [Themeable (true)] 382 public System.Globalization.CultureInfo Culture { 383 get { return Controller.Culture; } 384 set { Controller.Culture = value; } 385 } 386 387 [Category ("Action")] 388 public event EventHandler<ExecuteCommandEventArgs> ExecuteCommand; 389 390 [MergableProperty (false)] 391 [PersistenceMode (PersistenceMode.InnerProperty)] 392 [Category ("Behavior")] 393 [Localizable (false)] 394 [Themeable (false)] 395 public FileTypeCollection FileTypes { 396 get { return Controller.FileTypes; } 397 } 398 399 [Category ("Action")] 400 public event EventHandler<UploadFileEventArgs> FileUploaded; 401 402 [Category ("Action")] 403 public event EventHandler<UploadFileCancelEventArgs> FileUploading; 404 405 [DefaultValue ("")] 406 [Category ("Behavior")] 407 [Themeable (false)] 408 [Localizable (false)] 409 public string HiddenFiles { 410 get { return Controller.HiddenFiles; } 411 set { Controller.HiddenFiles = value; } 412 } 413 414 [DefaultValue ("")] 415 [Category ("Behavior")] 416 [Themeable (false)] 417 [Localizable (false)] 418 public string HiddenFilesAndFoldersPrefix { 419 get { return Controller.HiddenFilesAndFoldersPrefix; } 420 set { Controller.HiddenFilesAndFoldersPrefix = value; } 421 } 422 423 [DefaultValue (false)] 424 [Category ("Behavior")] 425 [Themeable (false)] 426 [Localizable (false)] 427 public bool ShowHiddenFilesAndFolders { 428 get { return Controller.ShowHiddenFilesAndFolders; } 429 set { Controller.ShowHiddenFilesAndFolders = value; } 430 } 431 432 [Category ("Action")] 433 public event EventHandler<RenameEventArgs> ItemRenamed; 434 435 [Category ("Action")] 436 public event EventHandler<RenameCancelEventArgs> ItemRenaming; 437 438 [Category ("Action")] 439 public event EventHandler<NewDocumentEventArgs> NewDocumentCreated; 440 441 [Category ("Action")] 442 public event EventHandler<NewDocumentCancelEventArgs> NewDocumentCreating; 443 444 [Category ("Action")] 445 public event EventHandler<NewFolderEventArgs> NewFolderCreated; 446 447 [Category ("Action")] 448 public event EventHandler<NewFolderCancelEventArgs> NewFolderCreating; 449 450 [DefaultValue ("")] 451 [Category ("Behavior")] 452 [Themeable (false)] 453 [Localizable (false)] 454 public string ProhibitedFiles { 455 get { return Controller.ProhibitedFiles; } 456 set { Controller.ProhibitedFiles = value; } 457 } 458 459 [MergableProperty (false)] 460 [Category ("Data")] 461 [PersistenceMode (PersistenceMode.InnerProperty)] 462 [Localizable (false)] 463 [Themeable (false)] 464 public RootDirectoryCollection RootDirectories { 465 get { return Controller.RootDirectories; } 466 } 467 468 [MergableProperty (false)] 469 [Category ("Behavior")] 470 [PersistenceMode (PersistenceMode.InnerProperty)] 471 [Localizable (false)] 472 [Themeable (false)] 473 public SpecialFolderCollection SpecialFolders { 474 get { return Controller.SpecialFolders; } 475 } 476 477 [Category ("Action")] 478 public event EventHandler<SelectedItemsActionEventArgs> SelectedItemsActionComplete; 479 480 [Category ("Action")] 481 public event EventHandler<SelectedItemsActionCancelEventArgs> SelectedItemsAction; 482 483 [Category ("Action")] 484 public event EventHandler<DownloadFileCancelEventArgs> FileDownload; 485 486 [MergableProperty (false)] 487 [PersistenceMode (PersistenceMode.InnerProperty)] 488 [Category ("Behavior")] 489 [Localizable (false)] 490 [Themeable (false)] 491 public NewDocumentTemplateCollection Templates { 492 get { return Controller.Templates; } 493 } 494 495 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 496 [DefaultValue ("")] 497 [UrlProperty] 498 [Bindable (true)] 499 [Category ("Appearance")] 500 public string FileSmallImageUrl { 501 get { return Controller.FileSmallImageUrl; } 502 set { Controller.FileSmallImageUrl = value; } 503 } 504 505 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 506 [DefaultValue ("")] 507 [UrlProperty] 508 [Bindable (true)] 509 [Category ("Appearance")] 510 public string FileLargeImageUrl { 511 get { return Controller.FileLargeImageUrl; } 512 set { Controller.FileLargeImageUrl = value; } 513 } 514 515 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 516 [DefaultValue ("")] 517 [UrlProperty] 518 [Bindable (true)] 519 [Category ("Appearance")] 520 public string FolderSmallImageUrl { 521 get { return Controller.FolderSmallImageUrl; } 522 set { Controller.FolderSmallImageUrl = value; } 523 } 524 525 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 526 [DefaultValue ("")] 527 [UrlProperty] 528 [Bindable (true)] 529 [Category ("Appearance")] 530 public string FolderLargeImageUrl { 531 get { return Controller.FolderLargeImageUrl; } 532 set { Controller.FolderLargeImageUrl = value; } 533 } 534 535 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 536 [DefaultValue ("")] 537 [UrlProperty] 538 [Bindable (true)] 539 [Category ("Appearance")] 540 public string RootFolderSmallImageUrl { 541 get { return Controller.RootFolderSmallImageUrl; } 542 set { Controller.RootFolderSmallImageUrl = value; } 543 } 544 545 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 546 [DefaultValue ("")] 547 [UrlProperty] 548 [Bindable (true)] 549 [Category ("Appearance")] 550 public string RootFolderLargeImageUrl { 551 get { return Controller.RootFolderLargeImageUrl; } 552 set { Controller.RootFolderLargeImageUrl = value; } 553 } 554 555 [DefaultValue ("")] 556 [Bindable (true)] 557 [Category ("Appearance")] 558 public string ImagesFolder { 559 get { return Controller.ImagesFolder; } 560 set { Controller.ImagesFolder = value; } 561 } 562 563 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 564 [DefaultValue ("")] 565 [UrlProperty] 566 [Bindable (true)] 567 [Category ("Appearance")] 568 public string DeleteImageUrl { 569 get { return Controller.DeleteImageUrl; } 570 set { Controller.DeleteImageUrl = value; } 571 } 572 573 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 574 [DefaultValue ("")] 575 [UrlProperty] 576 [Bindable (true)] 577 [Category ("Appearance")] 578 public string RenameImageUrl { 579 get { return Controller.RenameImageUrl; } 580 set { Controller.RenameImageUrl = value; } 581 } 582 583 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 584 [DefaultValue ("")] 585 [UrlProperty] 586 [Bindable (true)] 587 [Category ("Appearance")] 588 public string CopyImageUrl { 589 get { return Controller.CopyImageUrl; } 590 set { Controller.CopyImageUrl = value; } 591 } 592 593 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 594 [DefaultValue ("")] 595 [UrlProperty] 596 [Bindable (true)] 597 [Category ("Appearance")] 598 public string MoveImageUrl { 599 get { return Controller.MoveImageUrl; } 600 set { Controller.MoveImageUrl = value; } 601 } 602 603 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 604 [DefaultValue ("")] 605 [UrlProperty] 606 [Bindable (true)] 607 [Category ("Appearance")] 608 public string FolderUpImageUrl { 609 get { return Controller.FolderUpImageUrl; } 610 set { Controller.FolderUpImageUrl = value; } 611 } 612 613 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 614 [DefaultValue ("")] 615 [UrlProperty] 616 [Bindable (true)] 617 [Category ("Appearance")] 618 public string NewFolderImageUrl { 619 get { return Controller.NewFolderImageUrl; } 620 set { Controller.NewFolderImageUrl = value; } 621 } 622 623 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 624 [DefaultValue ("")] 625 [UrlProperty] 626 [Bindable (true)] 627 [Category ("Appearance")] 628 public string ViewImageUrl { 629 get { return Controller.ViewImageUrl; } 630 set { Controller.ViewImageUrl = value; } 631 } 632 633 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 634 [DefaultValue ("")] 635 [UrlProperty] 636 [Bindable (true)] 637 [Category ("Appearance")] 638 public string ProcessImageUrl { 639 get { return Controller.ProcessImageUrl; } 640 set { Controller.ProcessImageUrl = value; } 641 } 642 643 [Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))] 644 [DefaultValue ("")] 645 [UrlProperty] 646 [Bindable (true)] 647 [Category ("Appearance")] 648 public string RefreshImageUrl { 649 get { return Controller.RefreshImageUrl; } 650 set { Controller.RefreshImageUrl = value; } 651 } 652 653 #endregion 654 } 655}