Back to index

lightning-sunbird  0.9+nobinonly
nsAbMDBCardProperty.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsAbMDBCardProperty.h"    
00039 #include "nsIRDFService.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsRDFCID.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsReadableUtils.h"
00044 #include "nsAbBaseCID.h"
00045 #include "rdf.h"
00046 #include "nsCOMPtr.h"
00047 
00048 #include "nsAddrDatabase.h"
00049 #include "nsIAddrBookSession.h"
00050 #include "nsIAddressBook.h"
00051 
00052 nsAbMDBCardProperty::nsAbMDBCardProperty(void)
00053 {
00054        m_key = 0;
00055        m_dbTableID = 0;
00056        m_dbRowID = 0;
00057 }
00058 
00059 nsAbMDBCardProperty::~nsAbMDBCardProperty(void)
00060 {
00061        if (mCardDatabase)
00062               mCardDatabase = nsnull;
00063 }
00064 
00065 NS_IMPL_ISUPPORTS_INHERITED1(nsAbMDBCardProperty, nsAbCardProperty, nsIAbMDBCard)
00066 
00067 // nsIAbMDBCard attributes
00068 
00069 NS_IMETHODIMP nsAbMDBCardProperty::GetDbTableID(PRUint32 *aDbTableID)
00070 {
00071        *aDbTableID = m_dbTableID;
00072        return NS_OK;
00073 }
00074 
00075 NS_IMETHODIMP nsAbMDBCardProperty::SetDbTableID(PRUint32 aDbTableID)
00076 {
00077        m_dbTableID = aDbTableID;
00078        return NS_OK;
00079 }
00080 
00081 NS_IMETHODIMP nsAbMDBCardProperty::GetDbRowID(PRUint32 *aDbRowID)
00082 {
00083        *aDbRowID = m_dbRowID;
00084        return NS_OK;
00085 }
00086 
00087 NS_IMETHODIMP nsAbMDBCardProperty::SetDbRowID(PRUint32 aDbRowID)
00088 {
00089        m_dbRowID = aDbRowID;
00090        return NS_OK;
00091 }
00092 
00093 NS_IMETHODIMP nsAbMDBCardProperty::GetKey(PRUint32 *aKey)
00094 {
00095        *aKey = m_key;
00096               return NS_OK;
00097        }
00098 
00099 NS_IMETHODIMP nsAbMDBCardProperty::SetKey(PRUint32 key)
00100 {
00101        m_key = key;
00102        return NS_OK;
00103 }
00104 
00105 NS_IMETHODIMP nsAbMDBCardProperty::SetAbDatabase(nsIAddrDatabase* database)
00106 {
00107        mCardDatabase = database;
00108        return NS_OK;
00109 }
00110 
00111 NS_IMETHODIMP nsAbMDBCardProperty::SetStringAttribute(const char *name, const PRUnichar *value)
00112 {
00113   NS_ASSERTION(mCardDatabase, "no db");
00114   if (!mCardDatabase)
00115     return NS_ERROR_UNEXPECTED;
00116 
00117   return mCardDatabase->SetCardValue(this, name, value, PR_TRUE /* notify */);
00118 }      
00119 
00120 NS_IMETHODIMP nsAbMDBCardProperty::GetStringAttribute(const char *name, PRUnichar **value)
00121 {
00122   NS_ASSERTION(mCardDatabase, "no db");
00123   if (!mCardDatabase)
00124     return NS_ERROR_UNEXPECTED;
00125 
00126   return mCardDatabase->GetCardValue(this, name, value);
00127 }
00128 
00129 NS_IMETHODIMP nsAbMDBCardProperty::CopyCard(nsIAbMDBCard* srcCardDB)
00130 {
00131        nsresult rv = NS_OK;
00132        nsCOMPtr<nsIAbCard> srcCard(do_QueryInterface(srcCardDB, &rv));
00133        NS_ENSURE_SUCCESS(rv, rv);
00134 
00135        nsXPIDLString str;
00136        srcCard->GetFirstName(getter_Copies(str));
00137        SetFirstName(str);
00138 
00139        srcCard->GetLastName(getter_Copies(str));
00140        SetLastName(str);
00141        srcCard->GetPhoneticFirstName(getter_Copies(str));
00142        SetPhoneticFirstName(str);
00143        srcCard->GetPhoneticLastName(getter_Copies(str));
00144        SetPhoneticLastName(str);
00145        srcCard->GetDisplayName(getter_Copies(str));
00146        SetDisplayName(str);
00147        srcCard->GetNickName(getter_Copies(str));
00148        SetNickName(str);
00149        srcCard->GetPrimaryEmail(getter_Copies(str));
00150        SetPrimaryEmail(str);
00151        srcCard->GetSecondEmail(getter_Copies(str));
00152        SetSecondEmail(str);
00153   srcCard->GetDefaultEmail(getter_Copies(str));
00154   SetDefaultEmail(str);
00155   srcCard->GetCardType(getter_Copies(str));
00156   SetCardType(str);
00157 
00158   PRUint32 format = nsIAbPreferMailFormat::unknown;
00159   srcCard->GetPreferMailFormat(&format);
00160   SetPreferMailFormat(format);
00161 
00162   PRUint32 popularityIndex = 0;
00163   srcCard->GetPopularityIndex(&popularityIndex);
00164   SetPopularityIndex(popularityIndex);
00165 
00166   PRBool allowRemoteContent = PR_FALSE;
00167   srcCard->GetAllowRemoteContent(&allowRemoteContent);
00168   SetAllowRemoteContent(allowRemoteContent);
00169 
00170        srcCard->GetWorkPhone(getter_Copies(str));
00171        SetWorkPhone(str);
00172        srcCard->GetHomePhone(getter_Copies(str));
00173        SetHomePhone(str);
00174        srcCard->GetFaxNumber(getter_Copies(str));
00175        SetFaxNumber(str);
00176        srcCard->GetPagerNumber(getter_Copies(str));
00177        SetPagerNumber(str);
00178        srcCard->GetCellularNumber(getter_Copies(str));
00179        SetCellularNumber(str);
00180   srcCard->GetWorkPhoneType(getter_Copies(str));
00181   SetWorkPhoneType(str);
00182   srcCard->GetHomePhoneType(getter_Copies(str));
00183   SetHomePhoneType(str);
00184   srcCard->GetFaxNumberType(getter_Copies(str));
00185   SetFaxNumberType(str);
00186   srcCard->GetPagerNumberType(getter_Copies(str));
00187   SetPagerNumberType(str);
00188   srcCard->GetCellularNumberType(getter_Copies(str));
00189   SetCellularNumberType(str);
00190        srcCard->GetHomeAddress(getter_Copies(str));
00191        SetHomeAddress(str);
00192        srcCard->GetHomeAddress2(getter_Copies(str));
00193        SetHomeAddress2(str);
00194        srcCard->GetHomeCity(getter_Copies(str));
00195        SetHomeCity(str);
00196        srcCard->GetHomeState(getter_Copies(str));
00197        SetHomeState(str);
00198        srcCard->GetHomeZipCode(getter_Copies(str));
00199        SetHomeZipCode(str);
00200        srcCard->GetHomeCountry(getter_Copies(str));
00201        SetHomeCountry(str);
00202        srcCard->GetWorkAddress(getter_Copies(str));
00203        SetWorkAddress(str);
00204        srcCard->GetWorkAddress2(getter_Copies(str));
00205        SetWorkAddress2(str);
00206        srcCard->GetWorkCity(getter_Copies(str));
00207        SetWorkCity(str);
00208        srcCard->GetWorkState(getter_Copies(str));
00209        SetWorkState(str);
00210        srcCard->GetWorkZipCode(getter_Copies(str));
00211        SetWorkZipCode(str);
00212        srcCard->GetWorkCountry(getter_Copies(str));
00213        SetWorkCountry(str);
00214        srcCard->GetJobTitle(getter_Copies(str));
00215        SetJobTitle(str);
00216        srcCard->GetDepartment(getter_Copies(str));
00217        SetDepartment(str);
00218        srcCard->GetCompany(getter_Copies(str));
00219        SetCompany(str);
00220   srcCard->GetAimScreenName(getter_Copies(str));
00221   SetAimScreenName(str);
00222 
00223   srcCard->GetAnniversaryYear(getter_Copies(str));
00224   SetAnniversaryYear(str);
00225   srcCard->GetAnniversaryMonth(getter_Copies(str));
00226   SetAnniversaryMonth(str);
00227   srcCard->GetAnniversaryDay(getter_Copies(str));
00228   SetAnniversaryDay(str);
00229   srcCard->GetSpouseName(getter_Copies(str));
00230   SetSpouseName(str);
00231   srcCard->GetFamilyName(getter_Copies(str));
00232   SetFamilyName(str);
00233   srcCard->GetDefaultAddress(getter_Copies(str));
00234   SetDefaultAddress(str);
00235   srcCard->GetCategory(getter_Copies(str));
00236   SetCategory(str);
00237 
00238        srcCard->GetWebPage1(getter_Copies(str));
00239        SetWebPage1(str);
00240        srcCard->GetWebPage2(getter_Copies(str));
00241        SetWebPage2(str);
00242        srcCard->GetBirthYear(getter_Copies(str));
00243        SetBirthYear(str);
00244        srcCard->GetBirthMonth(getter_Copies(str));
00245        SetBirthMonth(str);
00246        srcCard->GetBirthDay(getter_Copies(str));
00247        SetBirthDay(str);
00248        srcCard->GetCustom1(getter_Copies(str));
00249        SetCustom1(str);
00250        srcCard->GetCustom2(getter_Copies(str));
00251        SetCustom2(str);
00252        srcCard->GetCustom3(getter_Copies(str));
00253        SetCustom3(str);
00254        srcCard->GetCustom4(getter_Copies(str));
00255        SetCustom4(str);
00256        srcCard->GetNotes(getter_Copies(str));
00257        SetNotes(str);
00258 
00259        PRUint32 tableID, rowID;
00260        srcCardDB->GetDbTableID(&tableID);
00261        SetDbTableID(tableID);
00262        srcCardDB->GetDbRowID(&rowID);
00263        SetDbRowID(rowID);
00264 
00265        return NS_OK;
00266 }
00267 
00268 NS_IMETHODIMP nsAbMDBCardProperty::EditCardToDatabase(const char *uri)
00269 {
00270        if (!mCardDatabase && uri)
00271               GetCardDatabase(uri);
00272 
00273        if (mCardDatabase)
00274        {
00275               mCardDatabase->EditCard(this, PR_TRUE);
00276     mCardDatabase->Commit(nsAddrDBCommitType::kLargeCommit);
00277               return NS_OK;
00278        }
00279        else
00280               return NS_ERROR_FAILURE;
00281 }
00282 
00283 // protected class methods
00284 
00285 nsresult nsAbMDBCardProperty::GetCardDatabase(const char *uri)
00286 {
00287        nsresult rv = NS_OK;
00288 
00289        nsCOMPtr<nsIAddrBookSession> abSession = 
00290                 do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); 
00291        if (NS_SUCCEEDED(rv))
00292        {
00293               nsCOMPtr<nsILocalFile> dbPath;
00294               rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath));
00295               NS_ENSURE_SUCCESS(rv, rv);
00296 
00297 
00298               rv = dbPath->AppendNative(nsDependentCString(&(uri[kMDBDirectoryRootLen])));
00299               NS_ENSURE_SUCCESS(rv, rv);
00300               
00301               PRBool fileExists;
00302               rv = dbPath->Exists(&fileExists);
00303               if (NS_SUCCEEDED(rv) && fileExists)
00304               {
00305                      nsCOMPtr<nsIAddrDatabase> addrDBFactory = 
00306                               do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv);
00307 
00308                      if (NS_SUCCEEDED(rv) && addrDBFactory)
00309                             rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(mCardDatabase));
00310               }
00311               else
00312                      rv = NS_ERROR_FAILURE;
00313        }
00314        return rv;
00315 }
00316 
00317 NS_IMETHODIMP nsAbMDBCardProperty::Equals(nsIAbCard *card, PRBool *result)
00318 {
00319   nsresult rv;
00320 
00321   if (this == card) {
00322     *result = PR_TRUE;
00323     return NS_OK;
00324   }
00325 
00326   // the reason we need this card at all is that multiple nsIAbCards
00327   // can exist for a given mdbcard
00328   nsCOMPtr <nsIAbMDBCard> mdbcard = do_QueryInterface(card, &rv);
00329   if (NS_FAILED(rv) || !mdbcard) {
00330     // XXX using ldap can get us here, we need to fix how the listeners work
00331     *result = PR_FALSE;
00332     return NS_OK;
00333   }
00334 
00335   // XXX todo
00336   // optimize this code, key might be enough
00337   PRUint32 dbRowID;
00338   rv = mdbcard->GetDbRowID(&dbRowID);
00339   NS_ENSURE_SUCCESS(rv,rv);
00340 
00341   PRUint32 dbTableID;
00342   rv = mdbcard->GetDbTableID(&dbTableID);
00343   NS_ENSURE_SUCCESS(rv,rv);
00344 
00345   PRUint32 key;
00346   rv = mdbcard->GetKey(&key);
00347   NS_ENSURE_SUCCESS(rv,rv);
00348 
00349   if (dbRowID == m_dbRowID && dbTableID == m_dbTableID && key == m_key)
00350     *result = PR_TRUE;
00351   else
00352     *result = PR_FALSE;
00353   return NS_OK;
00354 }
00355