/* * gtkcutlass-editcontact.c - the callbacks for the edit contact window * (c) 2004, 2005 - John Schweitzer * Todd MacDermid */ #include #include #include #include #include #include #include gint editchanged = 0; gchar *editname = NULL; gchar *editwindowtitle = NULL; gint editnamelen = 0; gchar *edithostname = NULL; gint edithostnamelen = 0; guint editpreferedport = CUT_DEFAULT_PORT; gchar *editnickname = NULL; extern user_info *userstuff; gchar *contactname=NULL; // the original name before editing. gint editinuse=0; contact_info *ci; void on_EditContactPeepNameEntry_changed ( GtkWidget *widget, gpointer user_data ) { gint length = 0; editchanged = 1; // what is the length of the entered string? length = strlen( gtk_entry_get_text( ( GtkEntry * ) widget ) ); if ( length >= 64 ) return ; if ( editname != NULL ) g_free( editname ); editname = ( gchar * ) g_strndup( gtk_entry_get_text( ( GtkEntry * ) widget ), 63 ); editwindowtitle = g_strdup_printf( "New Contact - %s", editname ); gtk_window_set_title( GTK_WINDOW( gtk_widget_get_toplevel( widget ) ), editwindowtitle ); //g_print("NCPNE - changed :%s\n",name); }; void on_EditContactCancelButton_activate ( GtkWidget *widget, gpointer user_data ) { if ( editchanged != 0 ) if ( YesNo( widget, "Entry Changed. Really Cancel?" ) == GTK_RESPONSE_NO ) return ; gtk_widget_destroy( gtk_widget_get_toplevel( widget ) ); editinuse=0; }; void on_EditContactOKButton_activate ( GtkWidget *widget, gpointer user_data ) { contact_info * newcontact; contact_info * oldcontact; extern user_info *userstuff; extern gui_status *currentstatus; gint validation = 0; GtkTreeIter iter; GtkTreeStore *gtkts; newcontact = contact_info_init(); newcontact->contactname = g_strndup( editname, 64 ); newcontact->contacthostname = g_strndup( edithostname, 128 ); newcontact->nickname = g_strndup( editnickname, 64 ); newcontact->port = editpreferedport; validation = validate_contactinfo ( newcontact ); if ( validation == 0 ) { gtkts = GTK_TREE_STORE(gtk_tree_view_get_model(currentstatus->Tree)); oldcontact = get_contact_from_name (contactname); if (oldcontact == NULL) g_warning ("error updating contact list"); iter = get_tree_iter_name ( contactname ); gtk_tree_store_remove( gtkts, &iter ); insert_treestore_contactinfo (newcontact, iter); userstuff->contacts = g_slist_remove(userstuff->contacts, (gconstpointer) oldcontact); userstuff->contacts = g_slist_append( userstuff->contacts, newcontact ); if (ci != NULL) contact_info_destroy(ci); save_contact_list(); editinuse=0; gtk_widget_destroy( gtk_widget_get_toplevel( widget ) ); } else switch ( validation ) { case GTKC_ERR_NONAME: { tellem( "Contact needs a name" ); break; } case GTKC_ERR_NULL: { tellem( "I was handed a NULL pointer. Notify the developers." ); break; } default: { tellem( "There is something so wrong with this contact that there is no error code for it. (!)" ); break; } } return ; }; void on_EditContactHostnameEntry_changed ( GtkWidget *widget, gpointer user_data ) { edithostname = ( gchar * ) g_strndup( gtk_entry_get_text( ( GtkEntry * ) widget ), GTKC_MAX_HOSTNAME_LEN ); editchanged = 1; }; void on_EditContactWindow_destroy ( GtkWidget *widget, gpointer user_data ) { if ( editchanged != 0 ) if ( YesNo( widget, "Entry Changed. Really Cancel?" ) == GTK_RESPONSE_NO ) return ; gtk_widget_destroy ( widget ); editinuse=0; }; void on_EditContactWindow_delete_event ( GtkWidget *widget, gpointer user_data ) { on_EditContactWindow_destroy ( widget, user_data ); editinuse=0; }; void on_EditContactNicknameentry_changed ( GtkWidget *widget, gpointer user_data ) { gint length = 0; editchanged = 1; length = strlen( gtk_entry_get_text( ( GtkEntry * ) widget ) ); if ( length >= 64 ) return ; if ( editnickname != NULL ) g_free( editnickname ); editnickname = ( gchar * ) g_strndup( gtk_entry_get_text( ( GtkEntry * ) widget ), 63 ); }; void on_Editcontactport_changed ( GtkWidget *widget, gpointer user_data ) { gdouble tempval = CUT_DEFAULT_PORT; tempval = gtk_spin_button_get_value( GTK_SPIN_BUTTON( widget ) ); editchanged = 1; if ( ( tempval <= 65535 ) && ( tempval >= 0 ) ) editpreferedport = ( guint ) tempval; } /* gint check_contact_exists( contact_info *ci, user_info *ui ) { // FIXME: The comparison is busted for UTF8 GSList * tmp = NULL; gint match = 0; contact_info *meh; if ( ci == NULL ) return -1; if ( ui == NULL ) return -1; tmp = ui->contacts; while ( tmp != NULL ) { meh = ( contact_info * ) tmp->data; if ( g_ascii_strcasecmp( ci->contactname, meh->contactname ) == 0 ) match += 1; if ( g_ascii_strcasecmp( ci->nickname, meh->nickname ) == 0 ) match += 1; tmp = tmp->next; } return ( match ); } */ void run_editcontact_window (GtkWidget *widget, gpointer user_data) { GladeXML * EditContactWindowXML; contact_info *ci=NULL; GtkWidget *contactnamewidgy; GtkWidget *contactnicknamewidgy; GtkWidget *contactportwidgy; GtkWidget *contacthostnamewidgy; extern gui_status *currentstatus; if ( editinuse != 0 ) return; editinuse = 1; contactname = get_treeview_selection_name(); if (contactname == NULL) return; ci = get_contact_from_name (contactname); if (ci == NULL) { g_warning ("corrupt contact information. Name in tree without contact information"); g_free(contactname); return; } EditContactWindowXML = glade_xml_new_from_buffer( currentstatus->XMLstuff, currentstatus->XMLstuffsize, "EditContactWindow", NULL ); if ( EditContactWindowXML == NULL ) { g_error( "Couldn't create window for new contact. (gtkcutlass.glade not found?) Aborting.\n" ); exit( -1 ); } contactnamewidgy = glade_xml_get_widget (EditContactWindowXML, "EditContactNameEntry"); contactnicknamewidgy = glade_xml_get_widget (EditContactWindowXML, "EditContactNicknameEntry"); contacthostnamewidgy = glade_xml_get_widget (EditContactWindowXML, "EditContactHostnameEntry"); contactportwidgy = glade_xml_get_widget (EditContactWindowXML, "EditContactPortSpinButton"); if ((contactnamewidgy == NULL) || (contactnicknamewidgy == NULL) || ( contacthostnamewidgy == NULL) || ( contactportwidgy == NULL) ) { g_warning ("couldn't find widgeys."); g_free(contactname); return; } gtk_entry_set_text (GTK_ENTRY(contactnamewidgy),ci->contactname); gtk_entry_set_text (GTK_ENTRY(contactnicknamewidgy),ci->nickname); gtk_entry_set_text (GTK_ENTRY(contacthostnamewidgy),ci->contacthostname); gtk_spin_button_set_value (GTK_SPIN_BUTTON(contactportwidgy), (gdouble) ci->port); editname = ci->contactname; edithostname = ci->contacthostname; editnickname = ci->nickname; editpreferedport = ci->port; ci = get_contact_from_name (editname); glade_xml_signal_autoconnect( EditContactWindowXML ); }