| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | import dbus |
|---|
| 5 | from sys import argv |
|---|
| 6 | from time import sleep |
|---|
| 7 | |
|---|
| 8 | # define some nice dbus helper, which I really like, cause make code easier to read :) |
|---|
| 9 | def getDbusObject (bus, busname , objectpath , interface): |
|---|
| 10 | dbusObject = bus.get_object(busname, objectpath) |
|---|
| 11 | return dbus.Interface(dbusObject, dbus_interface=interface) |
|---|
| 12 | |
|---|
| 13 | def print_array(array): |
|---|
| 14 | for entry in array: |
|---|
| 15 | print ' - ' + str(entry) |
|---|
| 16 | |
|---|
| 17 | def strip_tel(value): |
|---|
| 18 | if value.startswith('tel:'): |
|---|
| 19 | return value[4:] |
|---|
| 20 | elif value.startswith('mail:'): |
|---|
| 21 | return value[5:] |
|---|
| 22 | else: |
|---|
| 23 | return False |
|---|
| 24 | |
|---|
| 25 | bus = dbus.SystemBus() |
|---|
| 26 | |
|---|
| 27 | initialized = False |
|---|
| 28 | print "Waiting for opimd to be ready..." |
|---|
| 29 | while (not initialized): |
|---|
| 30 | try: |
|---|
| 31 | interface = getDbusObject (bus, "org.freesmartphone.opimd", "/org/freesmartphone/PIM/Contacts", "org.freesmartphone.PIM.Contacts") |
|---|
| 32 | types = getDbusObject (bus, "org.freesmartphone.opimd", "/org/freesmartphone/PIM/Contacts", "org.freesmartphone.PIM.Fields") |
|---|
| 33 | initialized = True |
|---|
| 34 | except: |
|---|
| 35 | sleep(10) |
|---|
| 36 | |
|---|
| 37 | x = interface.Query({}) |
|---|
| 38 | |
|---|
| 39 | query = getDbusObject (bus, "org.freesmartphone.opimd", x, "org.freesmartphone.PIM.ContactQuery") |
|---|
| 40 | |
|---|
| 41 | def list_phone_fields(): |
|---|
| 42 | return types.ListFieldsWithType('phonenumber') |
|---|
| 43 | |
|---|
| 44 | def list_mail_fields(): |
|---|
| 45 | return types.ListFieldsWithType('email') |
|---|
| 46 | |
|---|
| 47 | def add_phone_field(field): |
|---|
| 48 | global phone_fields |
|---|
| 49 | if not field in phone_fields: |
|---|
| 50 | types.AddField(field, 'phonenumber') |
|---|
| 51 | phone_fields.append(field) |
|---|
| 52 | |
|---|
| 53 | def add_mail_field(field): |
|---|
| 54 | global mail_fields |
|---|
| 55 | if not field in mail_fields: |
|---|
| 56 | types.AddField(field, 'email') |
|---|
| 57 | mail_fields.append(field) |
|---|
| 58 | |
|---|
| 59 | try: |
|---|
| 60 | phone_fields = list_phone_fields() |
|---|
| 61 | mail_fields = list_mail_fields() |
|---|
| 62 | except: |
|---|
| 63 | print "Error: Couldn't get field types. Still running old opimd?" |
|---|
| 64 | exit(1) |
|---|
| 65 | |
|---|
| 66 | results = query.GetResultCount() |
|---|
| 67 | for i in range(0, results): |
|---|
| 68 | x = query.GetContactPath() |
|---|
| 69 | print 'Processing ' + x |
|---|
| 70 | result = getDbusObject (bus, "org.freesmartphone.opimd", x, "org.freesmartphone.PIM.Contact") |
|---|
| 71 | content = result.GetContent() |
|---|
| 72 | to_update = {} |
|---|
| 73 | for field in content: |
|---|
| 74 | field = str(field) |
|---|
| 75 | if field.lower().endswith('phone'): |
|---|
| 76 | print ' Found field ' + field |
|---|
| 77 | if type(content[field]) == dbus.Array: |
|---|
| 78 | fresult = [] |
|---|
| 79 | for entry in content[field]: |
|---|
| 80 | fresult.append(strip_tel(entry)) |
|---|
| 81 | else: |
|---|
| 82 | fresult = strip_tel(content[field]) |
|---|
| 83 | if fresult: |
|---|
| 84 | to_update[field] = fresult |
|---|
| 85 | add_phone_field(field) |
|---|
| 86 | if field.lower().endswith('e-mail') or field.lower().endswith('email'): |
|---|
| 87 | print ' Found field '+field |
|---|
| 88 | if type(content[field]) == dbus.Array: |
|---|
| 89 | fresult = [] |
|---|
| 90 | for entry in content[field]: |
|---|
| 91 | fresult.append(strip_tel(entry)) |
|---|
| 92 | else: |
|---|
| 93 | fresult = strip_tel(content[field]) |
|---|
| 94 | if fresult: |
|---|
| 95 | to_update[field] = fresult |
|---|
| 96 | add_mail_field(field) |
|---|
| 97 | try: |
|---|
| 98 | if to_update!={}: |
|---|
| 99 | print to_update |
|---|
| 100 | print ' Updating entry...' |
|---|
| 101 | result.Update(to_update) |
|---|
| 102 | else: |
|---|
| 103 | print " Nothing to update" |
|---|
| 104 | except: |
|---|
| 105 | print ' Failed to update entry!' |
|---|
| 106 | print 'Finished' |
|---|
| 107 | print 'Fields with phonenumber type:' |
|---|
| 108 | print_array( phone_fields ) |
|---|
| 109 | print 'Fields with email type:' |
|---|
| 110 | print_array( mail_fields ) |
|---|