IconRequest.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. unit IconRequest;
  2. interface
  3. uses
  4. Classes, SysUtils, IdHTTP, IdComponent, Windows;
  5. type
  6. TIconResponseEvent = procedure(AppKey: string; AStream: TStream) of object;
  7. TIconRequest = class(TThread)
  8. private
  9. FApps: IInterfaceList;
  10. FMemoryStream: TMemoryStream;
  11. FOnCompleted: TIconResponseEvent;
  12. protected
  13. procedure Execute; override;
  14. public
  15. constructor Create(AApps: IInterfaceList);
  16. destructor Destroy; override;
  17. class function GetMD5Name(AUrl: string): string;
  18. property OnCompleted: TIconResponseEvent read FOnCompleted write FOnCompleted;
  19. end;
  20. const
  21. ICON_URL: string = 'http://%s:%d%s';
  22. implementation
  23. uses
  24. MD5_32, InterfaceAppCentre, LoggerImport, AppCentreImport, Dialogs, IdURI,
  25. InterfaceConfig, ConfigImport;
  26. const
  27. TEMP_DIR: string = 'Users\';
  28. { TIconRequest }
  29. constructor TIconRequest.Create(AApps: IInterfaceList);
  30. begin
  31. FApps := AApps;
  32. FMemoryStream := TMemoryStream.Create;
  33. FreeOnTerminate := False;
  34. inherited Create(False);
  35. end;
  36. //constructor TIconRequest.Create(AppKey, AUrl: String);
  37. //begin
  38. // FAppKey := AppKey;
  39. // FURL := AUrl;
  40. // FMD5Name := GetMD5Name(AUrl);
  41. // FMemoryStream := TMemoryStream.Create;
  42. // inherited Create(True);
  43. //end;
  44. destructor TIconRequest.Destroy;
  45. begin
  46. FOnCompleted := nil;
  47. FApps := nil;
  48. FreeAndNil(FMemoryStream);
  49. inherited;
  50. end;
  51. procedure TIconRequest.Execute;
  52. var
  53. AIdHttp: TIdHTTP;
  54. iLoop: Integer;
  55. ADownloadThread: TIconRequest;
  56. AHotApp: IHotApp;
  57. AApp: IApp;
  58. AConfig: IAppCentreConfig;
  59. AURL: string;
  60. begin
  61. AIdHttp := TIdHTTP.Create(nil);
  62. AIdHttp.Request.CustomHeaders.Values['Content-Type'] := 'application/octet-stream';
  63. try
  64. iLoop := 0;
  65. while (not Terminated) and (FApps <> nil) and (iLoop <= FApps.Count - 1) do
  66. begin
  67. if FApps[iLoop].QueryInterface(IHotApp, AHotApp) = S_OK then
  68. AApp := AHotApp.GetUserApp
  69. else if FApps[iLoop].QueryInterface(IApp, AApp) = S_OK then
  70. else
  71. begin
  72. inc(iLoop);
  73. Continue;
  74. end;
  75. Inc(iLoop);
  76. if Length(AApp.GetIcon) = 0 then
  77. Continue;
  78. FMemoryStream.Clear;
  79. AConfig := TConfigImport.GetAppCentreConfig;
  80. // AURL := AApp.GetIcon
  81. AIdHTTP.Get(TIdURI.URLEncode(AApp.GetIcon), FMemoryStream);
  82. if Terminated then
  83. Break;
  84. if Assigned(FOnCompleted) then
  85. FOnCompleted(AApp.GetAppKey, FMemoryStream);
  86. end;
  87. Debug(IntToStr(ThreadID)+ '-Ö´ÐÐÍê±Ï', 'TIconRequest.Execute');
  88. except
  89. on E: Exception do
  90. begin
  91. Debug(E.Message, 'TIconRequest.Execute');
  92. FApps := nil;
  93. FreeAndNil(AIdHTTP);
  94. end;
  95. end;
  96. FApps := nil;
  97. FreeAndNil(AIdHTTP);
  98. end;
  99. class function TIconRequest.GetMD5Name(AUrl: string): string;
  100. begin
  101. Result := '';
  102. if Length(AUrl) > 0 then
  103. Result := MD5Print(MD5String(AUrl));
  104. end;
  105. end.