Back to index

lightning-sunbird  0.9+nobinonly
MozABPCManager.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation
00018  *
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *       Rajiv Dayal <rdayal@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 #include <windows.h>
00040 #include <string.h>
00041 #include <stdio.h>
00042 #include <memory.h>
00043 #include <sys/stat.h>
00044 #include <TCHAR.H>
00045 
00046 #include <syncmgr.h>
00047 #include <Pgenerr.h>
00048 
00049 #include "MozABConduitRecord.h"
00050 #include "MozABPCManager.h"
00051 
00052 const IID IID_IPalmSync = {0xC8CE6FC1,0xCCF1,0x11d6,{0xB8,0xA5,0x00,0x00,0x64,0x65,0x73,0x74}};
00053 const CLSID CLSID_CPalmSyncImp = { 0xb20b4521, 0xccf8, 0x11d6, { 0xb8, 0xa5, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } };
00054 
00055 extern DWORD tId;
00056 
00057 /* static */BOOL MozABPCManager::gUseHomeAddress;
00058 /* static */ BOOL MozABPCManager::gPreferHomePhone;
00059 
00060 
00061 BOOL MozABPCManager::InitMozPalmSyncInstance(IPalmSync **aRetValue)
00062 {
00063     // Check wehther this thread has a valid Interface
00064     // by looking into thread-specific-data variable
00065 
00066     *aRetValue = (IPalmSync *)TlsGetValue(tId);
00067 
00068     // Check whether the pointer actually resolves to
00069     // a valid method call; otherwise mozilla is not running
00070 
00071     if ((*aRetValue) && (*aRetValue)->IsValid() == S_OK)
00072          return TRUE;
00073 
00074     HRESULT hRes = ::CoInitialize(NULL) ;
00075 
00076     hRes = ::CoCreateInstance(CLSID_CPalmSyncImp, NULL, CLSCTX_LOCAL_SERVER,
00077                                          IID_IPalmSync, (LPVOID *)aRetValue);
00078 
00079     if (hRes == S_OK)
00080         if (TlsSetValue(tId, (LPVOID)(*aRetValue)))
00081         {
00082             (*aRetValue)->nsUseABHomeAddressForPalmAddress(&gUseHomeAddress);
00083             (*aRetValue)->nsPreferABHomePhoneForPalmPhone(&gPreferHomePhone);
00084             return TRUE;
00085         }
00086 
00087     // Either CoCreate or TlsSetValue failed; so return FALSE
00088 
00089     if ((*aRetValue))
00090         (*aRetValue)->Release();
00091 
00092     ::CoUninitialize();
00093     return FALSE;
00094 }
00095 
00096 // this function allocates the list as well as the strings, caller should free list and delete strings
00097 long MozABPCManager::GetPCABList(DWORD * pCategoryCount, LONG ** pCategoryIndexList, CPString *** pCategoryNameList, CPString *** pCategoryUrlList, BOOL ** pDirFlags)
00098 {
00099     lpnsMozABDesc mozABNameList=NULL;
00100 
00101     short  dwMozABCount=0;
00102     long retval = 0;
00103     IPalmSync *pNsPalmSync = NULL;
00104 
00105     // get the interface 
00106     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00107         return GEN_ERR_NOT_SUPPORTED;
00108 
00109     // get the ABList
00110     HRESULT hres = pNsPalmSync->nsGetABList(FALSE, &dwMozABCount, 
00111                                         &mozABNameList, pCategoryIndexList, pDirFlags);
00112     if (hres != S_OK) {
00113         retval = (long) hres;
00114         return retval;
00115     }
00116 
00117     *pCategoryCount = dwMozABCount;
00118 
00119     CPString ** abNameList = (CPString **) malloc(sizeof(CPString *) * dwMozABCount);
00120     if (!abNameList) {
00121         free(mozABNameList);
00122         return GEN_ERR_LOW_MEMORY;
00123     }
00124     memset(abNameList, 0, sizeof(CPString *) * dwMozABCount);
00125     *pCategoryNameList = abNameList;
00126 
00127     CPString ** abUrlList = (CPString **) malloc(sizeof(CPString *) * dwMozABCount);
00128     if (!abUrlList) {
00129         free(mozABNameList);
00130         free(abNameList);
00131         return GEN_ERR_LOW_MEMORY;
00132     }
00133     memset(abUrlList, 0, sizeof(CPString *) * dwMozABCount);
00134     *pCategoryUrlList = abUrlList;
00135 
00136     for (int i=0; i < dwMozABCount; i++) {
00137         CPString * pABName = new CPString((LPCTSTR) mozABNameList[i].lpszABName);
00138         if (pABName)
00139             *abNameList = pABName;
00140         else
00141             return GEN_ERR_LOW_MEMORY;
00142         CoTaskMemFree(mozABNameList[i].lpszABName);
00143         abNameList++;
00144 
00145         CPString * pABUrl = new CPString((LPCTSTR) mozABNameList[i].lpszABUrl);
00146         if (pABUrl)
00147             *abUrlList = pABUrl;
00148         else
00149             return GEN_ERR_LOW_MEMORY;
00150         CoTaskMemFree(mozABNameList[i].lpszABUrl);
00151         abUrlList++;
00152     }
00153     
00154     CoTaskMemFree(mozABNameList);
00155     return retval;
00156 }
00157 
00158 // this function allocates the mozlist as well as the mozRecs, caller should free list and delete recs
00159 long MozABPCManager::SynchronizePCAB(LONG categoryIndex, LONG categoryId, CPString & categoryName,
00160                                    DWORD updatedPalmRecCount, CPalmRecord ** updatedPalmRecList,
00161                                    DWORD * pUpdatedPCRecListCount, CPalmRecord *** updatedPCRecList)
00162 {
00163     lpnsABCOMCardStruct mozCardList=NULL;  // freed by MSCOM/Mozilla.
00164     int       dwMozCardCount=0;
00165     long        retval = 0;
00166     IPalmSync     *pNsPalmSync = NULL;
00167 
00168     // get the interface 
00169     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00170         return GEN_ERR_NOT_SUPPORTED;
00171 
00172     CMozABConduitRecord ** tempMozABConduitRecList = new CMozABConduitRecord*[updatedPalmRecCount];
00173     nsABCOMCardStruct * palmCardList = new nsABCOMCardStruct[updatedPalmRecCount];
00174     if(palmCardList)
00175     {
00176         for(DWORD i=0; i<updatedPalmRecCount; i++) 
00177         {
00178             if(*updatedPalmRecList)
00179             {
00180                 CMozABConduitRecord * pConduitRecord = new CMozABConduitRecord(**updatedPalmRecList);
00181                 memcpy(&palmCardList[i], &pConduitRecord->m_nsCard, sizeof(nsABCOMCardStruct));
00182                 tempMozABConduitRecList[i]=pConduitRecord;
00183             }
00184             updatedPalmRecList++;
00185         }
00186         // synchronize and get the updated cards in MozAB
00187         HRESULT hres = pNsPalmSync->nsSynchronizeAB(FALSE, categoryIndex, categoryId, categoryName.GetBuffer(0),
00188                                                 updatedPalmRecCount, palmCardList,
00189                                                 &dwMozCardCount, &mozCardList);
00190         if(hres == S_OK && mozCardList) 
00191         {
00192             *pUpdatedPCRecListCount = dwMozCardCount;
00193             CPalmRecord ** mozRecordList = (CPalmRecord **) malloc(sizeof(CPalmRecord *) * dwMozCardCount);
00194             *updatedPCRecList = mozRecordList;
00195             if (mozRecordList) 
00196             {
00197                 memset(mozRecordList, 0, sizeof(CPalmRecord *) * dwMozCardCount);
00198                 int i=0;
00199                 for (i=0; i < dwMozCardCount; i++) 
00200                 {
00201                     CMozABConduitRecord * pConduitRecord = new CMozABConduitRecord(mozCardList[i]);
00202                     CPalmRecord * pMozRecord = new CPalmRecord;
00203                     pConduitRecord->ConvertToGeneric(*pMozRecord);
00204                     *mozRecordList = pMozRecord;
00205                     mozRecordList++;
00206                     delete pConduitRecord;
00207                     CMozABConduitRecord::CleanUpABCOMCardStruct(&mozCardList[i]);
00208                 }
00209             }
00210             else
00211                 retval = GEN_ERR_LOW_MEMORY;
00212             CoTaskMemFree(mozCardList);
00213         }
00214         else
00215             retval = (long) hres;
00216     }
00217     else
00218         retval = GEN_ERR_LOW_MEMORY;
00219 
00220 
00221         delete palmCardList;
00222     if(tempMozABConduitRecList) 
00223     {
00224         for(DWORD j=0; j<updatedPalmRecCount; j++)
00225             delete tempMozABConduitRecList[j];
00226         delete tempMozABConduitRecList;
00227     }
00228 
00229     return retval;
00230 }
00231 
00232 // this will add all records in a Palm category into a new or existing Mozilla AB 
00233 long MozABPCManager::AddRecords(BOOL replaceExisting, LONG categoryIndex, CPString & categoryName,
00234                                    DWORD updatedPalmRecCount, CPalmRecord ** updatedPalmRecList)
00235 {
00236     long        retval = 0;
00237     IPalmSync     *pNsPalmSync = NULL;
00238 
00239     // get the interface 
00240     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00241         return GEN_ERR_NOT_SUPPORTED;
00242 
00243     CMozABConduitRecord ** tempMozABConduitRecList = new CMozABConduitRecord*[updatedPalmRecCount];
00244     nsABCOMCardStruct * palmCardList = new nsABCOMCardStruct[updatedPalmRecCount];
00245     if(palmCardList) 
00246     {
00247         for(DWORD i=0; i<updatedPalmRecCount; i++) 
00248         {
00249             if(*updatedPalmRecList) 
00250             {
00251                 CMozABConduitRecord * pConduitRecord = new CMozABConduitRecord(**updatedPalmRecList);
00252                 memcpy(&palmCardList[i], &pConduitRecord->m_nsCard, sizeof(nsABCOMCardStruct));
00253                 tempMozABConduitRecList[i]=pConduitRecord;
00254             }
00255             updatedPalmRecList++;
00256         }
00257         // get the ABList
00258         HRESULT hres = pNsPalmSync->nsAddAllABRecords(FALSE, replaceExisting, categoryIndex, categoryName.GetBuffer(0),
00259                                                 updatedPalmRecCount, palmCardList);
00260         if (hres != S_OK)
00261             retval = (long) hres;
00262     }
00263     else
00264         retval = GEN_ERR_LOW_MEMORY;
00265 
00266         delete palmCardList;
00267     if(tempMozABConduitRecList) 
00268     {
00269 
00270         for(DWORD i=0; i<updatedPalmRecCount; i++)
00271             delete tempMozABConduitRecList[i];
00272         delete tempMozABConduitRecList;
00273     }
00274     return retval;
00275 }
00276 
00277 bool MozABPCManager::PCABDeleted(CPString &ABName)
00278 {
00279     IPalmSync     *pNsPalmSync = NULL;
00280     // get the interface 
00281     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00282         return false;
00283     BOOL abDeleted;
00284     HRESULT hres = pNsPalmSync->nsGetABDeleted(ABName.GetBuffer(0), &abDeleted);
00285     return (hres == S_OK) ? abDeleted : false; // assume false;
00286 
00287 }
00288 // this load all records in an Moz AB
00289 // this function allocates the mozlist as well as the mozRecs, caller should free list and delete recs
00290 long MozABPCManager::LoadAllRecords(CPString & ABName, DWORD * pPCRecListCount, CPalmRecord *** pPCRecList)
00291 {
00292     lpnsABCOMCardStruct mozCardList=NULL; // freed by MSCOM/Mozilla.
00293     int       dwMozCardCount=0;
00294     long        retval = 0;
00295     IPalmSync     *pNsPalmSync = NULL;
00296 
00297     // get the interface 
00298     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00299         return GEN_ERR_NOT_SUPPORTED;
00300 
00301     // get the ABList
00302     HRESULT hres = pNsPalmSync->nsGetAllABCards(FALSE, -1, ABName.GetBuffer(0),
00303                                             &dwMozCardCount, &mozCardList);
00304     if (hres == S_OK && mozCardList) {
00305         *pPCRecListCount = dwMozCardCount;
00306         CPalmRecord ** mozRecordList = (CPalmRecord **) malloc(sizeof(CPalmRecord *) * dwMozCardCount);
00307         *pPCRecList = mozRecordList;
00308         if (mozRecordList) {
00309             memset(mozRecordList, 0, sizeof(CPalmRecord *) * dwMozCardCount);
00310             for (int i=0; i < dwMozCardCount; i++) {
00311                 CMozABConduitRecord * pConduitRecord = new CMozABConduitRecord(mozCardList[i]);
00312                 CPalmRecord * pMozRecord = new CPalmRecord;
00313                 pConduitRecord->ConvertToGeneric(*pMozRecord);
00314                 *mozRecordList = pMozRecord;
00315                 mozRecordList++;
00316                 delete pConduitRecord;
00317                 CMozABConduitRecord::CleanUpABCOMCardStruct(&mozCardList[i]);
00318             }
00319         }
00320         else
00321             retval = GEN_ERR_LOW_MEMORY;
00322         CoTaskMemFree(mozCardList);
00323     }
00324     else
00325         retval = (long) hres;
00326     
00327     return retval;
00328 }
00329 
00330 long MozABPCManager::NotifySyncDone(BOOL success, LONG catIndex, DWORD newRecCount, DWORD * newRecIDList)
00331 {
00332     IPalmSync     *pNsPalmSync = NULL;
00333     // get the interface 
00334     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00335         return GEN_ERR_NOT_SUPPORTED;
00336 
00337     // MS COM Proxy stub Dll will not accept NULL for this address
00338     if(!newRecIDList)
00339         newRecIDList = &newRecCount;
00340 
00341     HRESULT hres = pNsPalmSync->nsAckSyncDone(success, catIndex, newRecCount, newRecIDList);
00342     long retval = (long) hres;
00343 
00344     return retval;
00345 }
00346 
00347 long MozABPCManager::UpdatePCABSyncInfo(LONG categoryIndex, CPString & categoryName)
00348 {
00349     IPalmSync     *pNsPalmSync = NULL;
00350     // get the interface 
00351     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00352         return GEN_ERR_NOT_SUPPORTED;
00353 
00354     HRESULT hres = pNsPalmSync->nsUpdateABSyncInfo(FALSE, categoryIndex, categoryName);
00355     long retval = (long) hres;
00356 
00357     return retval;
00358 }
00359 
00360 long MozABPCManager::DeletePCAB(LONG categoryIndex, CPString & categoryName, CPString & categoryUrl)
00361 {
00362     IPalmSync     *pNsPalmSync = NULL;
00363     // get the interface 
00364     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00365         return GEN_ERR_NOT_SUPPORTED;
00366 
00367     HRESULT hres = pNsPalmSync->nsDeleteAB(FALSE, categoryIndex, categoryName, categoryUrl);
00368     long retval = (long) hres;
00369 
00370     return retval;
00371 }
00372 
00373 long MozABPCManager::RenamePCAB(LONG categoryIndex, CPString & categoryName, CPString & categoryUrl)
00374 {
00375     IPalmSync     *pNsPalmSync = NULL;
00376     // get the interface 
00377     if (!InitMozPalmSyncInstance(&pNsPalmSync))
00378         return GEN_ERR_NOT_SUPPORTED;
00379 
00380     HRESULT hres = pNsPalmSync->nsRenameAB(FALSE, categoryIndex, categoryName, categoryUrl);
00381     return (long) hres;
00382 }