unit uViewDocumentos; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, uViewBase, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinSilver, dxSkinStardust, dxSkinsDefaultPainters, dxSkinValentine, dxSkinXmas2008Blue, cxControls, cxContainer, cxListBox, ActnList, TB2Item, TBX, TB2Dock, TB2Toolbar; type TfrViewDocumentos = class(TfrViewBase) ListaDocumentos: TcxListBox; EditorActionList: TActionList; TBXDock: TTBXDock; tbxMain: TTBXToolbar; TBXItem2: TTBXItem; actAnadir: TAction; actModificar: TAction; actEliminar: TAction; TBXItem1: TTBXItem; TBXItem3: TTBXItem; actEliminarTodo: TAction; TBXItem4: TTBXItem; actActualizarServidor: TAction; TBXItem5: TTBXItem; procedure actAnadirExecute(Sender: TObject); procedure actModificarExecute(Sender: TObject); procedure actEliminarExecute(Sender: TObject); procedure actEliminarTodoExecute(Sender: TObject); procedure CustomViewCreate(Sender: TObject); procedure CustomViewDestroy(Sender: TObject); procedure actActualizarServidorExecute(Sender: TObject); private FDirectorio: Variant; function GetDirectorio: Variant; function DarFicherosSeleccionados: TStringList; protected FListaDocumentos: TStringList; procedure Sincronizar; virtual; abstract; function RecuperarFicheroServidor(const NombreFichero: String; const DestinoFichero: String): Boolean; virtual; abstract; procedure Refrescar; virtual; public property Directorio: Variant read GetDirectorio; end; var frViewDocumentos: TfrViewDocumentos; implementation {$R *.dfm} uses uSistemaFunc, ShellAPI, uDialogUtils; procedure TfrViewDocumentos.actActualizarServidorExecute(Sender: TObject); begin inherited; Sincronizar; end; procedure TfrViewDocumentos.actAnadirExecute(Sender: TObject); var openDialog : TOpenDialog; FicheroOrigen : String; i : Integer; begin openDialog := TOpenDialog.Create(self); try // Set up the starting directory to be the current one // openDialog.InitialDir := GetCurrentDir; // Only allow existing files to be selected // openDialog.Options := [ofFileMustExist]; // Allow multiple files to be selected - of any type openDialog.Options := [ofAllowMultiSelect]; // Allow only .dpr and .pas files to be selected // openDialog.Filter := 'Delphi project files|*.dpr|Delphi pascal files|*.pas'; // Select pascal files as the starting filter type // openDialog.FilterIndex := 2; if openDialog.Execute then begin for i:=0 to openDialog.Files.Count - 1 do begin FicheroOrigen := openDialog.Files.Strings[i]; if Length(FicheroOrigen) > 0 then CopiarFichero(FicheroOrigen, (Directorio + ExtractFileName(FicheroOrigen))); //Lo añadimos a la lista de documentos local FListaDocumentos.Add(ExtractFileName(FicheroOrigen)) end; end; finally openDialog.Free; Refrescar; end; end; procedure TfrViewDocumentos.actEliminarExecute(Sender: TObject); var i, j: Integer; FicherosSeleccionados: TStringList; ANombreDocumento: String; begin inherited; FicherosSeleccionados := DarFicherosSeleccionados; for i := 0 to FicherosSeleccionados.Count - 1 do begin if FileExists(FicherosSeleccionados.Strings[i]) then if not DeleteFile(FicherosSeleccionados.Strings[i]) then Application.MessageBox('No se pudo borrar el fichero', 'Error', MB_ICONEXCLAMATION); ANombreDocumento := ExtractFileName(FicherosSeleccionados.Strings[i]); if FListaDocumentos.Find(ANombreDocumento, j) then FListaDocumentos.Delete(j); end; Refrescar; end; procedure TfrViewDocumentos.actEliminarTodoExecute(Sender: TObject); begin // if (ShowConfirmMessage('¿Desea borrar todos los documentos asociados a este presupuesto de cliente?', '') = IDYES) then // begin Deltree(Directorio, True); FListaDocumentos.Clear; FDirectorio := Null; // end; Refrescar; end; procedure TfrViewDocumentos.actModificarExecute(Sender: TObject); var i: Integer; FicherosSeleccionados: TStringList; begin inherited; FicherosSeleccionados := DarFicherosSeleccionados; for i := 0 to FicherosSeleccionados.Count - 1 do if FileExists(FicherosSeleccionados.Strings[i]) then begin // showmessage('Existe: ' + FicherosSeleccionados.Strings[i]); if ShellExecute(Handle, nil, pchar(FicherosSeleccionados.Strings[i]), nil, nil, SW_SHOWNORMAL) <= 32 then Application.MessageBox('No se pudo editar el fichero', 'Error', MB_ICONEXCLAMATION); end else begin // showmessage('No existe: ' + FicherosSeleccionados.Strings[i]); if not RecuperarFicheroServidor(ExtractFileName(FicherosSeleccionados.Strings[i]), ExtractFilePath(FicherosSeleccionados.Strings[i])) then Application.MessageBox('No se pudo RecuperarFicheroServidor', 'Error', MB_ICONEXCLAMATION); if ShellExecute(Handle, nil, pchar(FicherosSeleccionados.Strings[i]), nil, nil, SW_SHOWNORMAL) <= 32 then Application.MessageBox('No se pudo editar el fichero', 'Error', MB_ICONEXCLAMATION); end; end; procedure TfrViewDocumentos.CustomViewCreate(Sender: TObject); begin inherited; FDirectorio := Null; FListaDocumentos := Nil; end; procedure TfrViewDocumentos.CustomViewDestroy(Sender: TObject); begin FDirectorio := Null; FreeAndNil(FListaDocumentos); inherited; end; function TfrViewDocumentos.DarFicherosSeleccionados: TStringList; var i: Integer; begin Result := TStringList.Create; for i := 0 to ListaDocumentos.Count - 1 do begin if ListaDocumentos.Selected[i] then Result.Add(Directorio + ListaDocumentos.Items[i]); end; end; function TfrViewDocumentos.GetDirectorio: Variant; begin if VarIsNull(FDirectorio) then begin FDirectorio := DarDirectorioTemporal; if not CreateDir(FDirectorio) then raise Exception.Create('ERROR: No se ha podido crear el fichero temporal GetDirectorio'); end; Result := FDirectorio; end; procedure TfrViewDocumentos.Refrescar; var ADocumentos: TStringList; i: Integer; begin // ADocumentos := FindFile(Directorio + '*.*'); // for i := 0 to ADocumentos.Count - 1 do // ADocumentos.Strings[i] := ExtractFileName(ADocumentos.Strings[i]); ListaDocumentos.Clear; ListaDocumentos.Items.AddStrings(FListaDocumentos); // FreeAndNil(ADocumentos); end; end.