new-opimd: remove-tel

File remove-tel, 3.1 kB (added by mrmoku, 6 months ago)
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import dbus
5from sys import argv
6from time import sleep
7
8# define some nice dbus helper, which I really like, cause make code easier to read :)
9def getDbusObject (bus, busname , objectpath , interface):
10        dbusObject = bus.get_object(busname, objectpath)
11        return dbus.Interface(dbusObject, dbus_interface=interface)
12
13def print_array(array):
14  for entry in array:
15    print ' - ' + str(entry)
16
17def 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
25bus = dbus.SystemBus()
26
27initialized = False
28print "Waiting for opimd to be ready..."
29while (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
37x = interface.Query({})
38
39query = getDbusObject (bus, "org.freesmartphone.opimd", x, "org.freesmartphone.PIM.ContactQuery")
40
41def list_phone_fields():
42  return types.ListFieldsWithType('phonenumber')
43
44def list_mail_fields():
45  return types.ListFieldsWithType('email')
46
47def 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
53def 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
59try:
60  phone_fields = list_phone_fields()
61  mail_fields = list_mail_fields()
62except:
63  print "Error: Couldn't get field types. Still running old opimd?"
64  exit(1)
65
66results = query.GetResultCount()
67for 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!'
106print 'Finished'
107print 'Fields with phonenumber type:'
108print_array( phone_fields )
109print 'Fields with email type:'
110print_array( mail_fields )