From 8f5eddf80f57269815396e8821347a27aeff2505 Mon Sep 17 00:00:00 2001 From: Sivaiah Nallagatla Date: Sun, 28 Nov 2004 19:49:48 +0000 Subject: Define new list for storing required fields 2004-11-28 Sivaiah Nallagatla * gui/contact-editor/e-contact-editor.h : Define new list for storing required fields * gui/contact-editor/e-contact-editor.c (e_contact_editor_class_init) : install new REQUIRED_FIELDS property (e_contact_editor_set_property) (e_contact_editor_get_property) : added handling for new RQUIRED_FIELDS_PROPERTY (e_contact_editor_dispose) : unref the new required_fields member (required_fields_cb) : call back to set required fields into contact editor (is_non_string_field) : new method to detect whether a particular field in contact is a string or not (e_contact_editor_is_valid) : check for presence of all required fields (save_contact) : extract all the data before calling e_contact_editor_is_valid so that it can check for required fields. Clean up the contact if there is an error svn path=/trunk/; revision=28007 --- addressbook/gui/contact-editor/e-contact-editor.c | 139 ++++++++++++++++++++-- addressbook/gui/contact-editor/e-contact-editor.h | 2 + 2 files changed, 128 insertions(+), 13 deletions(-) (limited to 'addressbook/gui') diff --git a/addressbook/gui/contact-editor/e-contact-editor.c b/addressbook/gui/contact-editor/e-contact-editor.c index 49103d12af..d74b78b9f7 100644 --- a/addressbook/gui/contact-editor/e-contact-editor.c +++ b/addressbook/gui/contact-editor/e-contact-editor.c @@ -119,7 +119,8 @@ enum { PROP_IS_NEW_CONTACT, PROP_EDITABLE, PROP_CHANGED, - PROP_WRITABLE_FIELDS + PROP_WRITABLE_FIELDS, + PROP_REQUIRED_FIELDS }; enum { @@ -287,6 +288,13 @@ e_contact_editor_class_init (EContactEditorClass *klass) E_TYPE_LIST, G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_REQUIRED_FIELDS, + g_param_spec_object ("required_fields", + _("Required Fields"), + /*_( */"XXX blurb" /*)*/, + E_TYPE_LIST, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_EDITABLE, g_param_spec_boolean ("editable", _("Editable"), @@ -2118,7 +2126,6 @@ extract_simple_field (EContactEditor *editor, GtkWidget *widget, gint field_id) gtk_text_buffer_get_start_iter (buffer, &start); gtk_text_buffer_get_end_iter (buffer, &end); text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE); - e_contact_set (contact, field_id, text); g_free (text); } @@ -2736,15 +2743,20 @@ save_contact (EContactEditor *ce, gboolean should_close) { if (!ce->target_book) return; - - if (!e_contact_editor_is_valid (EAB_EDITOR (ce))) - return; + if (ce->target_editable && !e_book_is_writable (ce->source_book)) { if (e_error_run (GTK_WINDOW (ce->app), "addressbook:prompt-move", NULL) == GTK_RESPONSE_NO) return; } extract_all (ce); + + if (!e_contact_editor_is_valid (EAB_EDITOR (ce))) { + g_object_unref (ce->contact); + ce->contact = e_contact_new (); + return; + } + real_save_contact (ce, should_close); } @@ -2767,6 +2779,42 @@ e_contact_editor_close (EABEditor *editor) } } +EContactField non_string_fields [] = { + E_CONTACT_FULL_NAME, + E_CONTACT_ADDRESS, + E_CONTACT_ADDRESS_HOME, + E_CONTACT_ADDRESS_WORK, + E_CONTACT_ADDRESS_OTHER, + E_CONTACT_EMAIL, + E_CONTACT_IM_AIM, + E_CONTACT_IM_GROUPWISE, + E_CONTACT_IM_JABBER, + E_CONTACT_IM_YAHOO, + E_CONTACT_IM_MSN, + E_CONTACT_IM_ICQ, + E_CONTACT_PHOTO, + E_CONTACT_LOGO, + E_CONTACT_X509_CERT, + E_CONTACT_CATEGORY_LIST, + E_CONTACT_BIRTH_DATE, + E_CONTACT_ANNIVERSARY + + +}; + +static gboolean +is_non_string_field (EContactField id) +{ + int count = sizeof (non_string_fields) / sizeof (EContactField); + int i; + for (i = 0; i < count; i++) + if (id == non_string_fields[i]) + return TRUE; + return FALSE; + +} + + /* insert checks here (date format, for instance, etc.) */ static gboolean e_contact_editor_is_valid (EABEditor *editor) @@ -2774,6 +2822,7 @@ e_contact_editor_is_valid (EABEditor *editor) EContactEditor *ce = E_CONTACT_EDITOR (editor); GtkWidget *widget; gboolean validation_error = FALSE; + EIterator *iter; GString *errmsg = g_string_new (_("The contact data is invalid:\n\n")); widget = glade_xml_get_widget (ce->gui, "dateedit-birthday"); @@ -2791,14 +2840,38 @@ e_contact_editor_is_valid (EABEditor *editor) validation_error = TRUE; } - widget = glade_xml_get_widget (ce->gui, "entry-file-as"); - if (STRING_IS_EMPTY (gtk_entry_get_text (GTK_ENTRY (widget)))) { - g_string_append_printf (errmsg, "%s'%s' is empty", - validation_error ? ",\n" : "", - e_contact_pretty_name (E_CONTACT_FILE_AS)); - validation_error = TRUE; + iter = e_list_get_iterator (ce->required_fields); + for (e_iterator_last (iter); + e_iterator_is_valid (iter); + e_iterator_prev (iter)) { + const char *field_name = e_iterator_get (iter); + EContactField field_id = e_contact_field_id (field_name); + + if (is_non_string_field (field_id)) { + if (e_contact_get_const (ce->contact, field_id) == NULL) { + g_string_append_printf (errmsg, "%s'%s' is empty", + validation_error ? ",\n" : "", + e_contact_pretty_name (field_id)); + validation_error = TRUE; + break; + } + + } else { + + char *text = e_contact_get_const (ce->contact, field_id); + if (STRING_IS_EMPTY (text)) { + g_string_append_printf (errmsg, "%s'%s' is empty", + validation_error ? ",\n" : "", + e_contact_pretty_name (field_id)); + validation_error = TRUE; + break; + } + + + } } - + + if (validation_error) { g_string_append (errmsg, "."); e_error_run (GTK_WINDOW (ce->app), "addressbook:generic-error", @@ -3020,7 +3093,10 @@ e_contact_editor_dispose (GObject *object) g_object_unref(e_contact_editor->writable_fields); e_contact_editor->writable_fields = NULL; } - + if (e_contact_editor->required_fields) { + g_object_unref (e_contact_editor->required_fields); + e_contact_editor->required_fields = NULL; + } if (e_contact_editor->contact) { g_object_unref(e_contact_editor->contact); e_contact_editor->contact = NULL; @@ -3071,6 +3147,24 @@ supported_fields_cb (EBook *book, EBookStatus status, sensitize_all (ce); } +static void +required_fields_cb (EBook *book, EBookStatus status, + EList *fields, EContactEditor *ce) +{ + + if (!g_slist_find ((GSList*)eab_editor_get_all_editors (), ce)) { + g_warning ("supported_fields_cb called for book that's still around, but contact editor that's been destroyed."); + return; + } + + g_object_set (ce, + "required_fields", fields, + NULL); + + +} + + static void contact_editor_destroy_notify (void *data, GObject *where_the_object_was) @@ -3157,6 +3251,8 @@ e_contact_editor_set_property (GObject *object, guint prop_id, const GValue *val e_book_async_get_supported_fields (editor->target_book, (EBookEListCallback) supported_fields_cb, editor); + e_book_async_get_required_fields (editor->target_book, + (EBookEListCallback) required_fields_cb, editor); } writable = e_book_is_writable (editor->target_book); @@ -3195,6 +3291,8 @@ e_contact_editor_set_property (GObject *object, guint prop_id, const GValue *val e_book_async_get_supported_fields (editor->target_book, (EBookEListCallback) supported_fields_cb, editor); + e_book_async_get_required_fields (editor->target_book, + (EBookEListCallback) required_fields_cb, editor); if (!editor->is_new_contact) editor->changed = TRUE; @@ -3258,6 +3356,15 @@ e_contact_editor_set_property (GObject *object, guint prop_id, const GValue *val sensitize_all (editor); break; + case PROP_REQUIRED_FIELDS: + if (editor->required_fields) + g_object_unref (editor->required_fields); + editor->required_fields = g_value_get_object (value); + if (editor->required_fields) + g_object_ref (editor->required_fields); + else + editor->required_fields = e_list_new (NULL, NULL, NULL); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -3303,6 +3410,12 @@ e_contact_editor_get_property (GObject *object, guint prop_id, GValue *value, GP else g_value_set_object (value, NULL); break; + case PROP_REQUIRED_FIELDS: + if (e_contact_editor->required_fields) + g_value_set_object (value, e_list_duplicate (e_contact_editor->required_fields)); + else + g_value_set_object (value, NULL); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; diff --git a/addressbook/gui/contact-editor/e-contact-editor.h b/addressbook/gui/contact-editor/e-contact-editor.h index 6474e3c3a0..b27790d32a 100644 --- a/addressbook/gui/contact-editor/e-contact-editor.h +++ b/addressbook/gui/contact-editor/e-contact-editor.h @@ -89,6 +89,8 @@ struct _EContactEditor guint in_async_call : 1; EList *writable_fields; + + EList *required_fields; /* ID for async load_source call */ guint load_source_id; -- cgit v1.2.3