Subversion Repositories decoder

Rev

Blame | Last modification | View Log | RSS feed

  1. unit SecureMoveMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   DragDropHandler, SysUtils, IniFiles, Menus, ShellAPI, Windows, Classes,
  7.   ComServ, ComObj, Registry, Forms, ShlObj, ComCtrls;
  8.  
  9. {$ifndef VER13_PLUS}
  10. type
  11.   TDataModule = TForm;
  12. {$endif}
  13.  
  14. type
  15.   TDataModuleDragDropHandler = class(TDataModule, IUnknown, IShellExtInit, IContextMenu)
  16.     PopupMenu1: TPopupMenu;
  17.     MenuEncrypt: TMenuItem;
  18.     MenuLine1: TMenuItem;
  19.     procedure MenuEncryptClick(Sender: TObject);
  20.     procedure DragDropHandler1Popup(Sender: TObject);
  21.     procedure DataModuleCreate(Sender: TObject);
  22.     procedure DataModuleDestroy(Sender: TObject);
  23.   private
  24.     FFiles: TStrings;
  25.     DragDropHandler1: TDragDropHandler;
  26.     procedure MoveFile(const Filename: string);
  27.   public
  28.     property ContextMenuHandler: TDragDropHandler read DragDropHandler1
  29.       implements IShellExtInit, IContextMenu;
  30.   end;
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35.  
  36. type
  37.   TDragDropHandlerFactoryAbs = class(TDragDropHandlerFactory)
  38.   public
  39.     procedure UpdateRegistry(Register: boolean); override;
  40.   end;
  41.  
  42. const
  43.   CLSID_DragDropHandler: TGUID = '{54069E5A-C471-4B68-835C-FC845E64040B}';
  44.  
  45. var
  46.   sTitle: string;
  47.   sDescription: string;
  48.   langini: TIniFile;
  49.   path: string;
  50.   mydocuments: string;
  51.  
  52. resourcestring
  53.   sFileClass = 'Folder';
  54.   sFileExtension = '';
  55.   sClassName = 'SecureMove';
  56.  
  57. function ExecuteWithExitCode(filename, params, dir: string): integer;
  58. var
  59.   proc_info: TProcessInformation;
  60.   startinfo: TStartupInfo;
  61.   ExitCode: longword;
  62. begin
  63.   FillChar(proc_info, sizeof(TProcessInformation), 0);
  64.   FillChar(startinfo, sizeof(TStartupInfo), 0);
  65.   startinfo.cb := sizeof(TStartupInfo);
  66.  
  67.   if CreateProcess(nil, pchar(Format('"%s" %s', [filename, TrimRight(pchar(params))])), nil,
  68.       nil, false, NORMAL_PRIORITY_CLASS, nil, pchar(path),
  69.        startinfo, proc_info) <> False then
  70.   begin
  71.     // WaitForSingleObject(proc_info.hProcess, INFINITE);
  72.     while WaitForSingleObject(proc_info.hProcess, 0) = WAIT_TIMEOUT do
  73.     begin
  74.       // ProcessMessages(0);
  75.       Sleep(50);
  76.     end;
  77.     GetExitCodeProcess(proc_info.hProcess, ExitCode);
  78.     CloseHandle(proc_info.hThread);
  79.     CloseHandle(proc_info.hProcess);
  80.     result := ExitCode;
  81.   end
  82.   else
  83.   begin
  84.     result := -1;
  85.   end;
  86. end;
  87.  
  88. procedure TDragDropHandlerFactoryAbs.UpdateRegistry(Register: boolean);
  89. var
  90.   ClassID: string;
  91. begin
  92.   ClassID := GUIDToString(CLSID_DragDropHandler);
  93.  
  94.   if Register then
  95.   begin
  96.     inherited UpdateRegistry(Register);
  97.  
  98.     if (Win32Platform = VER_PLATFORM_WIN32_NT) then
  99.       with TRegistry.Create do
  100.         try
  101.           RootKey := HKEY_LOCAL_MACHINE;
  102.           OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved', True);
  103.           WriteString(ClassID, '(De)Coder-SecureMove');
  104.           CloseKey;
  105.         finally
  106.           Free;
  107.         end;
  108.   end
  109.   else
  110.   begin
  111.     if (Win32Platform = VER_PLATFORM_WIN32_NT) then
  112.       with TRegistry.Create do
  113.         try
  114.           RootKey := HKEY_LOCAL_MACHINE;
  115.           OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved', True);
  116.           DeleteValue(ClassID);
  117.           CloseKey;
  118.         finally
  119.           Free;
  120.         end;
  121.  
  122.     inherited UpdateRegistry(Register);
  123.   end;
  124. end;
  125.  
  126. // http://www.delphipraxis.net/topic1451_dateioperationen+mit+shfileoperation.html
  127. function DoFileWork(aOperation: FILEOP_FLAGS; aFrom, aTo: AnsiString;
  128.     Flags: FILEOP_FLAGS): Integer;
  129. var
  130.   FromPath, ToPath: AnsiString;
  131.   SHFileOpStruct: TSHFileOpStruct;
  132. begin
  133.   FromPath := aFrom + #0#0;
  134.   ToPath := aTo + #0#0;
  135.   with SHFileOpStruct do
  136.   begin
  137.     Wnd := 0;
  138.     wFunc := aOperation;
  139.     pFrom := PAnsiChar(FromPath);
  140.     if ToPath <> '' then
  141.     begin
  142.       pTo := PAnsiChar(ToPath)
  143.     end else begin // target available
  144.       pTo := nil;
  145.     end; // target not available
  146.     fFlags := Flags;
  147.   end; // structure
  148.   Result := SHFileOperationA(SHFileOpStruct);
  149. end;
  150.  
  151. procedure TDataModuleDragDropHandler.DataModuleCreate(Sender: TObject);
  152. begin
  153.   FFiles := TStringList.Create;
  154.   DragDropHandler1 := TDragDropHandler.Create(self);
  155.   DragDropHandler1.OnPopup := DragDropHandler1Popup;
  156.   DragDropHandler1.ContextMenu := popupmenu1;
  157.   popupmenu1.Items.Items[0].Hint := sDescription;
  158.   popupmenu1.Items.Items[0].Caption := sTitle;
  159. end;
  160.  
  161. procedure TDataModuleDragDropHandler.DataModuleDestroy(Sender: TObject);
  162. begin
  163.   FFiles.Free;
  164.   DragDropHandler1.free;
  165. end;
  166.  
  167. procedure TDataModuleDragDropHandler.MoveFile(const Filename: string);
  168. var
  169.   delform: TForm;
  170.   ani: TAnimate;
  171.   cd: integer;
  172. begin
  173.   // undo ist leider nicht möglich, weil es sich um keine move-aktion handelt
  174.   cd := DoFileWork(FO_COPY, Filename, DragDropHandler1.Folder+'\'+extractfilename(Filename), 0);
  175.   if cd = 0 then
  176.   begin
  177.     delform := TForm.Create(nil);
  178.     try
  179.       delform.caption := langini.ReadString('SecureMove', 'wait', '?');
  180.       delform.BorderStyle := bsSingle;
  181.       delform.BorderIcons := [biSystemMenu];
  182.       delform.Position := poDesktopCenter;
  183.  
  184.       ani := TAnimate.Create(delform);
  185.       try
  186.         ani.Parent := delform;
  187.         ani.CommonAVI := aviDeleteFile;
  188.         ani.Active := true;
  189.         ani.Top := 8;
  190.         ani.Left := 8;
  191.         ani.Visible := true;
  192.  
  193.         delform.ClientWidth := ani.Width + 16;
  194.         delform.ClientHeight := ani.Height + 16;
  195.         delform.visible := true;
  196.  
  197.         // shellexecute(handle, 'open', pchar(path+'Coder.exe'), pchar('"'+Filename+'" /e'), pchar(path), SW_NORMAL);
  198.         if ExecuteWithExitCode(path+'Coder.exe', '"'+Filename+'" /e', path) = 8 then
  199.         begin
  200.           Messagebox(handle, pchar(Format(
  201.           langini.ReadString('SecureMove', 'error_del', '?'),
  202.           [Filename]
  203.           )), pchar(langini.ReadString('ShlErase', 'error', '?')), MB_ICONERROR or MB_OK);
  204.         end;
  205.       finally
  206.         ani.free;
  207.       end;
  208.     finally
  209.       delform.Free;
  210.     end;
  211.   end
  212.   else
  213.   begin
  214.     // fehlercodes für abbruch beim überschreiben: 138, 7
  215.     Messagebox(handle, pchar(Format(
  216.     langini.ReadString('SecureMove', 'error_copy', '?'),
  217.     [Filename, DragDropHandler1.Folder+'\', cd]
  218.     )), pchar(langini.ReadString('ShlErase', 'error', '?')), MB_ICONERROR or MB_OK);
  219.   end;
  220. end;
  221.  
  222. procedure TDataModuleDragDropHandler.MenuEncryptClick(Sender: TObject);
  223. var
  224.   i: integer;
  225. begin
  226.   for i := 0 to FFiles.Count-1 do
  227.     MoveFile(FFiles[i]);
  228. end;
  229.  
  230. procedure TDataModuleDragDropHandler.DragDropHandler1Popup(Sender: TObject);
  231.  
  232.   procedure ClearItem(Item: TMenuItem);
  233.   begin
  234.   {$ifdef VER13_PLUS}
  235.     Item.Clear;
  236.   {$else}
  237.     while (Item.Count > 0) do
  238.       Item[0].Free;
  239.   {$endif}
  240.   end;
  241.  
  242. var
  243.   i: integer;
  244. begin
  245.   FFiles.Assign(DragDropHandler1.Files);
  246.  
  247.   for i := 0 to FFiles.Count-1 do
  248.   begin
  249.     // AUSNAHMEN - KEIN VERSCHIEBEN MÖGLICH
  250.     if  // nicht-existierendes (z.b. arbeitsplatz)
  251.         (not fileexists(ffiles.Strings[i]) and not directoryexists(ffiles.Strings[i])) or
  252.         // "eigene dateien"-ordner
  253.        (uppercase(ffiles.Strings[i]) = uppercase(mydocuments)) or
  254.        // innerhalb des selben orderns verschieben
  255.        (DragDropHandler1.Folder+'\' = extractfilepath(ffiles.Strings[i])) or
  256.        // verzeichnis in sich selbst kopieren
  257.        (ffiles.Strings[i] = DragDropHandler1.Folder) or
  258.        // unbekannt
  259.        (FFiles.Count = 0) then
  260.     begin
  261.       ClearItem(PopupMenu1.Items);
  262.     end;
  263.   end;
  264. end;
  265.  
  266. initialization
  267.   path := '';
  268.   mydocuments := '';
  269.  
  270.   with TRegistry.Create do
  271.     try
  272.       RootKey := HKEY_CURRENT_USER;
  273.       OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\(De)Coder_is1');
  274.       path := ReadString('InstallLocation');
  275.       CloseKey;
  276.       if path = '' then
  277.       begin
  278.         RootKey := HKEY_LOCAL_MACHINE;
  279.         OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\(De)Coder_is1');
  280.         path := ReadString('InstallLocation');
  281.         CloseKey;
  282.       end;
  283.  
  284.       RootKey := HKEY_CURRENT_USER;
  285.       OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders');
  286.       mydocuments := ReadString('Personal');
  287.       CloseKey;
  288.     finally
  289.       free;
  290.     end;
  291.  
  292.   if copy(mydocuments, length(mydocuments), 1) = '\' then
  293.     mydocuments := copy(mydocuments, 0, length(mydocuments)-1);
  294.  
  295.   langini := TIniFile.Create(path+'Language.ini');
  296.  
  297.   sTitle := langini.ReadString('SecureMove', 'title', '?');
  298.   sDescription := langini.ReadString('SecureMove', 'description', '?');
  299.  
  300.   TDragDropHandlerFactoryAbs.Create(ComServer, TDataModuleDragDropHandler,
  301.     CLSID_DragDropHandler, sClassName, sDescription, sFileClass,
  302.     sFileExtension, ciMultiInstance);
  303.  
  304. finalization
  305.   langini.Free;
  306. end.
  307.  
  308.