PasteBin

Created By davea at 2008/03/05 01:19

https://sucs.org/pb/259 (plain)
  1.  
  2.   import objc
  3.   import _uicaboodle
  4.   from objc import YES, NO
  5.   from sqlite3 import dbapi2 as sqlite
  6.   objc.loadBundle('UIKit', globals(), '/System/Library/Frameworks/UIKit.framework')
  7.  
  8.   class PYApplication(UIApplication):
  9.       def applicationDidFinishLaunching_(self, unused):
  10.           self.contacts = []
  11.           self.sections = []
  12.          
  13.           frame = UIHardware.fullScreenApplicationContentRect()
  14.  
  15.           self.window = UIWindow.alloc().initWithFrame_(frame)
  16.  
  17.           self.view = UIView.alloc().initWithFrame_(self.window.bounds())
  18.           self.window._.contentView = self.view
  19.  
  20.           self.window.orderFront_(self)
  21.           self.window.makeKey_(self)
  22.           self.window._setHidden_(NO)
  23.  
  24.           navsize = UINavigationBar.defaultSize()
  25.           navrect = ((0, 0), navsize)
  26.  
  27.           self.navbar = UINavigationBar.alloc().initWithFrame_(navrect)
  28.           self.view.addSubview_(self.navbar)
  29.  
  30.           self.navbar._.barStyle = 1
  31.           self.navbar._.delegate = self
  32.  
  33.           navitem = UINavigationItem.alloc().initWithTitle_(u"Contacts")
  34.           self.navbar.pushNavigationItem_(navitem)
  35.  
  36.           bounds = self.view.bounds()
  37.           tblrect = ((0, navsize[1]), (bounds[1][0], bounds[1][1] - navsize[1]))
  38.          
  39.           self.loadContactsFromDB()
  40.          
  41.           self.secList = UISectionList.alloc().initWithFrame_(tblrect)
  42.           self.secList._.dataSource = self
  43.           self.secList.reloadData()
  44.          
  45.           col = UITableColumn.alloc().initWithTitle_identifier_width_(u"Name", u"name", 320.0)
  46.           table = self.secList.table()
  47.           table._.separatorStyle = 1
  48.           table._.delegate = self
  49.           table.addTableColumn_(col)
  50.          
  51.           self.view.addSubview_(self.secList)
  52.      
  53.       def loadContactsFromDB(self):
  54.           db = sqlite.connect("/private/var/mobile/Library/AddressBook/AddressBook.sqlitedb") #FIXME: get proper path
  55.           cursor = db.cursor()
  56.           cursor.execute("select first, last from ABPerson where first is not null order by first")
  57.           row = 0
  58.           lastInitial = u""
  59.           for first, last in cursor.fetchall():
  60.               self.contacts.append({"first": (first or u""), "last": (last or u""), "cell": None})
  61.               thisInitial = unicode(first[0])
  62.               if thisInitial != lastInitial:
  63.                   self.sections.append((thisInitial, row))
  64.                   lastInitial = thisInitial
  65.               row += 1
  66.           cursor.close()
  67.           db.close()
  68.      
  69.       @objc.signature("I@:@")
  70.       def numberOfSectionsInSectionList_(self, sectionList):
  71.           print u"numberOfSectionsInSectionList_", sectionList
  72.           return len(self.sections)
  73.      
  74.       @objc.signature("@@:@I")
  75.       def sectionList_titleForSection_(self, sectionList, section):
  76.           print u"sectionList_titleForSection_", sectionList, section
  77.           return self.sections[section][0]
  78.      
  79.       @objc.signature("I@:@I")
  80.       def sectionList_rowForSection_(self, sectionList, section):
  81.           print u"sectionList_rowForSection_", sectionList, section
  82.           return self.sections[section][1]
  83.      
  84.       @objc.signature("I@:@")
  85.       def numberOfRowsInTable_(self, table):
  86.           print u"numberOfRowsInTable_", table
  87.           return len(self.contacts)
  88.      
  89.       @objc.signature("@@:@I@")
  90.       def table_cellForRow_column_(self, table, row, column):
  91.           print u"table_cellForRow_column_", table, row, column
  92.           if not self.contacts[row]['cell']:
  93.               cell = UIImageAndTextTableCell.alloc().init()
  94.               cell._.title = "%s %s" % (self.contacts[row]['first'], self.contacts[row]['last'])
  95.               self.contacts[row]['cell'] = cell
  96.           return self.contacts[row]['cell']
  97.      
  98.       @objc.signature("B@:@I")
  99.       def table_canSelectRow_(self, table, row):
  100.           return NO
  101.      
  102.      
  103.       @objc.signature("B@::")
  104.       def respondsToSelector_(self, selector):
  105.           responds = dir(self).count(selector.replace(":", "_")) > 0
  106.           if not responds:
  107.               print selector
  108.           return responds
  109.  
  110.   _uicaboodle.UIApplicationMain(['HelloPython'], PYApplication)

You must be logged in to paste new items to the PasteBin