Small step towards null pattern
[gc-dialer] / src / gc_views.py
1 #!/usr/bin/python2.5
2
3 # DialCentral - Front end for Google's Grand Central service.
4 # Copyright (C) 2008  Mark Bergman bergman AT merctech DOT com
5
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20 import threading
21 import time
22 import warnings
23
24 import gobject
25 import gtk
26
27 try:
28         import hildon
29 except ImportError:
30         hildon = None
31
32
33 def make_ugly(prettynumber):
34         """
35         function to take a phone number and strip out all non-numeric
36         characters
37
38         >>> make_ugly("+012-(345)-678-90")
39         '01234567890'
40         """
41         import re
42         uglynumber = re.sub('\D', '', prettynumber)
43         return uglynumber
44
45
46 def make_pretty(phonenumber):
47         """
48         Function to take a phone number and return the pretty version
49         pretty numbers:
50                 if phonenumber begins with 0:
51                         ...-(...)-...-....
52                 if phonenumber begins with 1: ( for gizmo callback numbers )
53                         1 (...)-...-....
54                 if phonenumber is 13 digits:
55                         (...)-...-....
56                 if phonenumber is 10 digits:
57                         ...-....
58         >>> make_pretty("12")
59         '12'
60         >>> make_pretty("1234567")
61         '123-4567'
62         >>> make_pretty("2345678901")
63         '(234)-567-8901'
64         >>> make_pretty("12345678901")
65         '1 (234)-567-8901'
66         >>> make_pretty("01234567890")
67         '+012-(345)-678-90'
68         """
69         if phonenumber is None or phonenumber is "":
70                 return ""
71
72         if len(phonenumber) < 3:
73                 return phonenumber
74
75         if phonenumber[0] == "0":
76                 prettynumber = ""
77                 prettynumber += "+%s" % phonenumber[0:3]
78                 if 3 < len(phonenumber):
79                         prettynumber += "-(%s)" % phonenumber[3:6]
80                         if 6 < len(phonenumber):
81                                 prettynumber += "-%s" % phonenumber[6:9]
82                                 if 9 < len(phonenumber):
83                                         prettynumber += "-%s" % phonenumber[9:]
84                 return prettynumber
85         elif len(phonenumber) <= 7:
86                 prettynumber = "%s-%s" % (phonenumber[0:3], phonenumber[3:])
87         elif len(phonenumber) > 8 and phonenumber[0] == "1":
88                 prettynumber = "1 (%s)-%s-%s" % (phonenumber[1:4], phonenumber[4:7], phonenumber[7:])
89         elif len(phonenumber) > 7:
90                 prettynumber = "(%s)-%s-%s" % (phonenumber[0:3], phonenumber[3:6], phonenumber[6:])
91         return prettynumber
92
93
94 def make_idler(func):
95         """
96         Decorator that makes a generator-function into a function that will continue execution on next call
97         """
98         a = []
99
100         def decorated_func(*args, **kwds):
101                 if not a:
102                         a.append(func(*args, **kwds))
103                 try:
104                         a[0].next()
105                         return True
106                 except StopIteration:
107                         del a[:]
108                         return False
109
110         decorated_func.__name__ = func.__name__
111         decorated_func.__doc__ = func.__doc__
112         decorated_func.__dict__.update(func.__dict__)
113
114         return decorated_func
115
116
117 class DummyAddressBook(object):
118         """
119         Minimal example of both an addressbook factory and an addressbook
120         """
121
122         def clear_caches(self):
123                 pass
124
125         def get_addressbooks(self):
126                 """
127                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
128                 """
129                 yield self, "", "None"
130
131         def open_addressbook(self, bookId):
132                 return self
133
134         @staticmethod
135         def contact_source_short_name(contactId):
136                 return ""
137
138         @staticmethod
139         def factory_name():
140                 return ""
141
142         @staticmethod
143         def get_contacts():
144                 """
145                 @returns Iterable of (contact id, contact name)
146                 """
147                 return []
148
149         @staticmethod
150         def get_contact_details(contactId):
151                 """
152                 @returns Iterable of (Phone Type, Phone Number)
153                 """
154                 return []
155
156
157 class MergedAddressBook(object):
158         """
159         Merger of all addressbooks
160         """
161
162         def __init__(self, addressbookFactories, sorter = None):
163                 self.__addressbookFactories = addressbookFactories
164                 self.__addressbooks = None
165                 self.__sort_contacts = sorter if sorter is not None else self.null_sorter
166
167         def clear_caches(self):
168                 self.__addressbooks = None
169                 for factory in self.__addressbookFactories:
170                         factory.clear_caches()
171
172         def get_addressbooks(self):
173                 """
174                 @returns Iterable of (Address Book Factory, Book Id, Book Name)
175                 """
176                 yield self, "", ""
177
178         def open_addressbook(self, bookId):
179                 return self
180
181         def contact_source_short_name(self, contactId):
182                 if self.__addressbooks is None:
183                         return ""
184                 bookIndex, originalId = contactId.split("-", 1)
185                 return self.__addressbooks[int(bookIndex)].contact_source_short_name(originalId)
186
187         @staticmethod
188         def factory_name():
189                 return "All Contacts"
190
191         def get_contacts(self):
192                 """
193                 @returns Iterable of (contact id, contact name)
194                 """
195                 if self.__addressbooks is None:
196                         self.__addressbooks = list(
197                                 factory.open_addressbook(id)
198                                 for factory in self.__addressbookFactories
199                                 for (f, id, name) in factory.get_addressbooks()
200                         )
201                 contacts = (
202                         ("-".join([str(bookIndex), contactId]), contactName)
203                                 for (bookIndex, addressbook) in enumerate(self.__addressbooks)
204                                         for (contactId, contactName) in addressbook.get_contacts()
205                 )
206                 sortedContacts = self.__sort_contacts(contacts)
207                 return sortedContacts
208
209         def get_contact_details(self, contactId):
210                 """
211                 @returns Iterable of (Phone Type, Phone Number)
212                 """
213                 if self.__addressbooks is None:
214                         return []
215                 bookIndex, originalId = contactId.split("-", 1)
216                 return self.__addressbooks[int(bookIndex)].get_contact_details(originalId)
217
218         @staticmethod
219         def null_sorter(contacts):
220                 """
221                 Good for speed/low memory
222                 """
223                 return contacts
224
225         @staticmethod
226         def basic_firtname_sorter(contacts):
227                 """
228                 Expects names in "First Last" format
229                 """
230                 contactsWithKey = [
231                         (contactName.rsplit(" ", 1)[0], (contactId, contactName))
232                                 for (contactId, contactName) in contacts
233                 ]
234                 contactsWithKey.sort()
235                 return (contactData for (lastName, contactData) in contactsWithKey)
236
237         @staticmethod
238         def basic_lastname_sorter(contacts):
239                 """
240                 Expects names in "First Last" format
241                 """
242                 contactsWithKey = [
243                         (contactName.rsplit(" ", 1)[-1], (contactId, contactName))
244                                 for (contactId, contactName) in contacts
245                 ]
246                 contactsWithKey.sort()
247                 return (contactData for (lastName, contactData) in contactsWithKey)
248
249         @staticmethod
250         def reversed_firtname_sorter(contacts):
251                 """
252                 Expects names in "Last, First" format
253                 """
254                 contactsWithKey = [
255                         (contactName.split(", ", 1)[-1], (contactId, contactName))
256                                 for (contactId, contactName) in contacts
257                 ]
258                 contactsWithKey.sort()
259                 return (contactData for (lastName, contactData) in contactsWithKey)
260
261         @staticmethod
262         def reversed_lastname_sorter(contacts):
263                 """
264                 Expects names in "Last, First" format
265                 """
266                 contactsWithKey = [
267                         (contactName.split(", ", 1)[0], (contactId, contactName))
268                                 for (contactId, contactName) in contacts
269                 ]
270                 contactsWithKey.sort()
271                 return (contactData for (lastName, contactData) in contactsWithKey)
272
273         @staticmethod
274         def guess_firstname(name):
275                 if ", " in name:
276                         return name.split(", ", 1)[-1]
277                 else:
278                         return name.rsplit(" ", 1)[0]
279
280         @staticmethod
281         def guess_lastname(name):
282                 if ", " in name:
283                         return name.split(", ", 1)[0]
284                 else:
285                         return name.rsplit(" ", 1)[-1]
286
287         @classmethod
288         def advanced_firtname_sorter(cls, contacts):
289                 contactsWithKey = [
290                         (cls.guess_firstname(contactName), (contactId, contactName))
291                                 for (contactId, contactName) in contacts
292                 ]
293                 contactsWithKey.sort()
294                 return (contactData for (lastName, contactData) in contactsWithKey)
295
296         @classmethod
297         def advanced_lastname_sorter(cls, contacts):
298                 contactsWithKey = [
299                         (cls.guess_lastname(contactName), (contactId, contactName))
300                                 for (contactId, contactName) in contacts
301                 ]
302                 contactsWithKey.sort()
303                 return (contactData for (lastName, contactData) in contactsWithKey)
304
305
306 class PhoneTypeSelector(object):
307
308         def __init__(self, widgetTree, gcBackend):
309                 self._gcBackend = gcBackend
310                 self._widgetTree = widgetTree
311                 self._dialog = self._widgetTree.get_widget("phonetype_dialog")
312
313                 self._selectButton = self._widgetTree.get_widget("select_button")
314                 self._selectButton.connect("clicked", self._on_phonetype_select)
315
316                 self._cancelButton = self._widgetTree.get_widget("cancel_button")
317                 self._cancelButton.connect("clicked", self._on_phonetype_cancel)
318
319                 self._typemodel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
320                 self._typeviewselection = None
321
322                 typeview = self._widgetTree.get_widget("phonetypes")
323                 typeview.connect("row-activated", self._on_phonetype_select)
324                 typeview.set_model(self._typemodel)
325                 textrenderer = gtk.CellRendererText()
326
327                 # Add the column to the treeview
328                 column = gtk.TreeViewColumn("Phone Numbers", textrenderer, text=1)
329                 column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
330
331                 typeview.append_column(column)
332
333                 self._typeviewselection = typeview.get_selection()
334                 self._typeviewselection.set_mode(gtk.SELECTION_SINGLE)
335
336         def run(self, contactDetails):
337                 self._typemodel.clear()
338
339                 for phoneType, phoneNumber in contactDetails:
340                         self._typemodel.append((phoneNumber, "%s - %s" % (make_pretty(phoneNumber), phoneType)))
341
342                 userResponse = self._dialog.run()
343
344                 if userResponse == gtk.RESPONSE_OK:
345                         model, itr = self._typeviewselection.get_selected()
346                         if itr:
347                                 phoneNumber = self._typemodel.get_value(itr, 0)
348                 else:
349                         phoneNumber = ""
350
351                 self._typeviewselection.unselect_all()
352                 self._dialog.hide()
353                 return phoneNumber
354
355         def _on_phonetype_select(self, *args):
356                 self._dialog.response(gtk.RESPONSE_OK)
357
358         def _on_phonetype_cancel(self, *args):
359                 self._dialog.response(gtk.RESPONSE_CANCEL)
360
361
362 class Dialpad(object):
363
364         def __init__(self, widgetTree):
365                 self._numberdisplay = widgetTree.get_widget("numberdisplay")
366                 self._phonenumber = ""
367                 self._prettynumber = ""
368                 self._clearall_id = None
369
370                 callbackMapping = {
371                         "on_dial_clicked": self._on_dial_clicked,
372                         "on_digit_clicked": self._on_digit_clicked,
373                         "on_clear_number": self._on_clear_number,
374                         "on_back_clicked": self._on_backspace,
375                         "on_back_pressed": self._on_back_pressed,
376                         "on_back_released": self._on_back_released,
377                 }
378                 widgetTree.signal_autoconnect(callbackMapping)
379                 widgetTree.get_widget("dial").grab_default()
380                 widgetTree.get_widget("dial").grab_focus()
381
382         def dial(self, number):
383                 raise NotImplementedError
384
385         def get_number(self):
386                 return self._phonenumber
387
388         def set_number(self, number):
389                 """
390                 Set the callback phonenumber
391                 """
392                 self._phonenumber = make_ugly(number)
393                 self._prettynumber = make_pretty(self._phonenumber)
394                 self._numberdisplay.set_label("<span size='30000' weight='bold'>%s</span>" % (self._prettynumber))
395
396         def clear(self):
397                 self.set_number("")
398
399         def _on_dial_clicked(self, widget):
400                 self.dial(self.get_number())
401
402         def _on_clear_number(self, *args):
403                 self.clear()
404
405         def _on_digit_clicked(self, widget):
406                 self.set_number(self._phonenumber + widget.get_name()[-1])
407
408         def _on_backspace(self, widget):
409                 self.set_number(self._phonenumber[:-1])
410
411         def _on_clearall(self):
412                 self.clear()
413                 return False
414
415         def _on_back_pressed(self, widget):
416                 self._clearall_id = gobject.timeout_add(1000, self._on_clearall)
417
418         def _on_back_released(self, widget):
419                 if self._clearall_id is not None:
420                         gobject.source_remove(self._clearall_id)
421                 self._clearall_id = None
422
423
424 class AccountInfo(object):
425
426         def __init__(self, widgetTree, backend = None):
427                 self._backend = backend
428
429                 self._callbackList = gtk.ListStore(gobject.TYPE_STRING)
430                 self._accountViewNumberDisplay = widgetTree.get_widget("gcnumber_display")
431                 self._callbackCombo = widgetTree.get_widget("callbackcombo")
432                 if hildon is not None:
433                         self._callbackCombo.get_child().set_property('hildon-input-mode', (1 << 4))
434
435                 callbackMapping = {
436                 }
437                 widgetTree.signal_autoconnect(callbackMapping)
438                 self._callbackCombo.get_child().connect("changed", self._on_callbackentry_changed)
439
440                 self.set_account_number("")
441
442         def get_selected_callback_number(self):
443                 return make_ugly(self._callbackCombo.get_child().get_text())
444
445         def set_account_number(self, number):
446                 """
447                 Displays current account number
448                 """
449                 self._accountViewNumberDisplay.set_label("<span size='23000' weight='bold'>%s</span>" % (number))
450
451         def update(self):
452                 self.populate_callback_combo()
453                 self.set_account_number(self._backend.get_account_number())
454
455         def clear(self):
456                 self._callbackCombo.get_child().set_text("")
457                 self.set_account_number("")
458
459         def populate_callback_combo(self):
460                 self._callbackList.clear()
461                 for number, description in self._backend.get_callback_numbers().iteritems():
462                         self._callbackList.append((make_pretty(number),))
463
464                 self._callbackCombo.set_model(self._callbackList)
465                 self._callbackCombo.set_text_column(0)
466                 self._callbackCombo.get_child().set_text(make_pretty(self._backend.get_callback_number()))
467
468         def _on_callbackentry_changed(self, *args):
469                 """
470                 @todo Potential blocking on web access, maybe we should defer this or put up a dialog?
471                 """
472                 text = self.get_selected_callback_number()
473                 if not self._backend.is_valid_syntax(text):
474                         warnings.warn("%s is not a valid callback number" % text, UserWarning, 2)
475                 elif text == self._backend.get_callback_number():
476                         warnings.warn("Callback number already is %s" % self._backend.get_callback_number(), UserWarning, 2)
477                 else:
478                         self._backend.set_callback_number(text)
479
480
481 class RecentCallsView(object):
482
483         def __init__(self, widgetTree, backend = None):
484                 self._backend = backend
485                 self._recenttime = 0.0
486                 self._recentmodel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
487                 self._recentview = widgetTree.get_widget("recentview")
488                 self._recentviewselection = None
489
490                 callbackMapping = {
491                         "on_recentview_row_activated": self._on_recentview_row_activated,
492                 }
493                 widgetTree.signal_autoconnect(callbackMapping)
494
495                 self._init_recent_view()
496                 if hildon is not None:
497                         hildon.hildon_helper_set_thumb_scrollbar(widgetTree.get_widget('recent_scrolledwindow'), True)
498
499         def number_selected(self, number):
500                 raise NotImplementedError
501
502         def update(self):
503                 if (time.time() - self._recenttime) < 300:
504                         return
505                 backgroundPopulate = threading.Thread(target=self._idly_populate_recentview)
506                 backgroundPopulate.setDaemon(True)
507                 backgroundPopulate.start()
508
509         def clear(self):
510                 self._recenttime = 0.0
511                 self._recentmodel.clear()
512
513         def _init_recent_view(self):
514                 self._recentview.set_model(self._recentmodel)
515                 textrenderer = gtk.CellRendererText()
516
517                 # Add the column to the treeview
518                 column = gtk.TreeViewColumn("Calls", textrenderer, text=1)
519                 column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
520
521                 self._recentview.append_column(column)
522
523                 self._recentviewselection = self._recentview.get_selection()
524                 self._recentviewselection.set_mode(gtk.SELECTION_SINGLE)
525
526         def _idly_populate_recentview(self):
527                 self._recenttime = time.time()
528                 self._recentmodel.clear()
529
530                 for personsName, phoneNumber, date, action in self._backend.get_recent():
531                         description = "%s on %s from/to %s - %s" % (action.capitalize(), date, personsName, phoneNumber)
532                         item = (phoneNumber, description)
533                         gtk.gdk.threads_enter()
534                         try:
535                                 self._recentmodel.append(item)
536                         finally:
537                                 gtk.gdk.threads_leave()
538
539                 return False
540
541         def _on_recentview_row_activated(self, treeview, path, view_column):
542                 model, itr = self._recentviewselection.get_selected()
543                 if not itr:
544                         return
545
546                 self.number_selected(self._recentmodel.get_value(itr, 0))
547                 self._recentviewselection.unselect_all()
548
549
550 class ContactsView(object):
551
552         def __init__(self, widgetTree, backend = None):
553                 self._backend = backend
554
555                 self._addressBook = None
556                 self._addressBookFactories = [DummyAddressBook()]
557
558                 self._booksList = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
559                 self._booksSelectionBox = widgetTree.get_widget("addressbook_combo")
560
561                 self._contactstime = 0.0
562                 self._contactsmodel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
563                 self._contactsviewselection = None
564                 self._contactsview = widgetTree.get_widget("contactsview")
565
566                 self._phoneTypeSelector = PhoneTypeSelector(widgetTree, self._backend)
567
568                 callbackMapping = {
569                         "on_contactsview_row_activated" : self._on_contactsview_row_activated,
570                         "on_addressbook_combo_changed": self._on_addressbook_combo_changed,
571                 }
572                 widgetTree.signal_autoconnect(callbackMapping)
573                 if hildon is not None:
574                         hildon.hildon_helper_set_thumb_scrollbar(widgetTree.get_widget('contacts_scrolledwindow'), True)
575
576                 self._init_contacts_view()
577
578         def number_selected(self, number):
579                 raise NotImplementedError
580
581         def get_addressbooks(self):
582                 """
583                 @returns Iterable of ((Factory Id, Book Id), (Factory Name, Book Name))
584                 """
585                 for i, factory in enumerate(self._addressBookFactories):
586                         for bookFactory, bookId, bookName in factory.get_addressbooks():
587                                 yield (i, bookId), (factory.factory_name(), bookName)
588
589         def open_addressbook(self, bookFactoryId, bookId):
590                 self._addressBook = self._addressBookFactories[bookFactoryId].open_addressbook(bookId)
591                 self._contactstime = 0
592                 backgroundPopulate = threading.Thread(target=self._idly_populate_contactsview)
593                 backgroundPopulate.setDaemon(True)
594                 backgroundPopulate.start()
595
596         def update(self):
597                 if (time.time() - self._contactstime) < 300:
598                         return
599                 backgroundPopulate = threading.Thread(target=self._idly_populate_contactsview)
600                 backgroundPopulate.setDaemon(True)
601                 backgroundPopulate.start()
602
603         def clear(self):
604                 self._contactstime = 0.0
605                 self._contactsmodel.clear()
606
607         def clear_caches(self):
608                 for factory in self._addressBookFactories:
609                         factory.clear_caches()
610                 self._addressBook.clear_caches()
611
612         def append(self, book):
613                 self._addressBookFactories.append(book)
614
615         def extend(self, books):
616                 self._addressBookFactories.extend(books)
617
618         def _init_contacts_view(self):
619                 self._contactsview.set_model(self._contactsmodel)
620
621                 # Add the column to the treeview
622                 column = gtk.TreeViewColumn("Contact")
623
624                 #displayContactSource = False
625                 displayContactSource = True
626                 if displayContactSource:
627                         textrenderer = gtk.CellRendererText()
628                         column.pack_start(textrenderer, expand=False)
629                         column.add_attribute(textrenderer, 'text', 0)
630
631                 textrenderer = gtk.CellRendererText()
632                 column.pack_start(textrenderer, expand=True)
633                 column.add_attribute(textrenderer, 'text', 1)
634
635                 textrenderer = gtk.CellRendererText()
636                 column.pack_start(textrenderer, expand=True)
637                 column.add_attribute(textrenderer, 'text', 4)
638
639                 column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
640                 column.set_sort_column_id(1)
641                 column.set_visible(True)
642                 self._contactsview.append_column(column)
643
644                 self._contactsviewselection = self._contactsview.get_selection()
645                 self._contactsviewselection.set_mode(gtk.SELECTION_SINGLE)
646
647         def _init_books_combo(self):
648                 self._booksList.clear()
649                 for (factoryId, bookId), (factoryName, bookName) in self.get_addressbooks():
650                         if factoryName and bookName:
651                                 entryName = "%s: %s" % (factoryName, bookName)
652                         elif factoryName:
653                                 entryName = factoryName
654                         elif bookName:
655                                 entryName = bookName
656                         else:
657                                 entryName = "Bad name (%d)" % factoryId
658                         row = (str(factoryId), bookId, entryName)
659                         self._booksList.append(row)
660
661                 self._booksSelectionBox.set_model(self._booksList)
662                 cell = gtk.CellRendererText()
663                 self._booksSelectionBox.pack_start(cell, True)
664                 self._booksSelectionBox.add_attribute(cell, 'text', 2)
665                 self._booksSelectionBox.set_active(0)
666
667         def _idly_populate_contactsview(self):
668                 #@todo Add a lock so only one code path can be in here at a time
669                 self.clear()
670
671                 # completely disable updating the treeview while we populate the data
672                 self._contactsview.freeze_child_notify()
673                 self._contactsview.set_model(None)
674
675                 addressBook = self._addressBook
676                 for contactId, contactName in addressBook.get_contacts():
677                         contactType = (addressBook.contact_source_short_name(contactId),)
678                         self._contactsmodel.append(contactType + (contactName, "", contactId) + ("",))
679
680                 # restart the treeview data rendering
681                 self._contactsview.set_model(self._contactsmodel)
682                 self._contactsview.thaw_child_notify()
683                 return False
684
685         def _on_addressbook_combo_changed(self, *args, **kwds):
686                 itr = self._booksSelectionBox.get_active_iter()
687                 if itr is None:
688                         return
689                 factoryId = int(self._booksList.get_value(itr, 0))
690                 bookId = self._booksList.get_value(itr, 1)
691                 self.open_addressbook(factoryId, bookId)
692
693         def _on_contactsview_row_activated(self, treeview, path, view_column):
694                 model, itr = self._contactsviewselection.get_selected()
695                 if not itr:
696                         return
697
698                 contactId = self._contactsmodel.get_value(itr, 3)
699                 contactDetails = self._addressBook.get_contact_details(contactId)
700                 contactDetails = [phoneNumber for phoneNumber in contactDetails]
701
702                 if len(contactDetails) == 0:
703                         phoneNumber = ""
704                 elif len(contactDetails) == 1:
705                         phoneNumber = contactDetails[0][1]
706                 else:
707                         phoneNumber = self._phoneTypeSelector.run(contactDetails)
708
709                 if 0 < len(phoneNumber):
710                         self.number_selected(phoneNumber)
711
712                 self._contactsviewselection.unselect_all()