uEmailAwake.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:14k
- unit uEmailAwake;
- interface
- uses Classes,StrUtils,SysUtils,DateUtils,NativeXmlAppend, NativeXml,uCommon, NativeXmlObjectStorage;
- type
- TAwakeModal=(amHour,amDay,amWeek,amManth,amYear);
- IAwake=interface
- procedure SaveToXml(Path:string);
- procedure LoadToForm(Node:TXmlNode;Awake:IAwake);
- end;
- TAwakeTime=TDateTime;
- TAwakeDate=TDateTime;
-
- TBaseAwake=class(TInterfacedPersistent,IAwake)
- private
- FBaseTime:TDateTime; //从这个时间开始计算
- FSingle:Boolean;
- FEmailAddr:string;
- FContentFilePath:string;
- FTitle:string;
- FAwakeTime:TAwakeTime;
- FAwakeDate:TAwakeDate;
- FNextAwake:TDateTime;
- FUserNextAwake:Boolean;
- FMemo:string;
- function GetNextAwake: TDateTime;
- //procedure SetNextAwake(const Value: TDateTime);
- protected
- procedure AssignTo(Dest: TPersistent);override;
- function NextAwakeTime:TDateTime; virtual;
- public
- constructor Create;
- procedure SaveToXml(Path:string='');virtual;
- procedure LoadToForm(Node:TXmlNode;Awake:IAwake);
- function IsAwake:Boolean;
- published
- property EmailAddr:string read FEmailAddr write FEmailAddr;
- property ContentFilePath:string read FContentFilePath write FContentFilePath;
- property Title:string read FTitle write FTitle;
- property AwakeTime:TAwakeTime read FAwakeTime write FAwakeTime;
- property AwakeDate:TAwakeDate read FAwakeDate write FAwakeDate;
- property Single:Boolean read FSingle write FSingle;
- property BaseTime:TDateTime read FBaseTime write FBaseTime;
- property Memo:string read FMemo write FMemo ;
- property UserNextAwake:Boolean read FUserNextAwake write FUserNextAwake;
- property NextAwake:TDateTime read GetNextAwake write FNextAwake ;
- end;
- TSingleAwake=class(TBaseAwake)
- private
- procedure InitData;
- public
- procedure AssignTo(Dest: TPersistent);override;
- Constructor Create;
- function NextAwakeTime:TDateTime; override;
- end;
- TManthDayOrWeek=(mdwDay,mdwWeek);
- TAwakeWeek=record
- WeekInterval:integer; //每几周提醒一次
- WeekDay:byte; //该周的星期几提醒
- end;
- TAwakeManth=class(TPersistent) //按月提醒
- private
- procedure InitData;
- public
- FManthInterval:integer; //几个月提醒一次
- FManthDayOrWeek:TManthDayOrWeek;
- FManthDay:byte; //该月的第几天提醒
- FManthWeek:byte; //该月的第几个星期的星期几提醒
- FWeekDay:Byte; //
- procedure AssignTo(Dest: TPersistent);override;
- constructor Create;
- destructor Destroy; override;
- published
- property ManthInterval:integer read FManthInterval write FManthInterval ;
- property ManthDayOrWeek:TManthDayOrWeek read FManthDayOrWeek write FManthDayOrWeek ;
- property ManthDay:byte read FManthDay write FManthDay ;
- property ManthWeek:byte read FManthWeek write FManthWeek ;
- property WeekDay:Byte read FWeekDay write FWeekDay ;
- end;
- TAwakeYear=class(TPersistent)
- private
- procedure InitData;
- public
- FYearInterval:integer;
- FYearDayOrWeek:TManthDayOrWeek;
- FYearDay:integer;
- FYearWeek:byte;
- FWeekDay:Byte;
- procedure AssignTo(Dest: TPersistent);override;
- constructor Create;
- published
- property YearInterval:integer read FYearInterval write FYearInterval ;
- property YearDayOrWeek:TManthDayOrWeek read FYearDayOrWeek write FYearDayOrWeek ;
- property YearDay:integer read FYearDay write FYearDay ;
- property YearWeek:byte read FYearWeek write FYearWeek ;
- property WeekDay:Byte read FWeekDay write FWeekDay ;
- end;
- TRepeatAwake=class(TBaseAwake)
- private
- procedure InitData;
- protected
- procedure AssignTo(Dest:TPersistent); override;
- public
- FAwakeModal:TAwakeModal;
- FAwakeHour:integer; //按天提醒和按小时提醒都使用这个变量
- FAwakeWeek:TAwakeWeek;
- FAwakeManth:TAwakeManth;
- FAwakeYear:TAwakeYear;
- constructor Create;
- destructor Destroy; override;
- function NextAwakeTime:TDateTime; override;
- published
- property AwakeModal:TAwakeModal read FAwakeModal write FAwakeModal ;
- property AwakeHour:integer read FAwakeHour write FAwakeHour;
- property AwakeWeek:TAwakeWeek read FAwakeWeek write FAwakeWeek;
- property AwakeManth:TAwakeManth read FAwakeManth write FAwakeManth;
- property AwakeYear:TAwakeYear read FAwakeYear write FAwakeYear;
- end;
- TEmailAwakeItem=class(TCollectionItem)
- private
- FBaseAwake:TBaseAwake;
- procedure SetBaseAwake(const Value: TBaseAwake);
- public
- constructor Create(Collection: TCollection); override;
- destructor Destroy; override;
- published
- property Awake:TBaseAwake read FBaseAwake write SetBaseAwake;
- end;
- TEmailAwakeCollection=class(TCollection)
- private
- function GetItem(Index: Integer): TEmailAwakeItem;
- procedure SetItem(Index: Integer; Value: TEmailAwakeItem);
- public
- constructor Create;
- function Add: TEmailAwakeItem;
- procedure Move(CurIndex, NewIndex: Integer);
- property Items[Index: Integer]: TEmailAwakeItem read GetItem write SetItem; default;
- end;
- procedure LoadAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection);
- procedure SaveAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection;XmlFilePath:string='');
- implementation
- uses uMyXml;
- procedure LoadAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection);
- var
- Xml:TAppXml;
- L:TList;
- I:Integer;
- N:TXmlNode;
- begin
- AwakeCollection.Clear;
- Xml:=TAppXml.Create;
- try
- N:=Xml.Root.FindNode('EmailAwake');
- if N=nil then Exit;
- L:=TList.Create;
- try
- N.NodesByName('Email',L);
- for I:=0 to L.Count-1 do
- begin
- if TXmlNode(L[I]).NodeCount<0 then Continue;
- ObjectLoadFromXmlNode(AwakeCollection.Add.Awake,TXmlNode(L[I]).Nodes[0],nil);
- end;
- finally
- L.Free;
- end;
- finally
- xml.Free;
- end;
- end;
- procedure SaveAwakeCollectionFromXml(AwakeCollection:TEmailAwakeCollection;XmlFilePath:string);
- var
- I:integer;
- begin
- for I:=0 to AwakeCollection.Count-1 do
- AwakeCollection.Items[I].FBaseAwake.SaveToXml(XmlFilePath);
- end;
- { TAwakeManth }
- procedure TAwakeManth.AssignTo(Dest: TPersistent);
- begin
- if Dest is TAwakeManth then
- begin
- TAwakeManth(Dest).ManthInterval:=ManthInterval;
- TAwakeManth(Dest).ManthDayOrWeek:=ManthDayOrWeek;
- TAwakeManth(Dest).ManthDay:=ManthDay;
- TAwakeManth(Dest).ManthWeek:=ManthWeek;
- TAwakeManth(Dest).WeekDay:=WeekDay;
- end
- else
- Inherited ;
- end;
- constructor TAwakeManth.Create;
- begin
- InitData;
- end;
- destructor TAwakeManth.Destroy;
- begin
- inherited;
- end;
- procedure TAwakeManth.InitData;
- begin
- ManthInterval:=1;
- ManthDayOrWeek:=mdwDay;
- ManthDay:=1;
- ManthWeek:=1;
- WeekDay:=1;
- end;
- { TRepeatAwake }
- procedure TRepeatAwake.AssignTo(Dest: TPersistent);
- begin
- if Dest is TRepeatAwake then
- begin
- TRepeatAwake(Dest).FAwakeModal:=AwakeModal;
- TRepeatAwake(Dest).AwakeWeek:=AwakeWeek;
- TRepeatAwake(Dest).AwakeHour:=AwakeHour;
- TRepeatAwake(Dest).AwakeManth.Assign(AwakeManth);
- TRepeatAwake(Dest).AwakeYear.Assign(AwakeYear);
- end;
- Inherited;
- end;
- constructor TRepeatAwake.Create;
- begin
- Inherited;
- AwakeManth:=TAwakeManth.Create;
- AwakeYear:=TAwakeYear.Create;
- InitData;
- end;
- destructor TRepeatAwake.Destroy;
- begin
- AwakeManth.Free;
- AwakeYear.Free;
- inherited;
- end;
- procedure TRepeatAwake.InitData;
- var
- week:TAwakeWeek;
- begin
- AwakeModal:=amHour;
- week.WeekInterval:=1;
- week.WeekDay:=1;
- AwakeWeek:=week;
- Single:=false;;
- end;
- function TRepeatAwake.NextAwakeTime: TDateTime;
- var
- T:TDateTime;
- begin
- case AwakeModal of
- amHour:
- Result:=(FAwakeHour) / 24+FBaseTime;//间隔FAwakeHour个小时提醒一次
-
- amDay:
- Result:=FAwakeHour+FBaseTime-TimeOf(FBaseTime)+AwakeTime;
- amWeek:
- Result:=(FAwakeWeek.WeekInterval-1)*7+FAwakeWeek.WeekDay+FBaseTime-TimeOf(FBaseTime)+TimeOf(AwakeTime);
- amManth:
- case FAwakeManth.FManthDayOrWeek of
- mdwDay:
- begin //每年的第a个月的第b日
- t:=EncodeDateTime(YearOf(BaseTime),MonthOf(BaseTime),AwakeManth.FManthDay,0,0,0,0)+TimeOf(AwakeTime);
- if CompareDateTime(t,BaseTime)>0 then
- Result:=T
- else
- Result:=incMonth(t,AwakeManth.ManthInterval);
- end;
- mdwWeek:
- begin //每年第a个月的第b个星期的星期c 的日期
- T:=EncodeDateMonthWeek(YearOf(BaseTime),MonthOf(BaseTime),AwakeManth.FManthWeek,AwakeManth.WeekDay)+TimeOf(AwakeTime);
- if CompareDateTime(t,BaseTime)>0 then
- Result:=t
- else
- Result:=IncMonth(t,AwakeManth.ManthInterval);
- end;
- else Raise Exception.Create('');
- end;
- amYear:
- begin
- case AwakeYear.FYearDayOrWeek of
- mdwDay:
- begin //FYearInterval:每年的第几个月. YearDay:每个月的第几天 . AwakeTime:几点钟提醒
- t:=EncodeDateTime(YearOf(BaseTime),AwakeYear.FYearInterval,AwakeYear.FYearDay,0,0,0,0)+TimeOf(AwakeTime);
- if CompareDateTime(t,FBaseTime)>0 then
- Result :=T
- else
- Result:=IncMonth(T,AwakeYear.FYearInterval);
- end;
- mdwWeek:
- begin //每年的第几个月的第几个星期的星期几提醒
- T:=EncodeDateMonthWeek(YearOf(FBaseTime),AwakeYear.FYearInterval,AwakeYear.FYearWeek,AwakeYear.FWeekDay)+TimeOf(AwakeTime);
- if CompareDateTime(t,FBaseTime)>0 then
- Result:=T
- else
- Result:=IncYear(t);
- end;
- else Raise Exception.Create('');
- end;
- end;
- else
- Raise Exception.Create('请选择:按时间提醒,按天提醒,按周提醒,按月提醒或按年提醒。');
- end;
- //WriteLog('TRepeatAwake.NextAwakeTime:'+DateTimeToStr(NextAwakeTime));
- end;
- { TAwakeYear }
- procedure TAwakeYear.AssignTo(Dest: TPersistent);
- begin
- if Dest is TAwakeYear then
- begin
- TAwakeYear(Dest).YearInterval:=YearInterval;
- TAwakeYear(Dest).YearDayOrWeek:=YearDayOrWeek;
- TAwakeYear(Dest).YearDay:=YearDay;
- TAwakeYear(Dest).YearWeek:=YearWeek;
- TAwakeYear(Dest).WeekDay:=WeekDay;
- end
- else
- Inherited ;
- end;
- constructor TAwakeYear.Create;
- begin
- InitData;
- end;
- procedure TAwakeYear.InitData;
- begin
- YearInterval:=1;
- YearDayOrWeek:=mdwDay;
- YearDay:=1;
- YearWeek:=1;
- WeekDay:=1;
- end;
- { TBaseAwake }
- procedure TBaseAwake.AssignTo(Dest: TPersistent);
- begin
- if Dest is TBaseAwake then
- begin
- TBaseAwake(Dest).BaseTime:=BaseTime;
- TBaseAwake(Dest).EmailAddr:=EmailAddr;
- TBaseAwake(Dest).ContentFilePath:=ContentFilePath;
- TBaseAwake(Dest).Title:=Title;
- TBaseAwake(Dest).AwakeTime:=AwakeTime;
- TBaseAwake(Dest).AwakeDate:=AwakeDate;
- end
- else
- Inherited ;
- end;
- constructor TBaseAwake.Create;
- begin
- EmailAddr:='';
- ContentFilePath:='';
- Title:='';
- AwakeTime:=now;
- UserNextAwake:=False;
- end;
- function TBaseAwake.GetNextAwake: TDateTime;
- begin
- if UserNextAwake then
- Result:=FNextAwake
- else
- Result:=NextAwakeTime;
- end;
- function TBaseAwake.IsAwake: Boolean;
- var
- t:TDateTime ;
- begin
- t:=GetNextAwake;
- if CompareDateTime(t,Now)<=0 then
- Result:=true
- else
- Result:=false;
- end;
- procedure TBaseAwake.LoadToForm( Node: TXmlNode;Awake: IAwake);
- begin
- NativeXmlObjectStorage.ObjectLoadFromXmlNode(TBaseAwake(Awake),Node,nil);
- end;
- {$warnings Off}
- function TBaseAwake.NextAwakeTime: TDateTime;
- begin
- end;
- {$warnings On}
- procedure TBaseAwake.SaveToXml(Path: string);
- var
- xml:TAppXml;
- N:TXmlNode;
- L:TList;
- I:integer;
- begin
- xml:=TAppXml.Create;
- try
- N:=Xml.Root.FindNode('EmailAwake');
- if N=nil then N:=N.NodeNew('EmailAwake');
- L:=TList.Create; //查找同一封邮件的提醒,删除。
- try
- N.NodesByName('Email',L);
- for I:=0 to L.Count-1 do
- begin
- if (TXmlNode(L[I]).AttributeByName['EmailAddr']=EmailAddr) and (TXmlNode(L[I]).AttributeByName['ContentFile']=ContentFilePath) then
- TXmlNode(L[I]).Delete;
- end;
- finally
- L.Free;
- end;
-
- N:=N.NodeNew('Email');
- N.AttributeByName['EmailAddr']:=EmailAddr;
- N.AttributeByName['ContentFile']:=ContentFilePath;
- N.AttributeByName['Single']:=BoolToStr(Single);
- N:=N.NodeNew('AwakeNode');
- ObjectSaveToXmlNode(self,N,nil);
- finally
- Xml.free;
- end;
- end;
- { TSingleAwake }
- procedure TSingleAwake.AssignTo(Dest: TPersistent);
- begin
- if Dest is TSingleAwake then
- TSingleAwake(Dest).Memo:=Memo;
- inherited;
- end;
- constructor TSingleAwake.Create;
- begin
- Inherited;
- InitData;
- end;
- procedure TSingleAwake.InitData;
- begin
- Memo:='';
- Single:=true;
- end;
- function TSingleAwake.NextAwakeTime: TDateTime;
- begin
- Result:=EncodeDateTime(YearOf(FAwakeDate),MonthOf(FAwakeDate),DayOf(FAwakeDate),0,0,0,0)+TimeOf(FAwakeTime);
- WriteLog('TSingleAwake.NextAwakeTime:'+DateTimeToStr(Result));
- end;
- { TEmailAwakeItem }
- constructor TEmailAwakeItem.Create(Collection: TCollection);
- begin
- inherited Create(Collection);
- FBaseAwake:=TBaseAwake.Create;
- end;
- destructor TEmailAwakeItem.Destroy;
- begin
- FBaseAwake.Free;
- inherited;
- end;
- procedure TEmailAwakeItem.SetBaseAwake(const Value: TBaseAwake);
- begin
- if FBaseAwake <> Value then
- FBaseAwake.Assign(Value);
- end;
- { TEmailAwakeCollection }
- function TEmailAwakeCollection.Add: TEmailAwakeItem;
- begin
- Result := TEmailAwakeItem(inherited Add);
- end;
- constructor TEmailAwakeCollection.Create;
- begin
- inherited Create(TEmailAwakeItem);
- end;
- function TEmailAwakeCollection.GetItem(Index: Integer): TEmailAwakeItem;
- begin
- Result := TEmailAwakeItem(inherited GetItem(Index));
- end;
- procedure TEmailAwakeCollection.Move(CurIndex, NewIndex: Integer);
- var
- TempItem: TEmailAwakeItem;
- begin
- if CurIndex <> NewIndex then
- begin
- BeginUpdate;
- try
- TempItem := TEmailAwakeItem.Create(nil);
- try
- TempItem.Assign(GetItem(CurIndex));
- Delete(CurIndex);
- Insert(NewIndex);
- SetItem(NewIndex,TempItem);
- finally
- TempItem.Free;
- end;
- finally
- EndUpdate;
- end;
- end;
- end;
- procedure TEmailAwakeCollection.SetItem(Index: Integer;
- Value: TEmailAwakeItem);
- begin
- inherited SetItem(Index, Value);
- end;
- end.