Changeset 12 for trunk/UContact.pas


Ignore:
Timestamp:
Feb 4, 2018, 1:11:07 AM (7 years ago)
Author:
chronos
Message:
  • Fixed: Correctly load and store photo as base64 encoded string.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UContact.pas

    r9 r12  
    66
    77uses
    8   Classes, SysUtils, Contnrs, Dialogs, UDataFile;
     8  Classes, SysUtils, Contnrs, Dialogs, UDataFile, LazUTF8, base64;
    99
    1010type
     
    207207  Output: TStringList;
    208208  I: Integer;
     209  PhotoBase64: string;
     210  Line: string;
     211
     212function IsAsciiString(Text: string): Boolean;
     213var
     214  I: Integer;
     215begin
     216  Result := True;
     217  for I := 1 to Length(Text) do
     218    if Ord(Text[I]) > 128 then begin
     219      Result := False;
     220      Break;
     221    end;
     222end;
     223
     224function NewItem(Key, Value: string): string;
     225var
     226  Charset: string;
     227begin
     228  if not IsAsciiString(Value) then Charset := ';CHARSET=UTF-8'
     229    else Charset := '';
     230  Result := Key + Charset + ':' + Value;
     231end;
     232
    209233begin
    210234  inherited;
     
    215239      Add('BEGIN:VCARD');
    216240      if Version <> '' then Add('VERSION:' + Version);
     241      if XTimesContacted <> '' then Add('X-TIMES_CONTACTED:' + XTimesContacted);
     242      if XLastTimeContacted <> '' then Add('X-LAST_TIME_CONTACTED:' + XLastTimeContacted);
    217243      if (LastName <> '') or (FirstName <> '') or (MiddleName <> '') or (TitleBefore <> '') or (TitleAfter <> '') then
    218         Add('N:' + LastName + ';' + FirstName + ';' + MiddleName + ';' + TitleBefore + ';' + TitleAfter);
    219       if FullName <> '' then Add('FN:' + FullName);
     244        Add(NewItem('N', LastName + ';' + FirstName + ';' + MiddleName + ';' + TitleBefore + ';' + TitleAfter));
     245      if FullName <> '' then Add(NewItem('FN', FullName));
     246      if TelCell <> '' then Add('TEL;CELL:' + TelCell);
    220247      if TelPrefCell <> '' then Add('TEL;PREF;CELL:' + TelPrefCell);
    221       if TelCell <> '' then Add('TEL;CELL:' + TelCell);
    222248      if TelHome <> '' then Add('TEL;HOME:' + TelHome);
    223249      if TelHome2 <> '' then Add('TEL;HOME2:' + TelHome2);
     
    228254      if TelHomeVoice <> '' then Add('TEL;HOME;VOICE:' + TelHomeVoice);
    229255      if TelWorkVoice <> '' then Add('TEL;WORK;VOICE:' + TelWorkVoice);
    230       if NickName <> '' then Add('X-NICKNAME:' + NickName);
    231       if XJabber <> '' then Add('X-JABBER:' + XJabber);
    232256      if Note <> '' then Add('NOTE:' + Note);
    233257      if AdrHome <> '' then Add('ADR;HOME:' + AdrHome);
    234258      if EmailHome <> '' then Add('EMAIL;HOME:' + EmailHome);
     259      if NickName <> '' then Add('X-NICKNAME:' + NickName);
    235260      if EmailInternet <> '' then Add('EMAIL;INTERNET:' + EmailInternet);
     261      if XJabber <> '' then Add('X-JABBER:' + XJabber);
    236262      if Role <> '' then Add('TITLE:' + Role);
    237263      if Categories <> '' then Add('CATEGORIES:' + Categories);
    238264      if Organization <> '' then Add('ORG:' + Organization);
    239       if XTimesContacted <> '' then Add('X-TIMES_CONTACTED:' + XTimesContacted);
    240       if XLastTimeContacted <> '' then Add('X-LAST_TIME_CONTACTED:' + XLastTimeContacted);
    241265      if (HomeAddressCity <> '') or (HomeAddressStreet <> '') or
    242266        (HomeAddressCountry <> '') then Add('ADR;HOME:;;' + HomeAddressStreet + ';' + HomeAddressCity + ';;;' + HomeAddressCountry);
    243       if Photo <> '' then Add('PHOTO;ENCODING=BASE64;JPEG:' + Photo);
     267      if Photo <> '' then begin
     268        PhotoBase64 := EncodeStringBase64(Photo);
     269
     270        Line := Copy(PhotoBase64, 1, 73 - Length('PHOTO;ENCODING=BASE64;JPEG:'));
     271        System.Delete(PhotoBase64, 1, Length(Line));
     272        Add('PHOTO;ENCODING=BASE64;JPEG:' + Line);
     273        while PhotoBase64 <> '' do begin
     274          Line := Copy(PhotoBase64, 1, 73);
     275          System.Delete(PhotoBase64, 1, Length(Line));
     276          Add(' ' + Line);
     277        end;
     278        Add('');
     279      end;
    244280      Add('END:VCARD');
    245281    end;
     
    315351        else if Command = 'X-NICKNAME' then NewRecord.NickName := Line
    316352        else if Command = 'EMAIL;HOME' then NewRecord.EmailHome := Line
    317         else if Command = 'EMAIL:INTERNET' then NewRecord.EmailInternet := Line
     353        else if Command = 'EMAIL;INTERNET' then NewRecord.EmailInternet := Line
    318354        else if Command = 'NOTE' then NewRecord.Note := Line
    319355        else if Command = 'ORG' then NewRecord.Organization := Line
     
    322358        else if Command = 'X-TIMES_CONTACTED' then NewRecord.XTimesContacted := Line
    323359        else if Command = 'X-LAST_TIME_CONTACTED' then NewRecord.XLastTimeContacted := Line
    324         else if Command = 'PHOTO' then begin
    325           NewRecord.Photo := Line;
     360        else if Command = 'PHOTO;JPEG' then begin
     361          NewRecord.Photo := Trim(Line);
    326362          repeat
    327363            Inc(I);
    328             Line := Lines[I];
    329             if Copy(Line, 1, 1) = ' ' then NewRecord.Photo := NewRecord.Photo + Line;
    330           until Copy(Line, 1, 1) = '';
     364            Line := Trim(Lines[I]);
     365            if Line <> '' then NewRecord.Photo := NewRecord.Photo + Line;
     366          until Line = '';
     367          NewRecord.Photo := DecodeStringBase64(NewRecord.Photo);
    331368        end
    332369        else if Assigned(FOnError) then FOnError('Unknown command: ' + Command);
Note: See TracChangeset for help on using the changeset viewer.