Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
nsAbLDIFService Class Reference

#include <nsAbLDIFService.h>

Inheritance diagram for nsAbLDIFService:
Inheritance graph
[legend]
Collaboration diagram for nsAbLDIFService:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSIABLDIFSERVICE 
nsAbLDIFService ()
virtual ~nsAbLDIFService ()
boolean isLDIFFile (in nsIFileSpec aSrc)
 Determine if a file is likely to be an LDIF file based on field names that commonly appear in LDIF files.
void importLDIFFile (in nsIAddrDatabase aDb, in nsIFileSpec aSrc, in boolean aStoreLocAsHome, inout unsigned long aProgress)
 Imports a file into the specified address book.

Private Member Functions

nsresult str_parse_line (char *line, char **type, char **value, int *vlen) const
char * str_getline (char **next) const
nsresult GetLdifStringRecord (char *buf, PRInt32 len, PRInt32 &stopPos)
void AddLdifRowToDatabase (PRBool aIsList)
void AddLdifColToDatabase (nsIMdbRow *newRow, char *typeSlot, char *valueSlot, PRBool bIsList)
void ClearLdifRecordBuffer ()
void SplitCRLFAddressField (nsCString &inputAddress, nsCString &outputLine1, nsCString &outputLine2) const

Private Attributes

PRBool mStoreLocAsHome
nsCString mLdifLine
PRInt32 mLFCount
PRInt32 mCRCount
nsCOMPtr< nsIAddrDatabasemDatabase

Detailed Description

Definition at line 45 of file nsAbLDIFService.h.


Constructor & Destructor Documentation

Definition at line 53 of file nsAbLDIFService.cpp.

Definition at line 60 of file nsAbLDIFService.cpp.

{
}

Member Function Documentation

void nsAbLDIFService::AddLdifColToDatabase ( nsIMdbRow newRow,
char *  typeSlot,
char *  valueSlot,
PRBool  bIsList 
) [private]

Definition at line 410 of file nsAbLDIFService.cpp.

{
  nsCAutoString colType(typeSlot);
  nsCAutoString column(valueSlot);

  // 4.x exports attributes like "givenname", 
  // mozilla does "givenName" to be compliant with RFC 2798
  ToLowerCase(colType);

  mdb_u1 firstByte = (mdb_u1)(colType.get())[0];
  switch ( firstByte )
  {
  case 'b':
    if (colType.EqualsLiteral("birthyear"))
      mDatabase->AddBirthYear(newRow, column.get());
    break; // 'b'

  case 'c':
    if (colType.EqualsLiteral("cn") || colType.EqualsLiteral("commonname"))
    {
      if (bIsList)
        mDatabase->AddListName(newRow, column.get());
      else
        mDatabase->AddDisplayName(newRow, column.get());
    }
    else if (colType.EqualsLiteral("c") || colType.EqualsLiteral("countryname"))
    {
      if (mStoreLocAsHome )
        mDatabase->AddHomeCountry(newRow, column.get());
      else
        mDatabase->AddWorkCountry(newRow, column.get());
    }

    else if (colType.EqualsLiteral("cellphone") )
      mDatabase->AddCellularNumber(newRow, column.get());

    else if (colType.EqualsLiteral("carphone"))
      mDatabase->AddCellularNumber(newRow, column.get());
        
    else if (colType.EqualsLiteral("custom1"))
      mDatabase->AddCustom1(newRow, column.get());
        
    else if (colType.EqualsLiteral("custom2"))
      mDatabase->AddCustom2(newRow, column.get());
        
    else if (colType.EqualsLiteral("custom3"))
      mDatabase->AddCustom3(newRow, column.get());
        
    else if (colType.EqualsLiteral("custom4"))
      mDatabase->AddCustom4(newRow, column.get());
        
    else if (colType.EqualsLiteral("company"))
      mDatabase->AddCompany(newRow, column.get());
    break; // 'c'

  case 'd':
    if (colType.EqualsLiteral("description"))
    {
      if (bIsList)
        mDatabase->AddListDescription(newRow, column.get());
      else
        mDatabase->AddNotes(newRow, column.get());
    }

    else if (colType.EqualsLiteral("department"))
      mDatabase->AddDepartment(newRow, column.get());

    else if (colType.EqualsLiteral("displayname"))
    {
      if (bIsList)
        mDatabase->AddListName(newRow, column.get());
      else
        mDatabase->AddDisplayName(newRow, column.get());
    }
    break; // 'd'

  case 'f':

    if (colType.EqualsLiteral("fax") ||
        colType.EqualsLiteral("facsimiletelephonenumber"))
      mDatabase->AddFaxNumber(newRow, column.get());
    break; // 'f'

  case 'g':
    if (colType.EqualsLiteral("givenname"))
      mDatabase->AddFirstName(newRow, column.get());

    break; // 'g'

  case 'h':
    if (colType.EqualsLiteral("homephone"))
      mDatabase->AddHomePhone(newRow, column.get());

    else if (colType.EqualsLiteral("homestreet"))
      mDatabase->AddHomeAddress(newRow, column.get());

    else if (colType.EqualsLiteral("homeurl"))
      mDatabase->AddWebPage2(newRow, column.get());
    break; // 'h'

  case 'l':
    if (colType.EqualsLiteral("l") || colType.EqualsLiteral("locality"))
    {
      if (mStoreLocAsHome)
        mDatabase->AddHomeCity(newRow, column.get());
      else
        mDatabase->AddWorkCity(newRow, column.get());
    }

    break; // 'l'

  case 'm':
    if (colType.EqualsLiteral("mail"))
      mDatabase->AddPrimaryEmail(newRow, column.get());

    else if (colType.EqualsLiteral("member") && bIsList)
      mDatabase->AddLdifListMember(newRow, column.get());

    else if (colType.EqualsLiteral("mobile"))
      mDatabase->AddCellularNumber(newRow, column.get());

    else if (colType.EqualsLiteral("mozilla_aimscreenname"))
      mDatabase->AddAimScreenName(newRow, column.get());

    else if (colType.EqualsLiteral("mozillacustom1"))
      mDatabase->AddCustom1(newRow, column.get());
        
    else if (colType.EqualsLiteral("mozillacustom2"))
      mDatabase->AddCustom2(newRow, column.get());
        
    else if (colType.EqualsLiteral("mozillacustom3"))
      mDatabase->AddCustom3(newRow, column.get());
        
    else if (colType.EqualsLiteral("mozillacustom4"))
      mDatabase->AddCustom4(newRow, column.get());

    else if (colType.EqualsLiteral("mozilladefaultemail"))
      mDatabase->AddDefaultEmail(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomecountryname"))
      mDatabase->AddHomeCountry(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomelocalityname"))
      mDatabase->AddHomeCity(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomestate"))
      mDatabase->AddHomeState(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomestreet2"))
      mDatabase->AddHomeAddress2(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomepostalcode"))
      mDatabase->AddHomeZipCode(newRow, column.get());

    else if (colType.EqualsLiteral("mozillahomeurl"))
      mDatabase->AddWebPage2(newRow, column.get());

    else if (colType.EqualsLiteral("mozillanickname"))
    {
      if (bIsList)
        mDatabase->AddListNickName(newRow, column.get());
      else
        mDatabase->AddNickName(newRow, column.get());
    }

    else if (colType.EqualsLiteral("mozillasecondemail"))
      mDatabase->Add2ndEmail(newRow, column.get());

    else if (colType.EqualsLiteral("mozillausehtmlmail"))
    {
      ToLowerCase(column);
      if (kNotFound != column.Find("true"))
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
      else if (kNotFound != column.Find("false"))
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
      else
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
    }

    else if (colType.EqualsLiteral("mozillaworkstreet2"))
      mDatabase->AddWorkAddress2(newRow, column.get());

    else if (colType.EqualsLiteral("mozillaworkurl"))
      mDatabase->AddWebPage1(newRow, column.get());

    break; // 'm'

  case 'n':
    if (colType.EqualsLiteral("notes"))
      mDatabase->AddNotes(newRow, column.get());

    else if (colType.EqualsLiteral("nscpaimscreenname") || 
             colType.EqualsLiteral("nsaimid"))
      mDatabase->AddAimScreenName(newRow, column.get());

    break; // 'n'

  case 'o':
    if (colType.EqualsLiteral("objectclass"))
      break;

    else if (colType.EqualsLiteral("ou") || colType.EqualsLiteral("orgunit"))
      mDatabase->AddDepartment(newRow, column.get());

    else if (colType.EqualsLiteral("o")) // organization
      mDatabase->AddCompany(newRow, column.get());

    break; // 'o'

  case 'p':
    if (colType.EqualsLiteral("postalcode"))
    {
      if (mStoreLocAsHome)
        mDatabase->AddHomeZipCode(newRow, column.get());
      else
        mDatabase->AddWorkZipCode(newRow, column.get());
    }

    else if (colType.EqualsLiteral("postofficebox"))
    {
      nsCAutoString workAddr1, workAddr2;
      SplitCRLFAddressField(column, workAddr1, workAddr2);
      mDatabase->AddWorkAddress(newRow, workAddr1.get());
      mDatabase->AddWorkAddress2(newRow, workAddr2.get());
    }
    else if (colType.EqualsLiteral("pager") || colType.EqualsLiteral("pagerphone"))
      mDatabase->AddPagerNumber(newRow, column.get());

    break; // 'p'

  case 'r':
    if (colType.EqualsLiteral("region"))
    {
      if (mStoreLocAsHome)
        mDatabase->AddWorkState(newRow, column.get());
      else
        mDatabase->AddWorkState(newRow, column.get());
    }

    break; // 'r'

  case 's':
    if (colType.EqualsLiteral("sn") || colType.EqualsLiteral("surname"))
      mDatabase->AddLastName(newRow, column.get());

    else if (colType.EqualsLiteral("street"))
      mDatabase->AddWorkAddress(newRow, column.get());

    else if (colType.EqualsLiteral("streetaddress"))
    {
      nsCAutoString addr1, addr2;
      SplitCRLFAddressField(column, addr1, addr2);
      if (mStoreLocAsHome)
      {
        mDatabase->AddHomeAddress(newRow, addr1.get());
        mDatabase->AddHomeAddress2(newRow, addr2.get());
      }
      else
      {
        mDatabase->AddWorkAddress(newRow, addr1.get());
        mDatabase->AddWorkAddress2(newRow, addr2.get());
      }
    }
    else if (colType.EqualsLiteral("st"))
    {
    if (mStoreLocAsHome)
      mDatabase->AddHomeState(newRow, column.get());
    else
      mDatabase->AddWorkState(newRow, column.get());
    }
        
    break; // 's'

  case 't':
    if (colType.EqualsLiteral("title"))
      mDatabase->AddJobTitle(newRow, column.get());

    else if (colType.EqualsLiteral("telephonenumber") )
    {
      mDatabase->AddWorkPhone(newRow, column.get());
    }

    break; // 't'

  case 'u':

    if (colType.EqualsLiteral("uniquemember") && bIsList)
      mDatabase->AddLdifListMember(newRow, column.get());

    break; // 'u'

  case 'w':
    if (colType.EqualsLiteral("workurl"))
      mDatabase->AddWebPage1(newRow, column.get());

    break; // 'w'

  case 'x':
    if (colType.EqualsLiteral("xmozillanickname"))
    {
      if (bIsList)
        mDatabase->AddListNickName(newRow, column.get());
      else
        mDatabase->AddNickName(newRow, column.get());
    }

    else if (colType.EqualsLiteral("xmozillausehtmlmail"))
    {
      ToLowerCase(column);
      if (kNotFound != column.Find("true"))
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::html);
      else if (kNotFound != column.Find("false"))
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::plaintext);
      else
        mDatabase->AddPreferMailFormat(newRow, nsIAbPreferMailFormat::unknown);
    }

    break; // 'x'

  case 'z':
    if (colType.EqualsLiteral("zip")) // alias for postalcode
    {
      if (mStoreLocAsHome)
        mDatabase->AddHomeZipCode(newRow, column.get());
      else
        mDatabase->AddWorkZipCode(newRow, column.get());
    }

    break; // 'z'

  default:
    break; // default
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 362 of file nsAbLDIFService.cpp.

{
  // If no data to process then reset CR/LF counters and return.
  if (mLdifLine.IsEmpty())
  {
    mLFCount = 0;
    mCRCount = 0;
    return;
  }

  nsCOMPtr <nsIMdbRow> newRow;
  if (mDatabase)
  {
    if (bIsList)
      mDatabase->GetNewListRow(getter_AddRefs(newRow)); 
    else
      mDatabase->GetNewRow(getter_AddRefs(newRow)); 

    if (!newRow)
      return;
  }
  else
    return;

  char* cursor = ToNewCString(mLdifLine); 
  char* saveCursor = cursor;  /* keep for deleting */ 
  char* line = 0; 
  char* typeSlot = 0; 
  char* valueSlot = 0; 
  int length = 0;  // the length  of an ldif attribute
  while ( (line = str_getline(&cursor)) != nsnull)
  {
    if ( str_parse_line(line, &typeSlot, &valueSlot, &length) == 0) {
      AddLdifColToDatabase(newRow, typeSlot, valueSlot, bIsList);
    }
    else
      continue; // parse error: continue with next loop iteration
  }
  nsMemory::Free(saveCursor);
  mDatabase->AddCardRowToDB(newRow);    

  if (bIsList)
    mDatabase->AddListDirNode(newRow);
        
  // Clear buffer for next record
  ClearLdifRecordBuffer();
}

Here is the call graph for this function:

Definition at line 745 of file nsAbLDIFService.cpp.

{
  if (!mLdifLine.IsEmpty())
  {
    mLdifLine.Truncate();
    mLFCount = 0;
    mCRCount = 0;
  }
}

Here is the caller graph for this function:

nsresult nsAbLDIFService::GetLdifStringRecord ( char *  buf,
PRInt32  len,
PRInt32 stopPos 
) [private]

Definition at line 322 of file nsAbLDIFService.cpp.

{
  for (; stopPos < len; stopPos++) 
  {
    char c = buf[stopPos];

    if (c == 0xA)
    {
      mLFCount++;
    }
    else if (c == 0xD)
    {
      mCRCount++;
    }
    else
    {
      if (mLFCount == 0 && mCRCount == 0)
        mLdifLine.Append(c);
      else if (( mLFCount > 1) || ( mCRCount > 2 && mLFCount ) ||
               ( !mLFCount && mCRCount > 1 ))
      {
        return NS_OK;
      }
      else if ((mLFCount == 1 || mCRCount == 1))
      {
        mLdifLine.Append('\n');
        mLdifLine.Append(c);
        mLFCount = 0;
        mCRCount = 0;
      }
    }
  }

  if ((stopPos == len) && (mLFCount > 1) || (mCRCount > 2 && mLFCount) ||
      (!mLFCount && mCRCount > 1))
    return NS_OK;

  return NS_ERROR_FAILURE;
}
void nsIAbLDIFService::importLDIFFile ( in nsIAddrDatabase  aDb,
in nsIFileSpec  aSrc,
in boolean  aStoreLocAsHome,
inout unsigned long  aProgress 
) [inherited]

Imports a file into the specified address book.

Parameters:
aDbThe address book to import addresses into.
aSrcThe file to import addresses from.
aStoreLocAsHomeStores the address as a home rather than work address.
aProgressMay be null, but if a pointer is supplied, then it will be updated regularly with the current position of reading from the file.

Determine if a file is likely to be an LDIF file based on field names that commonly appear in LDIF files.

Parameters:
aSrcThe file to examine
Returns:
PR_TRUE if the file appears to be of LDIF type, PR_FALSE otherwise
void nsAbLDIFService::SplitCRLFAddressField ( nsCString inputAddress,
nsCString outputLine1,
nsCString outputLine2 
) const [private]

Definition at line 859 of file nsAbLDIFService.cpp.

{
  PRInt32 crlfPos = inputAddress.Find("\r\n");
  if (crlfPos != kNotFound)
  {
    inputAddress.Left(outputLine1, crlfPos);
    inputAddress.Right(outputLine2, inputAddress.Length() - (crlfPos + 2));
  }
  else
    outputLine1.Assign(inputAddress);
}

Here is the caller graph for this function:

char * nsAbLDIFService::str_getline ( char **  next) const [private]

Definition at line 298 of file nsAbLDIFService.cpp.

{
  char    *lineStr;
  char    c;

  if ( *next == nsnull || **next == '\n' || **next == '\0' ) {
    return( nsnull);
  }

  lineStr = *next;
  while ( (*next = PL_strchr( *next, '\n' )) != NULL ) {
    c = *(*next + 1);
    if ( NS_IS_SPACE ( c ) && c != '\n' ) {
      **next = CONTINUED_LINE_MARKER;
      *(*next+1) = CONTINUED_LINE_MARKER;
    } else {
      *(*next)++ = '\0';
      break;
    }
  }

  return( lineStr );
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsAbLDIFService::str_parse_line ( char *  line,
char **  type,
char **  value,
int vlen 
) const [private]

Definition at line 190 of file nsAbLDIFService.cpp.

{
  char    *p, *s, *d, *byte, *stop;
  char    nib;
  int    i, b64;

  /* skip any leading space */
  while ( NS_IS_SPACE( *line ) ) {
    line++;
  }
  *type = line;

  for ( s = line; *s && *s != ':'; s++ )
    ;    /* NULL */
  if ( *s == '\0' ) {
    return NS_ERROR_FAILURE;
  }

  /* trim any space between type and : */
  for ( p = s - 1; p > line && nsCRT::IsAsciiSpace( *p ); p-- ) {
    *p = '\0';
  }
  *s++ = '\0';

  /* check for double : - indicates base 64 encoded value */
  if ( *s == ':' ) {
    s++;
    b64 = 1;
  /* single : - normally encoded value */
  } else {
    b64 = 0;
  }

  /* skip space between : and value */
  while ( NS_IS_SPACE( *s ) ) {
    s++;
  }

  /* if no value is present, error out */
  if ( *s == '\0' ) {
    return NS_ERROR_FAILURE;
  }

  /* check for continued line markers that should be deleted */
  for ( p = s, d = s; *p; p++ ) {
    if ( *p != CONTINUED_LINE_MARKER )
      *d++ = *p;
  }
  *d = '\0';

  *value = s;
  if ( b64 ) {
    stop = PL_strchr( s, '\0' );
    byte = s;
    for ( p = s, *vlen = 0; p < stop; p += 4, *vlen += 3 ) {
      for ( i = 0; i < 3; i++ ) {
        if ( p[i] != '=' && (p[i] & 0x80 ||
             b642nib[ p[i] & 0x7f ] > 0x3f) ) {
          return NS_ERROR_FAILURE;
        }
      }

      /* first digit */
      nib = b642nib[ p[0] & 0x7f ];
      byte[0] = nib << 2;
      /* second digit */
      nib = b642nib[ p[1] & 0x7f ];
      byte[0] |= nib >> 4;
      byte[1] = (nib & RIGHT4) << 4;
      /* third digit */
      if ( p[2] == '=' ) {
        *vlen += 1;
        break;
      }
      nib = b642nib[ p[2] & 0x7f ];
      byte[1] |= nib >> 2;
      byte[2] = (nib & RIGHT2) << 6;
      /* fourth digit */
      if ( p[3] == '=' ) {
        *vlen += 2;
        break;
      }
      nib = b642nib[ p[3] & 0x7f ];
      byte[2] |= nib;

      byte += 3;
    }
    s[ *vlen ] = '\0';
  } else {
    *vlen = (int) (d - s);
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 65 of file nsAbLDIFService.h.

Definition at line 66 of file nsAbLDIFService.h.

Definition at line 63 of file nsAbLDIFService.h.

Definition at line 64 of file nsAbLDIFService.h.

Definition at line 62 of file nsAbLDIFService.h.


The documentation for this class was generated from the following files: