Subversion Repositories recyclebinunit

Rev

Rev 90 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit RecyclerListingMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ComCtrls, ExtCtrls, ImgList;
  8.  
  9. type
  10.   TRecyclerListingMainForm = class(TForm)
  11.     TreeView1: TTreeView;
  12.     Panel1: TPanel;
  13.     Button1: TButton;
  14.     CheckBox1: TCheckBox;
  15.     Button2: TButton;
  16.     OpenDialog1: TOpenDialog;
  17.     LabeledEdit1: TLabeledEdit;
  18.     ImageList1: TImageList;
  19.     CheckBox2: TCheckBox;
  20.     Button3: TButton;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure FormShow(Sender: TObject);
  24.     procedure TreeView1DblClick(Sender: TObject);
  25.     procedure Button3Click(Sender: TObject);
  26.   private
  27.     localRecyclersNode: TTreeNode;
  28.     individualRecyclersNode: TTreeNode;
  29.   end;
  30.  
  31. var
  32.   RecyclerListingMainForm: TRecyclerListingMainForm;
  33.  
  34. implementation
  35.  
  36. {$R *.dfm}
  37.  
  38. uses
  39.   RecBinUnit2, ContNrs, SIDUnit, ShellAPI;
  40.  
  41. // TODO: SID Namen auflösen und dementsprechend anzeigen
  42. // TODO: zu jedem element mehr informationen anzeigen, nicht nur den ursprungsnamen
  43. // TODO: Einstellungen usw anzeigen, so wie im alten Demo
  44.  
  45. const
  46.   ICON_FILE = 0;
  47.   ICON_TREEVIEW = 2;
  48.   ICON_BIN = 4;
  49.   ICON_DRIVE = 6;
  50.   ICON_UNKNOWN = 8;
  51.   ICON_FOLDER = 10;
  52.  
  53. procedure TRecyclerListingMainForm.Button1Click(Sender: TObject);
  54. var
  55.   drives: TObjectList{TRbDrive};
  56.   iDrive: integer;
  57.   drive: TRbDrive;
  58.   nDrive: TTreeNode;
  59.  
  60.   bins: TObjectList{TRbRecycleBin};
  61.   iBin: integer;
  62.   bin: TRbRecycleBin;
  63.   nBin: TTreeNode;
  64.  
  65.   items: TObjectList{TRbRecycleBinItem};
  66.   iItem: integer;
  67.   item: TRbRecycleBinItem;
  68.   nItem: TTreeNode;
  69.   sCaption: string;
  70. resourcestring
  71.   S_DRIVE = 'Drive %s';
  72. begin
  73.   localRecyclersNode.DeleteChildren; // TODO: Will the objects be freed? This is important to avoid memory leaks.
  74.  
  75.   TreeView1.Items.BeginUpdate;
  76.   drives := TObjectList.Create(false);
  77.   bins := TObjectList.Create(false);
  78.   items := TObjectList.Create(false);
  79.   try
  80.     drives.Clear;
  81.     TRecycleBinManager.ListDrives(drives);
  82.     for iDrive := 0 to drives.Count - 1 do
  83.     begin
  84.       drive := drives.Items[iDrive] as TRbDrive;
  85.  
  86.       if drive.VolumeGUIDAvailable then
  87.         nDrive := TreeView1.Items.AddChildObject(localRecyclersNode, Format(S_DRIVE, [drive.DriveLetter])+': ' + GUIDToString(drive.VolumeGUID), drive)
  88.       else
  89.         nDrive := TreeView1.Items.AddChildObject(localRecyclersNode, Format(S_DRIVE, [drive.DriveLetter])+':', drive);
  90.       nDrive.ImageIndex := ICON_DRIVE;
  91.       nDrive.SelectedIndex := nDrive.ImageIndex;
  92.  
  93.       bins.Clear;
  94.       if CheckBox1.Checked then
  95.         drive.ListRecycleBins(bins, GetMySID)
  96.       else
  97.         drive.ListRecycleBins(bins);
  98.       for iBin := 0 to bins.Count - 1 do
  99.       begin
  100.         bin := bins.Items[iBin] as TRbRecycleBin;
  101.  
  102.         nBin := TreeView1.Items.AddChildObject(nDrive, bin.FileOrDirectory, bin);
  103.         nBin.ImageIndex := ICON_BIN;
  104.         nBin.SelectedIndex := nBin.ImageIndex;
  105.  
  106.         items.Clear;
  107.         bin.ListItems(items);
  108.         for iItem := 0 to items.Count - 1 do
  109.         begin
  110.           item := items.Items[iItem] as TRbRecycleBinItem;
  111.  
  112.           if not FileExists(item.PhysicalFile) and
  113.              not DirectoryExists(item.PhysicalFile) and
  114.              CheckBox2.Checked then continue;
  115.  
  116.           sCaption := item.Source;
  117.           if item is TRbVistaItem (*item.IndexFile <> ''*) then sCaption := sCaption + ' ('+ExtractFileName(item.IndexFile)+')';
  118.           nItem := TreeView1.Items.AddChildObject(nBin, sCaption, item);
  119.  
  120.           if FileExists(item.PhysicalFile) then
  121.             nItem.ImageIndex := ICON_FILE
  122.           else if DirectoryExists(item.PhysicalFile) then
  123.             nItem.ImageIndex := ICON_FOLDER // TODO: Feature: Read folder contents and display them in this treeview. (Also change icon to "open folder")
  124.           else
  125.             nItem.ImageIndex := ICON_UNKNOWN;
  126.           nItem.SelectedIndex := nItem.ImageIndex;
  127.         end;
  128.       end;
  129.     end;
  130.   finally
  131.     drives.Free;
  132.     bins.Free;
  133.     items.Free;
  134.     TreeView1.Items.EndUpdate;
  135.   end;
  136.  
  137.   localRecyclersNode.Expand(false);
  138. end;
  139.  
  140. procedure TRecyclerListingMainForm.Button2Click(Sender: TObject);
  141. var
  142.   bin: TRbRecycleBin;
  143.   nBin: TTreeNode;
  144.  
  145.   items: TObjectList{TRbRecycleBinItem};
  146.   iItem: integer;
  147.   item: TRbRecycleBinItem;
  148.   nItem: TTreeNode;
  149.   sCaption: string;
  150. begin
  151.   bin := TRbRecycleBin.Create(LabeledEdit1.Text);
  152.  
  153.   nBin := TreeView1.Items.AddChildObject(individualRecyclersNode, bin.FileOrDirectory, bin);
  154.   nBin.ImageIndex := ICON_BIN;
  155.   nBin.SelectedIndex := nBin.ImageIndex;
  156.   individualRecyclersNode.Expand(false);
  157.  
  158.   items := TObjectList.Create(false);
  159.   try
  160.     items.Clear;
  161.     bin.ListItems(items);
  162.     for iItem := 0 to items.Count - 1 do
  163.     begin
  164.       item := items.Items[iItem] as TRbRecycleBinItem;
  165.  
  166.       if not FileExists(item.PhysicalFile) and
  167.          not DirectoryExists(item.PhysicalFile) and
  168.          CheckBox2.Checked then continue;
  169.  
  170.       sCaption := item.Source;
  171.       if item is TRbVistaItem (*item.IndexFile <> ''*) then sCaption := sCaption + ' ('+ExtractFileName(item.IndexFile)+')';
  172.       nItem := TreeView1.Items.AddChildObject(nBin, sCaption, item);
  173.  
  174.       if FileExists(item.PhysicalFile) then
  175.         nItem.ImageIndex := ICON_FILE
  176.       else if DirectoryExists(item.PhysicalFile) then
  177.         nItem.ImageIndex := ICON_FOLDER // TODO: Feature: Read folder contents and display them in this treeview. (Also change icon to "open folder")
  178.       else
  179.         nItem.ImageIndex := ICON_UNKNOWN;
  180.       nItem.SelectedIndex := nItem.ImageIndex;
  181.     end;
  182.   finally
  183.     items.Free;
  184.   end;
  185.  
  186.   nBin.Expand(false);
  187. end;
  188.  
  189. procedure TRecyclerListingMainForm.Button3Click(Sender: TObject);
  190. begin
  191.   if OpenDialog1.Execute then
  192.     LabeledEdit1.Text := OpenDialog1.FileName;
  193. end;
  194.  
  195. procedure TRecyclerListingMainForm.FormShow(Sender: TObject);
  196. resourcestring
  197.   S_LOCAL_RECYCLE_BINS = 'Local recycle bins';
  198.   S_MANUAL_RECYCLE_BINS ='Manually added recycle bins';
  199. begin
  200.   localRecyclersNode := TreeView1.Items.Add(nil, S_LOCAL_RECYCLE_BINS);
  201.   localRecyclersNode.ImageIndex := ICON_TREEVIEW;
  202.   localRecyclersNode.SelectedIndex := localRecyclersNode.ImageIndex;
  203.  
  204.   individualRecyclersNode := TreeView1.Items.Add(nil, S_MANUAL_RECYCLE_BINS);
  205.   individualRecyclersNode.ImageIndex := ICON_TREEVIEW;
  206.   individualRecyclersNode.SelectedIndex := individualRecyclersNode.ImageIndex;
  207. end;
  208.  
  209. procedure TRecyclerListingMainForm.TreeView1DblClick(Sender: TObject);
  210. var
  211.   item: TRbRecycleBinItem;
  212. begin
  213.   if TreeView1.Selected.ImageIndex = ICON_FILE then
  214.   begin
  215.     // File
  216.     item := TRbRecycleBinItem(TreeView1.Selected.Data);
  217.     // TODO: Does not work if the file type is unknown
  218.     // TODO: Maybe we should add a feature to drag'n'drop a file/folder out of RecycleBinUnit into the explorer (With options copy or move, depending on the ShiftState)
  219.     ShellExecute(Handle, 'open', PChar(item.PhysicalFile), '', '', SW_NORMAL);
  220.   end;
  221.   if TreeView1.Selected.ImageIndex = ICON_FOLDER then
  222.   begin
  223.     // Folder
  224.     item := TRbRecycleBinItem(TreeView1.Selected.Data);
  225.     ShellExecute(Handle, 'open', PChar(item.PhysicalFile), '', '', SW_NORMAL);
  226.   end;
  227. end;
  228.  
  229. end.
  230.