import time import contacts import SOAPpy import mws from SOAPpy.Types import faultType, voidType, simplify, booleanType # Haven't found any method to add a contact to a specific group, thus that has not be implemented # Apart from that all methods have been implemented class ContactsMWS(mws.mwsAuthentication, mws.mwsGeneral): version = "v0.9" author = "Magnus Agust Skulason, maggias@gmail.com" def getContact(self, id, resultFilter): try: #define a dictionary variable to contain the fields for this contact contactFields = {} intId = 0 if id != None and id != "": intId = int(id) else: raise faultType("SOAP-ENV:Server","getContacts failed.","id is missing or of incorrect type, should be Integer") #open the default contacts database contactDatabase = contacts.open() try: #read the specific contact contact = contactDatabase[intId] contactFields = self.__createOutput__(contact,resultFilter) contactFields['id'] = intId except: raise faultType("SOAP-ENV:Server","getContacts failed.","An exception occurred at index: %s" % repr(index)) return contactFields except Exception, e: raise faultType("SOAP-ENV:Server","getContact failed.",e) def insertContact(self, contactDetails): try: #open the default contacts database contactDatabase = contacts.open() # add a contact contact = contactDatabase.add_contact() contact = self.__parseContactDetails__(contact,contactDetails) contact.commit() return contact.id except Exception, e: raise faultType("SOAP-ENV:Server","insertContact failed.",e) def updateContact(self, contactDetails): try: intId = 0 if 'id' in contactDetails._keyord: intId = int(contactDetails['id']) else: raise faultType("SOAP-ENV:Server","getContacts failed.","id is missing or of incorrect type, should be Integer") #open the default contacts database contactDatabase = contacts.open() #read the specific contact contact = contactDatabase[intId] contact.begin() contact = self.__parseContactDetails__(contact,contactDetails) contact.commit() return "success" except Exception, e: raise faultType("SOAP-ENV:Server","updateContact failed.",e) def deleteContact(self, id): try: intId = 0 if id != None and id != "": intId = int(id) else: raise faultType("SOAP-ENV:Server","getContacts failed.","id is missing or of incorrect type, should be Integer") #open the default contacts database contactDatabase = contacts.open() contactDatabase.__delitem__(intId) contactDatabase.compact() return "success" except Exception, e: raise faultType("SOAP-ENV:Server","getContacts failed.",e) def getContacts(self, resultFilter): try: # Array for outputing all the contacts output = [] #open the default contacts database contactDatabase = contacts.open() #Read the contacts contactsKeyList = contactDatabase.keys() #Loop through all contacts for index in range(0,len(contactsKeyList)): try: #read the specific contact contact = contactDatabase[contactsKeyList[index]] #define a dictionary variable to contain the fields for this contact contactFields = {} #loop through all fields contactFields = self.__createOutput__(contact,resultFilter) contactFields['id'] = contactsKeyList[index] except: raise faultType("SOAP-ENV:Server","getContacts failed.","An exception occurred at index: %s" % repr(index)) #Add the contact to the ouput array output.append(contactFields) return output except Exception, e: raise faultType("SOAP-ENV:Server","getContacts failed.",e) def findContacts (self, searchTerm, resultFilter): try: # Array for outputing all the contacts output = [] #open the default contacts database contactDatabase = contacts.open() #Read the contacts contactsKeyList = contactDatabase.find(searchTerm) #Loop through all contacts for index in range(0,len(contactsKeyList)): try: contact = contactsKeyList[index] #define a dictionary variable to contain the fields for this contact contactFields = {} contactFields = self.__createOutput__(contact,resultFilter) except: raise faultType("SOAP-ENV:Server","findContacts failed.","An exception occurred at index: %s" % repr(index)) #Add the contact to the ouput array output.append(contactFields) return output except Exception, e: raise faultType("SOAP-ENV:Server","findContacts failed.",e) def importVCards(self, vcards): # The underlying PyS60 function import_vcards does not seem to work, I have tried several VCard strings (taken from the device) and they don't import successfully. # The function works otherwise, that is there are not errors when executed so in theory it should work on a correct VCard string try: # open the default contacts database contactDatabase = contacts.open() # Import the vcards contactDatabase.import_vcards(vcards) # Compact the contact database contactDatabase.compact return "success" except Exception, e: raise faultType("SOAP-ENV:Server","importVCard failed.",e) def createGroup(self, name): try: #raise faultType("SOAP-ENV:Server","createGroup failed.","Has not been implemented yet.") #open the default contacts database contactDatabase = contacts.open() groups = contactDatabase.groups groups.add_group(name) contactDatabase.compact() return "success" except Exception, e: raise faultType("SOAP-ENV:Server","createGroup failed.",e) def deleteGroup(self, id): try: intId = 0 if id != None and id != "": intId = int(id) else: raise faultType("SOAP-ENV:Server","getContacts failed.","id is missing or of incorrect type, should be Integer") #open the default contacts database contactDatabase = contacts.open() contact = contactDatabase[intId] if contact.is_group: contactDatabase.__delitem__(intId) else: raise faultType("SOAP-ENV:Server","getContacts failed.","Not a group") contactDatabase.compact() return "success" except Exception, e: raise faultType("SOAP-ENV:Server","deleteGroup failed.",e) def getGroups(self): try: output = [] #open the default contacts database contactDatabase = contacts.open() groups = contactDatabase.groups groups_iter = groups.__iter__() while True: try: groupOut = {} groupId = groups_iter.next() group = groups.__getitem__(groupId) id = group.id name = group.name groupOut['id'] = id groupOut['name'] = name output.append(groupOut) except StopIteration: break return output except Exception, e: raise faultType("SOAP-ENV:Server","getGroups failed.",e) def moveToGroup(self, contactId, contactName, groupId, groupName): try: raise faultType("SOAP-ENV:Server","createGroup failed.","Has not been implemented yet.") except Exception, e: raise faultType("SOAP-ENV:Server","createGroup failed.",e) def __parseContactDetails__(self, contact, contactDetails): if 'city' in contactDetails._keyord: contact.add_field('city',contactDetails['city']) if 'companyName' in contactDetails._keyord: contact.add_field('company_name',contactDetails['companyName']) if 'country' in contactDetails._keyord: contact.add_field('country',contactDetails['country']) if 'date' in contactDetails._keyord: contact.add_field('date',contactDetails['date']) if 'dtmfString' in contactDetails._keyord: contact.add_field('dtmf_string',contactDetails['dtmfString']) if 'emailAddress' in contactDetails._keyord: contact.add_field('email_address',contactDetails['emailAddress']) if 'extendedAddress' in contactDetails._keyord: contact.add_field('extended_address',contactDetails['extendedAddress']) if 'faxNumber' in contactDetails._keyord: contact.add_field('fax_number',contactDetails['faxNumber']) if 'firstName' in contactDetails._keyord: contact.add_field('first_name',contactDetails['firstName']) if 'jobTitle' in contactDetails._keyord: contact.add_field('job_title',contactDetails['jobTitle']) if 'lastName' in contactDetails._keyord: contact.add_field('last_name',contactDetails['lastName']) if 'mobileNumber' in contactDetails._keyord: contact.add_field('mobile_number',contactDetails['mobileNumber']) if 'pagerNumber' in contactDetails._keyord: contact.add_field('pager_number',contactDetails['pagerNumber']) if 'phoneNumber' in contactDetails._keyord: contact.add_field('phone_number',contactDetails['phoneNumber']) if 'poBox' in contactDetails._keyord: contact.add_field('po_box',contactDetails['poBox']) if 'postalAddress' in contactDetails._keyord: contact.add_field('postal_address',contactDetails['postalAddress']) if 'postalCode' in contactDetails._keyord: contact.add_field('postal_code',contactDetails['postalCode']) if 'state' in contactDetails._keyord: contact.add_field('state',contactDetails['state']) if 'streetAddress' in contactDetails._keyord: contact.add_field('street_address',contactDetails['streetAddress']) if 'url' in contactDetails._keyord: contact.add_field('url',contactDetails['url']) if 'videoNumber' in contactDetails._keyord: contact.add_field('video_number',contactDetails['videoNumber']) if 'wvid' in contactDetails._keyord: contact.add_field('wvid',contactDetails['wvid']) if 'firstNameReading' in contactDetails._keyord: contact.add_field('first_name_reading',contactDetails['firstNameReading']) if 'lastNameReading' in contactDetails._keyord: contact.add_field('last_name_reading',contactDetails['lastNameReading']) if 'picture' in contactDetails._keyord: contact.add_field('picture',contactDetails['picture']) if 'speedDial' in contactDetails._keyord: contact.add_field('speed_dial',contactDetails['speedDial']) if 'thumbnailImage' in contactDetails._keyord: contact.add_field('thumbnail_image',contactDetails['thumbnailImage']) if 'voicetag' in contactDetails._keyord: contact.add_field('voicetag',contactDetails['voicetag']) if 'title' in contactDetails._keyord: contact.title = contactDetails['title'] return contact def __createOutput__(self, contact, resultFilter): #define a dictionary variable to contain the fields for this contact contactFields = {} #loop through all fields for field in contact: #if the field has type and value store it in the dictionary variable if field.type and field.value: if field.type == 'company_name': if resultFilter == "" or resultFilter.find('companyName') != -1: contactFields['companyName'] = field.value elif field.type == 'dtmf_string': if resultFilter == "" or resultFilter.find('dtmfString') != -1: contactFields['dtmfString'] = field.value elif field.type == 'email_address': if resultFilter == "" or resultFilter.find('emailAddress') != -1: contactFields['emailAddress'] = field.value elif field.type == 'extended_address': if resultFilter == "" or resultFilter.find('extendedAddress') != -1: contactFields['extendedAddress'] = field.value elif field.type == 'fax_number': if resultFilter == "" or resultFilter.find('faxNumber') != -1: contactFields['faxNumber'] = field.value elif field.type == 'first_name': if resultFilter == "" or resultFilter.find('firstName') != -1: contactFields['firstName'] = field.value elif field.type == 'job_title': if resultFilter == "" or resultFilter.find('jobTitle') != -1: contactFields['jobTitle'] = field.value elif field.type == 'last_name': if resultFilter == "" or resultFilter.find('lastName') != -1: contactFields['lastName'] = field.value elif field.type == 'mobile_number': if resultFilter == "" or resultFilter.find('mobileNumber') != -1: contactFields['mobileNumber'] = field.value elif field.type == 'pager_number': if resultFilter == "" or resultFilter.find('pagerNumber') != -1: contactFields['pagerNumber'] = field.value elif field.type == 'phone_number': if resultFilter == "" or resultFilter.find('phoneNumber') != -1: contactFields['phoneNumber'] = field.value elif field.type == 'po_box': if resultFilter == "" or resultFilter.find('poBox') != -1: contactFields['poBox'] = field.value elif field.type == 'postal_address': if resultFilter == "" or resultFilter.find('postalAddress') != -1: contactFields['postalAddress'] = field.value elif field.type == 'postal_code': if resultFilter == "" or resultFilter.find('postalCode') != -1: contactFields['postalCode'] = field.value elif field.type == 'street_address': if resultFilter == "" or resultFilter.find('streetAddress') != -1: contactFields['streetAddress'] = field.value elif field.type == 'video_number': if resultFilter == "" or resultFilter.find('videoNumber') != -1: contactFields['videoNumber'] = field.value elif field.type == 'first_name_reading': if resultFilter == "" or resultFilter.find('firstNameReading') != -1: contactFields['firstNameReading'] = field.value elif field.type == 'last_name_reading': if resultFilter == "" or resultFilter.find('lastNameReading') != -1: contactFields['lastNameReading'] = field.value elif field.type == 'speed_dial': if resultFilter == "" or resultFilter.find('speedDial') != -1: contactFields['speedDial'] = field.value elif field.type == 'thumbnail_image': if resultFilter == "" or resultFilter.find('thumbnailImage') != -1: contactFields['thumbnailImage'] = field.value elif field.type == 'unknown': if resultFilter == "" or resultFilter.find('synchronization') != -1: contactFields['synchronization'] = field.value else: if resultFilter == "" or resultFilter.find(field.type) != -1: contactFields[field.type] = field.value if resultFilter == "" or resultFilter.find('id') != -1: contactFields['id'] = contact.id if resultFilter == "" or resultFilter.find('title') != -1: contactFields['title'] = contact.title if resultFilter == "" or resultFilter.find('lastModified') != -1: if (self.getReturnSOAP()): contactFields['lastModified'] = SOAPpy.dateTimeType(contact.last_modified) else: contactFields['lastModified'] = contact.last_modified if resultFilter == "" or resultFilter.find('asVCard') != -1: contactFields['asVCard'] = contact.as_vcard() return contactFields