uEmailAwake.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:14k
源码类别:

Email服务器

开发平台:

Delphi

  1. unit uEmailAwake;
  2. interface
  3. uses Classes,StrUtils,SysUtils,DateUtils,NativeXmlAppend, NativeXml,uCommon, NativeXmlObjectStorage;
  4. type
  5.   TAwakeModal=(amHour,amDay,amWeek,amManth,amYear);
  6.   IAwake=interface
  7.     procedure SaveToXml(Path:string);
  8.     procedure LoadToForm(Node:TXmlNode;Awake:IAwake);
  9.   end;
  10.   TAwakeTime=TDateTime;
  11.   TAwakeDate=TDateTime;
  12.   
  13.   TBaseAwake=class(TInterfacedPersistent,IAwake)
  14.   private
  15.     FBaseTime:TDateTime;  //从这个时间开始计算
  16.     FSingle:Boolean;
  17.     FEmailAddr:string;
  18.     FContentFilePath:string;
  19.     FTitle:string;
  20.     FAwakeTime:TAwakeTime;
  21.     FAwakeDate:TAwakeDate;
  22.     FNextAwake:TDateTime;
  23.     FUserNextAwake:Boolean;
  24.     FMemo:string;
  25.     function GetNextAwake: TDateTime;
  26.     //procedure SetNextAwake(const Value: TDateTime);
  27.   protected
  28.     procedure AssignTo(Dest: TPersistent);override;
  29.     function NextAwakeTime:TDateTime;  virtual;
  30.   public
  31.     constructor Create;
  32.     procedure SaveToXml(Path:string='');virtual;
  33.     procedure LoadToForm(Node:TXmlNode;Awake:IAwake);
  34.     function IsAwake:Boolean;
  35.   published
  36.     property EmailAddr:string read FEmailAddr  write FEmailAddr;
  37.     property ContentFilePath:string read FContentFilePath  write FContentFilePath;
  38.     property Title:string read FTitle  write FTitle;
  39.     property AwakeTime:TAwakeTime read FAwakeTime  write FAwakeTime;
  40.     property AwakeDate:TAwakeDate read FAwakeDate  write FAwakeDate;
  41.     property Single:Boolean read FSingle write FSingle;
  42.     property BaseTime:TDateTime read FBaseTime  write  FBaseTime;
  43.     property Memo:string read FMemo  write FMemo ;
  44.     property UserNextAwake:Boolean read FUserNextAwake write FUserNextAwake;
  45.     property NextAwake:TDateTime read GetNextAwake write FNextAwake ;
  46.   end;
  47.   TSingleAwake=class(TBaseAwake)
  48.   private
  49.     procedure InitData;
  50.   public
  51.     procedure AssignTo(Dest: TPersistent);override;
  52.     Constructor Create;
  53.     function NextAwakeTime:TDateTime; override;
  54.   end;
  55.   TManthDayOrWeek=(mdwDay,mdwWeek);
  56.   TAwakeWeek=record
  57.     WeekInterval:integer;  //每几周提醒一次
  58.     WeekDay:byte;         //该周的星期几提醒
  59.   end;
  60.   TAwakeManth=class(TPersistent) //按月提醒
  61.   private
  62.     procedure InitData;
  63.   public
  64.     FManthInterval:integer; //几个月提醒一次
  65.     FManthDayOrWeek:TManthDayOrWeek;
  66.     FManthDay:byte;  //该月的第几天提醒
  67.     FManthWeek:byte;  //该月的第几个星期的星期几提醒
  68.     FWeekDay:Byte;    //
  69.     procedure AssignTo(Dest: TPersistent);override;
  70.     constructor Create;
  71.     destructor Destroy; override;
  72.   published
  73.     property ManthInterval:integer   read FManthInterval  write  FManthInterval ;
  74.     property ManthDayOrWeek:TManthDayOrWeek  read FManthDayOrWeek  write FManthDayOrWeek  ;
  75.     property ManthDay:byte   read FManthDay  write FManthDay  ;
  76.     property ManthWeek:byte   read FManthWeek  write FManthWeek  ;
  77.     property WeekDay:Byte   read FWeekDay  write FWeekDay  ;
  78.   end;
  79.   TAwakeYear=class(TPersistent)
  80.   private
  81.     procedure InitData;
  82.   public
  83.     FYearInterval:integer;
  84.     FYearDayOrWeek:TManthDayOrWeek;
  85.     FYearDay:integer;
  86.     FYearWeek:byte;
  87.     FWeekDay:Byte;
  88.     procedure AssignTo(Dest: TPersistent);override;
  89.     constructor Create;
  90.   published
  91.     property YearInterval:integer  read FYearInterval  write FYearInterval ;
  92.     property YearDayOrWeek:TManthDayOrWeek  read FYearDayOrWeek write FYearDayOrWeek ;
  93.     property YearDay:integer read FYearDay write FYearDay ;
  94.     property YearWeek:byte read FYearWeek write FYearWeek ;
  95.     property WeekDay:Byte read FWeekDay write FWeekDay ;
  96.   end;
  97.   TRepeatAwake=class(TBaseAwake)
  98.   private
  99.     procedure InitData;
  100.   protected
  101.     procedure AssignTo(Dest:TPersistent); override;
  102.   public
  103.     FAwakeModal:TAwakeModal;
  104.     FAwakeHour:integer; //按天提醒和按小时提醒都使用这个变量
  105.     FAwakeWeek:TAwakeWeek;
  106.     FAwakeManth:TAwakeManth;
  107.     FAwakeYear:TAwakeYear;
  108.     constructor Create;
  109.     destructor Destroy; override;
  110.     function NextAwakeTime:TDateTime; override;
  111.   published
  112.     property  AwakeModal:TAwakeModal  read FAwakeModal  write FAwakeModal ;
  113.     property AwakeHour:integer read FAwakeHour write FAwakeHour;
  114.     property AwakeWeek:TAwakeWeek read FAwakeWeek write FAwakeWeek;
  115.     property AwakeManth:TAwakeManth read FAwakeManth write FAwakeManth;
  116.     property AwakeYear:TAwakeYear read FAwakeYear write FAwakeYear;
  117.   end;
  118.   TEmailAwakeItem=class(TCollectionItem)
  119.   private
  120.     FBaseAwake:TBaseAwake;
  121.     procedure SetBaseAwake(const Value: TBaseAwake);
  122.   public
  123.     constructor Create(Collection: TCollection); override;
  124.     destructor Destroy; override;
  125.   published
  126.     property Awake:TBaseAwake read FBaseAwake write SetBaseAwake;
  127.   end;
  128.   TEmailAwakeCollection=class(TCollection)
  129.   private
  130.     function GetItem(Index: Integer): TEmailAwakeItem;
  131.     procedure SetItem(Index: Integer; Value: TEmailAwakeItem);
  132.   public
  133.     constructor Create;
  134.     function Add: TEmailAwakeItem;
  135.     procedure Move(CurIndex, NewIndex: Integer);
  136.     property Items[Index: Integer]: TEmailAwakeItem read GetItem write SetItem; default;
  137.   end;
  138.   procedure LoadAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection);
  139.   procedure SaveAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection;XmlFilePath:string='');
  140. implementation
  141. uses uMyXml;
  142. procedure LoadAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection);
  143. var
  144.   Xml:TAppXml;
  145.   L:TList;
  146.   I:Integer;
  147.   N:TXmlNode;
  148. begin
  149.   AwakeCollection.Clear;
  150.   Xml:=TAppXml.Create;
  151.   try                          
  152.     N:=Xml.Root.FindNode('EmailAwake');
  153.     if N=nil then Exit;
  154.     L:=TList.Create;
  155.     try
  156.       N.NodesByName('Email',L);
  157.       for I:=0 to L.Count-1 do
  158.       begin
  159.         if TXmlNode(L[I]).NodeCount<0 then Continue;
  160.         ObjectLoadFromXmlNode(AwakeCollection.Add.Awake,TXmlNode(L[I]).Nodes[0],nil);
  161.       end;
  162.     finally
  163.       L.Free;
  164.     end;
  165.   finally
  166.     xml.Free;
  167.   end;
  168. end;
  169. procedure SaveAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection;XmlFilePath:string);
  170. var
  171.   I:integer;
  172. begin
  173.   for I:=0 to AwakeCollection.Count-1 do
  174.     AwakeCollection.Items[I].FBaseAwake.SaveToXml(XmlFilePath);
  175. end;
  176. { TAwakeManth }
  177. procedure TAwakeManth.AssignTo(Dest: TPersistent);
  178. begin
  179.   if Dest is TAwakeManth then
  180.   begin
  181.     TAwakeManth(Dest).ManthInterval:=ManthInterval;
  182.     TAwakeManth(Dest).ManthDayOrWeek:=ManthDayOrWeek;
  183.     TAwakeManth(Dest).ManthDay:=ManthDay;
  184.     TAwakeManth(Dest).ManthWeek:=ManthWeek;
  185.     TAwakeManth(Dest).WeekDay:=WeekDay;
  186.   end
  187.   else
  188.     Inherited ;
  189. end;
  190. constructor TAwakeManth.Create;
  191. begin
  192.   InitData;
  193. end;
  194. destructor TAwakeManth.Destroy;
  195. begin
  196.   inherited;
  197. end;
  198. procedure TAwakeManth.InitData;
  199. begin
  200.     ManthInterval:=1;
  201.     ManthDayOrWeek:=mdwDay;
  202.     ManthDay:=1;
  203.     ManthWeek:=1;
  204.     WeekDay:=1;
  205. end;
  206. { TRepeatAwake }
  207. procedure TRepeatAwake.AssignTo(Dest: TPersistent);
  208. begin
  209.   if Dest is TRepeatAwake then
  210.   begin
  211.     TRepeatAwake(Dest).FAwakeModal:=AwakeModal;
  212.     TRepeatAwake(Dest).AwakeWeek:=AwakeWeek;
  213.     TRepeatAwake(Dest).AwakeHour:=AwakeHour;
  214.     TRepeatAwake(Dest).AwakeManth.Assign(AwakeManth);
  215.     TRepeatAwake(Dest).AwakeYear.Assign(AwakeYear);
  216.   end;
  217.   Inherited;
  218. end;
  219. constructor TRepeatAwake.Create;
  220. begin
  221.   Inherited;
  222.   AwakeManth:=TAwakeManth.Create;
  223.   AwakeYear:=TAwakeYear.Create;
  224.   InitData;
  225. end;
  226. destructor TRepeatAwake.Destroy;
  227. begin
  228.   AwakeManth.Free;
  229.   AwakeYear.Free;
  230.   inherited;
  231. end;
  232. procedure TRepeatAwake.InitData;
  233. var
  234.   week:TAwakeWeek;
  235. begin
  236.   AwakeModal:=amHour;
  237.   week.WeekInterval:=1;
  238.   week.WeekDay:=1;
  239.   AwakeWeek:=week;
  240.   Single:=false;;
  241. end;
  242. function TRepeatAwake.NextAwakeTime: TDateTime;
  243. var
  244.   T:TDateTime;
  245. begin
  246.   case AwakeModal of
  247.     amHour:
  248.       Result:=(FAwakeHour) / 24+FBaseTime;//间隔FAwakeHour个小时提醒一次
  249.       
  250.     amDay:
  251.       Result:=FAwakeHour+FBaseTime-TimeOf(FBaseTime)+AwakeTime;
  252.     amWeek:
  253.       Result:=(FAwakeWeek.WeekInterval-1)*7+FAwakeWeek.WeekDay+FBaseTime-TimeOf(FBaseTime)+TimeOf(AwakeTime);
  254.     amManth:
  255.       case FAwakeManth.FManthDayOrWeek of
  256.         mdwDay:
  257.           begin   //每年的第a个月的第b日 
  258.             t:=EncodeDateTime(YearOf(BaseTime),MonthOf(BaseTime),AwakeManth.FManthDay,0,0,0,0)+TimeOf(AwakeTime);
  259.             if CompareDateTime(t,BaseTime)>0 then
  260.               Result:=T
  261.             else
  262.               Result:=incMonth(t,AwakeManth.ManthInterval);
  263.           end;
  264.         mdwWeek:
  265.           begin    //每年第a个月的第b个星期的星期c 的日期
  266.             T:=EncodeDateMonthWeek(YearOf(BaseTime),MonthOf(BaseTime),AwakeManth.FManthWeek,AwakeManth.WeekDay)+TimeOf(AwakeTime);
  267.             if CompareDateTime(t,BaseTime)>0 then
  268.               Result:=t
  269.             else
  270.               Result:=IncMonth(t,AwakeManth.ManthInterval);
  271.           end;
  272.       else Raise Exception.Create('');  
  273.       end;
  274.     amYear:
  275.     begin
  276.       case AwakeYear.FYearDayOrWeek of
  277.         mdwDay:
  278.           begin  //FYearInterval:每年的第几个月.  YearDay:每个月的第几天 .   AwakeTime:几点钟提醒
  279.             t:=EncodeDateTime(YearOf(BaseTime),AwakeYear.FYearInterval,AwakeYear.FYearDay,0,0,0,0)+TimeOf(AwakeTime);
  280.             if CompareDateTime(t,FBaseTime)>0 then
  281.               Result :=T
  282.             else
  283.               Result:=IncMonth(T,AwakeYear.FYearInterval);
  284.           end;
  285.         mdwWeek:
  286.           begin    //每年的第几个月的第几个星期的星期几提醒
  287.             T:=EncodeDateMonthWeek(YearOf(FBaseTime),AwakeYear.FYearInterval,AwakeYear.FYearWeek,AwakeYear.FWeekDay)+TimeOf(AwakeTime);
  288.             if CompareDateTime(t,FBaseTime)>0 then
  289.               Result:=T
  290.             else
  291.               Result:=IncYear(t);
  292.           end;
  293.         else Raise Exception.Create('');
  294.       end;
  295.     end;
  296.   else
  297.       Raise Exception.Create('请选择:按时间提醒,按天提醒,按周提醒,按月提醒或按年提醒。');
  298.   end;
  299.   //WriteLog('TRepeatAwake.NextAwakeTime:'+DateTimeToStr(NextAwakeTime));
  300. end;
  301. { TAwakeYear }
  302. procedure TAwakeYear.AssignTo(Dest: TPersistent);
  303. begin
  304.   if Dest is TAwakeYear then
  305.   begin
  306.     TAwakeYear(Dest).YearInterval:=YearInterval;
  307.     TAwakeYear(Dest).YearDayOrWeek:=YearDayOrWeek;
  308.     TAwakeYear(Dest).YearDay:=YearDay;
  309.     TAwakeYear(Dest).YearWeek:=YearWeek;
  310.     TAwakeYear(Dest).WeekDay:=WeekDay;
  311.   end
  312.   else
  313.     Inherited ;
  314. end;
  315. constructor TAwakeYear.Create;
  316. begin
  317.   InitData;
  318. end;
  319. procedure TAwakeYear.InitData;
  320. begin
  321.   YearInterval:=1;
  322.   YearDayOrWeek:=mdwDay;
  323.   YearDay:=1;
  324.   YearWeek:=1;
  325.   WeekDay:=1;
  326. end;
  327. { TBaseAwake }
  328. procedure TBaseAwake.AssignTo(Dest: TPersistent);
  329. begin
  330.   if Dest is  TBaseAwake then
  331.   begin
  332.     TBaseAwake(Dest).BaseTime:=BaseTime;
  333.     TBaseAwake(Dest).EmailAddr:=EmailAddr;
  334.     TBaseAwake(Dest).ContentFilePath:=ContentFilePath;
  335.     TBaseAwake(Dest).Title:=Title;
  336.     TBaseAwake(Dest).AwakeTime:=AwakeTime;
  337.     TBaseAwake(Dest).AwakeDate:=AwakeDate;
  338.   end
  339.   else
  340.   Inherited ;
  341. end;
  342. constructor TBaseAwake.Create;
  343. begin
  344.   EmailAddr:='';
  345.   ContentFilePath:='';
  346.   Title:='';
  347.   AwakeTime:=now;
  348.   UserNextAwake:=False;
  349. end;
  350. function TBaseAwake.GetNextAwake: TDateTime;
  351. begin
  352.   if UserNextAwake then
  353.     Result:=FNextAwake
  354.   else
  355.     Result:=NextAwakeTime;
  356. end;
  357. function TBaseAwake.IsAwake: Boolean;
  358. var
  359.   t:TDateTime ;
  360. begin
  361.   t:=GetNextAwake;
  362.   if CompareDateTime(t,Now)<=0 then
  363.     Result:=true
  364.   else
  365.     Result:=false;  
  366. end;
  367. procedure TBaseAwake.LoadToForm( Node: TXmlNode;Awake: IAwake);
  368. begin
  369.     NativeXmlObjectStorage.ObjectLoadFromXmlNode(TBaseAwake(Awake),Node,nil);
  370. end;
  371. {$warnings Off}
  372. function TBaseAwake.NextAwakeTime: TDateTime;
  373. begin
  374. end;
  375. {$warnings On}
  376. procedure TBaseAwake.SaveToXml(Path: string);
  377. var
  378.   xml:TAppXml;
  379.   N:TXmlNode;
  380.   L:TList;
  381.   I:integer;
  382. begin
  383.   xml:=TAppXml.Create;
  384.   try
  385.     N:=Xml.Root.FindNode('EmailAwake');
  386.     if N=nil then N:=N.NodeNew('EmailAwake');
  387.     L:=TList.Create;   //查找同一封邮件的提醒,删除。
  388.     try
  389.       N.NodesByName('Email',L);
  390.       for I:=0 to L.Count-1 do
  391.       begin
  392.         if (TXmlNode(L[I]).AttributeByName['EmailAddr']=EmailAddr) and  (TXmlNode(L[I]).AttributeByName['ContentFile']=ContentFilePath) then
  393.           TXmlNode(L[I]).Delete;
  394.       end;
  395.     finally
  396.       L.Free;
  397.     end;
  398.     
  399.     N:=N.NodeNew('Email');
  400.     N.AttributeByName['EmailAddr']:=EmailAddr;
  401.     N.AttributeByName['ContentFile']:=ContentFilePath;
  402.     N.AttributeByName['Single']:=BoolToStr(Single);
  403.     N:=N.NodeNew('AwakeNode');
  404.     ObjectSaveToXmlNode(self,N,nil);
  405.   finally
  406.     Xml.free;
  407.   end;
  408. end;
  409. { TSingleAwake }
  410. procedure TSingleAwake.AssignTo(Dest: TPersistent);
  411. begin
  412.   if Dest is TSingleAwake then
  413.     TSingleAwake(Dest).Memo:=Memo;
  414.   inherited;
  415. end;
  416. constructor TSingleAwake.Create;
  417. begin
  418.   Inherited;
  419.   InitData;
  420. end;
  421. procedure TSingleAwake.InitData;
  422. begin
  423.   Memo:='';
  424.   Single:=true;
  425. end;
  426. function TSingleAwake.NextAwakeTime: TDateTime;
  427. begin
  428.   Result:=EncodeDateTime(YearOf(FAwakeDate),MonthOf(FAwakeDate),DayOf(FAwakeDate),0,0,0,0)+TimeOf(FAwakeTime);
  429.   WriteLog('TSingleAwake.NextAwakeTime:'+DateTimeToStr(Result));
  430. end;
  431. { TEmailAwakeItem }
  432. constructor TEmailAwakeItem.Create(Collection: TCollection);
  433. begin
  434.   inherited Create(Collection);
  435.   FBaseAwake:=TBaseAwake.Create;
  436. end;
  437. destructor TEmailAwakeItem.Destroy;
  438. begin
  439.   FBaseAwake.Free;
  440.   inherited;
  441. end;
  442. procedure TEmailAwakeItem.SetBaseAwake(const Value: TBaseAwake);
  443. begin
  444.   if FBaseAwake <> Value then
  445.     FBaseAwake.Assign(Value);
  446. end;
  447. { TEmailAwakeCollection }
  448. function TEmailAwakeCollection.Add: TEmailAwakeItem;
  449. begin
  450.   Result := TEmailAwakeItem(inherited Add);
  451. end;
  452. constructor TEmailAwakeCollection.Create;
  453. begin
  454.    inherited Create(TEmailAwakeItem);
  455. end;
  456. function TEmailAwakeCollection.GetItem(Index: Integer): TEmailAwakeItem;
  457. begin
  458.   Result := TEmailAwakeItem(inherited GetItem(Index));
  459. end;
  460. procedure TEmailAwakeCollection.Move(CurIndex, NewIndex: Integer);
  461. var
  462.   TempItem: TEmailAwakeItem;
  463. begin
  464.   if CurIndex <> NewIndex then
  465.   begin
  466.     BeginUpdate;
  467.     try
  468.       TempItem := TEmailAwakeItem.Create(nil);
  469.       try
  470.         TempItem.Assign(GetItem(CurIndex));
  471.         Delete(CurIndex);
  472.         Insert(NewIndex);
  473.         SetItem(NewIndex,TempItem);
  474.       finally
  475.         TempItem.Free;
  476.       end;
  477.     finally
  478.       EndUpdate;
  479.     end;
  480.   end;
  481. end;
  482. procedure TEmailAwakeCollection.SetItem(Index: Integer;
  483.   Value: TEmailAwakeItem);
  484. begin
  485.   inherited SetItem(Index, Value);
  486. end;
  487. end.