Back to index

lightning-sunbird  0.9+nobinonly
nsImportFieldMap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nscore.h"
00041 #include "nsIStringBundle.h"
00042 #include "nsImportFieldMap.h"
00043 #include "nsImportStringBundle.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsISupportsObsolete.h"
00046 
00047 #include "ImportDebug.h"
00048 
00049 
00050 
00052 
00053 
00054 
00055 NS_METHOD nsImportFieldMap::Create( nsISupports *aOuter, REFNSIID aIID, void **aResult)
00056 {
00057   if (aOuter)
00058     return NS_ERROR_NO_AGGREGATION;
00059 
00060   nsImportFieldMap *it = new nsImportFieldMap();
00061   if (it == nsnull)
00062     return NS_ERROR_OUT_OF_MEMORY;
00063 
00064   NS_ADDREF( it);
00065   nsresult rv = it->QueryInterface( aIID, aResult);
00066   NS_RELEASE( it);
00067   return rv;
00068 }
00069 
00070 NS_IMPL_THREADSAFE_ISUPPORTS1(nsImportFieldMap, nsIImportFieldMap)
00071 
00072 NS_IMPL_GETSET(nsImportFieldMap, SkipFirstRecord, PRBool, m_skipFirstRecord)
00073 
00074 nsImportFieldMap::nsImportFieldMap() 
00075 { 
00076        m_numFields = 0;
00077        m_pFields = nsnull;
00078        m_pActive = nsnull;
00079        m_allocated = 0;
00080        // need to init the description array
00081        m_mozFieldCount = 0;
00082     m_skipFirstRecord = false;
00083        nsIStringBundle *pBundle = nsImportStringBundle::GetStringBundleProxy();
00084 
00085        nsString *pStr;
00086        for (PRInt32 i = IMPORT_FIELD_DESC_START; i <= IMPORT_FIELD_DESC_END; i++, m_mozFieldCount++) {
00087               pStr = new nsString();
00088               if (pBundle) {
00089                      nsImportStringBundle::GetStringByID( i, *pStr, pBundle);       
00090               }
00091               else
00092                      pStr->AppendInt( i);
00093               m_descriptions.AppendElement( (void *)pStr);
00094        }
00095        
00096        NS_IF_RELEASE( pBundle);
00097 }
00098 
00099 nsImportFieldMap::~nsImportFieldMap() 
00100 { 
00101        if (m_pFields)
00102               delete [] m_pFields;
00103        if (m_pActive)
00104               delete [] m_pActive;
00105 
00106        nsString *    pStr;
00107        for (PRInt32 i = 0; i < m_mozFieldCount; i++) {
00108               pStr = (nsString *) m_descriptions.ElementAt( i);
00109               delete pStr;
00110        }
00111        m_descriptions.Clear();
00112 }
00113 
00114 
00115 NS_IMETHODIMP nsImportFieldMap::GetNumMozFields(PRInt32 *aNumFields)
00116 {
00117     NS_PRECONDITION(aNumFields != nsnull, "null ptr");
00118        if (!aNumFields)
00119               return NS_ERROR_NULL_POINTER;
00120 
00121        *aNumFields = m_mozFieldCount;
00122        return( NS_OK);
00123 }
00124 
00125 NS_IMETHODIMP nsImportFieldMap::GetMapSize(PRInt32 *aNumFields)
00126 {
00127     NS_PRECONDITION(aNumFields != nsnull, "null ptr");
00128        if (!aNumFields)
00129               return NS_ERROR_NULL_POINTER;
00130 
00131        *aNumFields = m_numFields;
00132        return( NS_OK);
00133 }
00134 
00135 NS_IMETHODIMP nsImportFieldMap::GetFieldDescription(PRInt32 index, PRUnichar **_retval)
00136 {
00137     NS_PRECONDITION(_retval != nsnull, "null ptr");
00138        if (!_retval)
00139               return NS_ERROR_NULL_POINTER;
00140        
00141        *_retval = nsnull;
00142        if ((index < 0) || (index >= m_descriptions.Count()))
00143               return( NS_ERROR_FAILURE);
00144        
00145        *_retval = ToNewUnicode(*((nsString *)m_descriptions.ElementAt(index)));
00146        return( NS_OK);
00147 }
00148 
00149 NS_IMETHODIMP nsImportFieldMap::SetFieldMapSize(PRInt32 size)
00150 {
00151        nsresult rv = Allocate( size);
00152        if (NS_FAILED( rv))
00153               return( rv);
00154        
00155        m_numFields = size;
00156 
00157        return( NS_OK);
00158 }
00159 
00160 
00161 NS_IMETHODIMP nsImportFieldMap::DefaultFieldMap(PRInt32 size)
00162 {
00163        nsresult rv = SetFieldMapSize( size);
00164        if (NS_FAILED( rv))
00165               return( rv);
00166        for (PRInt32 i = 0; i < size; i++) {
00167               m_pFields[i] = i;
00168               m_pActive[i] = PR_TRUE;
00169        }
00170        
00171        return( NS_OK);
00172 }
00173 
00174 NS_IMETHODIMP nsImportFieldMap::GetFieldMap(PRInt32 index, PRInt32 *_retval)
00175 {
00176     NS_PRECONDITION(_retval != nsnull, "null ptr");
00177        if (!_retval)
00178               return NS_ERROR_NULL_POINTER;
00179        
00180        
00181        if ((index < 0) || (index >= m_numFields))
00182               return( NS_ERROR_FAILURE);
00183 
00184        *_retval = m_pFields[index];
00185        return( NS_OK);
00186 }
00187 
00188 NS_IMETHODIMP nsImportFieldMap::SetFieldMap(PRInt32 index, PRInt32 fieldNum)
00189 {
00190        if (index == -1) {
00191               nsresult rv = Allocate( m_numFields + 1);
00192               if (NS_FAILED( rv))
00193                      return( rv);
00194               index = m_numFields;
00195               m_numFields++;
00196        }
00197        else {
00198               if ((index < 0) || (index >= m_numFields))
00199                      return( NS_ERROR_FAILURE);
00200        }
00201        
00202        if ((fieldNum != -1) && ((fieldNum < 0) || (fieldNum >= m_mozFieldCount)))
00203               return( NS_ERROR_FAILURE);
00204        
00205        m_pFields[index] = fieldNum;
00206        return( NS_OK);
00207 }
00208 
00209 NS_IMETHODIMP nsImportFieldMap::SetFieldMapByDescription(PRInt32 index, const PRUnichar *fieldDesc)
00210 {
00211     NS_PRECONDITION(fieldDesc != nsnull, "null ptr");
00212        if (!fieldDesc)
00213               return NS_ERROR_NULL_POINTER;
00214 
00215        PRInt32 i = FindFieldNum( fieldDesc);
00216        if (i == -1)
00217               return( NS_ERROR_FAILURE);
00218 
00219        return( SetFieldMap( index, i));
00220 }
00221 
00222 
00223 NS_IMETHODIMP nsImportFieldMap::GetFieldActive(PRInt32 index, PRBool *active)
00224 {
00225     NS_PRECONDITION(active != nsnull, "null ptr");
00226        if (!active)
00227               return NS_ERROR_NULL_POINTER;
00228        if ((index < 0) || (index >= m_numFields))
00229               return( NS_ERROR_FAILURE);
00230               
00231        *active = m_pActive[index];
00232        return( NS_OK);
00233 }
00234 
00235 NS_IMETHODIMP nsImportFieldMap::SetFieldActive(PRInt32 index, PRBool active)
00236 {
00237        if ((index < 0) || (index >= m_numFields))
00238               return( NS_ERROR_FAILURE);
00239 
00240        m_pActive[index] = active;
00241        return( NS_OK);
00242 }
00243 
00244 
00245 NS_IMETHODIMP nsImportFieldMap::SetFieldValue(nsIAddrDatabase *database, nsIMdbRow *row, PRInt32 fieldNum, const PRUnichar *value)
00246 {
00247     NS_PRECONDITION(database != nsnull, "null ptr");
00248     NS_PRECONDITION(row != nsnull, "null ptr");
00249     NS_PRECONDITION(value != nsnull, "null ptr");
00250        if (!database || !row || !value)
00251               return NS_ERROR_NULL_POINTER;
00252        
00253        // Allow the special value for a null field
00254        if (fieldNum == -1)
00255               return( NS_OK);
00256 
00257        if ((fieldNum < 0) || (fieldNum >= m_mozFieldCount))
00258               return( NS_ERROR_FAILURE);
00259        
00260        // UGGG!!!!! lot's of typing here!
00261        nsresult rv;
00262        
00263        nsString str(value);
00264        char *pVal = ToNewUTF8String(str);
00265 
00266        switch( fieldNum) {
00267        case 0:
00268               rv = database->AddFirstName( row, pVal);
00269               break;
00270        case 1:
00271               rv = database->AddLastName( row, pVal);
00272               break;
00273        case 2:
00274               rv = database->AddDisplayName( row, pVal);
00275               break;
00276        case 3:
00277               rv = database->AddNickName( row, pVal);
00278               break;
00279        case 4:
00280               rv = database->AddPrimaryEmail( row, pVal);
00281               break;
00282        case 5:
00283               rv = database->Add2ndEmail( row, pVal);
00284               break;
00285        case 6:
00286               rv = database->AddWorkPhone( row, pVal);
00287               break;
00288        case 7:
00289               rv = database->AddHomePhone( row, pVal);
00290               break;
00291        case 8:
00292               rv = database->AddFaxNumber( row, pVal);
00293               break;
00294        case 9:
00295               rv = database->AddPagerNumber( row, pVal);
00296               break;
00297        case 10:
00298               rv = database->AddCellularNumber( row, pVal);
00299               break;
00300        case 11:
00301               rv = database->AddHomeAddress( row, pVal);
00302               break;
00303        case 12:
00304               rv = database->AddHomeAddress2( row, pVal);
00305               break;
00306        case 13:
00307               rv = database->AddHomeCity( row, pVal);
00308               break;
00309        case 14:
00310               rv = database->AddHomeState( row, pVal);
00311               break;
00312        case 15:
00313               rv = database->AddHomeZipCode( row, pVal);
00314               break;
00315        case 16:
00316               rv = database->AddHomeCountry( row, pVal);
00317               break;
00318        case 17:
00319               rv = database->AddWorkAddress( row, pVal);
00320               break;
00321        case 18:
00322               rv = database->AddWorkAddress2( row, pVal);
00323               break;
00324        case 19:
00325               rv = database->AddWorkCity( row, pVal);
00326               break;
00327        case 20:
00328               rv = database->AddWorkState( row, pVal);
00329               break;
00330        case 21:
00331               rv = database->AddWorkZipCode( row, pVal);
00332               break;
00333        case 22:
00334               rv = database->AddWorkCountry( row, pVal);
00335               break;
00336        case 23:
00337               rv = database->AddJobTitle(row, pVal);
00338               break;
00339        case 24:
00340               rv = database->AddDepartment(row, pVal);
00341               break;
00342        case 25:
00343               rv = database->AddCompany(row, pVal);
00344               break;
00345        case 26:
00346               rv = database->AddWebPage1(row, pVal);
00347               break;
00348        case 27:
00349               rv = database->AddWebPage2(row, pVal);
00350               break;
00351        case 28:
00352               rv = database->AddBirthYear(row, pVal);
00353               break;
00354        case 29:
00355               rv = database->AddBirthMonth(row, pVal);
00356               break;
00357        case 30:
00358               rv = database->AddBirthDay(row, pVal);
00359               break;
00360        case 31:
00361               rv = database->AddCustom1(row, pVal);
00362               break;
00363        case 32:
00364               rv = database->AddCustom2(row, pVal);
00365               break;
00366        case 33:
00367               rv = database->AddCustom3(row, pVal);
00368               break;
00369        case 34:
00370               rv = database->AddCustom4(row, pVal);
00371               break;
00372        case 35:
00373               rv = database->AddNotes(row, pVal);
00374               break;
00375        default:
00376               /* Get the field description, and add it as an anonymous attr? */
00377               /* OR WHAT???? */
00378               {
00379                      rv = NS_ERROR_FAILURE;
00380               }
00381        }
00382        
00383        nsCRT::free( pVal);
00384 
00385        return( rv);
00386 }
00387 
00388 
00389 NS_IMETHODIMP nsImportFieldMap::SetFieldValueByDescription(nsIAddrDatabase *database, nsIMdbRow *row, const PRUnichar *fieldDesc, const PRUnichar *value)
00390 {
00391     NS_PRECONDITION(fieldDesc != nsnull, "null ptr");
00392        if (!fieldDesc)
00393               return NS_ERROR_NULL_POINTER;
00394        PRInt32 i = FindFieldNum( fieldDesc);
00395        if (i == -1)
00396               return( NS_ERROR_FAILURE);
00397        return( SetFieldValue( database, row, i, value));
00398 }
00399 
00400 
00401 NS_IMETHODIMP nsImportFieldMap::GetFieldValue(nsIAbCard *card, PRInt32 fieldNum, PRUnichar **_retval)
00402 {
00403     NS_PRECONDITION(_retval != nsnull, "null ptr");
00404     NS_PRECONDITION(card != nsnull, "null ptr");
00405        if (!_retval || !card)
00406               return NS_ERROR_NULL_POINTER;
00407        
00408        if (fieldNum == -1) {
00409               PRUnichar     c = 0;
00410               *_retval = nsCRT::strdup( &c);
00411               return( NS_OK);
00412        }
00413 
00414        if ((fieldNum < 0) || (fieldNum >= m_mozFieldCount))
00415               return( NS_ERROR_FAILURE);
00416 
00417        // ARRGGG!!! Lots of typing again
00418        // get the field from the card
00419        nsresult      rv;
00420        PRUnichar *   pVal = nsnull;
00421 
00422        switch (fieldNum) {
00423        case 0:
00424               rv = card->GetFirstName( &pVal);
00425               break;
00426        case 1:
00427               rv = card->GetLastName( &pVal);
00428               break;
00429        case 2:
00430               rv = card->GetDisplayName( &pVal);
00431               break;
00432        case 3:
00433               rv = card->GetNickName( &pVal);
00434               break;
00435        case 4:
00436               rv = card->GetPrimaryEmail( &pVal);
00437               break;
00438        case 5:
00439               rv = card->GetSecondEmail( &pVal);
00440               break;
00441        case 6:
00442               rv = card->GetWorkPhone( &pVal);
00443               break;
00444        case 7:
00445               rv = card->GetHomePhone( &pVal);
00446               break;
00447        case 8:
00448               rv = card->GetFaxNumber( &pVal);
00449               break;
00450        case 9:
00451               rv = card->GetPagerNumber( &pVal);
00452               break;
00453        case 10:
00454               rv = card->GetCellularNumber( &pVal);
00455               break;
00456        case 11:
00457               rv = card->GetHomeAddress( &pVal);
00458               break;
00459        case 12:
00460               rv = card->GetHomeAddress2( &pVal);
00461               break;
00462        case 13:
00463               rv = card->GetHomeCity( &pVal);
00464               break;
00465        case 14:
00466               rv = card->GetHomeState( &pVal);
00467               break;
00468        case 15:
00469               rv = card->GetHomeZipCode( &pVal);
00470               break;
00471        case 16:
00472               rv = card->GetHomeCountry( &pVal);
00473               break;
00474        case 17:
00475               rv = card->GetWorkAddress( &pVal);
00476               break;
00477        case 18:
00478               rv = card->GetWorkAddress2( &pVal);
00479               break;
00480        case 19:
00481               rv = card->GetWorkCity( &pVal);
00482               break;
00483        case 20:
00484               rv = card->GetWorkState( &pVal);
00485               break;
00486        case 21:
00487               rv = card->GetWorkZipCode( &pVal);
00488               break;
00489        case 22:
00490               rv = card->GetWorkCountry( &pVal);
00491               break;
00492        case 23:
00493               rv = card->GetJobTitle( &pVal);
00494               break;
00495        case 24:
00496               rv = card->GetDepartment( &pVal);
00497               break;
00498        case 25:
00499               rv = card->GetCompany( &pVal);
00500               break;
00501        case 26:
00502               rv = card->GetWebPage1( &pVal);
00503               break;
00504        case 27:
00505               rv = card->GetWebPage2( &pVal);
00506               break;
00507        case 28:
00508               rv = card->GetBirthYear( &pVal);
00509               break;
00510        case 29:
00511               rv = card->GetBirthMonth( &pVal);
00512               break;
00513        case 30:
00514               rv = card->GetBirthDay( &pVal);
00515               break;
00516        case 31:
00517               rv = card->GetCustom1( &pVal);
00518               break;
00519        case 32:
00520               rv = card->GetCustom2( &pVal);
00521               break;
00522        case 33:
00523               rv = card->GetCustom3( &pVal);
00524               break;
00525        case 34:
00526               rv = card->GetCustom4( &pVal);
00527               break;
00528        case 35:
00529               rv = card->GetNotes( &pVal);
00530               break;
00531        default:
00532               /* Get the field description, and add it as an anonymous attr? */
00533               /* OR WHAT???? */
00534               {
00535                      rv = NS_ERROR_FAILURE;
00536               }
00537        }
00538        
00539        *_retval = pVal;
00540 
00541        return( rv);
00542 }
00543 
00544 NS_IMETHODIMP nsImportFieldMap::GetFieldValueByDescription(nsIAbCard *card, const PRUnichar *fieldDesc, PRUnichar **_retval)
00545 {
00546     NS_PRECONDITION(fieldDesc != nsnull, "null ptr");
00547        if (!fieldDesc)
00548               return NS_ERROR_NULL_POINTER;
00549        PRInt32 i = FindFieldNum( fieldDesc);
00550        if (i == -1)
00551               return( NS_ERROR_FAILURE);
00552        return( GetFieldValue( card, i, _retval));
00553 }
00554 
00555 
00556 nsresult nsImportFieldMap::Allocate( PRInt32 newSize)
00557 {
00558        if (newSize <= m_allocated)
00559               return( NS_OK);
00560 
00561        PRInt32 sz = m_allocated;
00562        while (sz < newSize)
00563               sz += 30;
00564 
00565        PRInt32       *pData = new PRInt32[ sz];
00566        if (!pData)
00567               return( NS_ERROR_FAILURE);
00568        PRBool *pActive = new PRBool[sz];
00569        if (!pActive)
00570               return( NS_ERROR_FAILURE);
00571 
00572 
00573        PRInt32       i;
00574        for (i = 0; i < sz; i++) {
00575               pData[i] = -1;
00576               pActive[i] = PR_TRUE;
00577        }
00578        if (m_numFields) {
00579               for (i = 0; i < m_numFields; i++) {
00580                      pData[i] = m_pFields[i];
00581                      pActive[i] = m_pActive[i];
00582               }
00583               delete [] m_pFields;
00584               delete [] m_pActive;
00585        }
00586        m_allocated = sz;
00587        m_pFields = pData;
00588        m_pActive = pActive;
00589        return( NS_OK);
00590 }
00591 
00592 PRInt32 nsImportFieldMap::FindFieldNum( const PRUnichar *pDesc)
00593 {
00594        nsString *    pStr;
00595        for (PRInt32 i = 0; i < m_mozFieldCount; i++) {
00596               pStr = (nsString *)m_descriptions.ElementAt( i);
00597               if (!pStr->Equals(pDesc))
00598                      return( i);
00599        }
00600 
00601        return( -1);
00602 }
00603 
00604