Back to index

lightning-sunbird  0.9+nobinonly
nsAbDirProperty.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) 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 "nsAbDirProperty.h"        
00041 #include "nsIRDFService.h"
00042 #include "nsIRDFResource.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsRDFCID.h"
00045 #include "nsXPIDLString.h"
00046 #include "nsReadableUtils.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsAbBaseCID.h"
00049 #include "nsIAbCard.h"
00050 #include "nsDirPrefs.h"
00051 #include "prmem.h"
00052 #include "rdf.h"
00053 
00054 #include "mdb.h"
00055 
00056 nsAbDirProperty::nsAbDirProperty(void)
00057   : m_LastModifiedDate(0)
00058 {
00059        m_IsMailList = PR_FALSE;
00060 }
00061 
00062 nsAbDirProperty::~nsAbDirProperty(void)
00063 {
00064 #if 0
00065   // this code causes a regression #138647
00066   // don't turn it on until you figure it out
00067   if (m_AddressList) {
00068     PRUint32 count;
00069     nsresult rv;
00070     rv = m_AddressList->Count(&count);
00071     NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
00072     PRInt32 i;
00073     for (i = count - 1; i >= 0; i--)
00074       m_AddressList->RemoveElementAt(i);
00075   }
00076 #endif
00077 }
00078 
00079 NS_IMPL_ISUPPORTS1(nsAbDirProperty,nsIAbDirectory)
00080 
00081 NS_IMETHODIMP nsAbDirProperty::GetOperations(PRInt32 *aOperations)
00082 {
00083   // Default is to support all operations.
00084   // Inheriting implementations may override
00085   // to reduce supported operations
00086   NS_ENSURE_ARG_POINTER(aOperations);
00087        *aOperations = nsIAbDirectory::opRead |
00088               nsIAbDirectory::opWrite |
00089               nsIAbDirectory::opSearch;
00090 
00091        return NS_OK;
00092 }
00093 
00094 NS_IMETHODIMP nsAbDirProperty::GetDirName(PRUnichar **aDirName)
00095 {
00096        if (aDirName)
00097        {
00098               *aDirName = ToNewUnicode(m_DirName);
00099               if (!(*aDirName)) 
00100                      return NS_ERROR_OUT_OF_MEMORY;
00101               else
00102                      return NS_OK;
00103        }
00104        else
00105               return NS_ERROR_NULL_POINTER;
00106 }
00107 
00108 NS_IMETHODIMP nsAbDirProperty::SetDirName(const PRUnichar * aDirName)
00109 {
00110        if (aDirName)
00111               m_DirName = aDirName;
00112        return NS_OK;
00113 }
00114 
00115 NS_IMETHODIMP nsAbDirProperty::GetLastModifiedDate(PRUint32 *aLastModifiedDate)
00116 {
00117        if (aLastModifiedDate)
00118        {
00119               *aLastModifiedDate = m_LastModifiedDate;
00120               return NS_OK;
00121        }
00122        else
00123               return NS_RDF_NO_VALUE;
00124 }
00125 
00126 NS_IMETHODIMP nsAbDirProperty::SetLastModifiedDate(PRUint32 aLastModifiedDate)
00127 {
00128        if (aLastModifiedDate)
00129        {
00130               m_LastModifiedDate = aLastModifiedDate;
00131        }
00132        return NS_OK;
00133 }
00134 
00135 nsresult nsAbDirProperty::GetAttributeName(PRUnichar **aName, nsString& value)
00136 {
00137        if (aName)
00138        {
00139               *aName = ToNewUnicode(value);
00140               if (!(*aName)) 
00141                      return NS_ERROR_OUT_OF_MEMORY;
00142               else
00143                      return NS_OK;
00144        }
00145        else
00146               return NS_ERROR_NULL_POINTER;
00147 
00148 }
00149 
00150 nsresult nsAbDirProperty::SetAttributeName(const PRUnichar *aName, nsString& arrtibute)
00151 {
00152        if (aName)
00153               arrtibute = aName;
00154        return NS_OK;
00155 }
00156 
00157 NS_IMETHODIMP nsAbDirProperty::GetListNickName(PRUnichar * *aListNickName)
00158 { return GetAttributeName(aListNickName, m_ListNickName); }
00159 
00160 NS_IMETHODIMP nsAbDirProperty::SetListNickName(const PRUnichar * aListNickName)
00161 { return SetAttributeName(aListNickName, m_ListNickName); }
00162 
00163 NS_IMETHODIMP nsAbDirProperty::GetDescription(PRUnichar * *aDescription)
00164 { return GetAttributeName(aDescription, m_Description); }
00165 
00166 NS_IMETHODIMP nsAbDirProperty::SetDescription(const PRUnichar * aDescription)
00167 { return SetAttributeName(aDescription, m_Description); }
00168 
00169 NS_IMETHODIMP nsAbDirProperty::GetIsMailList(PRBool *aIsMailList)
00170 {
00171        *aIsMailList = m_IsMailList;
00172        return NS_OK;
00173 }
00174 
00175 NS_IMETHODIMP nsAbDirProperty::SetIsMailList(PRBool aIsMailList)
00176 {
00177        m_IsMailList = aIsMailList;
00178        return NS_OK;
00179 }
00180 
00181 NS_IMETHODIMP nsAbDirProperty::GetAddressLists(nsISupportsArray * *aAddressLists)
00182 {
00183        if (!m_AddressList)
00184        {
00185               NS_NewISupportsArray(getter_AddRefs(m_AddressList));
00186        }
00187 
00188        *aAddressLists = m_AddressList;
00189        NS_ADDREF(*aAddressLists);
00190        return NS_OK;
00191 }
00192 
00193 NS_IMETHODIMP nsAbDirProperty::SetAddressLists(nsISupportsArray * aAddressLists)
00194 {
00195        m_AddressList = aAddressLists;
00196        return NS_OK;
00197 }
00198 
00199 NS_IMETHODIMP nsAbDirProperty::CopyMailList(nsIAbDirectory* srcList)
00200 {
00201   nsXPIDLString str;
00202        srcList->GetDirName(getter_Copies(str));
00203        SetDirName(str);
00204        srcList->GetListNickName(getter_Copies(str));
00205        SetListNickName(str);
00206        srcList->GetDescription(getter_Copies(str));
00207        SetDescription(str);
00208 
00209        SetIsMailList(PR_TRUE);
00210 
00211        nsCOMPtr <nsISupportsArray> pAddressLists;
00212        srcList->GetAddressLists(getter_AddRefs(pAddressLists));
00213        SetAddressLists(pAddressLists);
00214        return NS_OK;
00215 }
00216 
00217 // nsIAbDirectory NOT IMPLEMENTED methods
00218 
00219 NS_IMETHODIMP
00220 nsAbDirProperty::GetChildNodes(nsISimpleEnumerator **childList)
00221 { return NS_ERROR_NOT_IMPLEMENTED; }
00222 
00223 NS_IMETHODIMP
00224 nsAbDirProperty::GetChildCards(nsIEnumerator **childCards)
00225 { return NS_ERROR_NOT_IMPLEMENTED; }
00226 
00227 NS_IMETHODIMP
00228 nsAbDirProperty::ModifyDirectory(nsIAbDirectory *directory, nsIAbDirectoryProperties *aProperties)
00229 { return NS_ERROR_NOT_IMPLEMENTED; }
00230 
00231 
00232 NS_IMETHODIMP
00233 nsAbDirProperty::DeleteDirectory(nsIAbDirectory *dierctory)
00234 { return NS_ERROR_NOT_IMPLEMENTED; }
00235 
00236 NS_IMETHODIMP
00237 nsAbDirProperty::DeleteCards(nsISupportsArray *cards)
00238 { return NS_ERROR_NOT_IMPLEMENTED; }
00239 
00240 NS_IMETHODIMP
00241 nsAbDirProperty::HasCard(nsIAbCard *cards, PRBool *hasCard)
00242 { return NS_ERROR_NOT_IMPLEMENTED; }
00243 
00244 NS_IMETHODIMP
00245 nsAbDirProperty::HasDirectory(nsIAbDirectory *dir, PRBool *hasDir)
00246 { return NS_ERROR_NOT_IMPLEMENTED; }
00247 
00248 NS_IMETHODIMP
00249 nsAbDirProperty::CreateNewDirectory(nsIAbDirectoryProperties *aProperties)
00250 { return NS_ERROR_NOT_IMPLEMENTED; }
00251 
00252 NS_IMETHODIMP nsAbDirProperty::CreateDirectoryByURI(const PRUnichar *dirName, const char *uri, PRBool migrating)
00253 { return NS_ERROR_NOT_IMPLEMENTED; }
00254 
00255 NS_IMETHODIMP nsAbDirProperty::AddMailList(nsIAbDirectory *list)
00256 { return NS_ERROR_NOT_IMPLEMENTED; }
00257 
00258 NS_IMETHODIMP nsAbDirProperty::AddMailListWithKey(nsIAbDirectory *list, PRUint32 *key)
00259 { return NS_ERROR_NOT_IMPLEMENTED; }
00260 
00261 NS_IMETHODIMP nsAbDirProperty::EditMailListToDatabase(const char *uri, nsIAbCard *listCard)
00262 { return NS_ERROR_NOT_IMPLEMENTED; }
00263 
00264 NS_IMETHODIMP nsAbDirProperty::AddCard(nsIAbCard *childCard, nsIAbCard **addedCard)
00265 { return NS_ERROR_NOT_IMPLEMENTED; }
00266 
00267 NS_IMETHODIMP nsAbDirProperty::DropCard(nsIAbCard *childCard, PRBool needToCopyCard)
00268 { return NS_ERROR_NOT_IMPLEMENTED; }
00269 
00270 NS_IMETHODIMP nsAbDirProperty::GetValueForCard(nsIAbCard *card, const char *name, PRUnichar **value)
00271 { return NS_ERROR_NOT_IMPLEMENTED; }
00272 
00273 NS_IMETHODIMP nsAbDirProperty::SetValueForCard(nsIAbCard *card, const char *name, const PRUnichar *value)
00274 { return NS_ERROR_NOT_IMPLEMENTED; }
00275 
00276 NS_IMETHODIMP nsAbDirProperty::GetSupportsMailingLists(PRBool *aSupportsMailingsLists)
00277 {
00278   NS_ENSURE_ARG_POINTER(aSupportsMailingsLists);
00279   *aSupportsMailingsLists = PR_TRUE;
00280   return NS_OK;
00281 }
00282 
00283 NS_IMETHODIMP nsAbDirProperty::GetIsRemote(PRBool *aIsRemote)
00284 {
00285   NS_ENSURE_ARG_POINTER(aIsRemote);
00286   *aIsRemote = PR_FALSE;
00287   return NS_OK;
00288 }
00289 
00290 NS_IMETHODIMP nsAbDirProperty::GetIsSecure(PRBool *aIsSecure)
00291 {
00292   NS_ENSURE_ARG_POINTER(aIsSecure);
00293   *aIsSecure = PR_FALSE;
00294   return NS_OK;
00295 }
00296 
00297 NS_IMETHODIMP nsAbDirProperty::GetSearchDuringLocalAutocomplete(PRBool *aSearchDuringLocalAutocomplete)
00298 {
00299   NS_ENSURE_ARG_POINTER(aSearchDuringLocalAutocomplete);
00300   *aSearchDuringLocalAutocomplete = PR_TRUE;
00301   return NS_OK;
00302 }
00303 
00304 NS_IMETHODIMP nsAbDirProperty::GetDirPrefId(nsACString &aDirPrefId)
00305 {
00306   aDirPrefId = m_DirPrefId;
00307   return NS_OK;
00308 }
00309 
00310 NS_IMETHODIMP nsAbDirProperty::SetDirPrefId(const nsACString &aDirPrefId)
00311 {
00312   m_DirPrefId.Assign(aDirPrefId);
00313   return NS_OK;
00314 }
00315 
00316 NS_IMETHODIMP nsAbDirProperty::GetDirectoryProperties(nsIAbDirectoryProperties **aDirectoryProperties)
00317 {
00318   NS_ENSURE_ARG_POINTER(aDirectoryProperties);
00319 
00320   nsresult rv;
00321   DIR_Server *server = (DIR_Server *)PR_Malloc(sizeof(DIR_Server));
00322   if (!server)
00323     return NS_ERROR_OUT_OF_MEMORY;
00324 
00325   DIR_InitServer(server);
00326   nsCAutoString prefId;
00327   rv = GetDirPrefId(prefId);
00328 
00329   if (NS_SUCCEEDED(rv)) {
00330     server->prefName = ToNewCString(prefId);
00331     DIR_GetPrefsForOneServer(server, PR_FALSE, PR_FALSE);
00332 
00333     // Now create the obj and move info in DIR_Server to it.
00334     nsCOMPtr<nsIAbDirectoryProperties> properties = do_CreateInstance(NS_ABDIRECTORYPROPERTIES_CONTRACTID, &rv);
00335 
00336     if (NS_SUCCEEDED(rv)) {
00337       NS_ConvertUTF8toUCS2 description (server->description);
00338 
00339       rv = properties->SetDescription(description);
00340 
00341       if (NS_SUCCEEDED(rv))
00342         rv = properties->SetFileName(server->fileName);
00343 
00344       if (NS_SUCCEEDED(rv))
00345         rv = properties->SetPrefName(server->prefName);
00346 
00347       if (NS_SUCCEEDED(rv))
00348         rv = properties->SetURI(server->uri);
00349 
00350       if (NS_SUCCEEDED(rv))
00351         rv = properties->SetDirType(server->dirType);
00352 
00353       if (NS_SUCCEEDED(rv))
00354         rv = properties->SetMaxHits(server->maxHits);
00355 
00356       if (NS_SUCCEEDED(rv))
00357         rv = properties->SetAuthDn(server->authDn);
00358 
00359       if (NS_SUCCEEDED(rv))
00360         rv = properties->SetCategoryId(server->PalmCategoryId);
00361 
00362       if (NS_SUCCEEDED(rv))
00363         rv = properties->SetSyncTimeStamp(server->PalmSyncTimeStamp);
00364 
00365       if (NS_SUCCEEDED(rv))
00366         rv = properties->SetPosition(server->position);
00367 
00368       if (NS_SUCCEEDED(rv))
00369         NS_ADDREF(*aDirectoryProperties = properties);
00370     }
00371   }
00372 
00373   DIR_DeleteServer(server);
00374   NS_ASSERTION(NS_SUCCEEDED(rv), "nsAbDirProperty::GetDirPrefId failed!");
00375   return rv;
00376 }
00377 
00379 
00380 nsAbDirectoryProperties::nsAbDirectoryProperties(void)
00381 {
00382   mDirType = LDAPDirectory;
00383   mMaxHits = 0;
00384   mSyncTimeStamp = 0;
00385   mCategoryId = -1;
00386   mPosition = 0;
00387 }
00388 
00389 nsAbDirectoryProperties::~nsAbDirectoryProperties(void)
00390 {
00391 }
00392 
00393 NS_IMPL_ISUPPORTS1(nsAbDirectoryProperties,nsIAbDirectoryProperties)
00394 
00395 NS_IMETHODIMP 
00396 nsAbDirectoryProperties::SetDescription(const nsAString &aDescription)
00397 {
00398   mDescription = aDescription;
00399   return NS_OK;
00400 }
00401 
00402 NS_IMETHODIMP 
00403 nsAbDirectoryProperties::GetDescription(nsAString &aDescription)
00404 {
00405   aDescription = mDescription;
00406   return NS_OK;
00407 }
00408 
00409 NS_IMETHODIMP 
00410 nsAbDirectoryProperties::SetURI(const char *aURI)
00411 {
00412   mURI = aURI;
00413   return NS_OK;
00414 }
00415 
00416 NS_IMETHODIMP 
00417 nsAbDirectoryProperties::GetURI(char **aURI)
00418 {
00419   NS_ENSURE_ARG_POINTER(aURI);
00420   *aURI = ToNewCString(mURI);
00421   return NS_OK;
00422 }
00423 
00424 NS_IMETHODIMP 
00425 nsAbDirectoryProperties::SetFileName(const char *aFileName)
00426 {
00427   mFileName = aFileName;
00428   return NS_OK;
00429 }
00430 
00431 NS_IMETHODIMP 
00432 nsAbDirectoryProperties::GetFileName(char **aFileName)
00433 {
00434   NS_ENSURE_ARG_POINTER(aFileName);
00435   *aFileName = ToNewCString(mFileName);
00436   return NS_OK;
00437 }
00438 
00439 NS_IMETHODIMP nsAbDirectoryProperties::SetPrefName(const char *aPrefName)
00440 {
00441   mPrefName = aPrefName;
00442   return NS_OK;
00443 }
00444 
00445 NS_IMETHODIMP nsAbDirectoryProperties::GetPrefName(char **aPrefName)
00446 {
00447   NS_ENSURE_ARG_POINTER(aPrefName);
00448   *aPrefName = ToNewCString(mPrefName);
00449   return NS_OK;
00450 }
00451 
00452 NS_IMETHODIMP nsAbDirectoryProperties::SetDirType(PRUint32 aDirType)
00453 {
00454   mDirType = aDirType;
00455   return NS_OK;
00456 }
00457 
00458 NS_IMETHODIMP nsAbDirectoryProperties::GetDirType(PRUint32 *aDirType)
00459 {
00460   NS_ENSURE_ARG_POINTER(aDirType);
00461   *aDirType = mDirType;
00462   return NS_OK;
00463 }
00464 
00465 NS_IMETHODIMP nsAbDirectoryProperties::SetMaxHits(PRUint32 aMaxHits)
00466 {
00467   mMaxHits = aMaxHits;
00468   return NS_OK;
00469 }
00470 
00471 NS_IMETHODIMP nsAbDirectoryProperties::GetMaxHits(PRUint32 *aMaxHits)
00472 {
00473   NS_ENSURE_ARG_POINTER(aMaxHits);
00474   *aMaxHits = mMaxHits;
00475   return NS_OK;
00476 }
00477 
00478 NS_IMETHODIMP nsAbDirectoryProperties::SetAuthDn(const char *aAuthDn)
00479 {
00480   mAuthDn = aAuthDn;
00481   return NS_OK;
00482 }
00483 
00484 NS_IMETHODIMP nsAbDirectoryProperties::GetAuthDn(char **aAuthDn)
00485 {
00486   NS_ENSURE_ARG_POINTER(aAuthDn);
00487   *aAuthDn = ToNewCString(mAuthDn);
00488   return NS_OK;
00489 }
00490 
00491 NS_IMETHODIMP nsAbDirectoryProperties::SetCategoryId(PRInt32 aCategoryId)
00492 {
00493   mCategoryId = aCategoryId;
00494   return NS_OK;
00495 }
00496 
00497 NS_IMETHODIMP nsAbDirectoryProperties::GetCategoryId(PRInt32 *aCategoryId)
00498 {
00499   NS_ENSURE_ARG_POINTER(aCategoryId);
00500   *aCategoryId = mCategoryId;
00501   return NS_OK;
00502 }
00503 
00504 NS_IMETHODIMP nsAbDirectoryProperties::SetPosition(PRInt32 aPosition)
00505 {
00506   mPosition = aPosition;
00507   return NS_OK;
00508 }
00509 
00510 NS_IMETHODIMP nsAbDirectoryProperties::GetPosition(PRInt32 *aPosition)
00511 {
00512   NS_ENSURE_ARG_POINTER(aPosition);
00513   *aPosition = mPosition;
00514   return NS_OK;
00515 }
00516 
00517 
00518 NS_IMETHODIMP nsAbDirectoryProperties::SetSyncTimeStamp(PRUint32 aSyncTimeStamp)
00519 {
00520   mSyncTimeStamp = aSyncTimeStamp;
00521   return NS_OK;
00522 }
00523 
00524 NS_IMETHODIMP nsAbDirectoryProperties::GetSyncTimeStamp(PRUint32 *aSyncTimeStamp)
00525 {
00526   NS_ENSURE_ARG_POINTER(aSyncTimeStamp);
00527   *aSyncTimeStamp = mSyncTimeStamp;
00528   return NS_OK;
00529 }