Angus Ainslie wrote:
On Tue, 2009-04-07 at 09:50 +0200, Ingvaldur Sigurjonsson wrote:
I wrote a little GUI using python-elementary which was a good way of getting started using the dbus API. I use that to to turn on WiFi and GPS, using the same methods as above, but with another resource ('WiFi' and 'GPS'). This works at least on FSO milestone5.5 (built at home).

Regards
- Ingi

Hi Ingi,

Do you have the code for your GUI posted anywhere, is it GPL ?

Thanks
Angus

Hi

The code is for 'my eyes only' for obvious reasons (you would be frightened on how ugly code can be sometimes...).

I was just experimenting with the 'Toggle'-widget from Elementary so I chop'ed and saw'ed from Elementary's 'test.py' to get a single Toggle displayed.

The only addition is the '.changed'-callback addition which is really simple in python i.e.


import elementary
...
import dbus
from dbus.mainloop.glib import DBusGMainLoop

DBusGMainLoop(set_as_default=True)

# Globals - Initialize dbus
bus = dbus.SystemBus()
#  get a handle to org.freesmartphone.Usage-interface
o_usage = bus.get_object('org.freesmartphone.ousaged', \
                         '/org/freesmartphone/Usage')
if_usage = dbus.Interface( o_usage, 'org.freesmartphone.Usage' )


...
        # Display enabled/auto
        tg_disp = elementary.Toggle(win)
        tg_disp.label_set("Display" )
        tg_disp.states_labels_set( "enabled", "auto" )

        if if_usage.GetResourceState('Display'):
                # set to 'enabled' !
                tg_disp.state_set( True )
        else:
                # 'auto' !
                tg_disp.state_set( False )

        # Connect callback
        tg_disp.changed = tg_disp_changed

        bx.pack_end(tg_disp)
        tg_disp.show()
...

  and then the callback:

    def tg_disp_changed(obj, event, *args, **kargs):

        if obj.state_get():
                if_usage.SetResourcePolicy( 'Display', 'enabled' )
        else:
                if_usage.SetResourcePolicy( 'Display', 'auto' )


Right now I have Linux '2.6.29-GTA02_andy-tracking-mokodev' and on that I have FSO milestone5.5. I've been using my Freerunner for a week now and I'm almost satisfied with it (I bought it last september!). Until the weekend I was missing all my contacts but I wrote a vcard_import.py for Paroli's contacts (data is stored in the file ./paroli/contacts/phone by using python's pickle!) so now I'm no longer dependent upon remembering all phone-numbers for my contacts (all three of them...).

I'm currently building 'shr-unstable', and I'm going to give it a try later tonight.

I enclosed the vcard_import.py if anyony is interested. It's of course free for anyone to use etc...

Regards
- Ingi
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, getopt
import cPickle as pickle

g_verbose = False

class VCardContact:
    def __init__( self ):
        self.fn = ""
        # dict with {type, number}
        self.tel = {}

    def reset( self ):
        self.fn = ""
        self.tel.clear()

    def setName( self, name ):
        self.fn = name.strip('\r\n') 

    def addPhoneNumber( self, type, number ):
        self.tel[ type ] = number.strip('\r\n') 

    def to_list( self ):
        """ return a list of entries for FN and numbers """
        phone_list = []
        for e,v in self.tel.iteritems():
            try:
                u_nam_typ = unicode(self.fn + ' ' + e, encoding="utf-8", errors='replace' )
                phone_list.append( [ u_nam_typ ,unicode(v)] )
            except:
                print "Error appending entry for ", self.fn, " phone: ", v
        return phone_list

def importVCardFile( file, outfile ):
    global g_verbose
    # Read a VCARD-entry from file containing one or more entries
    # Each entry may have following format.
    # Starts with a 'BEGIN:VCARD', ends with 'END:VCARD'
    # We're just extracting the fields 'FN' and 'TEL'
    # - We'll add a name and a number for each TEL-subfield i.e. 'WORK', 'HOME', 'CELL' 
    #   name + 'WORK' = number
    #   name + 'CELL' = number
    #
    # BEGIN:VCARD
    # VERSION:2.1
    # N;CHARSET=UTF-8:Sjöberg;Elisabet Eir
    # FN;CHARSET=UTF-8:Elisabet Eir Sjöberg
    # TEL;WORK;VOICE:0812341234
    # TEL;CELL;VOICE:+46707774488
    # EMAIL;PREF;INTERNET:elisa...@somewhere.se
    # END:VCARD

    contacts = []

    cur_contact = VCardContact()
    for line in file:
        if line[0:11] == 'BEGIN:VCARD':
            # Start with a new fresh entry
            cur_contact.reset()
        elif line[0:9] == 'END:VCARD':
            # unpack a list of name+type = number into dictionary entry...
            cur_list = cur_contact.to_list() 
            for i in cur_list:
                # Phone dictionary entry only consists of 'tel' and 'name' fields
                entry = {}
                entry['tel'] = i[1]
                entry['name'] = i[0]
                if g_verbose:
                    print "Adding entry > ", entry
                contacts.append( entry )
        elif line[0:2] == 'FN':
            # Assign the the value of FN...
            cur_contact.setName( line[line.index(':')+1:] )
        elif line[0:3] == 'TEL':
            try:
                # phone_info will contain a list like
                # ['CELL', 'VOICE:+460812341234']
                phone_info = line[4:].split(';')
                # phone_number (just eat away the 'VOICE:')
                phone_number = phone_info[1].split(':')[1]

                cur_contact.addPhoneNumber( phone_info[0], phone_number )
            except:
                # if it's isn't in the format accepted above it probably isn't 
                # anything we want to add
                # I'm ignoring 'TEL;FAX...' for example...
                print "Exception parsing line: ", line

    if g_verbose:
        print "Contacts: ", contacts
    ofile = open( outfile, 'w' )
    pickle.dump( contacts, ofile, pickle.HIGHEST_PROTOCOL )

    print len(contacts), "Contacts were successfully written to file: ", outfile

def usage():
    print "VCard-file importer for Paroli contacts"
    print "Usage:"
    print " vcard_import.py {[-i|--infile] infile.vcf} [[-o|--outfile] phone] [-v|--verbose] [-h|--help]"
    print ""
    print " outfile : defaults to 'phone'"
    print ""
    print " To install the file on the phone, quit all (kill!) the Paroli applications." 
    print " Then copy the 'outfile' to the phone like:"
    print "$ scp 'outfile' r...@192.168.0.202:/home/root/.paroli/contacts/phone"
    print "     ('outfile' is the filename you specified for outfile-argument)"

def main():
    global g_verbose
    try:
        opts, args = getopt.getopt( sys.argv[1:], 'i:o:hv', ['infile=','outfile=','help','verbose'] )
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)

    if len(opts) < 1:
        usage()
        sys.exit(2)

    infile = "-"
    outfile = "phone"
    for o, a in opts:
        if o in ("-v", "--verbose"):
            g_verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-i", "--infile"):
            infile = a
        elif o in ("-o", "--outfile"):
            outfile = a
        else:
            assert False, "unhandled option"
    # ...

    if infile == "-":
        print "Error: required argument '[-i | --infile] <filename>' is missing"
        usage()
        sys.exit(2)

    file = None
    try:
        file = open(infile, 'r')
    except:
        print "Error opening file: ", infile
        sys.exit(1)

    importVCardFile( file, outfile )

if __name__ == '__main__':
    main()
_______________________________________________
support mailing list
support@lists.openmoko.org
https://lists.openmoko.org/mailman/listinfo/support

Reply via email to