Back to index

lightning-sunbird  0.9+nobinonly
nsAbOutlookCard.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  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Seth Spitzer <sspitzer@netscape.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 #include "nsAbOutlookCard.h"
00039 #include "nsAbWinHelper.h"
00040 #include "nsIPrefService.h"
00041 #include "nsIPrefBranch.h"
00042 #include "nsIAddrBookSession.h"
00043 #include "nsAbBaseCID.h"
00044 
00045 #include "prlog.h"
00046 
00047 #ifdef PR_LOGGING
00048 static PRLogModuleInfo* gAbOutlookCardLog
00049     = PR_NewLogModule("nsAbOutlookCardLog");
00050 #endif
00051 
00052 #define PRINTF(args) PR_LOG(gAbOutlookCardLog, PR_LOG_DEBUG, args)
00053 
00054 extern const char *kOutlookDirectoryScheme ;
00055 extern const char *kOutlookCardScheme ;
00056 
00057 nsAbOutlookCard::nsAbOutlookCard(void)
00058 : nsRDFResource(), nsAbCardProperty(), mAbWinType(nsAbWinType_Unknown), mMapiData(nsnull)
00059 {
00060     mMapiData = new nsMapiEntry ;
00061 }
00062 
00063 nsAbOutlookCard::~nsAbOutlookCard(void)
00064 {
00065     if (mMapiData) { delete mMapiData ; }
00066 }
00067 
00068 NS_IMPL_ISUPPORTS_INHERITED1(nsAbOutlookCard, nsRDFResource, nsIAbCard)
00069 
00070 static void splitString(nsString& aSource, nsString& aTarget)
00071 {
00072     aTarget.Truncate() ;
00073     PRInt32 offset = aSource.FindChar('\n') ;
00074     
00075     if (offset >= 0) { 
00076         const PRUnichar *source = aSource.get() + offset + 1 ;
00077         
00078         while (*source) {
00079             if (*source == '\n' || *source == '\r') { aTarget.Append(PRUnichar(' ')) ; }
00080             else { aTarget.Append(*source) ; }
00081             ++ source ;
00082         }
00083         aSource.Truncate(offset) ; 
00084     }
00085 }
00086 
00087 static void wordToUnicode(WORD aWord, nsString& aUnicode)
00088 {
00089     aUnicode.Truncate() ;
00090     aUnicode.AppendInt((PRInt32) aWord) ;
00091 }
00092 
00093 enum 
00094 {
00095     index_DisplayName = 0,
00096     index_EmailAddress,
00097     index_FirstName,
00098     index_LastName,
00099     index_NickName,
00100     index_WorkPhoneNumber,
00101     index_HomePhoneNumber,
00102     index_WorkFaxNumber,
00103     index_PagerNumber,
00104     index_MobileNumber,
00105     index_HomeCity,
00106     index_HomeState,
00107     index_HomeZip,
00108     index_HomeCountry,
00109     index_WorkCity,
00110     index_WorkState,
00111     index_WorkZip,
00112     index_WorkCountry,
00113     index_JobTitle,
00114     index_Department,
00115     index_Company,
00116     index_WorkWebPage,
00117     index_HomeWebPage,
00118     index_Comments,
00119     index_LastProp
00120 } ;
00121 
00122 static const ULONG OutlookCardMAPIProps [] = 
00123 {
00124     PR_DISPLAY_NAME_W,
00125     PR_EMAIL_ADDRESS_W,
00126     PR_GIVEN_NAME_W,
00127     PR_SURNAME_W,
00128     PR_NICKNAME_W,
00129     PR_BUSINESS_TELEPHONE_NUMBER_W,
00130     PR_HOME_TELEPHONE_NUMBER_W,
00131     PR_BUSINESS_FAX_NUMBER_W,
00132     PR_PAGER_TELEPHONE_NUMBER_W,
00133     PR_MOBILE_TELEPHONE_NUMBER_W,
00134     PR_HOME_ADDRESS_CITY_W,
00135     PR_HOME_ADDRESS_STATE_OR_PROVINCE_W,
00136     PR_HOME_ADDRESS_POSTAL_CODE_W,
00137     PR_HOME_ADDRESS_COUNTRY_W,
00138     PR_BUSINESS_ADDRESS_CITY_W,
00139     PR_BUSINESS_ADDRESS_STATE_OR_PROVINCE_W,
00140     PR_BUSINESS_ADDRESS_POSTAL_CODE_W,
00141     PR_BUSINESS_ADDRESS_COUNTRY_W,
00142     PR_TITLE_W,
00143     PR_DEPARTMENT_NAME_W,
00144     PR_COMPANY_NAME_W,
00145     PR_BUSINESS_HOME_PAGE_W,
00146     PR_PERSONAL_HOME_PAGE_W,
00147     PR_COMMENT_W
00148 } ;
00149 
00150 nsresult nsAbOutlookCard::Init(const char *aUri)
00151 {
00152     nsresult retCode = nsRDFResource::Init(aUri) ;
00153     
00154     NS_ENSURE_SUCCESS(retCode, retCode) ;
00155     nsCAutoString entry ;
00156     nsCAutoString stub ;
00157 
00158     mAbWinType = getAbWinType(kOutlookCardScheme, mURI.get(), stub, entry) ;
00159     if (mAbWinType == nsAbWinType_Unknown) {
00160         PRINTF(("Huge problem URI=%s.\n", mURI.get())) ;
00161         return NS_ERROR_INVALID_ARG ;
00162     }
00163     nsAbWinHelperGuard mapiAddBook (mAbWinType) ;
00164 
00165     if (!mapiAddBook->IsOK()) { return NS_ERROR_FAILURE ; }
00166     mMapiData->Assign(entry) ;
00167     nsStringArray unichars ;
00168     ULONG i = 0 ;
00169 
00170     if (mapiAddBook->GetPropertiesUString(*mMapiData, OutlookCardMAPIProps, index_LastProp, unichars)) {
00171         SetFirstName(unichars [index_FirstName]->get()) ;
00172         SetLastName(unichars [index_LastName]->get()) ;
00173         SetDisplayName(unichars [index_DisplayName]->get()) ;
00174         SetNickName(unichars [index_NickName]->get()) ;
00175         SetPrimaryEmail(unichars [index_EmailAddress]->get()) ;
00176         SetWorkPhone(unichars [index_WorkPhoneNumber]->get()) ;
00177         SetHomePhone(unichars [index_HomePhoneNumber]->get()) ;
00178         SetFaxNumber(unichars [index_WorkFaxNumber]->get()) ;
00179         SetPagerNumber(unichars [index_PagerNumber]->get()) ;
00180         SetCellularNumber(unichars [index_MobileNumber]->get()) ;
00181         SetHomeCity(unichars [index_HomeCity]->get()) ;
00182         SetHomeState(unichars [index_HomeState]->get()) ;
00183         SetHomeZipCode(unichars [index_HomeZip]->get()) ;
00184         SetHomeCountry(unichars [index_HomeCountry]->get()) ;
00185         SetWorkCity(unichars [index_WorkCity]->get()) ;
00186         SetWorkState(unichars [index_WorkState]->get()) ;
00187         SetWorkZipCode(unichars [index_WorkZip]->get()) ;
00188         SetWorkCountry(unichars [index_WorkCountry]->get()) ;
00189         SetJobTitle(unichars [index_JobTitle]->get()) ;
00190         SetDepartment(unichars [index_Department]->get()) ;
00191         SetCompany(unichars [index_Company]->get()) ;
00192         SetWebPage1(unichars [index_WorkWebPage]->get()) ;
00193         SetWebPage2(unichars [index_HomeWebPage]->get()) ;
00194         SetNotes(unichars [index_Comments]->get()) ;
00195     }
00196     ULONG cardType = 0 ;
00197     nsCAutoString normalChars ;
00198     
00199     if (mapiAddBook->GetPropertyLong(*mMapiData, PR_OBJECT_TYPE, cardType)) {
00200         SetIsMailList(cardType == MAPI_DISTLIST) ;
00201         if (cardType == MAPI_DISTLIST) {
00202             buildAbWinUri(kOutlookDirectoryScheme, mAbWinType, normalChars) ;
00203             normalChars.Append(entry) ;
00204             SetMailListURI(normalChars.get()) ;
00205         }
00206     }
00207     nsAutoString unichar ;
00208     nsAutoString unicharBis ;
00209 
00210     if (mapiAddBook->GetPropertyUString(*mMapiData, PR_HOME_ADDRESS_STREET_W, unichar)) {
00211         splitString(unichar, unicharBis) ;
00212         SetHomeAddress(unichar.get()) ;
00213         SetHomeAddress2(unicharBis.get()) ;
00214     }
00215     if (mapiAddBook->GetPropertyUString(*mMapiData, PR_BUSINESS_ADDRESS_STREET_W, unichar)) {
00216         splitString(unichar, unicharBis) ;
00217         SetWorkAddress(unichar.get()) ;
00218         SetWorkAddress2(unicharBis.get()) ;
00219     }
00220     WORD year = 0 ;
00221     WORD month = 0 ;
00222     WORD day = 0 ;
00223 
00224     if (mapiAddBook->GetPropertyDate(*mMapiData, PR_BIRTHDAY, year, month, day)) {
00225         wordToUnicode(year, unichar) ;
00226         SetBirthYear(unichar.get()) ;
00227         wordToUnicode(month, unichar) ;
00228         SetBirthMonth(unichar.get()) ;
00229         wordToUnicode(day, unichar) ;
00230         SetBirthDay(unichar.get()) ;
00231     }
00232     return retCode ;
00233 }
00234 
00235 static void UnicodeToWord(const PRUnichar *aUnicode, WORD& aWord)
00236 {
00237     aWord = 0 ;
00238     if (aUnicode == nsnull || *aUnicode == 0) { return ; }
00239     PRInt32 errorCode = 0 ;
00240     nsAutoString unichar (aUnicode) ;
00241 
00242     aWord = NS_STATIC_CAST(WORD, unichar.ToInteger(&errorCode)) ;
00243     if (errorCode != 0) {
00244         PRINTF(("Error conversion string %S: %08x.\n", unichar.get(), errorCode)) ;
00245     }
00246 }
00247 
00248 #define PREF_MAIL_ADDR_BOOK_LASTNAMEFIRST "mail.addr_book.lastnamefirst"
00249 
00250 NS_IMETHODIMP nsAbOutlookCard::EditCardToDatabase(const char *aUru)
00251 {
00252     nsresult retCode = NS_OK ;
00253     nsXPIDLString *properties = nsnull ;
00254     nsAutoString utility ;
00255     nsAbWinHelperGuard mapiAddBook (mAbWinType) ;
00256 
00257     if (!mapiAddBook->IsOK()) { return NS_ERROR_FAILURE ; }
00258     // First, all the standard properties in one go
00259     properties = new nsXPIDLString [index_LastProp] ;
00260     if (!properties) {
00261         return NS_ERROR_OUT_OF_MEMORY ;
00262     }
00263     GetFirstName(getter_Copies(properties [index_FirstName])) ;
00264     GetLastName(getter_Copies(properties [index_LastName])) ;
00265     // This triple search for something to put in the name
00266     // is because in the case of a mailing list edition in 
00267     // Mozilla, the display name will not be provided, and 
00268     // MAPI doesn't allow that, so we fall back on an optional
00269     // name, and when all fails, on the email address.
00270     GetDisplayName(getter_Copies(properties [index_DisplayName])) ;
00271     if (*properties [index_DisplayName].get() == 0) {
00272       nsresult rv;
00273       nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
00274       NS_ENSURE_SUCCESS(rv,rv);
00275       
00276       PRInt32 format;
00277       rv = prefBranch->GetIntPref(PREF_MAIL_ADDR_BOOK_LASTNAMEFIRST, &format);
00278       NS_ENSURE_SUCCESS(rv,rv);
00279       
00280       nsCOMPtr<nsIAddrBookSession> abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv);
00281       NS_ENSURE_SUCCESS(rv,rv);
00282       
00283       rv = abSession->GenerateNameFromCard(this, format, getter_Copies(properties [index_DisplayName]));
00284       NS_ENSURE_SUCCESS(rv,rv);
00285       
00286         if (*properties[index_DisplayName].get() == 0) {
00287             GetPrimaryEmail(getter_Copies(properties [index_DisplayName])) ;
00288         }
00289     }
00290     SetDisplayName(properties [index_DisplayName]) ;
00291     GetNickName(getter_Copies(properties [index_NickName])) ;
00292     GetPrimaryEmail(getter_Copies(properties [index_EmailAddress])) ;
00293     GetWorkPhone(getter_Copies(properties [index_WorkPhoneNumber])) ;
00294     GetHomePhone(getter_Copies(properties [index_HomePhoneNumber])) ;
00295     GetFaxNumber(getter_Copies(properties [index_WorkFaxNumber])) ;
00296     GetPagerNumber(getter_Copies(properties [index_PagerNumber])) ;
00297     GetCellularNumber(getter_Copies(properties [index_MobileNumber])) ;
00298     GetHomeCity(getter_Copies(properties [index_HomeCity])) ;
00299     GetHomeState(getter_Copies(properties [index_HomeState])) ;
00300     GetHomeZipCode(getter_Copies(properties [index_HomeZip])) ;
00301     GetHomeCountry(getter_Copies(properties [index_HomeCountry])) ;
00302     GetWorkCity(getter_Copies(properties [index_WorkCity])) ;
00303     GetWorkState(getter_Copies(properties [index_WorkState])) ;
00304     GetWorkZipCode(getter_Copies(properties [index_WorkZip])) ;
00305     GetWorkCountry(getter_Copies(properties [index_WorkCountry])) ;
00306     GetJobTitle(getter_Copies(properties [index_JobTitle])) ;
00307     GetDepartment(getter_Copies(properties [index_Department])) ;
00308     GetCompany(getter_Copies(properties [index_Company])) ;
00309     GetWebPage1(getter_Copies(properties [index_WorkWebPage])) ;
00310     GetWebPage2(getter_Copies(properties [index_HomeWebPage])) ;
00311     GetNotes(getter_Copies(properties [index_Comments])) ;
00312     if (!mapiAddBook->SetPropertiesUString(*mMapiData, OutlookCardMAPIProps, 
00313         index_LastProp, properties)) {
00314         PRINTF(("Cannot set general properties.\n")) ;
00315     }
00316     delete [] properties ;
00317     nsXPIDLString unichar ;
00318     nsXPIDLString unichar2 ;
00319     WORD year = 0 ; 
00320     WORD month = 0 ;
00321     WORD day = 0 ;
00322     
00323     GetHomeAddress(getter_Copies(unichar)) ;
00324     GetHomeAddress2(getter_Copies(unichar2)) ;
00325     utility.Assign(unichar.get()) ;
00326     if (!utility.IsEmpty()) { utility.AppendWithConversion(CRLF) ; }
00327     utility.Append(unichar2.get()) ;
00328     if (!mapiAddBook->SetPropertyUString(*mMapiData, PR_HOME_ADDRESS_STREET_W, utility.get())) {
00329         PRINTF(("Cannot set home address.\n")) ;
00330     }
00331     GetWorkAddress(getter_Copies(unichar)) ;
00332     GetWorkAddress2(getter_Copies(unichar2)) ;
00333     utility.Assign(unichar.get()) ;
00334     if (!utility.IsEmpty()) { utility.AppendWithConversion(CRLF) ; }
00335     utility.Append(unichar2.get()) ;
00336     if (!mapiAddBook->SetPropertyUString(*mMapiData, PR_BUSINESS_ADDRESS_STREET_W, utility.get())) {
00337         PRINTF(("Cannot set work address.\n")) ;
00338     }
00339     GetBirthYear(getter_Copies(unichar)) ;
00340     UnicodeToWord(unichar.get(), year) ;
00341     GetBirthMonth(getter_Copies(unichar)) ;
00342     UnicodeToWord(unichar.get(), month) ;
00343     GetBirthDay(getter_Copies(unichar)) ;
00344     UnicodeToWord(unichar.get(), day) ;
00345     if (!mapiAddBook->SetPropertyDate(*mMapiData, PR_BIRTHDAY, year, month, day)) {
00346         PRINTF(("Cannot set date.\n")) ;
00347     }
00348     return retCode ;
00349 }
00350 
00351 
00352 
00353 
00354 
00355