Recent

Author Topic: download multiple files  (Read 411 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 302
download multiple files
« on: September 25, 2022, 08:17:45 pm »
hello brothers good day, I am using the project of the friend "getmem" of the following link:
https://forum.lazarus.freepascal.org/index.php/topic,52773.0.html
it works perfect, download all files at once, but i have 2 questions:
1. how to detect when each download is finished and show in the virtualtreeview
2. how to download one at a time, then the next one and show it in the virtualtreeview

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes,
  9.   SysUtils,
  10.   Forms,
  11.   Controls,
  12.   Graphics,
  13.   uDownload,
  14.   //opensslsockets,
  15.   Dialogs, StdCtrls,
  16.   VirtualTrees;
  17.  
  18. type
  19.   PData = ^TData;
  20.   TData = record
  21.     FUrl: String;
  22.     FName: String;
  23.     FState: String;
  24.   end;
  25.  
  26.  
  27.   { TForm1 }
  28.  
  29.   TForm1 = class(TForm)
  30.     Button1: TButton;
  31.     VST: TVirtualStringTree;
  32.     procedure Button1Click(Sender: TObject);
  33.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure VSTGetNodeDataSize(Sender: TBaseVirtualTree;
  36.       var NodeDataSize: Integer);
  37.     procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  38.       Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
  39.   private
  40.     FDownload: TDownload;
  41.     procedure DoOnDownloadProgress(Sender: TObject; {%H-}AFrom, ATo: String; APos, ASize, AElapsed, ARemaining, ASpeed: LongInt);
  42.     procedure DoOnDownloadCompleted(Sender: TObject);
  43.   public
  44.  
  45.   end;
  46.  
  47. var
  48.   Form1: TForm1;
  49.  
  50. implementation
  51.  
  52. {$R *.lfm}
  53.  
  54. { TForm1 }
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. var
  58.   Data: Pdata;
  59.   Node: PVirtualNode;
  60. begin
  61.   VST.Clear;
  62.   VST.NodeDataSize := SizeOf(TData);
  63.  
  64.   Node := VST.AddChild(nil);
  65.   Data := VST.GetNodeData(Node);
  66.   Data^.FUrl := 'https://d.winrar.es/d/103z1663045159/zqnGYe1hSuxmWneHCTGQ9Q/winrar-x64-611es.exe';
  67.   Data^.FName := 'winrar.exe';
  68.   Data^.FState := '';
  69.  
  70.   Node := VST.AddChild(nil);
  71.   Data := VST.GetNodeData(Node);
  72.   Data^.FUrl := 'https://download.mozilla.org/?product=firefox-latest&os=win64&lang=es-MX';
  73.   Data^.FName := 'firefox.exe';
  74.   Data^.FState := '';
  75.  
  76.   Node := VST.AddChild(nil);
  77.   Data := VST.GetNodeData(Node);
  78.   Data^.FUrl := 'https://ardownload2.adobe.com/pub/adobe/acrobat/win/AcrobatDC/2200220191/AcroRdrDCx642200220191_MUI.exe';
  79.   Data^.FName := 'adobe_reader.exe';
  80.   Data^.FState := '';
  81.  
  82.   Node := VST.AddChild(nil);
  83.   Data := VST.GetNodeData(Node);
  84.   Data^.FUrl := 'https://mirror.netcologne.de/videolan.org/vlc/3.0.17.4/win64/vlc-3.0.17.4-win64.exe';
  85.   Data^.FName := 'vlc.exe';
  86.   Data^.FState := '';
  87.  
  88.   Node := VST.AddChild(nil);
  89.   Data := VST.GetNodeData(Node);
  90.   Data^.FUrl := 'https://www.avast.com/es-ww/download-thank-you.php?product=FAV-AVAST&locale=es-ww&direct=1';
  91.   Data^.FName := 'avast.exe';
  92.   Data^.FState := '';
  93.  
  94.   Node := VST.AddChild(nil);
  95.   Data := VST.GetNodeData(Node);
  96.   Data^.FUrl := 'https://mirrors.ucr.ac.cr/tdf/libreoffice/stable/7.4.1/win/x86_64/LibreOffice_7.4.1_Win_x64.msi';
  97.   Data^.FName := 'libre_office.msi';
  98.   Data^.FState := '';
  99.  
  100.   Node := VST.AddChild(nil);
  101.   Data := VST.GetNodeData(Node);
  102.   Data^.FUrl := 'https://data-cdn.mbamupdates.com/web/mb4-setup-consumer/offline/mb4-setup-consumer-4.5.14.210-1.0.1751-1.0.59171.exe';
  103.   Data^.FName := 'malwarebytes.exe';
  104.   Data^.FState := '';
  105.  
  106.   FDownload := nil;
  107. end;
  108.  
  109. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  110. begin
  111.     if Assigned(FDownload) then
  112.     FDownload.CancelDownoad;
  113. end;
  114.  
  115. procedure TForm1.Button1Click(Sender: TObject);
  116. var
  117.    node : PVirtualNode ;
  118.    Data: PData;
  119. begin
  120.   // ShowMessage(ExtractFilePath(Application.ExeName));
  121.   node := VST.GetFirst ;
  122.   while assigned (node) do
  123.       begin
  124.       Data := VST.GetNodeData(Node);
  125.  
  126.       {
  127.       ShowMessage(Data^.FUrl);
  128.       ShowMessage(Data^.FName);
  129.       ShowMessage(Data^.FState);
  130.       }
  131.  
  132.       Data^.FState:='downloading';
  133.       FDownload := TDownload.Create;
  134.       FDownload.OnDownloadProgress := @DoOnDownloadProgress;
  135.       FDownload.OnDownloadCompleted := @DoOnDownloadCompleted;
  136.       FDownload.DownloadFile(Data^.FUrl, ExtractFilePath(Application.ExeName)+Data^.FName);
  137.  
  138.  
  139.       node := VST.GetNext(node) ;
  140.       end;
  141.       vst.setfocus;
  142.       vst.Refresh;
  143. end;
  144.  
  145. procedure TForm1.VSTGetNodeDataSize(Sender: TBaseVirtualTree;
  146.   var NodeDataSize: Integer);
  147. begin
  148.   NodeDataSize := SizeOf(TData);
  149. end;
  150.  
  151. procedure TForm1.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  152.   Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
  153. var
  154.   Data: PData;
  155. begin
  156.   // si no existen estos codes, el resultado sera el texto    "NODE"
  157.   Data := VST.GetNodeData(Node);
  158.   case Column of
  159.     0: CellText := Data^.FUrl;
  160.     1: CellText := Data^.FName;
  161.     2: CellText := Data^.FState;
  162.   end;
  163. End;
  164.  
  165. procedure TForm1.DoOnDownloadProgress(Sender: TObject; AFrom, ATo: String;
  166.   APos, ASize, AElapsed, ARemaining, ASpeed: LongInt);
  167. begin
  168.  
  169. end;
  170.  
  171. procedure TForm1.DoOnDownloadCompleted(Sender: TObject);
  172. begin
  173.      ShowMessage('Download completed!');
  174. end;
  175.  
  176. end.

PD: I am using lazarus + virtualtreeview on win10x64
attached project example and image

DLL SSL 32 bit: https://packages.lazarus-ide.org/openssl-1.0.2j-i386-win32.zip
DLL SSL 64 bit: https://packages.lazarus-ide.org/openssl-1.0.2j-x64_86-win64.zip
« Last Edit: September 25, 2022, 08:22:33 pm by Ericktux »

paweld

  • Hero Member
  • *****
  • Posts: 522
Re: download multiple files
« Reply #1 on: September 25, 2022, 09:00:33 pm »
Something like this - in the FDownload class add the OriginalURL property (without fix the protocol), and in OnProgress and OnCopleted we only update the node for the compatible URL
Best regards
paweld

Ericktux

  • Sr. Member
  • ****
  • Posts: 302
Re: download multiple files
« Reply #2 on: September 25, 2022, 09:30:18 pm »
hello friend thanks for answering, I have tried it and in the first click of the button it downloads only the first one, but if I give it a second click it downloads all

paweld

  • Hero Member
  • *****
  • Posts: 522
Re: download multiple files
« Reply #3 on: September 26, 2022, 12:28:22 am »
Try changing the line
Code: Pascal  [Select][+][-]
  1. Data^.FState := Format('downloading (%f %%)', [APos * 100 / ASize]);
  2.  
to:
Code: Pascal  [Select][+][-]
  1. Data^.FState := Format('downloading (%d bytes)', [APos]);
I didn't add checking if the size is different from 0
Best regards
paweld

Ericktux

  • Sr. Member
  • ****
  • Posts: 302
Re: download multiple files
« Reply #4 on: September 26, 2022, 01:19:53 am »
hello friend, now it works thanks.  :)
One question, to be able to use the percentage "%" of the download, the variable "ASize" is used, which is the total file size, but it only appears for the first download?
is there a way to get the "ASize" value for all downloads??
Thanks for your time.

Ericktux

  • Sr. Member
  • ****
  • Posts: 302
Re: download multiple files
« Reply #5 on: September 26, 2022, 06:54:39 am »
I found a solution for now  :), I had to modify the file "udownload.pas" adding the unit "openssl" and below the code "InitSSLInterface;" At the beginning of "procedure TDownload.Execute" I share the project.

 

TinyPortal © 2005-2018