RandomClear icon

"Save" report instead of sending

RandomClear | PRO | 07/23/15 09:09:50 AM UTC | 0 ⭐ | 10682 👁️ | Never ⏰ | []
Delphi |

4.62 KB

|

None

|

0 👍

/

0 👎

// See also: http://pastebin.com/WBYbtbSn - "how to always create ZIP/ELP file"
// See also: http://pastebin.com/3irtZ3iU - "how to save/capture ZIP/ELP file"
 
// Zipped bug report (.elp) is created only for sending.
// Therefore, only send code has access to zipped bug report.
// It's not created if no sending is used.
// This means that if we want to capture/save this file - then we can only do this from send code.
 
// Consider using "Save a ZIP file copy in case of send failure" and "Copy log text in case of send error" options 
// (see: http://www.eurekalog.com/help/eurekalog/advanced_page.php )
// instead of this code.
 
uses
  ActiveX,
  ETypes, ESend, ESysInfo, EModules;
 
type
  // Create a new "send engine".
  // It will be dummy that will perform no actual sending.
  // Instead - it will save all files attachments to folder.
  TSaveToFile = class(TELUniversalSender)
  private
    procedure Dummy(Sender: TObject);
  public
    function SendMessage: TResponse; override;
  end;
 
{ TSaveToFile }
 
function TSaveToFile.SendMessage: TResponse;
 
  procedure CopyBugReport(const AFileName: String);
  var
    X: Integer;
    TargetPath: String;
  begin
    // Copy primary attachment (.elp bug report)
    Win32Check(CopyFile(PChar(AttachedFiles[0]), PChar(AFileName), False));
 
    if AttachedFiles.Count = 1 then
      Exit;
 
    // Copy all remaining files attachments to some folder.
    // Usually there is only one file (.elp), but it can be several files - depends on your project options and your customization code
    TargetPath := ExtractFilePath(AFileName);
    for X := 1 to AttachedFiles.Count - 1 do
      Win32Check(CopyFile(PChar(AttachedFiles[X]), PChar(TargetPath + ExtractFileName(AttachedFiles[X])), False));
  end;
 
var
  SaveDialog: TSaveDialog;
  TargetFile: string;
  TargetExt: string;
  NeedToUninitialize: Boolean;
begin
  Finalize(Result);
  FillChar(Result, SizeOf(Result), 0);
  try
    if AttachedFiles.Count = 0 then
    begin
      // We should not really get there, this is just fail-safe check
      Result.SendResult := srNoExceptionInfo;
      Exit;
    end;
 
    // Ask where to save bug report
 
    // Send is performed in background thread by default (use options to change that)
    // We need to initialize COM for use in modern file dialogs (Vista+)
    NeedToUninitialize := Succeeded(CoInitialize(nil));
    try
 
      SaveDialog := TSaveDialog.Create(nil);
      try
        TargetFile := ExtractFileName(AttachedFiles[0]); // usually this is primary bug report file
        TargetExt := ExtractFileExt(TargetFile); // can be .el or .elp - depends on your options
 
        SaveDialog.Title := 'Save Bug Report';
        SaveDialog.InitialDir := GetFolderPersonal; // "My Documents"
        SaveDialog.FileName := TargetFile;
        SaveDialog.DefaultExt := TargetExt;
        SaveDialog.Filter := 'Bug Reports|*' + TargetExt + '|All Files|*.*';
        SaveDialog.FilterIndex := 0;
        SaveDialog.Options := [ofOverwritePrompt,ofHideReadOnly,ofPathMustExist,ofNoReadOnlyReturn,ofEnableSizing,ofDontAddToRecent];
 
        // Assigning OnIncludeItem, OnShow or OnClose will turn off Vista dialogs (which requires COM)
        // Classic dialogs do not require COM
        if not NeedToUninitialize then
          SaveDialog.OnClose := Dummy;
 
        if SaveDialog.Execute then // <- this may fail without COM initialization - depends on your IDE and OS
          CopyBugReport(SaveDialog.FileName);
 
      finally
        FreeAndNil(SaveDialog);
      end;
    finally
      if NeedToUninitialize then
        CoUninitialize;
    end;
 
    // Indicate that "send" was a success.
    Result.SendResult := srSent;
 
  except
    on E: Exception do
    begin
      // Indicate that "send" was a failure.
      Result.SendResult := srUnknownError;
      if E is EOSError then
        Result.ErrorCode := Integer(EOSError(E).ErrorCode);
      Result.ErrorMessage := E.Message;
    end;
  end;
end;
 
procedure TSaveToFile.Dummy(Sender: TObject);
begin
  // Do nothing
end;
 
initialization
  // Register our class to be used by EurekaLog
  RegisterSender(TSaveToFile);
 
  // Set our sender as the one and only - this is important
  CurrentEurekaLogOptions.SenderClasses := TSaveToFile.ClassName;
 
  // Change "Send" button to "Save" button in all dialogs
  CurrentEurekaLogOptions.CustomizedTexts[mtMSDialog_SendButtonCaption] := 'Save Report';
  CurrentEurekaLogOptions.CustomizedTexts[mtMSDialog_NoSendButtonCaption] := 'Don''t Save';
  CurrentEurekaLogOptions.CustomizedTexts[mtDialog_SendMessage] := 'Save this report into file';
end.

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎