From 4334fb47d6c397945bdf7e116515e512fac0828c Mon Sep 17 00:00:00 2001 From: "rdayal%netscape.com" Date: Thu, 3 Oct 2002 20:40:24 +0000 Subject: [PATCH] Bug #165247. Implementation of Palm AB HotSync support in Mozilla. NOT yet part of BUILD. - files in palmsync/build are for providing IPC support to get called from Palm Conduit loaded into Palm's HotSync Manager - files in palmsync/public is the XPCOM interface for Palm HotSync support Mozilla module - files in palmsync/src is the implementation of this module, the XPCOM interface implementation, implementation when IPC call is made and the implementation of the actual PalmSync Logic. r=cavin, sr=bienvenu. git-svn-id: svn://10.0.0.236/trunk@131095 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/mailnews/extensions/Makefile.in | 4 + .../mailnews/extensions/palmsync/Makefile.in | 32 + .../extensions/palmsync/build/IPalmSync.idl | 148 +++ .../extensions/palmsync/build/Makefile.in | 77 ++ .../palmsync/build/PalmSyncProxy.def | 47 + .../extensions/palmsync/public/Makefile.in | 36 + .../palmsync/public/nsIPalmSyncSupport.idl | 74 ++ .../extensions/palmsync/src/Makefile.in | 85 ++ .../palmsync/src/PalmSyncFactory.cpp | 118 +++ .../extensions/palmsync/src/PalmSyncFactory.h | 70 ++ .../extensions/palmsync/src/PalmSyncImp.cpp | 262 +++++ .../extensions/palmsync/src/PalmSyncImp.h | 94 ++ .../extensions/palmsync/src/Registry.cpp | 314 ++++++ .../extensions/palmsync/src/Registry.h | 56 ++ .../extensions/palmsync/src/nsAbIPCCard.cpp | 798 ++++++++++++++++ .../extensions/palmsync/src/nsAbIPCCard.h | 100 ++ .../extensions/palmsync/src/nsAbPalmSync.cpp | 902 ++++++++++++++++++ .../extensions/palmsync/src/nsAbPalmSync.h | 133 +++ .../palmsync/src/nsPalmSyncSupport.cpp | 190 ++++ .../palmsync/src/nsPalmSyncSupport.h | 69 ++ 20 files changed, 3609 insertions(+) create mode 100644 mozilla/mailnews/extensions/palmsync/Makefile.in create mode 100644 mozilla/mailnews/extensions/palmsync/build/IPalmSync.idl create mode 100644 mozilla/mailnews/extensions/palmsync/build/Makefile.in create mode 100644 mozilla/mailnews/extensions/palmsync/build/PalmSyncProxy.def create mode 100644 mozilla/mailnews/extensions/palmsync/public/Makefile.in create mode 100644 mozilla/mailnews/extensions/palmsync/public/nsIPalmSyncSupport.idl create mode 100644 mozilla/mailnews/extensions/palmsync/src/Makefile.in create mode 100644 mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.h create mode 100644 mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.h create mode 100644 mozilla/mailnews/extensions/palmsync/src/Registry.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/Registry.h create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.h create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.h create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.cpp create mode 100644 mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.h diff --git a/mozilla/mailnews/extensions/Makefile.in b/mozilla/mailnews/extensions/Makefile.in index 8a6e27a1a92..02b19ad2a38 100644 --- a/mozilla/mailnews/extensions/Makefile.in +++ b/mozilla/mailnews/extensions/Makefile.in @@ -40,5 +40,9 @@ ifdef BUILD_MAIL_SMOKETEST DIRS += smoketest endif +ifdef BUILD_PALMSYNC +DIRS += palmsync +endif + include $(topsrcdir)/config/rules.mk diff --git a/mozilla/mailnews/extensions/palmsync/Makefile.in b/mozilla/mailnews/extensions/palmsync/Makefile.in new file mode 100644 index 00000000000..b1d310402f2 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/Makefile.in @@ -0,0 +1,32 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 2002 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +DIRS = build public src + +include $(topsrcdir)/config/rules.mk + diff --git a/mozilla/mailnews/extensions/palmsync/build/IPalmSync.idl b/mozilla/mailnews/extensions/palmsync/build/IPalmSync.idl new file mode 100644 index 00000000000..4c3c0a83da8 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/build/IPalmSync.idl @@ -0,0 +1,148 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): Rajiv Dayal (rdayal@netscape.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +// This idl will be compiled by MIDL. MS-COM is used +// as brdige between MAPI clients and the Mozilla. + +import "unknwn.idl"; + +// this is defined so that we can return an array of strings(i.e array of AB Description strings) +typedef struct +{ + LPTSTR lpszABDesc; +}nsMozABDesc, *lpnsMozABDesc; + +// This structure defines the data fields for MozAB based on "nsIAbCard.idl" +// This structure is used for interaction with the other AddressBook Apps +// nsIAbCard changes should be reflected here to take care of those changes +// Take care when converting nsIAbCard to this struct +typedef struct +{ + unsigned long dwRecordId; // remote record Id for this card + unsigned long dwCategoryId; // remote category Id for this card + unsigned long dwStatus; // change status for this card + + // standard AB fields for this card + LPTSTR firstName; + LPTSTR lastName; + LPTSTR displayName; + LPTSTR nickName; + LPTSTR primaryEmail; + LPTSTR secondEmail; + LPTSTR workPhone; + LPTSTR homePhone; + LPTSTR faxNumber; + LPTSTR pagerNumber; + LPTSTR cellularNumber; + LPTSTR homeAddress; + LPTSTR homeAddress2; + LPTSTR homeCity; + LPTSTR homeState; + LPTSTR homeZipCode; + LPTSTR homeCountry; + LPTSTR workAddress; + LPTSTR workAddress2; + LPTSTR workCity; + LPTSTR workState; + LPTSTR workZipCode; + LPTSTR workCountry; + LPTSTR jobTitle; + LPTSTR department; + LPTSTR company; + /** + * webPage1 is work web page + */ + LPTSTR webPage1; + /** + * webPage2 is home web page + */ + LPTSTR webPage2; + LPTSTR birthYear; + LPTSTR birthMonth; + LPTSTR birthDay; + LPTSTR custom1; + LPTSTR custom2; + LPTSTR custom3; + LPTSTR custom4; + LPTSTR notes; + unsigned long lastModifiedDate; + + unsigned long preferMailFormat; + boolean isMailList; + /** + * If isMailList is true then mailListURI + * will contain the URI of the associated + * mail list + */ + LPSTR mailListURI; +}nsABCOMCardStruct, *lpnsABCOMCardStruct; + +[ + object, + uuid(C8CE6FC1-CCF1-11d6-B8A5-000064657374), + helpstring("nsIPalmSync Inteface"), + pointer_default(unique) +] + +interface IPalmSync : IUnknown +{ + HRESULT IsValid(); + + // Get the list of Address Books for the currently logged in user profile + HRESULT nsGetABList([in] BOOL aIsUnicode, [out] short * aABListCount, + [out, size_is(, *aABListCount)] lpnsMozABDesc * aABList, + [out, size_is(, *aABListCount)] long ** aABCatIDList, + [out, size_is(, *aABListCount)] BOOL ** aFirstTimeSyncList); + + // Synchronize the Address Book represented by the aCategoryId and the corresponding aABName in Mozilla + // this will take care of ABs existing on both ends. + HRESULT nsSynchronizeAB([in] BOOL aIsUnicode, [in] unsigned long aCategoryId, [in] LPTSTR aABName, + [in] int aModRemoteRecCount, [in, size_is(aModRemoteRecCount)] lpnsABCOMCardStruct aModRemoteRecList, + [out] int * aModMozRecCount, [out, size_is(, *aModMozRecCount)] lpnsABCOMCardStruct * aModMozRecList); + + // All records from a AB represented by aCategoryId and aABName into a new Mozilla AB + HRESULT nsAddAllABRecords([in] BOOL aIsUnicode, [in] unsigned long aCategoryId, [in] LPTSTR aABName, + [in] int aRemoteRecCount, [in, size_is(aRemoteRecCount)] lpnsABCOMCardStruct aRemoteRecList); + + // Get All records from a Mozilla AB + HRESULT nsGetAllABCards([in] BOOL aIsUnicode, [in] unsigned long aCategoryId, [in] LPTSTR aABName, + [out] int * aMozRecCount, [out, size_is(, *aMozRecCount)] lpnsABCOMCardStruct * aMozRecList); + + // Send an ack for done and update the palm rec id for new records added + HRESULT nsAckSyncDone([in] BOOL aIsSuccess, [in] int aCatID, [in] int aNewRecCount, [in, size_is(aNewRecCount)] unsigned long * aNewPalmRecIDList); + +}; + diff --git a/mozilla/mailnews/extensions/palmsync/build/Makefile.in b/mozilla/mailnews/extensions/palmsync/build/Makefile.in new file mode 100644 index 00000000000..2b0fd825d67 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/build/Makefile.in @@ -0,0 +1,77 @@ +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 2002 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = PalmSyncProxy +LIBRARY_NAME = $(MODULE) +DEFFILE = $(srcdir)/PalmSyncProxy.def + +EXPORTS = IPalmSync.h + +CSRCS = \ + dlldata.c \ + IPalmSync_p.c \ + IPalmSync_i.c \ + $(NULL) + +OS_LIBS += rpcrt4.lib + +MIDL_GENERATED_FILES = IPalmSync.h IPalmSync_p.c IPalmSync_i.c dlldata.c + +SRCDIR_CSRCS = $(addprefix $(srcdir)/,$(CSRCS)) + +GARBAGE += $(MIDL_GENERATED_FILES) done_gen $(CSRCS) $(SRCDIR_CSRCS) + +LOCAL_INCLUDES = -I. + +# Force use of PIC +FORCE_USE_PIC = 1 +FORCE_SHARED_LIB = 1 + +include $(topsrcdir)/config/rules.mk + +DEFINES += -DREGISTER_PROXY_DLL -DUNICODE -D_UNICODE + +$(MIDL_GENERATED_FILES): done_gen + +done_gen: IPalmSync.idl + $(RM) $(SRCDIR_CSRCS) + $(MIDL) $(UNICODE_FLAGS) $(srcdir)/IPalmSync.idl + touch $@ + +copy_csrcs: done_gen + @for f in $(CSRCS); do \ + if test ! -f $(srcdir)/$$f; then \ + cp $$f $(srcdir)/$$f; \ + fi ; \ + done + +$(SRCDIR_CSRCS): copy_csrcs + +export:: done_gen copy_csrcs + + diff --git a/mozilla/mailnews/extensions/palmsync/build/PalmSyncProxy.def b/mozilla/mailnews/extensions/palmsync/build/PalmSyncProxy.def new file mode 100644 index 00000000000..7b303d99ff0 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/build/PalmSyncProxy.def @@ -0,0 +1,47 @@ +; ***** BEGIN LICENSE BLOCK ***** +; Version: MPL 1.1/GPL 2.0/LGPL 2.1 +; +; The contents of this file are subject to the Mozilla Public License Version +; 1.1 (the "License"); you may not use this file except in compliance with +; the License. You may obtain a copy of the License at +; http://www.mozilla.org/MPL/ +; +; Software distributed under the License is distributed on an "AS IS" basis, +; WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +; for the specific language governing rights and limitations under the +; License. +; +; The Original Code is Mozilla. +; +; The Initial Developer of the Original Code is +; Netscape Communications Corp. +; Portions created by the Initial Developer are Copyright (C) 2002 +; the Initial Developer. All Rights Reserved. +; +; Contributor(s): Rajiv Dayal (rdayal@netscape.com) +; +; Alternatively, the contents of this file may be used under the terms of +; either the GNU General Public License Version 2 or later (the "GPL"), or +; the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +; in which case the provisions of the GPL or the LGPL are applicable instead +; of those above. If you wish to allow use of your version of this file only +; under the terms of either the GPL or the LGPL, and not to allow others to +; use your version of this file under the terms of the MPL, indicate your +; decision by deleting the provisions above and replace them with the notice +; and other provisions required by the GPL or the LGPL. If you do not delete +; the provisions above, a recipient may use your version of this file under +; the terms of any one of the MPL, the GPL or the LGPL. +; +; ***** END LICENSE BLOCK ***** + +LIBRARY PalmSyncProxy.dll +DESCRIPTION 'Proxy/Stub DLL' + +EXPORTS + DllGetClassObject @1 PRIVATE + DllCanUnloadNow @2 PRIVATE + GetProxyDllInfo @3 PRIVATE + DllRegisterServer @4 PRIVATE + DllUnregisterServer @5 PRIVATE + + diff --git a/mozilla/mailnews/extensions/palmsync/public/Makefile.in b/mozilla/mailnews/extensions/palmsync/public/Makefile.in new file mode 100644 index 00000000000..5ba2d713e54 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/public/Makefile.in @@ -0,0 +1,36 @@ +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 2002 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = palmSync +XPIDL_MODULE = palmSync + +XPIDLSRCS = \ + nsIPalmSyncSupport.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/mailnews/extensions/palmsync/public/nsIPalmSyncSupport.idl b/mozilla/mailnews/extensions/palmsync/public/nsIPalmSyncSupport.idl new file mode 100644 index 00000000000..2e14ef24f6e --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/public/nsIPalmSyncSupport.idl @@ -0,0 +1,74 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + +/** + * This interface provides support for registering Mozilla as a COM component + * for enabling Palm-Mozilla Address Book (and also other when implemented) Synchronization + * + */ + +[noscript, uuid(0071C291-CE82-11d6-B8A6-00B0D06E5F27)] + +interface nsIPalmSyncSupport : nsISupports { + + /** Initiates PalmSync support + */ + void initializePalmSyncSupport(); + + /** Register the PalmSync COM interface for IPC + * This is called only once from the installer + */ + void registerPalmSync(); + + /** Unregister the PalmSync COM interface for IPC + * This is called only once from the installer + */ + void unRegisterPalmSync(); + + /** Shuts down the PalmSync support + */ + void shutdownPalmSyncSupport(); +}; + +%{C++ +#define NS_IPALMSYNCSUPPORT_CONTRACTID "@mozilla.org/palmsyncsupport;1" +#define NS_IPALMSYNCSUPPORT_CLASSNAME "Mozilla PalmSync Support" +%} + + diff --git a/mozilla/mailnews/extensions/palmsync/src/Makefile.in b/mozilla/mailnews/extensions/palmsync/src/Makefile.in new file mode 100644 index 00000000000..d9d60e6e64d --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/Makefile.in @@ -0,0 +1,85 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 2002 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = palmsync +LIBRARY_NAME = $(MODULE) +EXPORT_LIBRARY = 1 +IS_COMPONENT = 1 +META_COMPONENT = mail +REQUIRES = xpcom \ + PalmSyncProxy \ + embedcomponents \ + pref \ + string \ + rdf \ + rdfutil \ + mork \ + unicharutil \ + MapiProxy \ + locale \ + msgbase \ + msgbaseutil \ + addrbook \ + $(NULL) + +CPPSRCS = \ + nsPalmSyncSupport.cpp \ + PalmSyncFactory.cpp \ + PalmSyncImp.cpp \ + Registry.cpp \ + nsAbIPCCard.cpp \ + nsAbPalmSync.cpp \ + $(NULL) + +EXPORTS = \ + nsAbIPCCard.h \ + nsAbPalmSync.h \ + $(NULL) + +LOBJS = ../build/IPalmSync_i.$(OBJ_SUFFIX) + +ifeq ($(USE_SHORT_LIBNAME),1) +EXTRA_DSO_LIBS = msgbsutl +else +EXTRA_DSO_LIBS = msgbaseutil +endif + +EXTRA_DSO_LIBS += addrbook_s + +EXTRA_DSO_LDOPTS= \ + $(EXTRA_DSO_LIBS) \ + $(XPCOM_LIBS) \ + $(MOZ_UNICHARUTIL_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + +OS_LIBS += ole32.lib + +include $(topsrcdir)/config/rules.mk + +DEFINES += -DUNICODE -D_UNICODE diff --git a/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.cpp b/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.cpp new file mode 100644 index 00000000000..0805a320928 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.cpp @@ -0,0 +1,118 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributors: + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +// #undef UNICODE +// #undef _UNICODE + +#include "PalmSyncImp.h" +#include "PalmSyncFactory.h" + +CPalmSyncFactory ::CPalmSyncFactory() +: m_cRef(1) +{ +} + +CPalmSyncFactory::~CPalmSyncFactory() +{ +} + +STDMETHODIMP CPalmSyncFactory::QueryInterface(const IID& aIid, void** aPpv) +{ + if ((aIid == IID_IUnknown) || (aIid == IID_IClassFactory)) + { + *aPpv = static_cast(this); + } + else + { + *aPpv = NULL; + return E_NOINTERFACE; + } + reinterpret_cast(*aPpv)->AddRef(); + return S_OK; +} + +STDMETHODIMP_(ULONG) CPalmSyncFactory::AddRef() +{ + return (PR_AtomicIncrement(&m_cRef)); +} + +STDMETHODIMP_(ULONG) CPalmSyncFactory::Release() +{ + PRInt32 temp; + temp = PR_AtomicDecrement(&m_cRef); + if (m_cRef == 0) + { + delete this; + return 0; + } + + return temp; +} + +STDMETHODIMP CPalmSyncFactory::CreateInstance(IUnknown* aUnknownOuter, + const IID& aIid, + void** aPpv) +{ + // Cannot aggregate. + + if (aUnknownOuter != NULL) + { + return CLASS_E_NOAGGREGATION ; + } + + // Create component. + + CPalmSyncImp* pImp = new CPalmSyncImp(); + if (pImp == NULL) + { + return E_OUTOFMEMORY ; + } + + // Get the requested interface. + HRESULT hr = pImp->QueryInterface(aIid, aPpv); + + // Release the IUnknown pointer. + // (If QueryInterface failed, component will delete itself.) + + pImp->Release(); + return hr; +} + +STDMETHODIMP CPalmSyncFactory::LockServer(PRBool aLock) +{ + return S_OK ; +} diff --git a/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.h b/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.h new file mode 100644 index 00000000000..bb5e6f3690c --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/PalmSyncFactory.h @@ -0,0 +1,70 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributors: + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef MSG_PALMSYNC_FACTORY_H +#define MSG_PALMSYNC_FACTORY_H + +#include +#include +#include "nspr.h" + + +class CPalmSyncFactory : public IClassFactory +{ +public : + + // IUnknown + + STDMETHODIMP QueryInterface (REFIID aIid, void** aPpv); + STDMETHODIMP_(ULONG) AddRef(void); + STDMETHODIMP_(ULONG) Release(void); + + // IClassFactory + + STDMETHODIMP CreateInstance (LPUNKNOWN aUnkOuter, REFIID aIid, void **aPpv); + STDMETHODIMP LockServer (BOOL aLock); + + CPalmSyncFactory (); + ~CPalmSyncFactory (); + +private : + + PRInt32 m_cRef; +}; + +#endif // MSG_PALMSYNC_FACTORY_H + diff --git a/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.cpp b/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.cpp new file mode 100644 index 00000000000..b6dba21a427 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.cpp @@ -0,0 +1,262 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal (rdayal@netscape.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsString.h" +#include "nsCOMPtr.h" +#include "nsISupports.h" + +#include "IPalmSync.h" +#include "PalmSyncImp.h" +#include "PalmSyncFactory.h" + +#include "nsAbPalmSync.h" +#include "nsMsgI18N.h" +#include "nsUnicharUtils.h" + + +CPalmSyncImp::CPalmSyncImp() +: m_cRef(1), + m_PalmHotSync(nsnull), + m_ServerDescList(nsnull), + m_FirstTimeSyncList(nsnull), + m_CatIDList(nsnull) +{ + +} + +CPalmSyncImp::~CPalmSyncImp() +{ + +} + +STDMETHODIMP CPalmSyncImp::QueryInterface(const IID& aIid, void** aPpv) +{ + if (aIid == IID_IUnknown) + { + *aPpv = static_cast(this); + } + else if (aIid == IID_IPalmSync) + { + *aPpv = static_cast(this); + } + else + { + *aPpv = nsnull; + return E_NOINTERFACE; + } + + reinterpret_cast(*aPpv)->AddRef(); + return S_OK; +} + +STDMETHODIMP_(ULONG) CPalmSyncImp::AddRef() +{ + return PR_AtomicIncrement(&m_cRef); +} + +STDMETHODIMP_(ULONG) CPalmSyncImp::Release() +{ + PRInt32 temp; + temp = PR_AtomicDecrement(&m_cRef); + if (m_cRef == 0) + { + delete this; + return 0; + } + + return temp; +} + +STDMETHODIMP CPalmSyncImp::IsValid() +{ + return S_OK; +} + +// Get the list of Address Books for the currently logged in user profile +STDMETHODIMP CPalmSyncImp::nsGetABList(BOOL aIsUnicode, short * aABListCount, + lpnsMozABDesc * aABList, long ** aABCatIDList, BOOL ** aFirstTimeSyncList) +{ + if (!DIR_GetDirectories()) + return E_FAIL; + + PRInt16 count = DIR_GetDirectories()->Count(); + // freed by MSCOM?? + m_ServerDescList = (lpnsMozABDesc) malloc(sizeof(nsMozABDesc) * count); + m_FirstTimeSyncList = (BOOL *) malloc(sizeof(BOOL) * count); + m_CatIDList = (long *) malloc(sizeof(long) * count); + + *aABListCount = count; + *aABList = m_ServerDescList; + *aFirstTimeSyncList = m_FirstTimeSyncList; + *aABCatIDList = m_CatIDList; + + nsresult rv=NS_OK; + for (PRInt16 i = 0; i < count; i++) + { + DIR_Server *server = (DIR_Server *)(DIR_GetDirectories()->ElementAt(i)); + + // if this is a 4.x, local .na2 addressbook (PABDirectory) + // we must skip it. + PRUint32 fileNameLen = strlen(server->fileName); + if (((fileNameLen > kABFileName_PreviousSuffixLen) && + strcmp(server->fileName + fileNameLen - kABFileName_PreviousSuffixLen, kABFileName_PreviousSuffix) == 0) && + (server->dirType == PABDirectory)) + continue; + + if(aIsUnicode) { + // convert to unicode + nsAutoString abName; + nsCAutoString platformCharSet(nsMsgI18NFileSystemCharset()); + rv = ConvertToUnicode(platformCharSet.get(), server->description, abName); + if (NS_FAILED(rv)) + break; + // add to the list + m_ServerDescList->lpszABDesc = (LPTSTR) malloc(sizeof(PRUnichar) * (abName.Length()+1)); + wcscpy(m_ServerDescList->lpszABDesc, abName.get()); + } + else { + // we are just typecasting ascii to unichar and back for the ascii description + m_ServerDescList->lpszABDesc = (LPTSTR) malloc(sizeof(char) * (strlen(server->description)+1)); + strcpy((char*)m_ServerDescList->lpszABDesc, server->description); + } + m_ServerDescList++; + + *m_FirstTimeSyncList = (server->PalmSyncTimeStamp <= 0); + m_FirstTimeSyncList++; + + *m_CatIDList = server->PalmCategoryId; + m_CatIDList++; + } + + // assign member variables to the beginning of the list + m_ServerDescList = *aABList; + m_FirstTimeSyncList = *aFirstTimeSyncList; + m_CatIDList = *aABCatIDList; + + if(NS_FAILED(rv)) + return E_FAIL; + + return S_OK; +} + + +// Synchronize the Address Book represented by the aCategoryId and/or corresponding aABName in Mozilla +STDMETHODIMP CPalmSyncImp::nsSynchronizeAB(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int aModRemoteRecCount, lpnsABCOMCardStruct aModRemoteRecList, + int * aModMozRecCount, lpnsABCOMCardStruct * aModMozRecList) +{ + // let the already continuing sync complete + if(m_PalmHotSync) + return E_FAIL; + + m_PalmHotSync = (nsAbPalmHotSync *) new nsAbPalmHotSync(aIsUnicode, aABName, (char*)aABName, aCategoryId); + if(!m_PalmHotSync) + return E_FAIL; + + nsresult rv = ((nsAbPalmHotSync *)m_PalmHotSync)->Initialize(); + if (NS_SUCCEEDED(rv)) + rv = ((nsAbPalmHotSync *)m_PalmHotSync)->DoSyncAndGetUpdatedCards(aModRemoteRecCount, aModRemoteRecList, aModMozRecCount, aModMozRecList); + + if (NS_FAILED(rv)) + return E_FAIL; + + return S_OK; +} + +// All records from a AB represented by aCategoryId and aABName into a new Mozilla AB +STDMETHODIMP CPalmSyncImp::nsAddAllABRecords(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int aRemoteRecCount, lpnsABCOMCardStruct aRemoteRecList) +{ + // since we are not returning any data we donot need to keep the nsAbPalmHotSync reference + // in order to free the returned data in its destructor. Just create a local nsAbPalmHotSync var. + nsAbPalmHotSync palmHotSync(aIsUnicode, aABName, (char*)aABName, aCategoryId); + + nsresult rv = palmHotSync.AddAllRecordsInNewAB(aRemoteRecCount, aRemoteRecList); + + if (NS_FAILED(rv)) + return E_FAIL; + + return S_OK; +} + +// Get All records from a Mozilla AB +STDMETHODIMP CPalmSyncImp::nsGetAllABCards(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int * aMozRecCount, lpnsABCOMCardStruct * aMozRecList) +{ + // if sync is already going on wait for AckSyncDone to be called + if(m_PalmHotSync) + return E_FAIL; + + m_PalmHotSync = (nsAbPalmHotSync *) new nsAbPalmHotSync(aIsUnicode, aABName, (char*)aABName, aCategoryId); + if(!m_PalmHotSync) + return E_FAIL; + + nsresult rv = ((nsAbPalmHotSync *)m_PalmHotSync)->Initialize(); + if (NS_SUCCEEDED(rv)) + rv = ((nsAbPalmHotSync *)m_PalmHotSync)->GetAllCards(aMozRecCount, aMozRecList); + + if (NS_FAILED(rv)) + return E_FAIL; + + return S_OK; +} + +// Send an ack for sync done on the remote AB to Mozilla, +// now we delete the data returned to the calling app. +STDMETHODIMP CPalmSyncImp::nsAckSyncDone(BOOL aIsSuccess, int aCatID, int aNewRecCount, unsigned long * aNewPalmRecIDList) +{ + nsAbPalmHotSync * tempPalmHotSync = (nsAbPalmHotSync *) m_PalmHotSync; + if(tempPalmHotSync) { + if(aNewRecCount && (aCatID > -1)) + tempPalmHotSync->Done(aIsSuccess, aCatID, aNewRecCount, aNewPalmRecIDList); + delete tempPalmHotSync; + } + m_PalmHotSync = nsnull; + + free(m_ServerDescList); + m_ServerDescList = nsnull; + + free(m_FirstTimeSyncList); + m_FirstTimeSyncList = nsnull; + + free(m_CatIDList); + m_CatIDList = nsnull; + + return S_OK; +} + + diff --git a/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.h b/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.h new file mode 100644 index 00000000000..62787201960 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/PalmSyncImp.h @@ -0,0 +1,94 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal (rdayal@netscape.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef PALMSYNC_IMP_H +#define PALMSYNC_IMP_H + +#include +#include "IPalmSync.h" +#include "nspr.h" + +const CLSID CLSID_CPalmSyncImp = { 0xb20b4521, 0xccf8, 0x11d6, { 0xb8, 0xa5, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }; + +// this class implements the MS COM interface IPalmSync that provides the methods +// called by the Palm Conduit to perform the AB Sync operations. +class CPalmSyncImp : public IPalmSync +{ + +public : + + // IUnknown + + STDMETHODIMP QueryInterface(const IID& aIid, void** aPpv); + STDMETHODIMP_(ULONG) AddRef(); + STDMETHODIMP_(ULONG) Release(); + + // Interface IPalmSync + + STDMETHODIMP IsValid(); + + CPalmSyncImp(); + ~CPalmSyncImp(); + + // Get the list of Address Books for the currently logged in user profile + STDMETHODIMP nsGetABList(BOOL aIsUnicode, short * aABListCount, + lpnsMozABDesc * aABList, long ** aABCatIDList, BOOL ** aFirstTimeSyncList); + + // Synchronize the Address Book represented by the aCategoryId and/or corresponding aABName in Mozilla + STDMETHODIMP nsSynchronizeAB(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int aModRemoteRecCount, lpnsABCOMCardStruct aModRemoteRecList, + int * aModMozRecCount, lpnsABCOMCardStruct * aModMozRecList); + + STDMETHODIMP nsAddAllABRecords(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int aRemoteRecCount, lpnsABCOMCardStruct aRemoteRecList); + + + STDMETHODIMP nsGetAllABCards(BOOL aIsUnicode, unsigned long aCategoryId, LPTSTR aABName, + int * aMozRecCount, lpnsABCOMCardStruct * aMozRecList); + + STDMETHODIMP nsAckSyncDone(BOOL aIsSuccess, int aCatID, int aNewRecCount, unsigned long * aNewPalmRecIDList); + +private : + PRInt32 m_cRef; + + void * m_PalmHotSync; + lpnsMozABDesc m_ServerDescList; + BOOL * m_FirstTimeSyncList; + long * m_CatIDList; +}; + +#endif // MSG_MAPI_IMP_H diff --git a/mozilla/mailnews/extensions/palmsync/src/Registry.cpp b/mozilla/mailnews/extensions/palmsync/src/Registry.cpp new file mode 100644 index 00000000000..3a58e6f1c25 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/Registry.cpp @@ -0,0 +1,314 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributors: + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#undef _UNICODE +#undef UNICODE + +#include +#include "nsString.h" +#include "Registry.h" + +#define PALMSYNC_PROXY_DLL_NAME "PalmSyncProxy.dll" +#define PALMSYNC_STARTUP_ARG " -PalmSyncStartup" +#define MAX_SIZE 2048 + +// Size of a CLSID as a string +const int CLSID_STRING_SIZE = 39; + +// Proxy/Stub Dll Routines + +typedef HRESULT (__stdcall ProxyServer)(); + + +// Convert a CLSID to a char string. + +BOOL CLSIDtochar(const CLSID& clsid, char* szCLSID, + int length) +{ + LPOLESTR wszCLSID = NULL; + + // Get CLSID + HRESULT hr = StringFromCLSID(clsid, &wszCLSID); + if (FAILED(hr)) + return FALSE; + + // Covert from wide characters to non-wide. + wcstombs(szCLSID, wszCLSID, length); + + // Free memory. + CoTaskMemFree(wszCLSID); + + return TRUE; +} + +// Create a key and set its value. + +BOOL setKeyAndValue(nsCAutoString keyName, const char* subKey, + const char* theValue) +{ + HKEY hKey; + BOOL retValue = TRUE; + + nsCAutoString theKey(keyName); + if (subKey != NULL) + { + theKey += "\\"; + theKey += subKey; + } + + // Create and open key and subkey. + long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT, theKey.get(), + 0, NULL, REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, NULL, &hKey, NULL); + if (lResult != ERROR_SUCCESS) + return FALSE ; + + // Set the Value. + if (theValue != NULL) + { + lResult = RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)theValue, + strlen(theValue)+1); + if (lResult != ERROR_SUCCESS) + retValue = FALSE; + } + + RegCloseKey(hKey); + return TRUE; +} + +// Delete a key and all of its descendents. + +LONG recursiveDeleteKey(HKEY hKeyParent, // Parent of key to delete + const char* lpszKeyChild) // Key to delete +{ + // Open the child. + HKEY hKeyChild ; + LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, + KEY_ALL_ACCESS, &hKeyChild) ; + if (lRes != ERROR_SUCCESS) + { + return lRes ; + } + + // Enumerate all of the decendents of this child. + FILETIME time ; + char szBuffer[MAX_SIZE] ; + DWORD dwSize = MAX_SIZE ; + while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL, + NULL, NULL, &time) == S_OK) + { + // Delete the decendents of this child. + lRes = recursiveDeleteKey(hKeyChild, szBuffer) ; + if (lRes != ERROR_SUCCESS) + { + // Cleanup before exiting. + RegCloseKey(hKeyChild) ; + return lRes; + } + dwSize = MAX_SIZE; + } + + // Close the child. + RegCloseKey(hKeyChild) ; + + // Delete this child. + return RegDeleteKey(hKeyParent, lpszKeyChild) ; +} + +void RegisterProxy() +{ + HINSTANCE h = NULL; + ProxyServer *RegisterFunc = NULL; + + char szModule[MAX_SIZE]; + char *pTemp = NULL; + + HMODULE hModule = GetModuleHandle(NULL); + DWORD dwResult = ::GetModuleFileName(hModule, szModule, + sizeof(szModule)/sizeof(char)); + if (dwResult == 0) + return; + + pTemp = strrchr(szModule, '\\'); + if (pTemp == NULL) + return; + + *pTemp = '\0'; + nsCAutoString proxyPath(szModule); + + proxyPath += "\\"; + proxyPath += PALMSYNC_PROXY_DLL_NAME; + + h = LoadLibrary(proxyPath.get()); + if (h == NULL) + return; + + RegisterFunc = (ProxyServer *) GetProcAddress(h, "DllRegisterServer"); + if (RegisterFunc) + RegisterFunc(); + + FreeLibrary(h); +} + +void UnRegisterProxy() +{ + HINSTANCE h = NULL; + ProxyServer *UnRegisterFunc = NULL; + + char szModule[MAX_SIZE]; + char *pTemp = NULL; + + HMODULE hModule = GetModuleHandle(NULL); + DWORD dwResult = ::GetModuleFileName(hModule, szModule, + sizeof(szModule)/sizeof(char)); + if (dwResult == 0) + return; + + pTemp = strrchr(szModule, '\\'); + if (pTemp == NULL) + return; + + *pTemp = '\0'; + nsCAutoString proxyPath(szModule); + + proxyPath += "\\"; + proxyPath += PALMSYNC_PROXY_DLL_NAME; + + h = LoadLibrary(proxyPath.get()); + if (h == NULL) + return; + + UnRegisterFunc = (ProxyServer *) GetProcAddress(h, "DllUnregisterServer"); + if (UnRegisterFunc) + UnRegisterFunc(); + + FreeLibrary(h); +} + +// Register the component in the registry. + +HRESULT RegisterServerForPalmSync(const CLSID& clsid, // Class ID + const char* szFriendlyName, // Friendly Name + const char* szVerIndProgID, // Programmatic + const char* szProgID) // IDs +{ + HMODULE hModule = GetModuleHandle(NULL); + char szModuleName[MAX_SIZE]; + char szCLSID[CLSID_STRING_SIZE]; + + nsCAutoString independentProgId(szVerIndProgID); + nsCAutoString progId(szProgID); + + MessageBox(NULL, "In Register Server", "PalmSync", MB_OK); + + DWORD dwResult = ::GetModuleFileName(hModule, szModuleName, + sizeof(szModuleName)/sizeof(char)); + + if (dwResult == 0) + return S_FALSE; + + nsCAutoString moduleName(szModuleName); + nsCAutoString registryKey("CLSID\\"); + + moduleName += PALMSYNC_STARTUP_ARG; + + // Convert the CLSID into a char. + + if (!CLSIDtochar(clsid, szCLSID, sizeof(szCLSID))) + return S_FALSE; + registryKey += szCLSID; + + // Add the CLSID to the registry. + if((!setKeyAndValue(registryKey, NULL, szFriendlyName)) + || (!setKeyAndValue(registryKey, "LocalServer32", moduleName.get())) + // Add the ProgID subkey under the CLSID key. + || (!setKeyAndValue(registryKey, "ProgID", szProgID)) + // Add the version-independent ProgID subkey under CLSID key. + || (!setKeyAndValue(registryKey, "VersionIndependentProgID", szVerIndProgID)) + // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT. + || (!setKeyAndValue(independentProgId, NULL, szFriendlyName)) + || (!setKeyAndValue(independentProgId, "CLSID", szCLSID)) + || (!setKeyAndValue(independentProgId, "CurVer", szProgID)) + // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT. + || (!setKeyAndValue(progId, NULL, szFriendlyName)) + || (!setKeyAndValue(progId, "CLSID", szCLSID)) + ) + return S_FALSE; + + RegisterProxy(); + + return S_OK; +} + +LONG UnregisterServerForPalmSync(const CLSID& clsid, // Class ID + const char* szVerIndProgID, // Programmatic + const char* szProgID) // IDs +{ + LONG lResult = S_OK; + + // Convert the CLSID into a char. + + char szCLSID[CLSID_STRING_SIZE]; + if (!CLSIDtochar(clsid, szCLSID, sizeof(szCLSID))) + return S_FALSE; + + UnRegisterProxy(); + + nsCAutoString registryKey("CLSID\\"); + registryKey += szCLSID; + + lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, registryKey.get()); + if (lResult == ERROR_SUCCESS || lResult == ERROR_FILE_NOT_FOUND) + return lResult; + + registryKey += "\\LocalServer32"; + + // Delete only the path for this server. + + lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, registryKey.get()); + if (lResult != ERROR_SUCCESS && lResult != ERROR_FILE_NOT_FOUND) + return lResult; + + // Delete the version-independent ProgID Key. + lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID); + if (lResult != ERROR_SUCCESS && lResult != ERROR_FILE_NOT_FOUND) + return lResult; + + lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID); + + return lResult; +} diff --git a/mozilla/mailnews/extensions/palmsync/src/Registry.h b/mozilla/mailnews/extensions/palmsync/src/Registry.h new file mode 100644 index 00000000000..e8303913c3e --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/Registry.h @@ -0,0 +1,56 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2001 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _REGISTRY_H_ +#define _REGISTRY_H_ + +#include + +// This function will register a component in the Registry. + +HRESULT RegisterServerForPalmSync(const CLSID& clsid, + const char* szFriendlyName, + const char* szVerIndProgID, + const char* szProgID) ; + +// This function will unregister a component. + +HRESULT UnregisterServerForPalmSync(const CLSID& clsid, + const char* szVerIndProgID, + const char* szProgID) ; + +#endif diff --git a/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.cpp b/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.cpp new file mode 100644 index 00000000000..c747968cba1 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.cpp @@ -0,0 +1,798 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Created by: Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include + +#include "nsAbIPCCard.h" +#include "nsUnicharUtils.h" +#include "nsAbMDBCard.h" +#include "nsAddrDatabase.h" +#include "prdtoa.h" + +NS_IMPL_ISUPPORTS_INHERITED1(nsAbIPCCard, nsAbCardProperty, nsIAbMDBCard) + +nsAbIPCCard::nsAbIPCCard() +{ + mRecordId = 0; + mCategoryId = -1; + mStatus = -1; +} + +nsAbIPCCard::~nsAbIPCCard() +{ + +} + +nsAbIPCCard::nsAbIPCCard(nsIAbCard *card) +{ + Copy(card); +} + +nsAbIPCCard::nsAbIPCCard(nsABCOMCardStruct *card, PRBool isUnicode) +{ + if(isUnicode) + Copy(card); + else + ConvertToUnicodeAndCopy(card); +} + +NS_IMETHODIMP nsAbIPCCard::Copy(nsIAbCard *srcCard) +{ + NS_ENSURE_ARG_POINTER(srcCard); + + nsresult rv = NS_OK; + nsCOMPtr dbCard; + dbCard = do_QueryInterface(srcCard, &rv); + if(NS_SUCCEEDED(rv) && dbCard) { + nsXPIDLString palmIDStr; + nsresult rv = dbCard->GetStringAttribute(CARD_ATTRIB_PALMID, getter_Copies(palmIDStr)); + if(NS_SUCCEEDED(rv) && palmIDStr.get()) { + PRFloat64 f = PR_strtod(NS_LossyConvertUCS2toASCII(palmIDStr).get(), nsnull); + PRInt64 l; + LL_D2L(l, f); + LL_L2UI(mRecordId, l); + } + else + mRecordId = 0; + // set tableID, RowID and Key for the card + PRUint32 tableID=0; + dbCard->GetDbTableID(&tableID); + SetDbTableID(tableID); + PRUint32 rowID=0; + dbCard->GetDbRowID(&rowID); + SetDbRowID(rowID); + PRUint32 key; + dbCard->GetKey(&key); + SetKey(key); + } + PRUint32 lastModifiedDate = 0; + srcCard->GetLastModifiedDate(&lastModifiedDate); + mStatus = (lastModifiedDate) ? ATTR_MODIFIED : ATTR_NEW; + + return nsAbCardProperty::Copy(srcCard); +} + +nsresult nsAbIPCCard::Copy(nsABCOMCardStruct * srcCard) +{ + NS_ENSURE_ARG_POINTER(srcCard); + + mRecordId = srcCard->dwRecordId; + mCategoryId = srcCard->dwCategoryId; + mStatus = srcCard->dwStatus; + + SetFirstName(srcCard->firstName); + SetLastName(srcCard->lastName); + SetDisplayName(srcCard->displayName); + SetNickName(srcCard->nickName); + SetPrimaryEmail(srcCard->primaryEmail); + SetSecondEmail(srcCard->secondEmail); + SetPreferMailFormat(srcCard->preferMailFormat); + SetWorkPhone(srcCard->workPhone); + SetHomePhone(srcCard->homePhone); + SetFaxNumber(srcCard->faxNumber); + SetPagerNumber(srcCard->pagerNumber); + SetCellularNumber(srcCard->cellularNumber); + SetHomeAddress(srcCard->homeAddress); + SetHomeAddress2(srcCard->homeAddress2); + SetHomeCity(srcCard->homeCity); + SetHomeState(srcCard->homeState); + SetHomeZipCode(srcCard->homeZipCode); + SetHomeCountry(srcCard->homeCountry); + SetWorkAddress(srcCard->workAddress); + SetWorkAddress2(srcCard->workAddress2); + SetWorkCity(srcCard->workCity); + SetWorkState(srcCard->workState); + SetWorkZipCode(srcCard->workZipCode); + SetWorkCountry(srcCard->workCountry); + SetJobTitle(srcCard->jobTitle); + SetDepartment(srcCard->department); + SetCompany(srcCard->company); + SetWebPage1(srcCard->webPage1); + SetWebPage2(srcCard->webPage2); + SetBirthYear(srcCard->birthYear); + SetBirthMonth(srcCard->birthMonth); + SetBirthDay(srcCard->birthDay); + SetCustom1(srcCard->custom1); + SetCustom2(srcCard->custom2); + SetCustom3(srcCard->custom3); + SetCustom4(srcCard->custom4); + SetNotes(srcCard->notes); + SetLastModifiedDate(srcCard->lastModifiedDate); + SetIsMailList(srcCard->isMailList); + SetMailListURI(srcCard->mailListURI); + + return NS_OK; +} + +#define CONVERT_ASSIGNTO_UNICODE(d, s) d.SetLength(0); if((char*) s) d=NS_ConvertUTF8toUCS2((char*)s); + +nsresult nsAbIPCCard::ConvertToUnicodeAndCopy(nsABCOMCardStruct * srcCard) +{ + NS_ENSURE_ARG_POINTER(srcCard); + + mRecordId = srcCard->dwRecordId; + mCategoryId = srcCard->dwCategoryId; + mStatus = srcCard->dwStatus; + + nsAutoString str; + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->firstName); + SetFirstName(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->lastName); + SetLastName(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->displayName); + SetDisplayName(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->nickName); + SetNickName(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->primaryEmail); + SetPrimaryEmail(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->secondEmail); + SetSecondEmail(str.get()); + + SetPreferMailFormat(srcCard->preferMailFormat); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workPhone); + SetWorkPhone(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homePhone); + SetHomePhone(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->faxNumber); + SetFaxNumber(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->pagerNumber); + SetPagerNumber(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->cellularNumber); + SetCellularNumber(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeAddress); + SetHomeAddress(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeAddress2); + SetHomeAddress2(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeCity); + SetHomeCity(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeState); + SetHomeState(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeZipCode); + SetHomeZipCode(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->homeCountry); + SetHomeCountry(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workAddress); + SetWorkAddress(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workAddress2); + SetWorkAddress2(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workCity); + SetWorkCity(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workState); + SetWorkState(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workZipCode); + SetWorkZipCode(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->workCountry); + SetWorkCountry(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->jobTitle); + SetJobTitle(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->department); + SetDepartment(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->company); + SetCompany(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->webPage1); + SetWebPage1(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->webPage2); + SetWebPage2(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->birthYear); + SetBirthYear(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->birthMonth); + SetBirthMonth(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->birthDay); + SetBirthDay(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->custom1); + SetCustom1(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->custom2); + SetCustom2(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->custom3); + SetCustom3(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->custom4); + SetCustom4(str.get()); + + CONVERT_ASSIGNTO_UNICODE(str, srcCard->notes); + SetNotes(str.get()); + + SetLastModifiedDate(srcCard->lastModifiedDate); + SetIsMailList(srcCard->isMailList); + SetMailListURI(srcCard->mailListURI); + + return NS_OK; +} + + +PRBool nsAbIPCCard::EqualsAfterUnicodeConversion(nsABCOMCardStruct * card, nsStringArray & differingAttrs) +{ + if(!card) + return PR_FALSE; + + // convert to Unicode first + nsAbIPCCard card1(card, PR_FALSE); + nsABCOMCardStruct * newCard = new nsABCOMCardStruct; + // get the unicode nsABCOMCardStruct and compare + card1.GetABCOMCardStruct(PR_TRUE, newCard); + PRBool ret = Equals(newCard, differingAttrs); + delete newCard; + return ret; +} + + +PRBool nsAbIPCCard::Equals(nsABCOMCardStruct * card, nsStringArray & differingAttrs) +{ + if(!card) + return PR_FALSE; + + differingAttrs.Clear(); + + if(card->firstName) + if (Compare(nsDependentString(card->firstName), m_FirstName, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kFirstNameColumn)); + if(card->lastName) + if (Compare(nsDependentString(card->lastName), m_LastName, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kLastNameColumn)); + if(card->displayName) + if (Compare(nsDependentString(card->displayName), m_DisplayName, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kDisplayNameColumn)); + if(card->nickName) + if (Compare(nsDependentString(card->nickName), m_NickName, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kNicknameColumn)); + if(card->primaryEmail) + if (Compare(nsDependentString(card->primaryEmail), m_PrimaryEmail, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kPriEmailColumn)); + if(card->secondEmail) + if (Compare(nsDependentString(card->secondEmail), m_SecondEmail, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(k2ndEmailColumn)); + if(card->workPhone) + if (Compare(nsDependentString(card->workPhone), m_WorkPhone, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkPhoneColumn)); + if(card->homePhone) + if (Compare(nsDependentString(card->homePhone), m_HomePhone, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomePhoneColumn)); + if(card->faxNumber) + if (Compare(nsDependentString(card->faxNumber), m_FaxNumber, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kFaxColumn)); + if(card->pagerNumber) + if (Compare(nsDependentString(card->pagerNumber), m_PagerNumber, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kPagerColumn)); + if(card->cellularNumber) + if (Compare(nsDependentString(card->cellularNumber), m_CellularNumber, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCellularColumn)); + if(card->homeAddress) + if (Compare(nsDependentString(card->homeAddress), m_HomeAddress, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeAddressColumn)); + if(card->homeAddress2) + if (Compare(nsDependentString(card->homeAddress2), m_HomeAddress2, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeAddress2Column)); + if(card->homeCity) + if (Compare(nsDependentString(card->homeCity), m_HomeCity, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeCityColumn)); + if(card->homeState) + if (Compare(nsDependentString(card->homeState), m_HomeState, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeStateColumn)); + if(card->homeZipCode) + if (Compare(nsDependentString(card->homeZipCode), m_HomeZipCode, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeZipCodeColumn)); + if(card->homeCountry) + if (Compare(nsDependentString(card->homeCountry), m_HomeCountry, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kHomeCountryColumn)); + if(card->workAddress) + if (Compare(nsDependentString(card->workAddress), m_WorkAddress, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkAddressColumn)); + if(card->workAddress2) + if (Compare(nsDependentString(card->workAddress2), m_WorkAddress2, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkAddress2Column)); + if(card->workCity) + if (Compare(nsDependentString(card->workCity), m_WorkCity, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkCityColumn)); + if(card->workState) + if (Compare(nsDependentString(card->workState), m_WorkState, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkStateColumn)); + if(card->workZipCode) + if (Compare(nsDependentString(card->workZipCode), m_WorkZipCode, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkZipCodeColumn)); + if(card->workCountry) + if (Compare(nsDependentString(card->workCountry), m_WorkCountry, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWorkCountryColumn)); + if(card->jobTitle) + if (Compare(nsDependentString(card->jobTitle), m_JobTitle, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kJobTitleColumn)); + if(card->department) + if (Compare(nsDependentString(card->department), m_Department, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kDepartmentColumn)); + if(card->company) + if (Compare(nsDependentString(card->company), m_Company, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCompanyColumn)); + if(card->webPage1) + if (Compare(nsDependentString(card->webPage1), m_WebPage1, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWebPage1Column)); + if(card->webPage2) + if (Compare(nsDependentString(card->webPage2), m_WebPage2, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kWebPage2Column)); + if(card->birthYear) + if (Compare(nsDependentString(card->birthYear), m_BirthYear, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kBirthYearColumn)); + if(card->birthMonth) + if (Compare(nsDependentString(card->birthMonth), m_BirthMonth, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kBirthMonthColumn)); + if(card->birthDay) + if (Compare(nsDependentString(card->birthDay), m_BirthDay, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kBirthDayColumn)); + if(card->custom1) + if (Compare(nsDependentString(card->custom1), m_Custom1, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCustom1Column)); + if(card->custom2) + if (Compare(nsDependentString(card->custom2), m_Custom2, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCustom2Column)); + if(card->custom3) + if (Compare(nsDependentString(card->custom3), m_Custom3, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCustom3Column)); + if(card->custom4) + if (Compare(nsDependentString(card->custom4), m_Custom4, nsCaseInsensitiveStringComparator())) + differingAttrs.AppendString(NS_LITERAL_STRING(kCustom4Column)); + if (card->isMailList != m_IsMailList) + differingAttrs.AppendString(NS_LITERAL_STRING(kMailListName)); + if(card->mailListURI) { + nsCAutoString str(card->mailListURI); + if (str.Compare(m_MailListURI, PR_TRUE)) + differingAttrs.AppendString(NS_LITERAL_STRING(kMailListDescription)); + } + + return (differingAttrs.Count() == 0); +} + + +NS_IMETHODIMP nsAbIPCCard::Equals(nsIAbCard *card, PRBool *_retval) +{ + NS_ENSURE_ARG_POINTER(card); + NS_ENSURE_ARG_POINTER(_retval); + + nsXPIDLString str; + *_retval = PR_FALSE; + + card->GetFirstName(getter_Copies(str)); + if (Compare(str, m_FirstName, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetLastName(getter_Copies(str)); + if (Compare(str, m_LastName, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetDisplayName(getter_Copies(str)); + if (Compare(str, m_DisplayName, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetNickName(getter_Copies(str)); + if (Compare(str, m_NickName, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetPrimaryEmail(getter_Copies(str)); + if (Compare(str, m_PrimaryEmail, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetSecondEmail(getter_Copies(str)); + if (Compare(str, m_SecondEmail, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkPhone(getter_Copies(str)); + if (Compare(str, m_WorkPhone, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomePhone(getter_Copies(str)); + if (Compare(str, m_HomePhone, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetFaxNumber(getter_Copies(str)); + if (Compare(str, m_FaxNumber, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetPagerNumber(getter_Copies(str)); + if (Compare(str, m_PagerNumber, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCellularNumber(getter_Copies(str)); + if (Compare(str, m_CellularNumber, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeAddress(getter_Copies(str)); + if (Compare(str, m_HomeAddress, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeAddress2(getter_Copies(str)); + if (Compare(str, m_HomeAddress2, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeCity(getter_Copies(str)); + if (Compare(str, m_HomeCity, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeState(getter_Copies(str)); + if (Compare(str, m_HomeState, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeZipCode(getter_Copies(str)); + if (Compare(str, m_HomeZipCode, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetHomeCountry(getter_Copies(str)); + if (Compare(str, m_HomeCountry, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkAddress(getter_Copies(str)); + if (Compare(str, m_WorkAddress, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkAddress2(getter_Copies(str)); + if (Compare(str, m_WorkAddress2, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkCity(getter_Copies(str)); + if (Compare(str, m_WorkCity, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkState(getter_Copies(str)); + if (Compare(str, m_WorkState, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkZipCode(getter_Copies(str)); + if (Compare(str, m_WorkZipCode, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWorkCountry(getter_Copies(str)); + if (Compare(str, m_WorkCountry, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetJobTitle(getter_Copies(str)); + if (Compare(str, m_JobTitle, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetDepartment(getter_Copies(str)); + if (Compare(str, m_Department, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCompany(getter_Copies(str)); + if (Compare(str, m_Company, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWebPage1(getter_Copies(str)); + if (Compare(str, m_WebPage1, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetWebPage2(getter_Copies(str)); + if (Compare(str, m_WebPage2, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetBirthYear(getter_Copies(str)); + if (Compare(str, m_BirthYear, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetBirthMonth(getter_Copies(str)); + if (Compare(str, m_BirthMonth, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetBirthDay(getter_Copies(str)); + if (Compare(str, m_BirthDay, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCustom1(getter_Copies(str)); + if (Compare(str, m_Custom1, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCustom2(getter_Copies(str)); + if (Compare(str, m_Custom2, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCustom3(getter_Copies(str)); + if (Compare(str, m_Custom3, nsCaseInsensitiveStringComparator())) + return NS_OK; + + card->GetCustom4(getter_Copies(str)); + if (Compare(str, m_Custom4, nsCaseInsensitiveStringComparator())) + return NS_OK; + + PRBool isMailList=PR_FALSE; + card->GetIsMailList(&isMailList); + if (isMailList != m_IsMailList) + return NS_OK; + + nsCAutoString tempStr(m_MailListURI); + nsXPIDLCString str2; + card->GetMailListURI(getter_Copies(str2)); + if (tempStr.Compare(str2.get(), PR_TRUE)) + return NS_OK; + + *_retval = PR_TRUE; + return NS_OK; +} + +PRBool nsAbIPCCard::CompareValue(PRBool isUnicode, LPTSTR cardValue, nsString & attribValue) +{ + if(cardValue) { + if(isUnicode) { + if (Compare(nsDependentString(cardValue), attribValue, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + } + else { + nsAutoString str; + CONVERT_ASSIGNTO_UNICODE(str, cardValue); + if (Compare(str, attribValue, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + } + } + + return PR_TRUE; +} + +PRBool nsAbIPCCard::Same(nsABCOMCardStruct * card, PRBool isUnicode) +{ + if(!card) + return PR_FALSE; + + if(mRecordId && card->dwRecordId) { + if(mRecordId == card->dwRecordId) + return PR_TRUE; + else + return PR_FALSE; + } + + if(CompareValue(isUnicode, card->firstName, m_FirstName)) + if(CompareValue(isUnicode, card->lastName, m_LastName)) + if(CompareValue(isUnicode, card->displayName, m_DisplayName)) + if(CompareValue(isUnicode, card->nickName, m_NickName)) + return PR_TRUE; + + return PR_FALSE; +} + + +PRBool nsAbIPCCard::Same(nsIAbCard *card) +{ + if(!card) + return PR_FALSE; + + nsresult rv = NS_OK; + nsCOMPtr dbCard; + dbCard = do_QueryInterface(card, &rv); + if(NS_SUCCEEDED(rv)) { + // first check the palmID for the cards if they exist + nsXPIDLString palmIDStr; + rv = dbCard->GetStringAttribute(CARD_ATTRIB_PALMID, getter_Copies(palmIDStr)); + if(NS_SUCCEEDED(rv) && palmIDStr.get()) { + PRInt32 palmID=0; + PRFloat64 f = PR_strtod(NS_LossyConvertUCS2toASCII(palmIDStr).get(), nsnull); + PRInt64 l; + LL_D2L(l, f); + LL_L2UI(palmID, l); + if(palmID && mRecordId) + return mRecordId == palmID; + } + } + + nsXPIDLString str; + card->GetFirstName(getter_Copies(str)); + if (Compare(str, m_FirstName, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + card->GetLastName(getter_Copies(str)); + if (Compare(str, m_LastName, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + card->GetDisplayName(getter_Copies(str)); + if (Compare(str, m_DisplayName, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + card->GetNickName(getter_Copies(str)); + if (Compare(str, m_NickName, nsCaseInsensitiveStringComparator())) + return PR_FALSE; + + return PR_TRUE; +} + + +void nsAbIPCCard::CopyValue(PRBool isUnicode, nsString & attribValue, LPTSTR * result) +{ + if(attribValue.Length()) { + if(isUnicode) { + PRUnichar * Str = new PRUnichar[attribValue.Length()+1]; + wcscpy(Str, attribValue.get()); + *result = Str; + } + else { + char * str = new char[attribValue.Length()+1]; + nsCAutoString cStr; + if(attribValue.get()) + cStr = NS_ConvertUCS2toUTF8(attribValue); + strcpy(str, cStr.get()); + *result = (LPTSTR) str; + } + } +} + + +nsresult nsAbIPCCard::GetABCOMCardStruct(PRBool isUnicode, nsABCOMCardStruct * card) +{ + NS_ENSURE_ARG_POINTER(card); + + memset(card, 0, sizeof(nsABCOMCardStruct)); + + card->dwRecordId = mRecordId; + card->dwCategoryId = mCategoryId; + card->dwStatus = mStatus; + + CopyValue(isUnicode, m_FirstName, &card->firstName); + CopyValue(isUnicode, m_LastName, &card->lastName); + CopyValue(isUnicode, m_DisplayName, &card->displayName); + CopyValue(isUnicode, m_NickName, &card->nickName); + CopyValue(isUnicode, m_PrimaryEmail, &card->primaryEmail); + CopyValue(isUnicode, m_SecondEmail, &card->secondEmail); + CopyValue(isUnicode, m_WorkPhone, &card->workPhone); + CopyValue(isUnicode, m_HomePhone, &card->homePhone); + CopyValue(isUnicode, m_FaxNumber, &card->faxNumber); + CopyValue(isUnicode, m_PagerNumber, &card->pagerNumber); + CopyValue(isUnicode, m_CellularNumber, &card->cellularNumber); + CopyValue(isUnicode, m_HomeAddress, &card->homeAddress); + CopyValue(isUnicode, m_HomeAddress2, &card->homeAddress2); + CopyValue(isUnicode, m_HomeCity, &card->homeCity); + CopyValue(isUnicode, m_HomeState, &card->homeState); + CopyValue(isUnicode, m_HomeZipCode, &card->homeZipCode); + CopyValue(isUnicode, m_HomeCountry, &card->homeCountry); + CopyValue(isUnicode, m_WorkAddress, &card->workAddress); + CopyValue(isUnicode, m_WorkAddress2, &card->workAddress2); + CopyValue(isUnicode, m_WorkCity, &card->workCity); + CopyValue(isUnicode, m_WorkState, &card->workState); + CopyValue(isUnicode, m_WorkZipCode, &card->workZipCode); + CopyValue(isUnicode, m_WorkCountry, &card->workCountry); + CopyValue(isUnicode, m_JobTitle, &card->jobTitle); + CopyValue(isUnicode, m_Department, &card->department); + CopyValue(isUnicode, m_Company, &card->company); + CopyValue(isUnicode, m_WebPage1, &card->webPage1); + CopyValue(isUnicode, m_WebPage2, &card->webPage2); + CopyValue(isUnicode, m_BirthYear, &card->birthYear); + CopyValue(isUnicode, m_BirthMonth, &card->birthMonth); + CopyValue(isUnicode, m_BirthDay, &card->birthDay); + CopyValue(isUnicode, m_Custom1, &card->custom1); + CopyValue(isUnicode, m_Custom2, &card->custom2); + CopyValue(isUnicode, m_Custom3, &card->custom3); + CopyValue(isUnicode, m_Custom4, &card->custom4); + CopyValue(isUnicode, m_Note, &card->notes); + + card->lastModifiedDate = m_LastModDate; + card->preferMailFormat = m_PreferMailFormat; + + card->isMailList = m_IsMailList; + if(m_MailListURI) { + card->mailListURI = new char[strlen(m_MailListURI)+1]; + strcpy(card->mailListURI, m_MailListURI); + } + + return NS_OK; +} + +void nsAbIPCCard::CleanUpABCOMCardStruct(nsABCOMCardStruct * card) +{ + if(!card) + return; + + delete (card->firstName); + delete (card->lastName); + delete (card->displayName); + delete (card->nickName); + delete card->primaryEmail; + delete card->secondEmail; + delete card->workPhone; + delete card->homePhone; + delete card->faxNumber; + delete card->pagerNumber; + delete card->cellularNumber; + delete card->homeAddress; + delete card->homeAddress2; + delete card->homeCity; + delete card->homeState; + delete card->homeZipCode; + delete card->homeCountry; + delete card->workAddress; + delete card->workAddress2; + delete card->workCity; + delete card->workState; + delete card->workZipCode; + delete card->workCountry; + delete card->jobTitle; + delete card->department; + delete card->company; + delete card->webPage1; + delete card->webPage2; + delete card->birthYear; + delete card->birthMonth; + delete card->birthDay; + delete card->custom1; + delete card->custom2; + delete card->custom3; + delete card->custom4; + delete card->notes; + delete card->mailListURI; +} + diff --git a/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.h b/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.h new file mode 100644 index 00000000000..2cdef045189 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsAbIPCCard.h @@ -0,0 +1,100 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2001 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Created by: Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsAbIPCCard_h__ +#define nsAbIPCCard_h__ + +#include "nsAbMDBCardProperty.h" +#include "nsIAbMDBCard.h" +#include "nsISupportsArray.h" +#include "IPalmSync.h" + +// these are states of Palm record +// as defined in the Palm CDK +#define ATTR_DELETED 0x0001 +#define ATTR_ARCHIVED 0x0002 +#define ATTR_MODIFIED 0x0004 +#define ATTR_NEW 0x0008 +#define ATTR_NONE 0x0020 +#define ATTR_NO_REC 0x0040 + +class nsAbIPCCard : public nsAbMDBCardProperty +{ +public: + NS_DECL_ISUPPORTS_INHERITED + + // this checks for all card data fields + NS_IMETHOD Equals(nsIAbCard *card, PRBool *_retval); + PRBool Equals(nsABCOMCardStruct * card, nsStringArray & differingAttrs); + PRBool EqualsAfterUnicodeConversion(nsABCOMCardStruct * card, nsStringArray & differingAttrs); + + // this check names attribs: first name + last name + display name + nickname + PRBool Same(nsABCOMCardStruct * card, PRBool isUnicode=PR_TRUE); + PRBool Same(nsIAbCard *card); + + NS_IMETHOD Copy(nsIAbCard *srcCard); + nsresult Copy(nsABCOMCardStruct * srcCard); + nsresult ConvertToUnicodeAndCopy(nsABCOMCardStruct * srcCard); + + // this function will allocate new memory for the passed in card struct data members + // the caller needs to call CleanUpABCOMCardStruct once it is done using the card struct + nsresult GetABCOMCardStruct(PRBool isUnicode, nsABCOMCardStruct * card); + static void CleanUpABCOMCardStruct(nsABCOMCardStruct * card); + + nsAbIPCCard(); + nsAbIPCCard(nsIAbCard *card); + nsAbIPCCard(nsABCOMCardStruct *card, PRBool isUnicode=PR_TRUE); + virtual ~nsAbIPCCard(); + + void SetStatus(PRUint32 status) { mStatus = status; } + PRUint32 GetStatus() { return mStatus; } + void SetRecordId(PRUint32 recID) { mRecordId = recID; } + PRUint32 GetRecordId() { return mRecordId; } + void SetCategoryId(PRUint32 catID) { mCategoryId = catID; } + PRUint32 GetCategoryId() { return mCategoryId; } + +private: + PRUint32 mRecordId; + PRUint32 mCategoryId; + PRUint32 mStatus; + + void CopyValue(PRBool isUnicode, nsString & attribValue, LPTSTR * result); + PRBool CompareValue(PRBool isUnicode, LPTSTR cardValue, nsString & attribValue); +}; + +#endif diff --git a/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp b/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp new file mode 100644 index 00000000000..9aeabd77d64 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp @@ -0,0 +1,902 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation + * + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + + +#include "nsAbPalmSync.h" +#include "nsIAddrBookSession.h" +#include "nsAbBaseCID.h" +#include "nsMsgI18N.h" +#include "nsIRDFService.h" +#include "nsRDFCID.h" +#include "nsUnicharUtils.h" +#include "nsAbMDBCard.h" +#include "nsAbCardProperty.h" +#include "prdtoa.h" + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); + +#define PREVIOUS_EXTENSION ".prev" + +#ifdef RAJIV_DEBUG + PRBool PalmDataDisplayed = PR_FALSE; + void DisplayTestData(nsABCOMCardStruct * aIPCCard, PRBool IsUnicode) + { + if (IsUnicode) { + if(aIPCCard->displayName) + printf("\n Card Data For : %S:", aIPCCard->displayName); + if(aIPCCard->firstName) + printf("\n First Name : %S", aIPCCard->firstName); + if(aIPCCard->lastName) + printf("\n Last Name : %S", aIPCCard->lastName); + if(aIPCCard->primaryEmail) + printf("\n Email : %S", aIPCCard->primaryEmail); + if(aIPCCard->workPhone) + printf("\n Work Phone : %S", aIPCCard->workPhone); + if(aIPCCard->homePhone) + printf("\n Home Phone : %S", aIPCCard->homePhone); + if(aIPCCard->faxNumber) + printf("\n Fax Number : %S", aIPCCard->faxNumber); + if(aIPCCard->company) + printf("\n Company : %S", aIPCCard->company); + if(aIPCCard->jobTitle) + printf("\n Job Title : %S", aIPCCard->jobTitle); + if(aIPCCard->homeAddress) + printf("\n Home Address : %S", aIPCCard->homeAddress); + if(aIPCCard->homeCity) + printf("\n Home City : %S", aIPCCard->homeCity); + if(aIPCCard->homeState) + printf("\n Home State : %S", aIPCCard->homeState); + printf("\n"); + } + else { + if(aIPCCard->displayName) + printf("\n Card Data For : %s:", (char *) aIPCCard->displayName); + if(aIPCCard->firstName) + printf("\n First Name : %s", (char *) aIPCCard->firstName); + if(aIPCCard->lastName) + printf("\n Last Name : %s", (char *) aIPCCard->lastName); + if(aIPCCard->primaryEmail) + printf("\n Email : %s", (char *) aIPCCard->primaryEmail); + if(aIPCCard->workPhone) + printf("\n Work Phone : %s", (char *) aIPCCard->workPhone); + if(aIPCCard->homePhone) + printf("\n Home Phone : %s", (char *) aIPCCard->homePhone); + if(aIPCCard->faxNumber) + printf("\n Fax Number : %s", (char *) aIPCCard->faxNumber); + if(aIPCCard->company) + printf("\n Company : %s", (char *) aIPCCard->company); + if(aIPCCard->jobTitle) + printf("\n Job Title : %s", (char *) aIPCCard->jobTitle); + if(aIPCCard->homeAddress) + printf("\n Home Address : %s", (char *) aIPCCard->homeAddress); + if(aIPCCard->homeCity) + printf("\n Home City : %s", (char *) aIPCCard->homeCity); + if(aIPCCard->homeState) + printf("\n Home State : %s", (char *) aIPCCard->homeState); + printf("\n"); + } + } +#endif + +nsAbPalmHotSync::nsAbPalmHotSync(PRBool aIsUnicode, PRUnichar * aAbDescUnicode, char * aAbDesc, PRInt32 aPalmCatID) +{ + mTotalCardCount=0; + + mCardForPalmCount = 0; + mCardListForPalm = nsnull; + mDirServerInfo = nsnull; + + mInitialized = PR_FALSE; + mDBOpen = PR_FALSE; + + if(aIsUnicode) + mAbName.Assign(aAbDescUnicode); + else + // mAbName.AssignWithConversion(aAbDesc); + mAbName = NS_ConvertUTF8toUCS2(aAbDesc); + mAbName.Trim(" "); + + mPalmCategoryId = aPalmCatID; + + mIsPalmDataUnicode = aIsUnicode; + + mNewCardCount = 0; + NS_NewISupportsArray(getter_AddRefs(mNewCardsArray)); + mIsNewCardForPalm = PR_FALSE; +} + +nsAbPalmHotSync::~nsAbPalmHotSync() +{ + if(mCardListForPalm) { + for(PRUint32 i=0; iClose(PR_FALSE); +} + +// this is a utility function +void nsAbPalmHotSync::ConvertAssignPalmIDAttrib(PRUint32 id, nsIAbMDBCard * card) +{ + PRInt64 l; + LL_UI2L(l, id); + PRFloat64 f; + LL_L2F(f, l); + char buf[128]; + PR_cnvtf(buf, 128, 0, f); + card->SetAbDatabase(mABDB); + card->SetStringAttribute(CARD_ATTRIB_PALMID,NS_ConvertASCIItoUCS2(buf).get()); +} + +nsresult nsAbPalmHotSync::Initialize() +{ + nsresult rv = NS_OK; + + if(mInitialized) + return rv; + + if (!DIR_GetDirectories()) + return NS_ERROR_FAILURE; + + PRInt32 count = DIR_GetDirectories()->Count(); + + DIR_Server *server = nsnull; + for (PRInt32 i = 0; i < count; i++) { + server = (DIR_Server *)(DIR_GetDirectories()->ElementAt(i)); + + // if this is a 4.x, local .na2 addressbook (PABDirectory) + // we must skip it. + PRUint32 fileNameLen = strlen(server->fileName); + if (((fileNameLen > kABFileName_PreviousSuffixLen) && + strcmp(server->fileName + fileNameLen - kABFileName_PreviousSuffixLen, kABFileName_PreviousSuffix) == 0) && + (server->dirType == PABDirectory)) + continue; + + // if Palm category is already assigned to AB then just check that + if((server->PalmCategoryId > -1) && (mPalmCategoryId == server->PalmCategoryId)) + break; + + // convert to unicode + nsAutoString abName; + nsCAutoString platformCharSet(nsMsgI18NFileSystemCharset()); + rv = ConvertToUnicode(platformCharSet.get(), server->description, abName); + if(NS_FAILED(rv)) return rv ; + + // if Palm category is not already assigned check the AB name + if(abName.Find(mAbName) != kNotFound) + break; + + server = nsnull; + } + + if(!server || !server->fileName) + return NS_ERROR_FILE_NOT_FOUND; + + mDirServerInfo = server; + + mInitialized = TRUE; + + return NS_OK; +} + +nsresult nsAbPalmHotSync::AddAllRecordsInNewAB(PRInt32 aCount, lpnsABCOMCardStruct aPalmRecords) +{ + NS_ENSURE_ARG_POINTER(aPalmRecords); + + DIR_Server * server = nsnull; + nsAutoString fileName(mAbName.get()); + fileName.AppendWithConversion(".mab"); + nsresult rv = DIR_AddNewAddressBook(mAbName.get(), NS_ConvertUCS2toUTF8(fileName).get(), + PR_FALSE, MAPIDirectory, &server); + if(NS_FAILED(rv)) + return rv; + + mDirServerInfo = server; + mInitialized = TRUE; + + // open the Moz AB database + rv = OpenABDBForHotSync(PR_TRUE); + if(NS_FAILED(rv)) + return rv; + + // we are just storing the pointer array here not record arrays + for (PRInt32 i=0; i < aCount; i++) + mPalmRecords.AppendElement(&aPalmRecords[i]); + + // new DB here so no need to backup + rv = UpdateMozABWithPalmRecords(); + + rv = mABDB->Close(NS_SUCCEEDED(rv)); + + if(NS_SUCCEEDED(rv)) { + mDBOpen = PR_FALSE; + PRUint32 modTimeInSec; + nsAddrDatabase::PRTime2Seconds(PR_Now(), &modTimeInSec); + mDirServerInfo->PalmSyncTimeStamp = modTimeInSec; + mDirServerInfo->PalmCategoryId = mPalmCategoryId; + DIR_SavePrefsForOneServer(mDirServerInfo); + } + else { // get back the previous file + rv = mABDB->ForceClosed(); + if(NS_SUCCEEDED(rv)) { + nsCAutoString leafName; + mABFile->GetNativeLeafName(leafName); + PRBool bExists=PR_FALSE; + mPreviousABFile->Exists(&bExists); + if(bExists) { + nsCOMPtr parent; + rv = mABFile->GetParent(getter_AddRefs(parent)); + if (NS_SUCCEEDED(rv)) { + mABFile->Remove(PR_FALSE); + mPreviousABFile->CopyToNative(parent, leafName); + } + } + mDBOpen = PR_FALSE; + } + } + + return rv; +} + +nsresult nsAbPalmHotSync::GetAllCards(PRInt32 * aCount, lpnsABCOMCardStruct * aCardList) +{ + NS_ENSURE_ARG_POINTER(aCount); + NS_ENSURE_ARG_POINTER(aCardList); + + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + // open the Moz AB database + nsresult rv = OpenABDBForHotSync(PR_FALSE); + if(NS_FAILED(rv)) + return rv; + + // create the list to be sent back to Palm + mABDB->GetCardCount(&mTotalCardCount); + *aCount = mTotalCardCount; + if(!mTotalCardCount) { + mABDB->Close(PR_FALSE); + return NS_OK; + } + + mCardListForPalm = (lpnsABCOMCardStruct) calloc(mTotalCardCount, sizeof(nsABCOMCardStruct)); + if(!mCardListForPalm) { + mABDB->Close(PR_FALSE); + return NS_ERROR_OUT_OF_MEMORY; + } + + nsCOMPtr cardsEnumerator; + rv = mABDB->EnumerateCards(mDirectory, getter_AddRefs(cardsEnumerator)); + if (NS_FAILED(rv) || !cardsEnumerator) { + mABDB->Close(PR_FALSE); + return rv; + } + + nsCOMPtr item; + nsCOMPtr card; + for (rv = cardsEnumerator->First(); NS_SUCCEEDED(rv); rv = cardsEnumerator->Next()) { + rv = cardsEnumerator->CurrentItem(getter_AddRefs(item)); + if (NS_FAILED(rv)) + continue; + + card = do_QueryInterface(item, &rv); + if (NS_FAILED(rv)) // can this be anything but a card? + continue; + + PRBool isMailingList=PR_FALSE; + rv = card->GetIsMailList(&isMailingList); + if (NS_FAILED(rv) || isMailingList) // if mailing list go to cards + continue; + + nsAbIPCCard ipcCard(card); + ipcCard.SetStatus(ATTR_NEW); + + rv = AddToListForPalm(ipcCard); + if(NS_FAILED(rv)) + break; + + mNewCardsArray->AppendElement(card); + mNewCardCount++; + } + + // this should really never happen but just to make sure there is no memory leaks! + if(!mCardForPalmCount && mCardListForPalm) { + free(mCardListForPalm); + mCardListForPalm = nsnull; + } + + *aCount = mCardForPalmCount; + *aCardList = mCardListForPalm; + + // Don't close the DB yet, it will be closed when Done is called + + return NS_OK; +} + +nsresult nsAbPalmHotSync::DoSyncAndGetUpdatedCards(PRInt32 aPalmCount, lpnsABCOMCardStruct aPalmRecords, + PRInt32 * aMozCount, lpnsABCOMCardStruct * aMozCards) +{ + NS_ENSURE_ARG_POINTER(aPalmRecords); + NS_ENSURE_ARG_POINTER(aMozCount); + NS_ENSURE_ARG_POINTER(aMozCards); + + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + // open the Moz AB database + nsresult rv = OpenABDBForHotSync(PR_FALSE); + if(NS_FAILED(rv)) + return rv; + + // we are just storing the pointer array here not record arrays + for (PRInt32 i=0; i < aPalmCount; i++) { + mPalmRecords.AppendElement(&aPalmRecords[i]); +#ifdef RAJIV_DEBUG + DisplayTestData(&aPalmRecords[i],PR_FALSE); +#endif + } + + // create the list to be sent back to Palm + mABDB->GetCardCount(&mTotalCardCount); + PRUint32 deletedCardCount=0; + mABDB->GetDeletedCardCount(&deletedCardCount); + mTotalCardCount += deletedCardCount; + if(mTotalCardCount) { + mCardListForPalm = (lpnsABCOMCardStruct) calloc(mTotalCardCount, sizeof(nsABCOMCardStruct)); + if(!mCardListForPalm) { + mABDB->Close(PR_FALSE); + return NS_ERROR_OUT_OF_MEMORY; + } + } + + // initialize the flag for any new Moz cards to be added to Palm + mIsNewCardForPalm = PR_FALSE; + + // deal with new / modified or first time sync + rv = LoadNewModifiedCardsSinceLastSync(); + if(NS_SUCCEEDED(rv)) + // deal with deleted + rv = LoadDeletedCardsSinceLastSync(); + if(NS_SUCCEEDED(rv)) + // first backup the existing DB as Previous + rv = KeepCurrentStateAsPrevious(); + if(NS_SUCCEEDED(rv)) + // update local DB for sync + rv = UpdateMozABWithPalmRecords(); + + if(!mCardForPalmCount && mCardListForPalm) { + free(mCardListForPalm); + mCardListForPalm = nsnull; + } + + // if there are no new cards to be added in Palm close DB + // else wait for Done to be called. + if(!mIsNewCardForPalm) { + rv = mABDB->Close(NS_SUCCEEDED(rv) && mPalmRecords.Count()); + + if(NS_SUCCEEDED(rv)) { + mDBOpen = PR_FALSE; + PRUint32 modTimeInSec; + nsAddrDatabase::PRTime2Seconds(PR_Now(), &modTimeInSec); + mDirServerInfo->PalmSyncTimeStamp = modTimeInSec; + mDirServerInfo->PalmCategoryId = mPalmCategoryId; + DIR_SavePrefsForOneServer(mDirServerInfo); + } + else { // get back the previous file + rv = mABDB->ForceClosed(); + if(NS_SUCCEEDED(rv)) { + nsCAutoString leafName; + mABFile->GetNativeLeafName(leafName); + PRBool bExists=PR_FALSE; + mPreviousABFile->Exists(&bExists); + if(bExists) { + nsCOMPtr parent; + rv = mABFile->GetParent(getter_AddRefs(parent)); + if (NS_SUCCEEDED(rv)) { + mABFile->Remove(PR_FALSE); + mPreviousABFile->CopyToNative(parent, leafName); + } + } + mDBOpen = PR_FALSE; + } + } + } + + *aMozCount = mCardForPalmCount; + *aMozCards = mCardListForPalm; + + return rv; +} + +// this takes care of the cases when the state is deleted +nsresult nsAbPalmHotSync::LoadDeletedCardsSinceLastSync() +{ + if(!mDBOpen || !mABDB || !mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + nsISupportsArray * deletedCardArray; + PRUint32 deletedCardCount; + nsresult rv = mABDB->GetDeletedCardList(&deletedCardCount, &deletedCardArray); + if(NS_FAILED(rv)) + return rv; + + for(PRUint32 i=0; i < deletedCardCount; i++) + { + nsCOMPtr card; + rv = deletedCardArray->QueryElementAt(i, NS_GET_IID(nsIAbCard), getter_AddRefs(card)); + if (NS_FAILED(rv)) // can this be anything but a card? + continue; + + PRBool isMailingList=PR_FALSE; + rv = card->GetIsMailList(&isMailingList); + if (NS_FAILED(rv) || isMailingList) + continue; + + PRUint32 lastModifiedDate = 0; + rv = card->GetLastModifiedDate(&lastModifiedDate); + if (NS_FAILED(rv) || !lastModifiedDate) + continue; + + if(lastModifiedDate > mDirServerInfo->PalmSyncTimeStamp) { + nsAbIPCCard ipcCard(card); + // check in the list of Palm records + for(PRInt32 i=mPalmRecords.Count()-1; i >=0; i--) { + nsABCOMCardStruct * palmRec = (nsABCOMCardStruct *) mPalmRecords.ElementAt(i); + // if same record exists in palm list, donot delete it from Palm + if(ipcCard.Same(palmRec, mIsPalmDataUnicode)) + continue; + } + ipcCard.SetStatus(ATTR_DELETED); + + rv = AddToListForPalm(ipcCard); + if(NS_FAILED(rv)) + break; + } + } + return rv; +} + + +nsresult nsAbPalmHotSync::LoadNewModifiedCardsSinceLastSync() +{ + if(!mDBOpen || !mABDB || !mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + nsCOMPtr cardsEnumerator; + nsresult rv = mABDB->EnumerateCards(mDirectory, getter_AddRefs(cardsEnumerator)); + if (NS_FAILED(rv) || !cardsEnumerator) + return NS_ERROR_NOT_AVAILABLE; // no cards available + + // create the list of cards to be sent to Palm + nsCOMPtr item; + nsCOMPtr card; + for (rv = cardsEnumerator->First(); NS_SUCCEEDED(rv); rv = cardsEnumerator->Next()) { + rv = cardsEnumerator->CurrentItem(getter_AddRefs(item)); + if (NS_FAILED(rv)) + return rv; + + card = do_QueryInterface(item, &rv); + if (NS_FAILED(rv)) // can this be anything but a card? + continue; + + PRBool isMailingList=PR_FALSE; + rv = card->GetIsMailList(&isMailingList); + if (NS_FAILED(rv) || isMailingList) // if mailing list go to cards + continue; + + PRUint32 lastModifiedDate = 0; + rv = card->GetLastModifiedDate(&lastModifiedDate); + if (NS_FAILED(rv)) + continue; + + if(lastModifiedDate > mDirServerInfo->PalmSyncTimeStamp // take care of modified + || !lastModifiedDate || !mDirServerInfo->PalmSyncTimeStamp) { // take care of new or never before sync + + //create nsAbIPCCard and assign its status based on lastModifiedDate + nsAbIPCCard ipcCard(card); + ipcCard.SetStatus((lastModifiedDate) ? ATTR_MODIFIED : ATTR_NEW); + + // Check with the palm list (merging is done if required) + if (!CheckWithPalmRecord(&ipcCard)) + continue; + + rv = AddToListForPalm(ipcCard); + if(NS_FAILED(rv)) + break; + + if(ipcCard.GetStatus() == ATTR_NEW) { + mNewCardsArray->AppendElement(card); + mNewCardCount++; + mIsNewCardForPalm = PR_TRUE; + } + } + } + + return NS_OK; +} + + +// this take care of the all cases when the state is either mod or new +PRBool nsAbPalmHotSync::CheckWithPalmRecord(nsAbIPCCard * aIPCCard) +{ + NS_ENSURE_ARG_POINTER(aIPCCard); + + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + PRBool keep = PR_TRUE; + + for(PRInt32 i=mPalmRecords.Count()-1; i >=0; i--) { + nsABCOMCardStruct * palmRec = (nsABCOMCardStruct *) mPalmRecords.ElementAt(i); + + // if same records exists in palm list also + if(aIPCCard->Same(palmRec, mIsPalmDataUnicode)) { + // if the state deleted on both sides no need to do anything + if ((palmRec->dwStatus == ATTR_DELETED) && (aIPCCard->GetStatus() == ATTR_DELETED)) { + mPalmRecords.RemoveElementAt(i); + return PR_FALSE; + } + // if deleted on Palm and added or modified on Moz, donot delete on Moz + if ( (palmRec->dwStatus == ATTR_DELETED) && + ((aIPCCard->GetStatus() == ATTR_NEW) + ||(aIPCCard->GetStatus() == ATTR_MODIFIED)) ) { + mPalmRecords.RemoveElementAt(i); + return PR_TRUE; + } + + // set the palm record ID in the card if not already set + if(!aIPCCard->GetRecordId()) { + ConvertAssignPalmIDAttrib(palmRec->dwRecordId, aIPCCard); + mABDB->EditCard(aIPCCard, PR_FALSE); + aIPCCard->SetRecordId(palmRec->dwRecordId); + } + + // if deleted in Moz and added or modified on Palm, donot delete on Palm + if ( (aIPCCard->GetStatus() == ATTR_DELETED) && + ((palmRec->dwStatus == ATTR_NEW) + ||(palmRec->dwStatus == ATTR_MODIFIED)) ) { + return PR_FALSE; + } + + // for rest of the cases with state as either mod or new:: + nsStringArray diffAttrs; + PRBool isFieldsMatch=PR_FALSE; + if(mIsPalmDataUnicode) + isFieldsMatch = aIPCCard->Equals(palmRec, diffAttrs); + else + isFieldsMatch = aIPCCard->EqualsAfterUnicodeConversion(palmRec, diffAttrs); + + // if fields are ditto no need to keep it for sync + if(isFieldsMatch) { + keep = PR_FALSE; + // since the fields are ditto no need to update Moz AB with palm record + mPalmRecords.RemoveElementAt(i); + } + else { + // we add an additional record on both sides alike Palm Desktop sync + palmRec->dwStatus = ATTR_NONE; + aIPCCard->SetStatus(ATTR_NEW); + keep = PR_TRUE; + } + // since we found the same record in palm sync list + // no need to go thru the rest of the mPalmRecords list + break; + } + } + + return keep; +} + +// utility function +nsresult nsAbPalmHotSync::AddToListForPalm(nsAbIPCCard & ipcCard) +{ + // this should never happen but check for crossing array index + if(mCardForPalmCount >= mTotalCardCount) + return NS_ERROR_UNEXPECTED; + + nsresult rv = ipcCard.GetABCOMCardStruct(mIsPalmDataUnicode, &mCardListForPalm[mCardForPalmCount]); + if(NS_SUCCEEDED(rv)) { + mCardListForPalm[mCardForPalmCount].dwCategoryId = mPalmCategoryId; + mCardForPalmCount++; + } + + return rv; +} + +nsresult nsAbPalmHotSync::OpenABDBForHotSync(PRBool aCreate) +{ + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + // if already open + if(mDBOpen && mABDB && mDirectory) + return NS_OK; + + if(!mDirServerInfo->fileName) + return NS_ERROR_FILE_INVALID_PATH; + + nsresult rv = NS_OK; + + nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); + if(NS_FAILED(rv)) + return rv; + + nsFileSpec* dbPath; + rv = abSession->GetUserProfileDirectory(&dbPath); + if(NS_FAILED(rv)) + return rv; + + (*dbPath) += mDirServerInfo->fileName; + + // get nsIFile for nsFileSpec from abSession, why use a obsolete class if not required! + rv = NS_FileSpecToIFile(dbPath, getter_AddRefs(mABFile)); + if(NS_FAILED(rv)) { + delete dbPath; + return rv; + } + + nsCOMPtr addrDBFactory = + do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); + if(NS_FAILED(rv)) { + delete dbPath; + return rv; + } + + rv = addrDBFactory->Open(dbPath, aCreate, getter_AddRefs(mABDB), PR_TRUE); + delete dbPath; + if(NS_FAILED(rv)) { + return rv; + } + + mDBOpen = PR_TRUE; // Moz AB DB is now Open + + // This is in case the uri is never set + // in the nsDirPref.cpp code. + if (!mDirServerInfo->uri) { + nsCAutoString URI; + URI = NS_LITERAL_CSTRING(kMDBDirectoryRoot) + nsDependentCString(mDirServerInfo->fileName); + mDirServerInfo->uri = ToNewCString(URI); + } + + // Get the RDF service... + nsCOMPtr rdfService(do_GetService(kRDFServiceCID, &rv)); + if (NS_FAILED(rv)) + return rv; + + nsCOMPtr resource; + if ((strncmp(mDirServerInfo->uri, "ldap:", 5) == 0) || + (strncmp(mDirServerInfo->uri, "ldaps:", 6) == 0)) { + nsCAutoString bridgeURI; + bridgeURI = NS_LITERAL_CSTRING(kLDAPDirectoryRoot) + nsDependentCString(mDirServerInfo->prefName); + rv = rdfService->GetResource(bridgeURI.get(), getter_AddRefs(resource)); + if (NS_FAILED(rv)) + return rv; + } + else { + rv = rdfService->GetResource(mDirServerInfo->uri, getter_AddRefs(resource)); + if (NS_FAILED(rv)) + return rv; + } + + // get the directory of cards for the Moz AB + mDirectory = do_QueryInterface(resource, &rv); + + return rv; +} + +nsresult nsAbPalmHotSync::KeepCurrentStateAsPrevious() +{ + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + nsresult rv = NS_OK; + + nsCAutoString previousLeafName(mDirServerInfo->fileName); + previousLeafName += PREVIOUS_EXTENSION; + + if(!mPreviousABFile) { + nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); + if(NS_FAILED(rv)) + return rv; + nsFileSpec* dbPath; + rv = abSession->GetUserProfileDirectory(&dbPath); // this still uses nsFileSpec!!! + if(NS_SUCCEEDED(rv)) { + (*dbPath) += previousLeafName.get(); + // get nsIFile for nsFileSpec from abSession, why use a obsolete class if not required! + rv = NS_FileSpecToIFile(dbPath, getter_AddRefs(mPreviousABFile)); + delete dbPath; + if(NS_FAILED(rv)) + return rv; + } + } + + PRBool bExists=PR_FALSE; + mABFile->Exists(&bExists); + if(bExists) { + mPreviousABFile->Exists(&bExists); + if(bExists) + rv = mPreviousABFile->Remove(PR_FALSE); + nsCOMPtr parent; + rv = mABFile->GetParent(getter_AddRefs(parent)); + if (NS_SUCCEEDED(rv)) + rv = mABFile->CopyToNative(parent, previousLeafName); + } + return rv; +} + + +nsresult nsAbPalmHotSync::UpdateMozABWithPalmRecords() +{ + if(!mInitialized || !mABDB || !mDBOpen) + return NS_ERROR_NOT_INITIALIZED; + + nsresult rv = NS_OK; + + for(PRInt32 i=mPalmRecords.Count()-1; i >=0; i--) { + nsABCOMCardStruct * palmRec = (nsABCOMCardStruct *)mPalmRecords.ElementAt(i); + nsAbIPCCard ipcCard(palmRec, PR_FALSE); + + char recordIDBuf[128]; + PRInt64 l; + LL_UI2L(l, palmRec->dwRecordId); + PRFloat64 f; + LL_L2F(f, l); + PR_cnvtf(recordIDBuf, 128, 0, f); + + // if the card already exist + nsCOMPtr existingCard; + rv = mABDB->GetCardFromAttribute(nsnull, CARD_ATTRIB_PALMID, recordIDBuf, + PR_FALSE, getter_AddRefs(existingCard)); + if(NS_SUCCEEDED(rv) && existingCard) { + if(palmRec->dwStatus == ATTR_DELETED) { + mABDB->DeleteCard(existingCard, PR_FALSE); + continue; + } + if(palmRec->dwStatus == ATTR_NEW) + continue; + if(palmRec->dwStatus == ATTR_MODIFIED) { + PRBool isEqual=PR_FALSE; + ipcCard.Equals(existingCard, &isEqual); + if(isEqual) + continue; + else { + existingCard->Copy(&ipcCard); + rv = mABDB->EditCard(existingCard, PR_FALSE); + continue; + } + } + } + + nsCOMPtr dbCard; + dbCard = do_CreateInstance(NS_ABMDBCARD_CONTRACTID, &rv); + if(NS_FAILED(rv)) + continue; + + nsCOMPtr newCard; + newCard = do_QueryInterface(dbCard, &rv); + if(NS_FAILED(rv)) + continue; + + rv = newCard->Copy(&ipcCard); + if(NS_FAILED(rv)) + continue; + + // if the card does not exist + if((ipcCard.GetStatus() == ATTR_NEW) + ||(ipcCard.GetStatus() == ATTR_MODIFIED) + || (ipcCard.GetStatus() == ATTR_NONE)) { + PRUint32 modTimeInSec; + nsAddrDatabase::PRTime2Seconds(PR_Now(), &modTimeInSec); + ipcCard.SetLastModifiedDate(modTimeInSec); + rv = mABDB->CreateNewCardAndAddToDB(newCard, PR_FALSE); + if(NS_SUCCEEDED(rv)) { + // now set the attribute for the PalmRecID in the card in the DB + dbCard->SetAbDatabase(mABDB); + dbCard->SetStringAttribute(CARD_ATTRIB_PALMID, NS_ConvertASCIItoUCS2(recordIDBuf).get()); + newCard = do_QueryInterface(dbCard, &rv); + if(NS_SUCCEEDED(rv)) + rv = mABDB->EditCard(newCard, PR_FALSE); + } + } + } + + return rv; +} + + +nsresult nsAbPalmHotSync::Done(PRBool aSuccess, PRInt32 aPalmCatID, PRUint32 aPalmRecIDListCount, unsigned long * aPalmRecordIDList) +{ + if(!mInitialized) + return NS_ERROR_NOT_INITIALIZED; + + nsresult rv=NS_ERROR_UNEXPECTED; + + if(aPalmRecIDListCount == mNewCardCount) { + for(PRUint32 i=0; i dbCard; + rv = mNewCardsArray->QueryElementAt(i, NS_GET_IID(nsIAbMDBCard), getter_AddRefs(dbCard)); + if(NS_SUCCEEDED(rv) && dbCard) { + ConvertAssignPalmIDAttrib(aPalmRecordIDList[i], dbCard); + nsCOMPtr newCard; + newCard = do_QueryInterface(dbCard, &rv); + if(NS_SUCCEEDED(rv)) + mABDB->EditCard(newCard, PR_FALSE); + } + } + } + + if(mABDB && mDBOpen) { + if(aSuccess) { + rv = mABDB->Close(PR_TRUE); + if(NS_SUCCEEDED(rv)) { + mDBOpen = PR_FALSE; + PRUint32 modTimeInSec; + nsAddrDatabase::PRTime2Seconds(PR_Now(), &modTimeInSec); + mDirServerInfo->PalmSyncTimeStamp = modTimeInSec; + mDirServerInfo->PalmCategoryId = aPalmCatID; + DIR_SavePrefsForOneServer(mDirServerInfo); + } + } + if(NS_FAILED(rv) || !aSuccess) { // get back the previous file + rv = mABDB->ForceClosed(); + if(NS_SUCCEEDED(rv)) { + nsCAutoString leafName; + mABFile->GetNativeLeafName(leafName); + PRBool bExists=PR_FALSE; + mPreviousABFile->Exists(&bExists); + if(bExists) { + nsCOMPtr parent; + rv = mABFile->GetParent(getter_AddRefs(parent)); + if (NS_SUCCEEDED(rv)) { + mABFile->Remove(PR_FALSE); + mPreviousABFile->CopyToNative(parent, leafName); + } + } + mDBOpen = PR_FALSE; + } + } + } + + return rv; +} diff --git a/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.h b/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.h new file mode 100644 index 00000000000..dc28fb013d5 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsAbPalmSync.h @@ -0,0 +1,133 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsAbPalmHotSync_h__ +#define nsAbPalmHotSync_h__ + +#include "msgMapi.h" +#include "nsString.h" +#include "nsAbIPCCard.h" +#include "nsIAbCard.h" +#include "nsAddrDatabase.h" +#include "nsILocalFile.h" +#include "nsIAbDirectory.h" + +class nsAbPalmHotSync +{ +public: + + // this class will do HotSync for a specific AB + nsAbPalmHotSync(PRBool aIsUnicode, PRUnichar * aAbDescUnicode, char * aAbDesc, PRInt32 aPalmCatID); + ~nsAbPalmHotSync(); + + // initialize the object, info for AB for the object, etc + nsresult Initialize(); + + // this will take in all updated/new Palm records and start Sync. + // Sync process will load updated/new cards and compare with updated/new + // Palm records to create final list to be sent to Palm and to update AB. + nsresult DoSyncAndGetUpdatedCards(PRInt32 aPalmCount, lpnsABCOMCardStruct aPalmRecords, PRInt32 * aMozCount, lpnsABCOMCardStruct * aMozCards); + + // this will return all cards in an AB + nsresult GetAllCards(PRInt32 * aCount, lpnsABCOMCardStruct * aCardList); + + // this will return the final list created by above Sync methods + // allocates aCardList as a memory block and populates it with mIPCCardList to be sent to Palm + nsresult GetCardsToBeSentToPalm(PRInt32 * aCount, lpnsABCOMCardStruct * aCardList); + + // this will create a new AB and all data into it + nsresult AddAllRecordsInNewAB(PRInt32 aCount, lpnsABCOMCardStruct aPalmRecords); + + // this will be called when an AckSyncDone is recieved from the Conduit + nsresult Done(PRBool aSuccess, PRInt32 aPalmCatID, PRUint32 aPalmRecIDListCount = 0, unsigned long * aPalmRecordIDList = nsnull); + +protected: + + PRBool mIsPalmDataUnicode; + + // data members related to AB DB + PRUint32 mTotalCardCount; + nsCOMPtr mABDB; + nsCOMPtr mABFile; + PRBool mDBOpen; + // pref for the AB DB + nsString mAbName; + DIR_Server * mDirServerInfo; + PRInt32 mPalmCategoryId; + // cards directory for the AB + nsCOMPtr mDirectory; + + // previous AB, the AB state before the last sync + nsCOMPtr mPreviousABFile; + + // Palm records list + nsVoidArray mPalmRecords; + // CardList to be sent to Palm + PRUint32 mCardForPalmCount; + lpnsABCOMCardStruct mCardListForPalm; + // since cards are already in memory because the db is open, + // this doesn't add much to memory overhead + PRUint32 mNewCardCount; + nsCOMPtr mNewCardsArray; + PRBool mIsNewCardForPalm; + + PRBool mInitialized; +protected: + // disallow calling of default constructor + nsAbPalmHotSync(); + + // this will take care of opening, nsIAbDirectroy creation, DB backup, etc + nsresult OpenABDBForHotSync(PRBool aCreate); + nsresult KeepCurrentStateAsPrevious(); + nsresult UpdateMozABWithPalmRecords(); + + // this will load and create a list of cards since last sync + nsresult LoadNewModifiedCardsSinceLastSync(); + nsresult LoadDeletedCardsSinceLastSync(); + + // Checks with the corresponding Palm record for any discrepencies + // if any modification needed, this will return the modified card + // returns if card to be kept or discarded completely + PRBool CheckWithPalmRecord(nsAbIPCCard * aIPCCard); + + // utility function + nsresult AddToListForPalm(nsAbIPCCard & ipcCard); + void ConvertAssignPalmIDAttrib(PRUint32 id, nsIAbMDBCard * card); + +}; + +#endif diff --git a/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.cpp b/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.cpp new file mode 100644 index 00000000000..375bafe435a --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.cpp @@ -0,0 +1,190 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + * Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +#include "nsCOMPtr.h" +#include "objbase.h" +#include "nsISupports.h" + +#include "nsIGenericFactory.h" +#include "nsIObserverService.h" +#include "nsIAppStartupNotifier.h" +#include "nsIServiceManager.h" +#include "nsIComponentManager.h" +#include "nsICategoryManager.h" +#include "nsCRT.h" + +#include "nsIPrefService.h" +#include "nsIPrefBranch.h" +#include "nsIPrefBranchInternal.h" + +#include "PalmSyncImp.h" +#include "nsPalmSyncSupport.h" +#include "Registry.h" + +/** Implementation of the IPalmSyncSupport interface. + * Use standard implementation of nsISupports stuff. + * This also implements the Observors for App Startup + * at which time it will register the MSCOM interface + * for PalmSync used for IPC with the Palm Conduits + * + */ + +NS_IMPL_THREADSAFE_ISUPPORTS2(nsPalmSyncSupport, nsIPalmSyncSupport, nsIObserver); + +static NS_METHOD nsPalmSyncRegistrationProc(nsIComponentManager *aCompMgr, + nsIFile *aPath, const char *registryLocation, const char *componentType, + const nsModuleComponentInfo *info) +{ + + nsresult rv; + + nsCOMPtr categoryManager(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv)); + if (NS_SUCCEEDED(rv)) + rv = categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY, "PalmSync Support", + "service," NS_IPALMSYNCSUPPORT_CONTRACTID, PR_TRUE, PR_TRUE, nsnull); + + return rv ; + +} + +NS_IMETHODIMP +nsPalmSyncSupport::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData) +{ + nsresult rv = NS_OK ; + + if (!strcmp(aTopic, "profile-after-change")) + return InitializePalmSyncSupport(); + + if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) + return ShutdownPalmSyncSupport(); + + nsCOMPtr observerService(do_GetService("@mozilla.org/observer-service;1", &rv)); + if (NS_FAILED(rv)) return rv; + + rv = observerService->AddObserver(this,"profile-after-change", PR_FALSE); + if (NS_FAILED(rv)) return rv; + + return observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE); +} + + +nsPalmSyncSupport::nsPalmSyncSupport() +: m_dwRegister(0), + m_nsPalmSyncFactory(nsnull) +{ + NS_INIT_ISUPPORTS(); +} + +nsPalmSyncSupport::~nsPalmSyncSupport() +{ +} + +NS_IMETHODIMP +nsPalmSyncSupport::InitializePalmSyncSupport() +{ + ::CoInitialize(nsnull) ; + + if (m_nsPalmSyncFactory == nsnull) // No Registering if already done. Sanity Check!! + { + m_nsPalmSyncFactory = new CPalmSyncFactory(); + + if (m_nsPalmSyncFactory != nsnull) + { + HRESULT hr = ::CoRegisterClassObject(CLSID_CPalmSyncImp, \ + m_nsPalmSyncFactory, \ + CLSCTX_LOCAL_SERVER, \ + REGCLS_MULTIPLEUSE, \ + &m_dwRegister); + + if (FAILED(hr)) + { + m_nsPalmSyncFactory->Release() ; + m_nsPalmSyncFactory = nsnull; + return NS_ERROR_FAILURE; + } + } + } + + return NS_OK; +} + +NS_IMETHODIMP +nsPalmSyncSupport::ShutdownPalmSyncSupport() +{ + if (m_dwRegister != 0) + ::CoRevokeClassObject(m_dwRegister); + + if (m_nsPalmSyncFactory != nsnull) + { + m_nsPalmSyncFactory->Release(); + m_nsPalmSyncFactory = nsnull; + } + + ::CoUninitialize(); + + return NS_OK ; +} + +NS_IMETHODIMP +nsPalmSyncSupport::RegisterPalmSync() +{ + return RegisterServerForPalmSync(CLSID_CPalmSyncImp, "Mozilla PalmSync", "mozMapi", "mozMapi.1"); +} + +NS_IMETHODIMP +nsPalmSyncSupport::UnRegisterPalmSync() +{ + return UnregisterServerForPalmSync(CLSID_CPalmSyncImp, "mozMapi", "mozMapi.1"); +} + + +NS_GENERIC_FACTORY_CONSTRUCTOR(nsPalmSyncSupport); + +// The list of components we register +static const nsModuleComponentInfo components[] = +{ + { + NS_IPALMSYNCSUPPORT_CLASSNAME, + NS_IPALMSYNCSUPPORT_CID, + NS_IPALMSYNCSUPPORT_CONTRACTID, + nsPalmSyncSupportConstructor, + nsPalmSyncRegistrationProc, + nsnull + } +}; + +NS_IMPL_NSGETMODULE(PalmSyncModule, components); + diff --git a/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.h b/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.h new file mode 100644 index 00000000000..fc6f995e758 --- /dev/null +++ b/mozilla/mailnews/extensions/palmsync/src/nsPalmSyncSupport.h @@ -0,0 +1,69 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla + * + * The Initial Developer of the Original Code is + # Netscape Communications Corp. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rajiv Dayal + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef MSG_PALMSYNC_SUPPORT_H_ +#define MSG_PALMSYNC_SUPPORT_H_ + +#include "nsIObserver.h" +#include "nsIPalmSyncSupport.h" +#include "PalmSyncFactory.h" + +// {44812571-CE84-11d6-B8A6-00B0D06E5F27} +#define NS_IPALMSYNCSUPPORT_CID \ + { 0x44812571, 0xce84, 0x11d6, \ + { 0xb8, 0xa6, 0x0, 0xb0, 0xd0, 0x6e, 0x5f, 0x27 } } + + +class nsPalmSyncSupport : public nsIPalmSyncSupport, + public nsIObserver +{ + public : + nsPalmSyncSupport(); + ~nsPalmSyncSupport(); + + // Declare all interface methods we must implement. + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER + NS_DECL_NSIPALMSYNCSUPPORT + + private : + + DWORD m_dwRegister; + CPalmSyncFactory *m_nsPalmSyncFactory; +}; + +#endif // MSG_PALMSYNC_SUPPORT_H_