📄 abook.c
字号:
return; name= gtk_entry_get_text(GTK_ENTRY(abw.name)); numbers = gtk_editable_get_chars(GTK_EDITABLE(abw.numbers),0,-1); if (*numbers == '\0') { g_free(numbers); return; } abook_insert(nick,name,numbers,"\n"); g_free(numbers); gtk_entry_set_text (GTK_ENTRY(abw.nick), ""); gtk_entry_set_text (GTK_ENTRY(abw.name), ""); gtk_editable_delete_text(GTK_EDITABLE(abw.numbers),0, -1); abook_sort(); }}static void edit_apply_cb (GnomePropertyBox *pbox, gint page, gpointer data){ gchar *nick, *name, *numbers; if (page == -1) { /* Nick must not be NULL */ nick = gtk_entry_get_text (GTK_ENTRY(abw.nick)); if (*nick == '\0') return; name= gtk_entry_get_text(GTK_ENTRY(abw.name)); numbers = gtk_editable_get_chars(GTK_EDITABLE(abw.numbers),0,-1); if (*numbers == '\0') { g_free(numbers); return; } if(sel_nick != NULL) { abook_delete_entry(sel_nick); sel_nick = NULL; } else abook_delete_entry( (gchar*) gtk_ctree_node_get_row_data (GTK_CTREE(abw.tree), GTK_CTREE_NODE(insnode))); abook_insert(nick,name,numbers,"\n"); g_free(numbers); abook_sort(); }}static void tree_select_item_cb (GtkCTree *ctree, GList *node, gint column, gpointer user_data) { GtkCTreeRow *row; row = GTK_CTREE_ROW(node); if (row->parent != NULL) sel_alias = TRUE; else sel_alias = FALSE; sel_nick = (gchar*) gtk_ctree_node_get_row_data (GTK_CTREE(abw.tree), GTK_CTREE_NODE(node));}static void tree_unselect_item_cb (GtkCTree *ctree, GList *node, gint column, gpointer user_data) { sel_nick = NULL;}static void abook_save_cb (GtkWidget * widget, gpointer data){ abook_save();}static voidabook_insert ( gchar *nickname, gchar *fullname, gchar *numbers, gchar *delimiter){ gint i=0; gchar *line[]={"", "", ""}; gchar **elements; GtkCTreeNode *pnode, *node; AbookEntry *new_entry, *alias_entry=NULL; line[0]=nickname; line[1]=fullname; elements = g_strsplit (numbers, delimiter, 100); if(abook_search(elements[0]) == NULL && elements[1] == NULL) { line[2] = elements[0]; new_entry = g_new (AbookEntry, 1); new_entry->nick = g_strdup(line[0]); new_entry->name = g_strdup(line[1]); new_entry->number= g_strdup(line[2]); new_entry->alias=NULL; abook=g_list_append(abook, new_entry); node=gtk_ctree_insert_node(GTK_CTREE(abw.tree), NULL, NULL, line, 0, NULL, NULL, NULL, NULL, TRUE, FALSE); gtk_ctree_node_set_row_data(GTK_CTREE(abw.tree), node, new_entry->nick); insnode = node; } else { line[2] = NULL; new_entry = g_new (AbookEntry, 1); new_entry->nick = g_strdup(line[0]); new_entry->name = g_strdup(line[1]); new_entry->number= NULL; new_entry->alias=NULL; pnode=gtk_ctree_insert_node(GTK_CTREE(abw.tree), NULL, NULL, line, 0, NULL, NULL, NULL, NULL, FALSE, FALSE); gtk_ctree_node_set_row_data(GTK_CTREE(abw.tree), pnode, new_entry->nick); insnode = pnode; while(elements[i] != NULL) { /* Look up for nick in abook */ alias_entry = abook_search(elements[i]); if(alias_entry != NULL) { new_entry->alias=g_list_append(new_entry->alias, alias_entry); line[0] = alias_entry->nick; line[1] = alias_entry->name; line[2] = alias_entry->number; node = gtk_ctree_insert_node(GTK_CTREE(abw.tree), pnode, NULL, line, 0, NULL, NULL, NULL, NULL, TRUE, FALSE); gtk_ctree_node_set_row_data(GTK_CTREE(abw.tree), node, alias_entry->nick); } i++; } abook=g_list_append(abook, new_entry); } g_strfreev (elements); }static void abook_save(){ GList *abook_ptr, *alias_ptr; GString *line; gchar *filename; AbookEntry *entry, *alias; FILE *out; filename = g_strdup_printf("%s/.gsms/abook.csv",g_get_home_dir ()); if((out = fopen (filename, "w+")) == NULL) { g_log ("MAIN", G_LOG_LEVEL_WARNING ,"Could not open file:%s", strerror(errno)); g_free(filename); return; } g_free(filename); abook_ptr = abook; while(abook_ptr != NULL) { line = g_string_new(""); entry = (AbookEntry*) abook_ptr->data; if(entry->alias != NULL) { line = g_string_append(line, "ALIAS;"); g_string_sprintfa(line, "%s;",entry->nick); g_string_sprintfa(line, "%s;",entry->name); alias_ptr = entry->alias; while(alias_ptr != NULL) { alias = (AbookEntry*) alias_ptr->data; line = g_string_append(line, alias->nick); line = g_string_append_c(line,','); alias_ptr = g_list_next(alias_ptr); } g_string_erase (line,line->len-1,1); } else { line = g_string_append(line, "ENTRY;"); g_string_sprintfa(line, "%s;",entry->nick); g_string_sprintfa(line, "%s;",entry->name); g_string_sprintfa(line, "%s",entry->number); } line = g_string_append_c(line,'\n'); if( fputs(line->str, out) == EOF) { g_log ("MAIN", G_LOG_LEVEL_WARNING , "Could not write to file: %s", strerror(errno)); g_string_free(line, TRUE); break; } g_string_free(line, TRUE); abook_ptr = g_list_next(abook_ptr); } fclose(out); }static void abook_load (void) { gint pos; gchar line[MAXLINE], *filename; gchar **elements; FILE *in; const gchar *delimiter=";"; filename = g_strdup_printf("%s/.gsms/abook.csv",g_get_home_dir ()); if((in = fopen (filename, "r")) == NULL) { g_log ("MAIN", G_LOG_LEVEL_WARNING ,"Could not open file: %s", strerror(errno)); g_free(filename); return; } g_free(filename); while(fgets(line, sizeof(line), in) != NULL) { elements = g_strsplit (line, delimiter, 5); if (elements != NULL) { pos = strlen(elements[3])-1; elements[3][pos]='\0'; abook_insert(elements[1], elements[2], elements[3],","); } g_strfreev (elements); } if(ferror(in)) { g_log ("MAIN", G_LOG_LEVEL_WARNING , "Could not read from file: %s", strerror(errno)); return; } fclose(in); abook_sort();}static void abook_clear(void){ while(abook != NULL) { AbookEntry *entry = abook->data; if(entry->alias != NULL) { GList *alias_list_ptr = entry->alias; while(alias_list_ptr != NULL) { /* The elements itself has not to be freed */ g_free(alias_list_ptr->data); entry->alias = g_list_remove(alias_list_ptr, alias_list_ptr->data); alias_list_ptr=entry->alias; } } g_free(entry->nick); g_free(entry->name); g_free(entry->number); g_free(entry); abook = g_list_remove(abook, entry); } }static void abook_delete_entry(gchar *nick){ AbookEntry *entry; GtkCTreeNode *node; /* Alias node holds the same row data */ while ((node = gtk_ctree_find_by_row_data(GTK_CTREE(abw.tree), NULL, nick)) != NULL) gtk_ctree_remove_node(GTK_CTREE(abw.tree), node); if((entry = abook_search(nick)) != NULL) { if(entry->alias != NULL) { GList *alias_list_ptr = entry->alias; while(alias_list_ptr != NULL) { /* The elements itself has not to be freed */ entry->alias = g_list_remove(alias_list_ptr, alias_list_ptr->data); alias_list_ptr=entry->alias; } } g_free(entry->nick); g_free(entry->name); g_free(entry->number); g_free(entry); abook = g_list_remove(abook, entry); /* Find and remove nick in aliases */ find_remove_entry(nick); }}static void find_remove_entry (gchar *nick) { GList *list_ptr=abook, *alias_list_ptr; AbookEntry *entry, *alias_entry; while(list_ptr != NULL) { entry = list_ptr->data; if(entry->alias != NULL) { alias_list_ptr = entry->alias; while(alias_list_ptr != NULL) { alias_entry = alias_list_ptr->data; alias_list_ptr = g_list_next(alias_list_ptr); if (g_strcasecmp(alias_entry->nick, nick) == 0) { g_free(alias_entry); entry->alias = g_list_remove(entry->alias, alias_entry); } } } list_ptr = g_list_next(list_ptr); } }static void abook_sort(void){ GList *list = abook; gtk_ctree_sort_recursive(GTK_CTREE(abw.tree), NULL); abook=g_list_sort (list, &abook_compare);}gint abook_compare(gconstpointer a, gconstpointer b){ AbookEntry *entry1, *entry2; entry1 = (AbookEntry*) a; entry2 = (AbookEntry*) b; if ((entry1->alias == NULL) && (entry2->alias != NULL)) return -1; if ((entry1->alias != NULL) && (entry2->alias == NULL)) return 1; return (strcmp(entry1->nick, entry2->nick));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -