Advertisement
RandomClear

"Save" report instead of sending

Jul 23rd, 2015
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.62 KB | None | 0 0
  1. // See also: http://pastebin.com/WBYbtbSn - "how to always create ZIP/ELP file"
  2. // See also: http://pastebin.com/3irtZ3iU - "how to save/capture ZIP/ELP file"
  3.  
  4. // Zipped bug report (.elp) is created only for sending.
  5. // Therefore, only send code has access to zipped bug report.
  6. // It's not created if no sending is used.
  7. // This means that if we want to capture/save this file - then we can only do this from send code.
  8.  
  9. // Consider using "Save a ZIP file copy in case of send failure" and "Copy log text in case of send error" options
  10. // (see: http://www.eurekalog.com/help/eurekalog/advanced_page.php )
  11. // instead of this code.
  12.  
  13. uses
  14.   ActiveX,
  15.   ETypes, ESend, ESysInfo, EModules;
  16.  
  17. type
  18.   // Create a new "send engine".
  19.   // It will be dummy that will perform no actual sending.
  20.   // Instead - it will save all files attachments to folder.
  21.   TSaveToFile = class(TELUniversalSender)
  22.   private
  23.     procedure Dummy(Sender: TObject);
  24.   public
  25.     function SendMessage: TResponse; override;
  26.   end;
  27.  
  28. { TSaveToFile }
  29.  
  30. function TSaveToFile.SendMessage: TResponse;
  31.  
  32.   procedure CopyBugReport(const AFileName: String);
  33.   var
  34.     X: Integer;
  35.     TargetPath: String;
  36.   begin
  37.     // Copy primary attachment (.elp bug report)
  38.     Win32Check(CopyFile(PChar(AttachedFiles[0]), PChar(AFileName), False));
  39.  
  40.     if AttachedFiles.Count = 1 then
  41.       Exit;
  42.  
  43.     // Copy all remaining files attachments to some folder.
  44.     // Usually there is only one file (.elp), but it can be several files - depends on your project options and your customization code
  45.     TargetPath := ExtractFilePath(AFileName);
  46.     for X := 1 to AttachedFiles.Count - 1 do
  47.       Win32Check(CopyFile(PChar(AttachedFiles[X]), PChar(TargetPath + ExtractFileName(AttachedFiles[X])), False));
  48.   end;
  49.  
  50. var
  51.   SaveDialog: TSaveDialog;
  52.   TargetFile: string;
  53.   TargetExt: string;
  54.   NeedToUninitialize: Boolean;
  55. begin
  56.   Finalize(Result);
  57.   FillChar(Result, SizeOf(Result), 0);
  58.   try
  59.     if AttachedFiles.Count = 0 then
  60.     begin
  61.       // We should not really get there, this is just fail-safe check
  62.       Result.SendResult := srNoExceptionInfo;
  63.       Exit;
  64.     end;
  65.  
  66.     // Ask where to save bug report
  67.  
  68.     // Send is performed in background thread by default (use options to change that)
  69.     // We need to initialize COM for use in modern file dialogs (Vista+)
  70.     NeedToUninitialize := Succeeded(CoInitialize(nil));
  71.     try
  72.  
  73.       SaveDialog := TSaveDialog.Create(nil);
  74.       try
  75.         TargetFile := ExtractFileName(AttachedFiles[0]); // usually this is primary bug report file
  76.         TargetExt := ExtractFileExt(TargetFile); // can be .el or .elp - depends on your options
  77.  
  78.         SaveDialog.Title := 'Save Bug Report';
  79.         SaveDialog.InitialDir := GetFolderPersonal; // "My Documents"
  80.         SaveDialog.FileName := TargetFile;
  81.         SaveDialog.DefaultExt := TargetExt;
  82.         SaveDialog.Filter := 'Bug Reports|*' + TargetExt + '|All Files|*.*';
  83.         SaveDialog.FilterIndex := 0;
  84.         SaveDialog.Options := [ofOverwritePrompt,ofHideReadOnly,ofPathMustExist,ofNoReadOnlyReturn,ofEnableSizing,ofDontAddToRecent];
  85.  
  86.         // Assigning OnIncludeItem, OnShow or OnClose will turn off Vista dialogs (which requires COM)
  87.         // Classic dialogs do not require COM
  88.         if not NeedToUninitialize then
  89.           SaveDialog.OnClose := Dummy;
  90.  
  91.         if SaveDialog.Execute then // <- this may fail without COM initialization - depends on your IDE and OS
  92.           CopyBugReport(SaveDialog.FileName);
  93.  
  94.       finally
  95.         FreeAndNil(SaveDialog);
  96.       end;
  97.     finally
  98.       if NeedToUninitialize then
  99.         CoUninitialize;
  100.     end;
  101.  
  102.     // Indicate that "send" was a success.
  103.     Result.SendResult := srSent;
  104.  
  105.   except
  106.     on E: Exception do
  107.     begin
  108.       // Indicate that "send" was a failure.
  109.       Result.SendResult := srUnknownError;
  110.       if E is EOSError then
  111.         Result.ErrorCode := Integer(EOSError(E).ErrorCode);
  112.       Result.ErrorMessage := E.Message;
  113.     end;
  114.   end;
  115. end;
  116.  
  117. procedure TSaveToFile.Dummy(Sender: TObject);
  118. begin
  119.   // Do nothing
  120. end;
  121.  
  122. initialization
  123.   // Register our class to be used by EurekaLog
  124.   RegisterSender(TSaveToFile);
  125.  
  126.   // Set our sender as the one and only - this is important
  127.   CurrentEurekaLogOptions.SenderClasses := TSaveToFile.ClassName;
  128.  
  129.   // Change "Send" button to "Save" button in all dialogs
  130.   CurrentEurekaLogOptions.CustomizedTexts[mtMSDialog_SendButtonCaption] := 'Save Report';
  131.   CurrentEurekaLogOptions.CustomizedTexts[mtMSDialog_NoSendButtonCaption] := 'Don''t Save';
  132.   CurrentEurekaLogOptions.CustomizedTexts[mtDialog_SendMessage] := 'Save this report into file';
  133. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement