Back to index

lightning-sunbird  0.9+nobinonly
nsAddrBookSession.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *   Seth Spitzer <sspitzer@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsCOMPtr.h"
00041 #include "nsAddrBookSession.h"
00042 #include "nsIAddrBookSession.h"
00043 #include "nsIFileSpec.h"
00044 #include "nsILocalFile.h"
00045 #include "nsIDirectoryService.h"
00046 #include "nsAppDirectoryServiceDefs.h"
00047 #include "nsXPIDLString.h"
00048 
00049 NS_IMPL_THREADSAFE_ISUPPORTS1(nsAddrBookSession, nsIAddrBookSession)
00050     
00051 nsAddrBookSession::nsAddrBookSession()
00052 {
00053 }
00054 
00055 nsAddrBookSession::~nsAddrBookSession()
00056 {
00057 }
00058 
00059 
00060 // nsIAddrBookSession
00061 
00062 NS_IMETHODIMP nsAddrBookSession::AddAddressBookListener(nsIAbListener *listener, PRUint32 notifyFlags)
00063 {
00064   if (!mListeners)
00065   {
00066     NS_NewISupportsArray(getter_AddRefs(mListeners));
00067   NS_ENSURE_TRUE(mListeners, NS_ERROR_NULL_POINTER);
00068   }
00069   else if (mListeners->IndexOf(listener) != -1)
00070     return NS_OK;
00071   mListeners->AppendElement(listener);
00072   mListenerNotifyFlags.Add(notifyFlags);
00073   return NS_OK;
00074 }
00075 
00076 NS_IMETHODIMP nsAddrBookSession::RemoveAddressBookListener(nsIAbListener * listener)
00077 {
00078   NS_ENSURE_TRUE(mListeners, NS_ERROR_NULL_POINTER);
00079 
00080   PRInt32 index;
00081   nsresult rv = mListeners->GetIndexOf(listener, &index);
00082   NS_ENSURE_SUCCESS(rv,rv);
00083   NS_ASSERTION(index >= 0, "removing non-existent listener");
00084   if (index >= 0)
00085   {
00086     mListenerNotifyFlags.RemoveAt(index);
00087     mListeners->RemoveElement(listener);
00088   }
00089   return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP nsAddrBookSession::NotifyItemPropertyChanged(nsISupports *item, const char *property, const PRUnichar* oldValue, const PRUnichar* newValue)
00093 {
00094   NS_ENSURE_TRUE(mListeners, NS_ERROR_NULL_POINTER);
00095 
00096   PRUint32 count = 0;
00097   PRUint32 i;
00098   nsresult rv = mListeners->Count(&count);
00099   NS_ENSURE_SUCCESS(rv, rv);
00100   for(i = 0; i < count; i++)
00101   {
00102     if (mListenerNotifyFlags[i] & changed) {
00103       nsCOMPtr<nsIAbListener> listener;
00104       mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
00105       NS_ASSERTION(listener, "listener is null");
00106       if (listener)
00107         listener->OnItemPropertyChanged(item, property, oldValue, newValue);
00108     }
00109   }
00110   return NS_OK;
00111 }
00112 
00113 NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryItemAdded(nsIAbDirectory *directory, nsISupports *item)
00114 {
00115   if (mListeners)
00116   {
00117     PRUint32 count = 0;
00118     PRUint32 i;
00119     nsresult rv = mListeners->Count(&count);
00120     NS_ENSURE_SUCCESS(rv, rv);
00121     for(i = 0; i < count; i++)
00122     {
00123       if (mListenerNotifyFlags[i] & added) {
00124         nsCOMPtr<nsIAbListener> listener;
00125         mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
00126         NS_ASSERTION(listener, "listener is null");
00127         if (listener)
00128           listener->OnItemAdded(directory, item);
00129       }
00130     }
00131   }
00132   return NS_OK;
00133 
00134 }
00135 
00136 NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryItemDeleted(nsIAbDirectory *directory, nsISupports *item)
00137 {
00138   if (mListeners)
00139   {
00140     PRUint32 count = 0;
00141     PRUint32 i;
00142     nsresult rv = mListeners->Count(&count);
00143     NS_ENSURE_SUCCESS(rv, rv);
00144     for(i = 0; i < count; i++)
00145     {
00146       if (mListenerNotifyFlags[i] & directoryItemRemoved) {
00147         nsCOMPtr<nsIAbListener> listener;
00148         mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
00149         NS_ASSERTION(listener, "listener is null");
00150         if (listener)
00151           listener->OnItemRemoved(directory, item);
00152       }
00153     }
00154   }
00155   return NS_OK;
00156 }
00157 
00158 NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryDeleted(nsIAbDirectory *directory, nsISupports *item)
00159 {
00160   if (mListeners)
00161   {
00162     PRUint32 count = 0;
00163     PRUint32 i;
00164     nsresult rv = mListeners->Count(&count);
00165     NS_ENSURE_SUCCESS(rv, rv);
00166     for(i = 0; i < count; i++)
00167     {
00168       if (mListenerNotifyFlags[i] & directoryRemoved) {
00169         nsCOMPtr<nsIAbListener> listener;
00170         mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
00171         NS_ASSERTION(listener, "listener is null");
00172         if (listener)
00173           listener->OnItemRemoved(directory, item);
00174       }
00175     }
00176   }
00177   return NS_OK;
00178 }
00179 
00180 NS_IMETHODIMP nsAddrBookSession::GetUserProfileDirectory(nsILocalFile * *userDir)
00181 {
00182   NS_ENSURE_ARG_POINTER(userDir);
00183   *userDir = nsnull;
00184 
00185   nsresult rv;
00186   nsCOMPtr<nsIFile> profileDir;
00187   nsCAutoString pathBuf;
00188 
00189   rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
00190   NS_ENSURE_SUCCESS(rv, rv);
00191 
00192   rv = profileDir->GetNativePath(pathBuf);
00193   NS_ENSURE_SUCCESS(rv, rv);
00194 
00195   return NS_NewNativeLocalFile(pathBuf, PR_TRUE, userDir);
00196 }
00197 
00198 #define kDisplayName 0
00199 #define kLastFirst   1
00200 #define kFirstLast   2
00201 
00202 NS_IMETHODIMP nsAddrBookSession::GenerateNameFromCard(nsIAbCard *card, PRInt32 generateFormat, PRUnichar **aName)
00203 {
00204   nsresult rv = NS_OK;
00205   
00206   if (generateFormat == kDisplayName) {
00207     rv = card->GetDisplayName(aName);
00208   }
00209   else  {
00210     nsXPIDLString firstName;
00211     nsXPIDLString lastName;
00212     
00213     rv = card->GetFirstName(getter_Copies(firstName));
00214     NS_ENSURE_SUCCESS(rv, rv);       
00215     
00216     rv = card->GetLastName(getter_Copies(lastName));
00217     NS_ENSURE_SUCCESS(rv,rv);
00218     
00219     if (!lastName.IsEmpty() && !firstName.IsEmpty()) {
00220       if (!mBundle) {       
00221         nsCOMPtr<nsIStringBundleService> stringBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); 
00222         NS_ENSURE_SUCCESS(rv,rv);
00223         
00224         rv = stringBundleService->CreateBundle("chrome://messenger/locale/addressbook/addressBook.properties", getter_AddRefs(mBundle));
00225         NS_ENSURE_SUCCESS(rv,rv);
00226       }
00227       
00228       nsXPIDLString generatedName;
00229       
00230       if (generateFormat == kLastFirst) {
00231         const PRUnichar *stringParams[2] = {lastName.get(), firstName.get()};
00232         
00233         rv = mBundle->FormatStringFromName(NS_LITERAL_STRING("lastFirstFormat").get(), stringParams, 2, 
00234           getter_Copies(generatedName));
00235       }
00236       else {
00237         const PRUnichar *stringParams[2] = {firstName.get(), lastName.get()};
00238         
00239         rv = mBundle->FormatStringFromName(NS_LITERAL_STRING("firstLastFormat").get(), stringParams, 2, 
00240           getter_Copies(generatedName));
00241         
00242       }
00243       
00244       NS_ENSURE_SUCCESS(rv,rv); 
00245       *aName = ToNewUnicode(generatedName);
00246     }
00247     else {
00248       if (lastName.Length())
00249         *aName = ToNewUnicode(lastName);
00250       else {
00251         // aName may be empty here, but that's ok.
00252         *aName = ToNewUnicode(firstName);
00253       }
00254     }
00255   }
00256   
00257   if (!*aName || **aName == '\0') 
00258   {
00259     // see bug #211078
00260     // if there is no generated name at this point
00261     // use the userid from the email address
00262     // it is better than nothing.
00263     nsXPIDLString primaryEmail;
00264     card->GetPrimaryEmail(getter_Copies(primaryEmail));
00265     PRInt32 index = primaryEmail.FindChar('@');
00266     if (index != kNotFound)
00267       primaryEmail.Truncate(index);
00268     if (*aName)
00269       nsMemory::Free(*aName);
00270     *aName = ToNewUnicode(primaryEmail);
00271   }
00272 
00273   return NS_OK;
00274 }
00275 
00276 NS_IMETHODIMP nsAddrBookSession::GeneratePhoneticNameFromCard(nsIAbCard *aCard, PRBool aLastNameFirst, PRUnichar **aName)
00277 {
00278   NS_ENSURE_ARG_POINTER(aCard);
00279   NS_ENSURE_ARG_POINTER(aName);
00280 
00281   nsXPIDLString firstName;
00282   nsXPIDLString lastName;
00283   
00284   nsresult rv = aCard->GetPhoneticFirstName(getter_Copies(firstName));
00285   NS_ENSURE_SUCCESS(rv, rv);       
00286 
00287   rv = aCard->GetPhoneticLastName(getter_Copies(lastName));
00288   NS_ENSURE_SUCCESS(rv,rv);
00289 
00290   if (aLastNameFirst)
00291     *aName = ToNewUnicode(lastName + firstName);
00292   else
00293     *aName = ToNewUnicode(firstName + lastName);
00294 
00295   return *aName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00296 }