FaceService.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. unit FaceService;
  2. interface
  3. uses
  4. Classes, SysUtils, RealICQModel;
  5. type
  6. TWaitingFace = class
  7. FCreateTicket: Cardinal;
  8. FFormID: string;
  9. FFaceMD5Code: string;
  10. FFaceID: string;
  11. public
  12. constructor Create;
  13. property FormID: string read FFormID;
  14. property FaceMD5Code: string read FFaceMD5Code;
  15. property FaceID: string read FFaceID;
  16. end;
  17. TFaceService = class
  18. private
  19. FTempFaces,
  20. FFaces,
  21. FWaitingFaces: TStringList;
  22. function GetFacePath(AMD5String: string): string;
  23. public
  24. function GetFace(AIndex: Integer): TFace;
  25. function IsSystemFace(AIndex: Integer): Boolean;
  26. function ParseToHtml(AContent, AFormID: string; var NoFoundFaces: TStringList): string;
  27. class function GetService: TFaceService; static;
  28. constructor Create;
  29. destructor Destroy; override;
  30. property Faces : TStringList read FFaces;
  31. property TempFaces : TStringList read FTempFaces;
  32. property WaitingFaces : TStringList read FWaitingFaces;
  33. end;
  34. const
  35. FaceSmallBMP: string = '_SmallBMP';
  36. FacePreviewBMP: string = '_PreviewBMP';
  37. FaceSmallSize: Integer = 19;
  38. FacePreviewSize: Integer = 92;
  39. SystemFaceGroup: string = '默认表情';
  40. NOFaceCategory: string = '未分组表情';
  41. implementation
  42. uses
  43. DirectoryService, StrUtils, Windows,RealICQUtility, Forms;
  44. var
  45. AFaceService: TFaceService;
  46. const
  47. BaseTempFaceIndex: Integer = 10000;
  48. constructor TFaceService.Create;
  49. begin
  50. FTempFaces := TStringList.Create;
  51. FFaces := TStringList.Create;
  52. FWaitingFaces := TStringList.Create;
  53. end;
  54. destructor TFaceService.Destroy;
  55. begin
  56. TRealICQUtility.FreeStringList(FTempFaces);
  57. TRealICQUtility.FreeStringList(FFaces);
  58. TRealICQUtility.FreeStringList(FWaitingFaces);
  59. inherited;
  60. end;
  61. function TFaceService.GetFace(AIndex: Integer): TFace;
  62. begin
  63. if AIndex >= BaseTempFaceIndex then
  64. Result := FTempFaces.Objects[AIndex - BaseTempFaceIndex] as TFace
  65. else
  66. Result := FFaces.Objects[AIndex] as TFace;
  67. end;
  68. class function TFaceService.GetService: TFaceService;
  69. begin
  70. if AFaceService = nil then
  71. AFaceService := TFaceService.Create;
  72. Result := AFaceService;
  73. end;
  74. function TFaceService.IsSystemFace(AIndex: Integer): Boolean;
  75. begin
  76. end;
  77. function TFaceService.GetFacePath(AMD5String: string): string;
  78. var
  79. DSearchRec: TSearchRec;
  80. FindResult: Integer;
  81. begin
  82. Result := '';
  83. FindResult := FindFirst(TDirectoryService.GetService.GetReceivedFaceDir + AMD5String + '.*', faAnyFile, DSearchRec);
  84. while FindResult = 0 do
  85. begin
  86. if (DSearchRec.Name <> '.') and (DSearchRec.Name <> '..') then
  87. begin
  88. if AnsiSameText(ExtractFileExt(DSearchRec.Name), '.BMP') or
  89. AnsiSameText(ExtractFileExt(DSearchRec.Name), '.PNG') or
  90. AnsiSameText(ExtractFileExt(DSearchRec.Name), '.JPG') or
  91. AnsiSameText(ExtractFileExt(DSearchRec.Name), '.JPEG') or
  92. AnsiSameText(ExtractFileExt(DSearchRec.Name), '.GIF') then
  93. begin
  94. Result := TDirectoryService.GetService.GetReceivedFaceDir + ExtractFileName(DSearchRec.Name);
  95. Exit;
  96. end;
  97. end;
  98. FindResult := FindNext(DSearchRec);
  99. end;
  100. end;
  101. function TFaceService.ParseToHtml(AContent, AFormID: string; var NoFoundFaces: TStringList): string;
  102. var
  103. Face: TFace;
  104. iLoop,
  105. iStart,
  106. iCount,
  107. iIndex: Integer;
  108. MD5String: String;
  109. AWaitingFace: TWaitingFace;
  110. FaceID,
  111. AFileName,
  112. ContextMenuStr: String;
  113. begin
  114. iCount := 0;
  115. iStart := AnsiPos('[IMG:', AContent);
  116. while iStart <> 0 do
  117. begin
  118. if Copy(AContent, iStart + 37, 1) = ']' then
  119. begin
  120. MD5String := Copy(AContent, iStart + 5, 32);
  121. ContextMenuStr := 'oncontextmenu="location.href=''FaceMenu_' + MD5String + ''';return false;"';
  122. iIndex := FFaces.IndexOf(MD5String);
  123. if iIndex >= 0 then
  124. begin
  125. Face := GetFace(iIndex);
  126. AContent := AnsiReplaceStr(AContent,
  127. Copy(AContent, iStart, 38),
  128. '<img ' + ContextMenuStr + ' src="' + Face.FileName + '" align="absBottom" hspace="1" >');
  129. end
  130. else if FileExists(GetFacePath(MD5String)) then
  131. begin
  132. AFileName := GetFacePath(MD5String);
  133. AContent := AnsiReplaceStr(AContent,
  134. Copy(AContent, iStart, 38),
  135. '<img ' + ContextMenuStr + ' src="' + AFileName + '" align="absBottom" hspace="1" >');
  136. end
  137. else
  138. begin
  139. FaceID := MD5String + IntToStr(GetTickCount) + IntToStr(WaitingFaces.Count) + IntToStr(iStart);
  140. AContent := Copy(AContent, 1, iStart - 1) +
  141. '<img ' + ContextMenuStr + ' ID = "' + FaceID + '" src="' + ExtractFilePath(Application.ExeName) + 'Images\progress.gif' + '" align="absBottom" hspace="1" >' +
  142. Copy(AContent, iStart + 38, Length(AContent));
  143. Inc(iCount);
  144. AWaitingFace := TWaitingFace.Create;
  145. AWaitingFace.FFaceMD5Code := MD5String;
  146. AWaitingFace.FFormID := AFormID;
  147. AWaitingFace.FFaceID := FaceID;
  148. FWaitingFaces.AddObject(MD5String, AWaitingFace);
  149. if NoFoundFaces <> nil then
  150. NoFoundFaces.AddObject(MD5String, AWaitingFace);
  151. end;
  152. end
  153. else
  154. begin
  155. AContent := Copy(AContent, 1, iStart - 1) + '[/IMG:' + Copy(AContent, iStart + 5, Length(AContent));
  156. end;
  157. iStart := AnsiPos('[IMG:', AContent);
  158. end;
  159. AContent := AnsiReplaceStr(AContent, '[/IMG:', '[IMG:');
  160. //取系统表情
  161. for iLoop := 0 to FFaces.Count - 1 do
  162. begin
  163. Face := FFaces.Objects[iLoop] as TFace;
  164. ContextMenuStr := 'oncontextmenu="location.href=''StandardFaceMenu_' + Face.ShortCut + ''';return false;"';
  165. AContent := AnsiReplaceStr(AContent,
  166. Face.ShortCut,
  167. '<img ' + ContextMenuStr + ' src="' + Face.FileName + '" align="absBottom" hspace="1" >');
  168. end;
  169. Result := AContent;
  170. end;
  171. { TWaitingFace }
  172. constructor TWaitingFace.Create;
  173. begin
  174. FCreateTicket := GetTickCount;
  175. end;
  176. initialization
  177. finalization
  178. if AFaceService <> nil then
  179. FreeAndNil(AFaceService);
  180. end.