# Tichy # copyright 2008 Guillaume Chereau (charlie@openmoko.org) # # This file is part of Tichy. # # Tichy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Tichy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Tichy. If not, see . import tichy import tichy.gui as gui from tichy.phone import TelNumber class Contacts(tichy.Application): name = 'Contacts' icon = 'icon.png' def run(self, parent): self.window = gui.Window(parent) frame = gui.ApplicationFrame(self.window, self, back_button=True) vbox = gui.Box(frame, axis = 1) self.contacts = tichy.Service('Contacts') self.contacts_list = tichy.ActorList() self.update_list() list_view = self.contacts_list.view(vbox) gui.Spring(vbox, axis = 1) new_menu = frame.actor.new_action('New') new_menu.connect('activated', self.on_new) yield tichy.Wait(frame, 'back') # Wait until the quit button is clicked self.window.close() # Don't forget to close the window def update_list(self): self.contacts_list.clear() for contact in self.contacts.values(): actor = tichy.Actor(contact) call = actor.new_action('Call') call.connect('activated', self.on_call_contact) edit = actor.new_action('Edit') edit.connect('activated', self.on_edit_contact) delete = actor.new_action('Delete') delete.connect('activated', self.on_delete_contact) self.contacts_list.append(actor) def on_call_contact(self, action, contact, view): if not 'tel' in contact: yield gui.Message(view.window, "Contact has no tel") else: caller = tichy.Service('Caller') yield caller.call(view.window, str(contact['tel'])) def on_edit_contact(self, item, contact, view): yield Contact(view.window, contact) self.update_list() def on_delete_contact(self, item, contact, window): self.contacts.remove(contact) self.update_list() def on_new(self, *args): contact = self.contacts.create() yield Contact(self.window, contact) self.update_list() class ContactAttrItem(tichy.Item): """This item is used to print a contact attribute""" def __init__(self, name, value): super(ContactAttrItem, self).__init__() self.name = name self.value = value # We override item.get_text cause we want to use our own view method # Instead ot relying on the item name def get_text(self): return self def __str__(self): return self.name def view(self, parent): ret = gui.Box(parent, axis = 0, border = 0) gui.Label(ret, self.name) self.value.view(ret) return ret class Contact(tichy.Application): def run(self, window, contact): self.contact = contact self.name = "Edit %s" % contact['name'] self.window = gui.Window(window, modal = True) frame = gui.ApplicationFrame(self.window, self, back_button=True) vbox = gui.Box(frame, axis = 1) self.attr_list = tichy.ActorList() self.update_list() list_view = self.attr_list.view(vbox) gui.Spring(vbox, axis = 1) add_menu = frame.actor.new_action('Add') add_tel_menu = add_menu.new_action('Tel') add_note_menu = add_menu.new_action('Note') add_note_menu.connect('activated', self.on_add, tichy.Text, 'note') add_tel_menu.connect('activated', self.on_add, TelNumber, 'tel') yield tichy.Wait(frame, 'back') self.window.close() def on_add(self, action, item, view, item_cls, name): # first we have to set up a unique name for the new attr i = 1 final_name = name while final_name in self.contact: i += 1 final_name = '%s %d' % (name, i) name = final_name value = item_cls("") self.contact[name] = value self.update_list() value.edit(self.window, name = name) def update_list(self): self.attr_list.clear() for attr, value in self.contact.items(): actor = tichy.Actor(ContactAttrItem(attr, value)) edit = actor.new_action('Edit') edit.connect('activated', self.on_edit_attr) delete = actor.new_action('Delete') delete.connect('activated', self.on_delete_attr) self.attr_list.append(actor) def on_edit_attr(self, action, attr, view): yield self.contact[attr.name].edit(view.window, name = attr.name) def on_delete_attr(self, action, attr, view): if attr.name == 'name': yield gui.Message(view.window, "Can't delete name") else: self.attr_list.remove(action.actor) del self.contact[attr.name] class SelectContactApp(tichy.Application): name = "Select Contact" enabled = False def run(self, window): self.window = gui.Window(window) frame = gui.ApplicationFrame(self.window, self) vbox = gui.Box(frame, axis = 1) contacts = tichy.Service('Contacts') for name, contact in contacts.items(): button = gui.Button(vbox) gui.Label(button, name) button.connect('clicked', self.on_select, name) yield tichy.Wait(self.window, 'destroyed') # Wait until the quit button is clicked yield self.ret def on_select(self, b, name): self.ret = name self.window.close() class MySelectContactService(tichy.Service): service = 'SelectContact' def select(self, window): return SelectContactApp(window)