def on_remove_button_clicked(self,widget,event=None):
        selection = self._contact_treeview.get_selection()

        if selection.count_selected_rows()!= 1:
            return
        
        # aviso
      
        model ,itera  = selection.get_selected()
        contact_id = model.get_value(itera,0)
        target_contact  = MDContact.get(contact_id)
        dlg =gtk.MessageDialog(type=gtk.MESSAGE_WARNING,
                               buttons=gtk.BUTTONS_OK_CANCEL,
                               message_format='¿Deseas eliminar el contacto "%s"?' % (target_contact.name))
        dlg.set_title("Eliminar contacto")
        dlg.set_icon_from_file(MSD.icons_files_dir + "addressbook_16x16.png")
        response = dlg.run()
        dlg.destroy()

        if response != gtk.RESPONSE_OK:
            print "Canceled"
            return

        target_contact.destroySelf()
        self.__build_model() # TODO optimizar
        self.__check_button_sensitivity()
        self._contact_treeview.set_cursor(0)
 def __on_copy_clicked (self, widget, event=None):
     selection = self._contact_treeview.get_selection()
     if selection.count_selected_rows() != 1:
         return
     
     model ,itera  = selection.get_selected()
     contact_id = model.get_value(itera,0)
     self._clip_board  = MDContact.get(contact_id)
    def __selected_contact(self):
        selection = self._contact_treeview.get_selection()

        if selection.count_selected_rows()!= 1:
            return None
      
        model ,itera  = selection.get_selected()
        contact_id = model.get_value(itera,0)
        target_contact  = MDContact.get(contact_id)
        return target_contact
 def __cell_edited_cb(self,cell, path, new_text, field_index_db_field_tuple=None):
     field_index, db_field = field_index_db_field_tuple
     if new_text == "":
         return
     
     obj_id = self._model[path][0]
     value =  self._model[path][field_index]
     contact = MDContact.get(obj_id)
     d ={db_field : new_text}
     contact.set(**d)
     self.__build_model()
    def __on_cut_clicked (self, widget, event=None):
        selection = self._contact_treeview.get_selection()
        if selection.count_selected_rows() != 1:
            return
        
        model ,itera  = selection.get_selected()
        contact_id = model.get_value(itera,0)
        target_contact  = MDContact.get(contact_id)
        self._clip_board  = target_contact
        target_contact.destroySelf()

        self.__build_model() # TODO optimizar
        self._contact_treeview.set_cursor(0)
        self.__check_button_sensitivity()
    def on_edit_button_clicked(self,widget,event=None):
        selection = self._contact_treeview.get_selection()

        if selection.count_selected_rows()!= 1:
            return

        model ,itera  = selection.get_selected()
        contact_id = model.get_value(itera,0)
        target_contact  = MDContact.get(contact_id)
        
        response = self._contact_editor.run(target_contact)
        if response != gtk.RESPONSE_OK:
            print "Canceled"
            return
        d = self._contact_editor.get_dict()
        target_contact.set(**d)
        self.__build_model() # TODO optimizar
        self.__check_button_sensitivity()
    def __check_button_sensitivity(self):
        selected_rows =  self._contact_treeview.get_selection().count_selected_rows()
        buttons = (self._edit_contact_button,self._remove_contact_button,self._send_mail_button, self._menu_modif_contact_item, self._menu_remove_contact_item)
        new_state = selected_rows > 0
        for button in buttons:
            button.set_sensitive(new_state)

        # Check email selected
        if selected_rows > 0:
            selection = self._contact_treeview.get_selection()
            model ,itera  = selection.get_selected()
            contact_id = model.get_value(itera,0)
            contact = MDContact.get(contact_id)
            if contact.email == "":
                self._send_mail_button.set_sensitive (False)
                self._ctx_send_mail_menu_item.set_sensitive (False)
            else:
                self._ctx_send_mail_menu_item.set_sensitive (True)
    def __contact_selection_foreach_cb (self, model, path, iter, contacts):
        contact_id = model.get_value(iter,0)
        target_contact  = MDContact.get(contact_id)
	contacts.append (target_contact)