Add record key for address book sync

This commit is contained in:
chuang%netscape.com 2000-04-26 13:46:00 +00:00
Родитель 63bf1a14ea
Коммит 8154a1aa34
5 изменённых файлов: 218 добавлений и 4 удалений

Просмотреть файл

@ -72,6 +72,7 @@ interface nsIAbCard : nsISupports {
attribute unsigned long lastModifiedDate;
attribute wstring name;
readonly attribute string printCardUrl;
readonly attribute unsigned long key;
attribute boolean sendPlainText;
attribute boolean isMailList;
@ -101,4 +102,6 @@ interface nsIAbCard : nsISupports {
void editCardToDatabase(in string uri);
void copyCard(in nsIAbCard srcCard);
wstring getCollationKey(in wstring str);
void setRecordKey(in unsigned long key);
void dropCardToDatabase(in string uri);
};

Просмотреть файл

@ -96,6 +96,7 @@ nsAbCardProperty::nsAbCardProperty(void)
NS_INIT_REFCNT();
m_LastModDate = 0;
m_Key = 0;
m_bSendPlainText = PR_TRUE;
m_bIsMailList = PR_FALSE;
@ -682,6 +683,25 @@ NS_IMETHODIMP nsAbCardProperty::AddCardToDatabase(const char *uri)
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsAbCardProperty::DropCardToDatabase(const char *uri)
{
nsresult rv = NS_OK;
nsCOMPtr<nsIAddrDatabase> dropDatabase;
NS_WITH_SERVICE(nsIAddressBook, addresBook, kAddrBookCID, &rv);
if (NS_SUCCEEDED(rv))
rv = addresBook->GetAbDatabaseFromURI(uri, getter_AddRefs(dropDatabase));
if (dropDatabase)
{
dropDatabase->CreateNewCardAndAddToDB(this, PR_FALSE);
dropDatabase->Commit(kLargeCommit);
return NS_OK;
}
else
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsAbCardProperty::EditCardToDatabase(const char *uri)
{
if (!mCardDatabase && uri)
@ -986,8 +1006,15 @@ nsAbCardProperty::GetNotes(PRUnichar * *aNotes)
NS_IMETHODIMP
nsAbCardProperty::GetLastModifiedDate(PRUint32 *aLastModifiedDate)
{ return *aLastModifiedDate = m_LastModDate; }
{ *aLastModifiedDate = m_LastModDate; return NS_OK; }
NS_IMETHODIMP
nsAbCardProperty::GetKey(PRUint32 *aKey)
{ *aKey = m_Key; return NS_OK; }
NS_IMETHODIMP
nsAbCardProperty::SetRecordKey(PRUint32 key)
{ m_Key = key; return NS_OK; }
NS_IMETHODIMP
nsAbCardProperty::SetFirstName(const PRUnichar * aFirstName)

Просмотреть файл

@ -97,6 +97,7 @@ protected:
nsString m_Custom4;
nsString m_Note;
PRUint32 m_LastModDate;
PRUint32 m_Key;
PRBool m_bSendPlainText;
PRBool m_bIsMailList;

Просмотреть файл

@ -75,6 +75,9 @@ const char *kPabTableKind = "ns:addrbk:db:table:kind:pab";
const char *kCardRowScope = "ns:addrbk:db:row:scope:card:all";
const char *kListRowScope = "ns:addrbk:db:row:scope:list:all";
const char *kDataRowScope = "ns:addrbk:db:row:scope:data:all";
#define DATAROW_ROWID 1
const char *kFirstNameColumn = "FirstName";
const char *kLastNameColumn = "LastName";
@ -114,8 +117,10 @@ const char *kCustom3Column = "Custom3";
const char *kCustom4Column = "Custom4";
const char *kNotesColumn = "Notes";
const char *kLastModifiedDateColumn = "LastModifiedDate";
const char *kRecordKeyColumn = "RecordKey";
const char *kAddressCharSetColumn = "AddrCharSet";
const char *kLastRecordKeyColumn = "LastRecordKey";
const char *kMailListName = "ListName";
const char *kMailListNickName = "ListNickName";
@ -177,6 +182,7 @@ nsAddrDatabase::nsAddrDatabase()
m_LastModDateColumnToken(0),
m_PlainTextColumnToken(0),
m_AddressCharSetColumnToken(0),
m_LastRecordKey(0),
m_dbDirectory(nsnull)
{
NS_INIT_REFCNT();
@ -572,9 +578,8 @@ NS_IMETHODIMP nsAddrDatabase::Open
if (NS_SUCCEEDED(err))
{
pAddressBookDB->SetDbPath(pabName);
GetDBCache()->AppendElement(pAddressBookDB);
*pAddrDB = pAddressBookDB;
if (pAddressBookDB)
GetDBCache()->AppendElement(pAddressBookDB);
}
else
{
@ -820,6 +825,8 @@ nsresult nsAddrDatabase::InitNewDB()
if (NS_SUCCEEDED(err))
{
err = InitPabTable();
err = InitLastRecorKey();
Commit(kLargeCommit);
}
return err;
}
@ -834,6 +841,88 @@ nsresult nsAddrDatabase::InitPabTable()
return mdberr;
}
//save the last record number, store in m_DataRowScopeToken, row 1
nsresult nsAddrDatabase::InitLastRecorKey()
{
if (!m_mdbPabTable)
return NS_ERROR_NULL_POINTER;
nsresult err = NS_OK;
nsIMdbRow *pDataRow = nsnull;
mdbOid dataRowOid;
dataRowOid.mOid_Scope = m_DataRowScopeToken;
dataRowOid.mOid_Id = DATAROW_ROWID;
err = GetStore()->NewRowWithOid(GetEnv(), &dataRowOid, &pDataRow);
if (NS_SUCCEEDED(err) && pDataRow)
{
m_LastRecordKey = 0;
err = AddIntColumn(pDataRow, m_LastRecordKeyColumnToken, 0);
err = m_mdbPabTable->AddRow(GetEnv(), pDataRow);
pDataRow->CutStrongRef(GetEnv());
}
return err;
}
nsresult nsAddrDatabase::GetDataRow(nsIMdbRow **pDataRow)
{
nsIMdbRow *pRow = nsnull;
mdbOid dataRowOid;
dataRowOid.mOid_Scope = m_DataRowScopeToken;
dataRowOid.mOid_Id = DATAROW_ROWID;
GetStore()->GetRow(GetEnv(), &dataRowOid, &pRow);
*pDataRow = pRow;
if (pRow)
return NS_OK;
else
return NS_ERROR_FAILURE;
}
nsresult nsAddrDatabase::GetLastRecorKey()
{
if (!m_mdbPabTable)
return NS_ERROR_NULL_POINTER;
nsresult err = NS_OK;
nsIMdbRow *pDataRow = nsnull;
err = GetDataRow(&pDataRow);
if (NS_SUCCEEDED(err) && pDataRow)
{
m_LastRecordKey = 0;
err = GetIntColumn(pDataRow, m_LastRecordKeyColumnToken, &m_LastRecordKey, 0);
if (NS_FAILED(err))
err = NS_ERROR_NOT_AVAILABLE;
pDataRow->CutStrongRef(GetEnv());
return NS_OK;
}
else
return NS_ERROR_NOT_AVAILABLE;
return err;
}
nsresult nsAddrDatabase::UpdateLastRecordKey()
{
if (!m_mdbPabTable)
return NS_ERROR_NULL_POINTER;
nsresult err = NS_OK;
nsIMdbRow *pDataRow = nsnull;
err = GetDataRow(&pDataRow);
if (NS_SUCCEEDED(err) && pDataRow)
{
err = AddIntColumn(pDataRow, m_LastRecordKeyColumnToken, m_LastRecordKey);
err = m_mdbPabTable->AddRow(GetEnv(), pDataRow);
pDataRow->CutStrongRef(GetEnv());
return NS_OK;
}
else
return NS_ERROR_NOT_AVAILABLE;
return err;
}
nsresult nsAddrDatabase::InitAnonymousTable()
{
nsIMdbStore *store = GetStore();
@ -855,10 +944,60 @@ nsresult nsAddrDatabase::InitExistingDB()
err = GetStore()->StringToToken(GetEnv(), kAnonymousTableKind, &m_AnonymousTableKind);
err = GetStore()->GetTable(GetEnv(), &gAnonymousTableOID, &m_mdbAnonymousTable);
err = GetLastRecorKey();
if (err == NS_ERROR_NOT_AVAILABLE)
CheckAndUpdateRecordKey();
}
return err;
}
nsresult nsAddrDatabase::CheckAndUpdateRecordKey()
{
nsresult err = NS_OK;
nsIMdbTableRowCursor* rowCursor = nsnull;
nsIMdbRow* findRow = nsnull;
mdb_pos rowPos = 0;
err = m_mdbPabTable->GetTableRowCursor(GetEnv(), -1, &rowCursor);
if (NS_FAILED(err) || !rowCursor)
return NS_ERROR_FAILURE;
mdb_count total = 0;
err = rowCursor->GetCount(GetEnv(), &total);
nsIMdbRow *pDataRow = nsnull;
err = GetDataRow(&pDataRow);
if (NS_FAILED(err))
InitLastRecorKey();
if (total == 0)
return NS_OK;
do
{ //add key to each card and mailing list row
err = rowCursor->NextRow(GetEnv(), &findRow, &rowPos);
if (NS_SUCCEEDED(err) && findRow)
{
mdbOid rowOid;
if (findRow->GetOid(GetEnv(), &rowOid) == NS_OK)
{
if (!IsDataRowScopeToken(rowOid.mOid_Scope))
{
m_LastRecordKey++;
err = AddIntColumn(findRow, m_RecordKeyColumnToken, m_LastRecordKey);
}
}
}
} while (findRow);
UpdateLastRecordKey();
Commit(kLargeCommit);
return NS_OK;
}
// initialize the various tokens and tables in our db's env
nsresult nsAddrDatabase::InitMDBInfo()
{
@ -869,6 +1008,7 @@ nsresult nsAddrDatabase::InitMDBInfo()
m_mdbTokensInitialized = PR_TRUE;
err = GetStore()->StringToToken(GetEnv(), kCardRowScope, &m_CardRowScopeToken);
err = GetStore()->StringToToken(GetEnv(), kListRowScope, &m_ListRowScopeToken);
err = GetStore()->StringToToken(GetEnv(), kDataRowScope, &m_DataRowScopeToken);
gAddressBookTableOID.mOid_Scope = m_CardRowScopeToken;
gAddressBookTableOID.mOid_Id = ID_PAB_TABLE;
gAnonymousTableOID.mOid_Scope = m_CardRowScopeToken;
@ -913,8 +1053,11 @@ nsresult nsAddrDatabase::InitMDBInfo()
GetStore()->StringToToken(GetEnv(), kCustom4Column, &m_Custom4ColumnToken);
GetStore()->StringToToken(GetEnv(), kNotesColumn, &m_NotesColumnToken);
GetStore()->StringToToken(GetEnv(), kLastModifiedDateColumn, &m_LastModDateColumnToken);
GetStore()->StringToToken(GetEnv(), kRecordKeyColumn, &m_RecordKeyColumnToken);
GetStore()->StringToToken(GetEnv(), kAddressCharSetColumn, &m_AddressCharSetColumnToken);
GetStore()->StringToToken(GetEnv(), kLastRecordKeyColumn, &m_LastRecordKeyColumnToken);
err = GetStore()->StringToToken(GetEnv(), kPabTableKind, &m_PabTableKind);
GetStore()->StringToToken(GetEnv(), kMailListName, &m_ListNameColumnToken);
@ -928,6 +1071,20 @@ nsresult nsAddrDatabase::InitMDBInfo()
////////////////////////////////////////////////////////////////////////////////
nsresult nsAddrDatabase::AddRecordKeyColumnToRow(nsIMdbRow *pRow)
{
if (pRow)
{
m_LastRecordKey++;
nsresult err = AddIntColumn(pRow, m_RecordKeyColumnToken, m_LastRecordKey);
err = m_mdbPabTable->AddRow(GetEnv(), pRow);
UpdateLastRecordKey();
return NS_OK;
}
else
return NS_ERROR_NULL_POINTER;
}
nsresult nsAddrDatabase::AddAttributeColumnsToRow(nsIAbCard *card, nsIMdbRow *cardRow)
{
nsresult err = NS_OK;
@ -1402,6 +1559,7 @@ NS_IMETHODIMP nsAddrDatabase::CreateNewCardAndAddToDB(nsIAbCard *newCard, PRBool
if (NS_SUCCEEDED(err) && cardRow)
{
AddAttributeColumnsToRow(newCard, cardRow);
AddRecordKeyColumnToRow(cardRow);
err = m_mdbPabTable->AddRow(GetEnv(), cardRow);
cardRow->CutStrongRef(GetEnv());
}
@ -1622,6 +1780,7 @@ NS_IMETHODIMP nsAddrDatabase::CreateMailListAndAddToDB(nsIAbDirectory *newList,
if (NS_SUCCEEDED(err) && listRow)
{
AddListAttributeColumnsToRow(newList, listRow);
AddRecordKeyColumnToRow(listRow);
err = m_mdbPabTable->AddRow(GetEnv(), listRow);
listRow->CutStrongRef(GetEnv());
}
@ -2933,6 +3092,11 @@ nsresult nsAddrDatabase::GetCardFromDB(nsIAbCard *newCard, nsIMdbRow* cardRow)
nsAllocator::Free(tempCString);
PR_Free(unicodeStr);
}
PRUint32 key = 0;
err = GetIntColumn(cardRow, m_RecordKeyColumnToken, &key, 0);
if (NS_SUCCEEDED(err))
newCard->SetRecordKey(key);
return err;
}
@ -2975,7 +3139,10 @@ nsresult nsAddrDatabase::GetListCardFromDB(nsIAbCard *listCard, nsIMdbRow* cardR
nsAllocator::Free(tempCString);
PR_Free(unicodeStr);
}
PRUint32 key = 0;
err = GetIntColumn(cardRow, m_RecordKeyColumnToken, &key, 0);
if (NS_SUCCEEDED(err))
listCard->SetRecordKey(key);
return err;
}
@ -3101,6 +3268,10 @@ NS_IMETHODIMP nsAddrDBEnumerator::Next(void)
mCurrentRowIsList = PR_FALSE;
return NS_OK;
}
else if (mDB->IsDataRowScopeToken(rowOid.mOid_Scope))
{
return Next();
}
else
return NS_ERROR_FAILURE;
}

Просмотреть файл

@ -249,6 +249,7 @@ public:
PRBool IsListRowScopeToken(mdb_scope scope) { return (scope == m_ListRowScopeToken) ? PR_TRUE: PR_FALSE; }
PRBool IsCardRowScopeToken(mdb_scope scope) { return (scope == m_CardRowScopeToken) ? PR_TRUE: PR_FALSE; }
PRBool IsDataRowScopeToken(mdb_scope scope) { return (scope == m_DataRowScopeToken) ? PR_TRUE: PR_FALSE; }
nsresult GetCardRowByRowID(mdb_id rowID, nsIMdbRow **dbRow);
nsresult GetListRowByRowID(mdb_id rowID, nsIMdbRow **dbRow);
@ -284,6 +285,7 @@ protected:
nsresult GetListCardFromDB(nsIAbCard *listCard, nsIMdbRow* cardRow);
nsresult GetListFromDB(nsIAbDirectory *newCard, nsIMdbRow* cardRow);
nsresult GetAnonymousAttributesFromDB();
nsresult AddRecordKeyColumnToRow(nsIMdbRow *pRow);
nsresult AddAttributeColumnsToRow(nsIAbCard *card, nsIMdbRow *cardRow);
nsresult AddListCardColumnsToRow(nsIAbCard *pCard, nsIMdbRow *pListRow, PRUint32 pos);
nsresult AddListAttributeColumnsToRow(nsIAbDirectory *list, nsIMdbRow *listRow);
@ -323,6 +325,12 @@ protected:
nsresult InitPabTable();
nsresult InitAnonymousTable();
nsresult InitLastRecorKey();
nsresult GetDataRow(nsIMdbRow **pDataRow);
nsresult GetLastRecorKey();
nsresult UpdateLastRecordKey();
nsresult CheckAndUpdateRecordKey();
nsIMdbEnv *m_mdbEnv; // to be used in all the db calls.
nsIMdbStore *m_mdbStore;
nsIMdbTable *m_mdbPabTable;
@ -345,6 +353,7 @@ protected:
mdb_scope m_CardRowScopeToken;
mdb_scope m_ListRowScopeToken;
mdb_scope m_DataRowScopeToken;
mdb_token m_FirstNameColumnToken;
mdb_token m_LastNameColumnToken;
@ -383,16 +392,19 @@ protected:
mdb_token m_Custom4ColumnToken;
mdb_token m_NotesColumnToken;
mdb_token m_LastModDateColumnToken;
mdb_token m_RecordKeyColumnToken;
mdb_token m_PlainTextColumnToken;
mdb_token m_AddressCharSetColumnToken;
mdb_token m_LastRecordKeyColumnToken;
mdb_token m_ListNameColumnToken;
mdb_token m_ListNickNameColumnToken;
mdb_token m_ListDescriptionColumnToken;
mdb_token m_ListTotalColumnToken;
PRUint32 m_LastRecordKey;
nsIAbDirectory* m_dbDirectory;
nsCOMPtr<nsICollation> m_collationKeyGenerator;