
import objc
import _uicaboodle
from objc import YES, NO
from sqlite3 import dbapi2 as sqlite
objc.loadBundle(&#039;UIKit&#039;, globals(), &#039;/System/Library/Frameworks/UIKit.framework&#039;)

class PYApplication(UIApplication):
    def applicationDidFinishLaunching_(self, unused):
        self.contacts = []
        self.sections = []
        
        frame = UIHardware.fullScreenApplicationContentRect()

        self.window = UIWindow.alloc().initWithFrame_(frame)

        self.view = UIView.alloc().initWithFrame_(self.window.bounds())
        self.window._.contentView = self.view

        self.window.orderFront_(self)
        self.window.makeKey_(self)
        self.window._setHidden_(NO)

        navsize = UINavigationBar.defaultSize()
        navrect = ((0, 0), navsize)

        self.navbar = UINavigationBar.alloc().initWithFrame_(navrect)
        self.view.addSubview_(self.navbar)

        self.navbar._.barStyle = 1
        self.navbar._.delegate = self

        navitem = UINavigationItem.alloc().initWithTitle_(u&quot;Contacts&quot;)
        self.navbar.pushNavigationItem_(navitem)

        bounds = self.view.bounds()
        tblrect = ((0, navsize[1]), (bounds[1][0], bounds[1][1] - navsize[1]))
        
        self.loadContactsFromDB()
        
        self.secList = UISectionList.alloc().initWithFrame_(tblrect)
        self.secList._.dataSource = self
        self.secList.reloadData()
        
        col = UITableColumn.alloc().initWithTitle_identifier_width_(u&quot;Name&quot;, u&quot;name&quot;, 320.0)
        table = self.secList.table()
        table._.separatorStyle = 1
        table._.delegate = self
        table.addTableColumn_(col)
        
        self.view.addSubview_(self.secList)
    
    def loadContactsFromDB(self):
        db = sqlite.connect(&quot;/private/var/mobile/Library/AddressBook/AddressBook.sqlitedb&quot;) #FIXME: get proper path
        cursor = db.cursor()
        cursor.execute(&quot;select first, last from ABPerson where first is not null order by first&quot;)
        row = 0
        lastInitial = u&quot;&quot;
        for first, last in cursor.fetchall():
            self.contacts.append({&quot;first&quot;: (first or u&quot;&quot;), &quot;last&quot;: (last or u&quot;&quot;), &quot;cell&quot;: None})
            thisInitial = unicode(first[0])
            if thisInitial != lastInitial:
                self.sections.append((thisInitial, row))
                lastInitial = thisInitial
            row += 1
        cursor.close()
        db.close()
    
    @objc.signature(&quot;I@:@&quot;)
    def numberOfSectionsInSectionList_(self, sectionList):
        print u&quot;numberOfSectionsInSectionList_&quot;, sectionList
        return len(self.sections)
    
    @objc.signature(&quot;@@:@I&quot;)
    def sectionList_titleForSection_(self, sectionList, section):
        print u&quot;sectionList_titleForSection_&quot;, sectionList, section
        return self.sections[section][0]
    
    @objc.signature(&quot;I@:@I&quot;)
    def sectionList_rowForSection_(self, sectionList, section):
        print u&quot;sectionList_rowForSection_&quot;, sectionList, section
        return self.sections[section][1]
    
    @objc.signature(&quot;I@:@&quot;)
    def numberOfRowsInTable_(self, table):
        print u&quot;numberOfRowsInTable_&quot;, table
        return len(self.contacts)
    
    @objc.signature(&quot;@@:@I@&quot;)
    def table_cellForRow_column_(self, table, row, column):
        print u&quot;table_cellForRow_column_&quot;, table, row, column
        if not self.contacts[row][&#039;cell&#039;]:
            cell = UIImageAndTextTableCell.alloc().init()
            cell._.title = &quot;%s %s&quot; % (self.contacts[row][&#039;first&#039;], self.contacts[row][&#039;last&#039;])
            self.contacts[row][&#039;cell&#039;] = cell
        return self.contacts[row][&#039;cell&#039;]
    
    @objc.signature(&quot;B@:@I&quot;)
    def table_canSelectRow_(self, table, row):
        return NO
    
    
    @objc.signature(&quot;B@::&quot;)
    def respondsToSelector_(self, selector):
        responds = dir(self).count(selector.replace(&quot;:&quot;, &quot;_&quot;)) &gt; 0
        if not responds:
            print selector
        return responds

_uicaboodle.UIApplicationMain([&#039;HelloPython&#039;], PYApplication)

