⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pfgpalmsynccomponent.pas

📁 delphi编写与Palm数据交换管道连接程序。
💻 PAS
📖 第 1 页 / 共 3 页
字号:
(*
eSlow Perform a slow synchronization:
every record is read from the handheld and compared
to the corresponding record on the desktop computer
(the data in the .dat file and
the data in the .bak file).
Used when the handheld has been synchronized with multiple computers
*)
begin
{$IFDEF debugmsg}
  DebugStrF('%s , SyncSlow', [pfgPalmRemoteTable.TableName]);
{$ENDIF}

  Result := 0; // Suppresses warning on return value

  try
    try
      pfgPalmRemoteTable.Options := pfgPalmRemoteTable.Options - [opModified];
      if not pfgPalmRemoteTable.Exists then
      begin
        if AllowCreatePalmTable then
        begin
          pfgPalmRemoteTable.CreateTable;
        end
        else
        begin
          raise exception.CreateFmt('Palm table: "%s" does not exist', [pfgPalmRemoteTable.TableName]);
        end;
      end
      else
      begin
        pfgPalmRemoteTable.Open;
      end;
      LocalDataSet.Open;

      pfgPalmRemoteTable.First;
      while not pfgPalmRemoteTable.eof do
      begin
{$IFDEF debugmsg}
        DebugStrF('Record number %d', [pfgPalmRemoteTable.RecNum]);
{$ENDIF}
        SyncYieldCycles(1);
        if FOnFindLocalRecordID(self, pfgPalmRemoteTable.Fields.RecordID) then
        begin
          if compare_and_act(0) <> DeletePalmPC then
          begin
            pfgPalmRemoteTable.next;
          end;
        end
        else
        begin
          if not isdeleted(pfgPalmRemoteTable.fields.Flags) then
          begin
            LocalDataSet.Insert;
            DoCopyToPC;
            try
              LocalDataSet.Post;
            except
              on e: exception do
              begin
                SendErrorMsg(e.message + ' in ' + pfgPalmRemoteTable.TableName);
                LocalDataSet.cancel;
              end;
            end;
          end;
          pfgPalmRemoteTable.next;
        end;
      end;
//
      DoBeforeLocalList;
      LocalDataSet.Open;
      LocalDataSet.first;
      while not LocalDataSet.Eof do
      begin
        SyncYieldCycles(1);
        if (DoGetLocalRecId <> 0) and pfgPalmRemoteTable.locateByID(DoGetLocalRecId) then
        begin
          if compare_and_act(0) <> DeletePalmPC then
          begin
            LocalDataSet.Next;
          end;
        end
        else
        begin
          LocalDataSet.Edit;
          DoSetLocalAttribs(DoGetLocalAttribs and (not eRecAttrDirty));
          pfgPalmRemoteTable.Insert;
          DoCopyToPalm;
          pfgPalmRemoteTable.Post;
          DoSetLocalRecId(pfgPalmRemoteTable.Fields.RecordID);
          LocalDataSet.Post;
          LocalDataSet.Next;
        end;
      end;
      pfgPalmRemoteTable.PurgeTable;
      result := 0;
    except
      on e: exception do
      begin
        SendErrorMsg(e.message + ' in ' + pfgPalmRemoteTable.TableName);
        result := -1;
      end;
    end;
  finally
    LocalDataSet.close;
    pfgPalmRemoteTable.close;
  end;
end;

function TpfgPalmSyncComponent.SyncFast: integer;
(*
eFast Perform a fast synchronization:
only records that have been added, archived,
deleted, or modified are
exchanged between the handheld and the desktop computer.
*)
begin
  Result := 0; // Suppresses warning on return value
  try
    try
      pfgPalmRemoteTable.Options := pfgPalmRemoteTable.Options + [opModified];
      if not pfgPalmRemoteTable.Exists then
      begin
        if AllowCreatePalmTable then
        begin
          pfgPalmRemoteTable.CreateTable;
        end
        else
        begin
          raise exception.CreateFmt('Palm table: "%s" does not exist', [pfgPalmRemoteTable.TableName]);
        end;
      end
      else
      begin
        pfgPalmRemoteTable.Open;
      end;
      LocalDataSet.Open;
//
      pfgPalmRemoteTable.First;
      while not pfgPalmRemoteTable.eof do
      begin
        SyncYieldCycles(1);
        if FOnFindLocalRecordID(self, pfgPalmRemoteTable.Fields.RecordID) then
        begin
          compare_and_act(0);
          pfgPalmRemoteTable.next;
        end
        else
        begin
          if not isdeleted(pfgPalmRemoteTable.fields.Flags) then
          begin
            LocalDataSet.Append;
            DoCopyToPC;
            try
              LocalDataSet.Post;
            except
              on e: exception do
              begin
                SendErrorMsg(e.message + ' in ' + pfgPalmRemoteTable.TableName);
                LocalDataSet.cancel;
              end;
            end;
          end;
          pfgPalmRemoteTable.next;
        end;
      end;
//
      DoBeforeLocalList;
      LocalDataSet.Open;
      LocalDataSet.first;
      while not LocalDataSet.Eof do
      begin
        SyncYieldCycles(1);
        if isdirty(DoGetLocalAttribs) then
        begin
          if (DoGetLocalRecId <> 0) and pfgPalmRemoteTable.locateByID(DoGetLocalRecId) then
          begin
            if compare_and_act(0) <> DeletePalmPC then
            begin
              LocalDataSet.Next;
            end;
          end
          else
          begin
            LocalDataSet.Edit;
            DoSetLocalAttribs(DoGetLocalAttribs and (not
              eRecAttrDirty));
            pfgPalmRemoteTable.Insert;
            DoCopyToPalm;
            pfgPalmRemoteTable.Post;
            DoSetLocalRecId(pfgPalmRemoteTable.Fields.RecordID);
            LocalDataSet.Post;
            LocalDataSet.Next;
          end;
        end
        else
        begin
          LocalDataSet.Next;
        end;
      end;
      pfgPalmRemoteTable.PurgeTable;
      result := 0;
    except
      on e: exception do
      begin
        SendErrorMsg(e.message + ' in ' + pfgPalmRemoteTable.TableName);
        result := -1;
      end;
    end;
  finally
    LocalDataSet.close;
    pfgPalmRemoteTable.close;
  end;
end;

procedure TpfgPalmSyncComponent.SetOnCompareRecord(
  const Value: TOnCompareRecord);
begin
  FOnCompareRecord := Value;
end;

function TpfgPalmSyncComponent.CompareRecord(const PalmFlags,
  PCFlags: byte): TRecordAction;
begin
  if assigned(FOnCompareRecord) then
  begin
    result := FOnCompareRecord(self, SyncProperties, PalmFlags, PCFlags);
  end
  else
  begin
    result := DefaultCompareRecord(PalmFlags, PCFlags);
  end;
end;

function TpfgPalmSyncComponent.DefaultCompareRecord(const PalmFlags,
  PCFlags: byte): TRecordAction;
begin
  result := DoNothing;
  if IsDirty(PalmFlags) then
  begin
// palm modified - Palm wins !
    result := CopyToPC;
  end
  else
  begin
// palm NOT modified !
    if IsDirty(PCFlags) and (not IsDeleted(PCFlags)) then
    begin
// PC modified - PC wins !
      result := CopyToPalm;
    end
    else
    begin
      if IsDeleted(PalmFlags) or
        IsDeleted(PCFlags) then
      begin
// Deleted on Palm or on PC !
        result := DeletePalmPC;
      end;
    end;
  end;
end;

function TpfgPalmSyncComponent.compare_and_act(const maskattr: byte): TRecordAction;
begin
  result := CompareRecord(pfgPalmRemoteTable.Fields.Flags, DoGetLocalAttribs);
{$IFDEF debugmsg}
  DebugStr(GetEnumName(Typeinfo(TRecordAction), Ord(result)));
{$ENDIF}

  case result of
    CopyToPalm:
      begin
        LocalDataSet.Edit;
        DoSetLocalAttribs((DoGetLocalAttribs and (not eRecAttrDirty) and
          (not maskattr)));
        pfgPalmRemoteTable.Edit;
        DoCopyToPalm;
        pfgPalmRemoteTable.Post;
        LocalDataSet.Post;
      end;
    CopyToPC:
      begin
        pfgPalmRemoteTable.Edit;
        pfgPalmRemoteTable.Fields.Flags := (pfgPalmRemoteTable.Fields.Flags and
          (not eRecAttrDirty) and (not maskattr));
        LocalDataSet.Edit;
        DoCopyToPC;
        LocalDataSet.Post;
        pfgPalmRemoteTable.Post;
      end;
    DeletePalmPC:
      begin
        DoDeletePalm;
        DoDeletePC;
      end;
    DoNothing:
      begin
      end;
  end;
end;


procedure TpfgPalmSyncComponent.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FpfgPalmRemoteTable) then
  begin
    FpfgPalmRemoteTable := nil;
  end;
  if (Operation = opRemove) and (AComponent = FLocalDataSet) then
  begin
    FLocalDataSet := nil;

// pel 2001-05-13
// needed to remove other things that will disapear if table is
// deleted !!!
    FLocalCategoryField := nil;
    FLocalRecIdField := nil;
    FLocalDeletedField := nil;
    FLocalDirtyField := nil;
    FLocalAttribsField := nil;
// pel 2001-07-17
    FOnEmptyLocalTable := nil;
  end;
end;

procedure TpfgPalmSyncComponent.SetOnBeforeSync(
  const Value: TOnBeforeSync);
begin
  FOnBeforeSync := Value;
end;

procedure TpfgPalmSyncComponent.SetOnFindLocalRecordID(
  const Value: TOnFindLocalRecordID);
begin
  FOnFindLocalRecordID := Value;
end;

function TpfgPalmSyncComponent.DoCopyToPalm: integer;
var
  i: integer;
  s: string;
begin
  result := 0;
  try
    pfgPalmRemoteTable.Fields.Flags := DoGetLocalAttribs;
    pfgPalmRemoteTable.Fields.RecordID := DoGetLocalRecId;
    pfgPalmRemoteTable.Fields.Category := DoGetLocalCatId;
    for i := 0 to pfgPalmRemoteTable.FieldCount - 1 do
    begin
      s := pfgPalmRemoteTable.Fielddefs[i].FieldName;
      pfgPalmRemoteTable.fieldbyname(s).AsString :=
        LocalDataSet.FieldByName(s).AsString;
    end;
  except
    result := -1;
  end;
end;

function TpfgPalmSyncComponent.DoCopyToPC: integer;
var
  i: integer;
  s: string;
begin
  result := 0;
  try
    DoSetLocalAttribs(pfgPalmRemoteTable.Fields.Flags);
    DoSetLocalRecId(pfgPalmRemoteTable.Fields.RecordID);
    DoSetLocalCatId(pfgPalmRemoteTable.Fields.Category);
    for i := 0 to pfgPalmRemoteTable.FieldCount - 1 do
    begin
      s := pfgPalmRemoteTable.Fielddefs[i].FieldName;
      LocalDataSet.FieldByName(s).AsString :=
        pfgPalmRemoteTable.fieldbyname(s).AsString;
    end;
  except
    result := -1;
  end;
end;

function TpfgPalmSyncComponent.DoDeletePalm: integer;
//PEL 2001-07-17
//It now only sets the deleted attribute
//- the record will be deleted later by calling PurgeTable
begin
  result := 0;
  try
    pfgPalmRemoteTable.Edit;
    pfgPalmRemoteTable.Fields.Flags :=
      pfgPalmRemoteTable.Fields.Flags or eRecAttrDeleted;
    pfgPalmRemoteTable.Post;
  except
    result := -1;
  end;
end;

function TpfgPalmSyncComponent.DoDeletePC: integer;
begin
  result := 0;
  try
    LocalDataSet.Delete;
  except
    result := -1;
  end;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -