uvbase.pas

来自「FMA is a free1 powerful phone editing to」· PAS 代码 · 共 1,002 行 · 第 1/2 页

PAS
1,002 行
字号

  if FIsSet then
  begin
    Result := PropertyName;

    strTemp := PropertyValue;

    if Owner <> nil then
    begin
      if Owner.OutputCharSet <> tecNone then
      begin
        if (GetParamIndex('UTF-8') < 0) and (GetParamIndex('QUOTED-PRINTABLE') < 0) then
        begin
          if (Ord(Owner.OutputCharSet) and Ord(tecUtf8)) = Ord(tecUtf8) then
          begin
            strUtf8 := WideStringToUTF8(strTemp);
            //strUtf8 := LongStringToWideString(WideStringToUTF8(strTemp));
            if strUtf8 <> strTemp then
            begin
              strTemp := strUtf8;
              Result := Result + ';CHARSET=UTF-8';
            end;
          end;
          if (Ord(Owner.OutputCharSet) and Ord(tecAscii)) = Ord(tecAscii) then
          begin
            strQP := Str2QP(strTemp);
            if strQP <> strTemp then
            begin
              strTemp := strQP;
              Result := Result + ';ENCODING=QUOTED-PRINTABLE';
            end;
          end;
        end;
      end;
    end;

    if PropertyParams.Count <> 0 then
      Result := Result + ';' + LongStringToWideString(PropertyParams.DelimitedText);
    Result := Result + ':' + strTemp;
  end;
end;

procedure TVProperty.SetEncodedText(const Value: WideString);
begin
  Text := Value;

  CheckUTFs;
end;

function TVProperty.GetPropertyName: WideString;
begin
  Result := FPropertyName;
end;

procedure TVProperty.SetPropertyName(const Value: WideString);
begin
  FPropertyName := Value;
end;

function TVProperty.GetPropertyValue: WideString;
begin
  Result := FPropertyValue;
end;

procedure TVProperty.SetPropertyValue(const Value: WideString);
begin
  FIsSet := False;
  FPropertyValue := Value;

  if FPropertyValue <> '' then FIsSet := True;
end;

procedure TVProperty.SetPropertyParams(const Value: TStrings);
begin
  FPropertyParams.DelimitedText := Value.DelimitedText;
end;

procedure TVProperty.CheckUTFs;
  var
    Index: Integer;
begin
  Index := GetParamIndex('UTF-7');
  if Index >= 0 then
  begin
    PropertyValue := UTF7ToWideString(WideStringToLongString(PropertyValue));
    PropertyParams.Delete(Index);
    Exit;
  end;

  Index := GetParamIndex('UTF-8');
  if Index >= 0 then
  begin
    PropertyValue := UTF8StringToWideString(PropertyValue);
    PropertyParams.Delete(Index);
    Exit;
  end;
end;

function TVProperty.GetParamIndex(const Value: WideString): Integer;
  var
    I: Integer;
begin
  Result := -1;

  for I := Pred(PropertyParams.Count) downto 0 do
  begin
    if Pos(Value, PropertyParams[I]) <> 0 then
    begin
      Result := I;
      Break;
    end
  end;
end;

{ TVEntityType }

procedure TVEntityType.SetEntityType(const Value: TVType);
begin
  FEntityType := Value;

  FIsSet := Value <> tenVUnknown;
end;

function TVEntityType.GetPropertyValue: WideString;
begin
  Result := '';

  if FIsSet then Result := VEntityType[Ord(FEntityType)];
end;

procedure TVEntityType.SetPropertyValue(const Value: WideString);
  var
    Pos: Integer;
begin
  FIsSet := False;

  Pos := PosStrInArray(Value, VEntityType);

  if Pos < 0 then FEntityType := tenVUnknown
  else begin
    FEntityType := TVType(Pos);
    FIsSet := True;
  end;
end;

{ TVBaseObj }

const VObjProperties : array [0..1] of WideString = (
  'VERSION',
  // FMA Specific
  'X-FMA-STATE'
);

constructor TVBaseObj.Create;
begin
  inherited;

  FOwner := nil;

  isDestroying := False;

  FItemIndex := -1;
  FItemCounter := 0;

  VType := TVEntityType.Create(Self);
  VFmaState := 3;

  FStrList := TStringList.Create;
end;

destructor TVBaseObj.Destroy;
begin
  isDestroying := True;

  FOwner := nil;

  VType.Free;
  FStrList.Free;

  inherited;
end;

procedure TVBaseObj.Clear;
begin
  inherited;

  if not isDestroying then begin
    FItemIndex := 0;
    FItemCounter := 0;

    VType.IsSet := False;
    VVersion := '';
    VFmaState := 3;

    FStrList.Clear;
  end;
end;

function TVBaseObj.Add(AVObj: TVBaseObj): Integer;
begin
  Result := inherited Add(AVObj);

  AVObj.FItemIndex := FItemCounter;
  AVObj.FOwner := Self;
  Inc(FItemCounter);
end;

function TVBaseObj.GetRaw: TStrings;
var
  I: Integer;
  AItem: TVBaseObj;
  OutStrList: TStrings;
begin
  OutStrList := TStringList.Create;
  try
    OutStrList.Add('BEGIN:' + VType.PropertyValue);

    if VVersion <> '' then OutStrList.Add('VERSION:' + VVersion);
    if FStrList.Text <> '' then OutStrList.Add(TrimRight(FStrList.Text));
    if VFmaState <> 3 then OutStrList.Add('X-FMA-STATE:' + IntToStr(VFmaState));

    for I := 0 to Count - 1 do begin
      AItem := (Items[I] as TVBaseObj);

      OutStrList.Add(TrimRight(AItem.Raw.Text));
    end;

    OutStrList.Add('END:' + VType.PropertyValue);

    FStrList.Text := OutStrList.Text;
    Result := FStrList;
  finally
    OutStrList.Free;
  end;
end;

procedure TVBaseObj.SetRaw(const Value: TStrings);
var
  CurrPos: Integer;
  EmbeddedStrList: TStrings;
  Prop: TVProperty;
  AItem: TVBaseObj;
  isEmbedded: Boolean;
  QPData: String;
begin
  Clear;

  EmbeddedStrList := TStringList.Create;
  Prop := TVProperty.Create(Self);

  CurrPos := 0;
  while CurrPos < Value.Count do
  begin
    Prop.Text := Value[CurrPos];

    // Decode QP
    if Pos('QUOTED-PRINTABLE', Prop.PropertyParams.Text) > 0 then
    begin
      // remove of SoftLineBreakes if any
      QPData := RemoveSoftLineBreakes(Prop.PropertyValue, Value, CurrPos);

      // Convert QP to Str
      Prop.PropertyValue := QP2Str(QPData);

      // Remove QP param
      try
        Prop.PropertyParams.Delete(Prop.GetParamIndex('QUOTED-PRINTABLE'))
      except
      end;
    end;

    if pos('BEGIN', Prop.PropertyName) = 1 then
    begin
      // Is embedded
      if CurrPos > 0 then
      begin
        EmbeddedStrList.Clear;

        isEmbedded := True;
        while isEmbedded and (CurrPos < Value.Count) do
        begin
          EmbeddedStrList.Add(Value[CurrPos]);

          if Pos('END:' + Prop.PropertyValue, Value[CurrPos]) = 1 then isEmbedded := False;

          Inc(CurrPos);
        end;

        Dec(CurrPos);

        AItem := CreateVObject(Prop.PropertyValue);
        if AItem <> nil then
        begin
          AItem.Raw := EmbeddedStrList;

          Add(AItem);
        end;
      end
      else begin
        VType.Text := Prop.Text;
      end;
    end
    else begin
      // If end is found, break loop
      if (pos('END', Prop.PropertyName) = 1) and (Prop.PropertyValue = VType.PropertyValue) then break;

      Prop.PropertyValue := Prop.PropertyValue + UnfoldLines(Value, CurrPos);
      Prop.CheckUTFs;
      SetProperty(Prop);
    end;

    Inc(CurrPos);
  end;

  EmbeddedStrList.Free;
  Prop.Free;
end;

procedure TVBaseObj.SetProperty(AProp: TVProperty);
begin
  case PosStrInArray(AProp.PropertyName, VObjProperties) of
    // VERSION
    0: VVersion := AProp.PropertyValue;
    // X-FMA-STATE
    1: VFmaState := StrToInt(AProp.PropertyValue);
  end;
end;

function TVBaseObj.GetByItemIndex(const Value: Integer): TVBaseObj;
  var
    I: Integer;
    AVObj: TVBaseObj;
begin
  Result := nil;
  for I := 0 to Count - 1 do
  begin
    AVObj := TVBaseObj(Items[I]);
    if AVObj.ItemIndex = Value then
    begin
      Result := AVObj;
      Exit;
    end;
  end;
end;

function TVBaseObj.GetCalCharSet: TVCalCharSet;
begin
  if Owner <> nil then Result := Owner.OutputCharSet
  else Result := FCalCharSet;
end;

{ TVObjStorage }

constructor TVObjStorage.Create;
begin
  inherited;

  isDestroying := False;

  FItemCounter := 0;
  FStrList := TStringList.Create;
end;

destructor TVObjStorage.Destroy;
begin
  isDestroying := True;

  FStrList.Free;
  
  inherited;
end;

procedure TVObjStorage.Clear;
begin
  inherited;

  if not isDestroying then
  begin
    FItemCounter := 0;
    FStrList.Clear;
  end;
end;

function TVObjStorage.Add(AVObj: TVBaseObj): Integer;
begin
  Result := inherited Add(AVObj);

  AVObj.FItemIndex := FItemCounter;
  Inc(FItemCounter);
end;

function TVObjStorage.GetRaw: TStrings;
  var
    I: Integer;
    AItem: TVBaseObj;
begin
  FStrList.Clear;

  for I := 0 to Count - 1 do
  begin
    AItem := (Items[I] as TVBaseObj);

    FStrList.Add(TrimRight(AItem.Raw.Text));
  end;

  Result := FStrList;
end;

procedure TVObjStorage.SetRaw(const Value: TStrings);
  var
    CurrPos: Integer;
    EmbeddedStrList: TStrings;
    Prop: TVProperty;
    AItem: TVBaseObj;
    isEmbedded: Boolean;
begin
  Clear;

  EmbeddedStrList := TStringList.Create;
  Prop := TVProperty.Create(nil);

  CurrPos := 0;
  while CurrPos < Value.Count do
  begin
    Prop.Text := Value[CurrPos];

    if pos('BEGIN', Prop.PropertyName) = 1 then
    begin
      // Is embedded
      isEmbedded := True;
      EmbeddedStrList.Clear;
      while isEmbedded and (CurrPos < Value.Count) do
      begin
        EmbeddedStrList.Add(Value[CurrPos]);

        if Pos('END:' + Prop.PropertyValue, Value[CurrPos]) = 1 then isEmbedded := False;

        Inc(CurrPos);
      end;

      Dec(CurrPos);

      AItem := CreateVObject(Prop.PropertyValue);
      if AItem <> nil then
      begin
        AItem.Raw := EmbeddedStrList;

        Add(AItem);
      end;
    end;

    Inc(CurrPos);
  end;

  EmbeddedStrList.Free;
  Prop.Free;
end;

procedure TVObjStorage.SaveToFile(const FileName: string);
begin
  Raw.SaveToFile(FileName);
end;

procedure TVObjStorage.LoadFromFile(const FileName: string);
var
  sl,unfoldersl: TStrings;
  pos: integer;
begin
  sl := TStringList.Create;
  unfoldersl := TStringList.Create;
  try
    sl.LoadFromFile(FileName);

    pos := 0;
    while pos < sl.Count do begin
      unfoldersl.Add(sl[pos] + UnfoldLines(sl,pos));
      inc(pos);
    end;
    
    Raw := unfoldersl;
  finally
    unfoldersl.Free;
    sl.Free;
  end;
end;

function TVObjStorage.CreateVObject(Value: WideString): TVBaseObj;
begin
  Result := nil;

  case PosStrInArray(Value, VEntityType) of
    // VCARD
// Temporary removed
//    1: Result := TVCard.Create;
    // VCALENDAR
    2: Result := TVCalendar.Create;
  end;
end;

end.

⌨️ 快捷键说明

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