Index: conduit/modules/GoogleModule/GoogleModule.py
===================================================================
--- conduit/modules/GoogleModule/GoogleModule.py	(revision 1924)
+++ conduit/modules/GoogleModule/GoogleModule.py	(working copy)
@@ -811,6 +811,7 @@
     def __init__(self, *args):
         _GoogleBase.__init__(self,gdata.contacts.service.ContactsService())
         DataProvider.TwoWay.__init__(self)
+        self.allContacts = None
         
     def _google_contact_from_conduit_contact(self, contact, gc=None):
         """
@@ -830,16 +831,16 @@
 
         #Create all emails, make first one primary, if the contact doesnt
         #already have a primary email address
-        primary = 'false'
+        primary = 'true'
         existing = []
         for ex in gc.email:
             if ex.primary and ex.primary == 'true':
-                primary = 'true'
+                primary = 'false'
                 existing.append(ex)      
         
         for email in emails:
             if email not in existing:
-                log.debug("Adding new email address %s %s" % (email, existing))
+                log.debug("Adding new email address %s %s setting primary to %s" % (email, existing,  primary))
                 gc.email.append(gdata.contacts.Email(
                                             address=email, 
                                             primary=primary))#,rel=gdata.contacts.REL_WORK))
@@ -876,17 +877,23 @@
     def _create_contact(self, contact):
         gc = self._google_contact_from_conduit_contact(contact)
         if not gc:
-            log.info("Could not create google contact from conduit contact")
+            log.info("Could not create google contact object from conduit contact")
             return None
-
         try:            
             entry = self.service.CreateContact(gc)
         except gdata.service.RequestError, e:
             #If the response dict reson is 'Conflict' then we are trying to
             #store a contact with the same email as one which already exists
             if e.message.get("reason","") == "Conflict":
-                log.warn("FIXME: FIND THE OLD CONTACT BY EMAIL, GET IT, AND RAISE A CONFLICT EXCEPTION")
-                raise Exceptions.SynchronizeConflictError("FIXME", "FIXME", "FIXME")
+                for email in gc.email:
+                    if email.primary:
+                        log.debug("Looking up id for email address %s" % email.address)
+                        if self.allContacts.has_key(email.address):
+                            id=self.allContacts[email.address]
+                            log.debug('Found id %s for email %s' %(id,  email.address))
+                            oldData=self._get_contact(id)
+                comp = contact.compare(oldData)
+                raise Exceptions.SynchronizeConflictError(comp, contact, oldData)
         except Exception, e:
             log.warn("Error creating contact: %s" % e)
             return None
@@ -915,7 +922,9 @@
         return LUID
     
     def _get_contact(self, LUID):
+        log.debug("Getting contact LUID = %s" % LUID)
         if not LUID:
+            log.debug("Asked to get a contact but given no LUID")
             return None
 
         #get the gdata contact from google
@@ -933,13 +942,23 @@
         feed = self.service.GetContactsFeed()
         if not feed.entry:
             return []
-        return [str(contact.id.text) for contact in feed.entry]
+        res = []
+        while True:
+            for contact in feed.entry:
+                res.append(str(contact.id.text))
+            nextLink = feed.GetNextLink()
+            if nextLink == None:
+                break
+            feed = self.service.GetContactsFeed(uri=nextLink.href)
+        log.debug("Retrieved %s contacts from Google" % len(res))
+        return res
         
     def refresh(self):
         DataProvider.TwoWay.refresh(self)
         self._login()
         if not self.loggedIn:
             raise Exceptions.RefreshError("Could not log in")
+        self._get_all_uids()
 
     def get_all(self):
         DataProvider.TwoWay.get_all(self)
@@ -953,9 +972,41 @@
         if c == None:
             log.warn("Error getting/parsing gdata contact")
         return c
-        
+    
+    def _get_all_uids(self):
+        '''gdata doesn't support searching for a contact be email so get all contacts and build a dictionary'''
+        log.debug("Looking up all email addresses in existing gmail addressbook")
+        if self.allContacts:
+            query=gdata.contacts.service.ContactsQuery()
+            query.updated_min = self.allContactsUpdated
+            feed=self.service.GetContactsFeed(query.ToUri())
+        else:
+            feed = self.service.GetContactsFeed()
+        if not feed.entry:
+            return []
+        res = {}
+        while True:
+            for contact in feed.entry:
+                id=contact.id.text
+                if len(contact.email)==1:
+                    primary_email_address = contact.email[0].address
+                    res[primary_email_address]=id
+                else:
+                    for email in contact.email:
+                        if email.primary:
+                            primary_email_address = email.address
+                            res[primary_email_address]=id
+            log.debug("Getting next 25 results")
+            nextLink = feed.GetNextLink()
+            if nextLink == None:
+                break
+            feed = self.service.GetContactsFeed(uri=nextLink.href)
+        log.debug("Retrieved %s contacts from Google" % len(res.keys()))
+        self.allContacts=res
+        self.allContactsUpdated = feed.updated.text
+            
     def put(self, data, overwrite, LUID=None):
-        #http://www.conduit-project.org/wiki/WritingADataProvider/GeneralPutInstructions
+        '''If LUID exists then we have synced this data before'''  
         DataProvider.TwoWay.put(self, data, overwrite, LUID)
         if overwrite and LUID:
             LUID = self._update_contact(LUID, data)
@@ -975,6 +1026,7 @@
                     raise Exceptions.SynchronizeConflictError(comp, data, oldData)
             else:
                 #Possibility 5:
+                log.debug("No LUID so creating contact")
                 LUID = self._create_contact(data)
                 
         #now return the rid
