1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| /*this function belows gets the contacts from the addressbook*/
int addressbook_get_addresses_from_xml_file(char *filename)
{
xmlDocPtr doc;
xmlNodePtr cur, child;
address = (AddressBook*)malloc(sizeof(AddressBook));
if (filename == NULL)
filename = "addressbook.xml";
/* Parse our addressbook file */
if(!(doc = xmlParseFile(imsua_addpath(filename))))
{
g_warning("Error opening addressbook file: %s", imsua_addpath(filename));
xmlFreeDoc( doc );
return FALSE;
}
if(!(cur = xmlDocGetRootElement(doc)))
{
g_warning("Addressbook document has no root element");
xmlFreeDoc( doc );
return FALSE;
}
if(xmlStrcmp(cur->name, (const xmlChar *) "addressbook" ))
{
g_warning("XML document of the wrong type, root node != addressbook");
xmlFreeDoc( doc );
return FALSE;
}
cur = cur->xmlChildrenNode;
/* Traverse through document looking for our addressbook */
while(cur)
{
if (cur->type == XML_ELEMENT_NODE)
{
if(child = cur->xmlChildrenNode)
{
if(!xmlStrcmp(cur->name, (const xmlChar *)"contact")){
cur = cur->xmlChildrenNode;
while(cur) {
if(cur->type == XML_ELEMENT_NODE){
if(child = cur->xmlChildrenNode){
if(!xmlStrcmp(cur->name, (const xmlChar *)"lastname")){
strcpy(address->lastname, child->content);
printf("Last Nmae: %s\n", address->lastname);
}
if(!xmlStrcmp(cur->name, (const xmlChar *)"firstname")){
strcpy(address->firstname, child->content);
printf("First Name: %s\n", address->firstname);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"homephone")){
//address->homephone = atoi(child->content);
strcpy(address->homephone, child->content);
printf("Home Phone: %s\n", address->homephone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"workphone")){
strcpy(address->workphone, child->content);
printf("Work Phone: %s\n", address->workphone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"cellphone")){
strcpy(address->cellphone, child->content);
printf("Cell Phone: %s\n", address->cellphone);}
if(!xmlStrcmp(cur->name, (const xmlChar *)"relationship"))
{
strcpy(address->relationship, child->content);
printf("Relationship: %s\n", address->relationship);}
}
}
cur = cur->next;
}
return TRUE;
}
}
}
cur = cur->next;
}
return TRUE;
}
/*this sets up the treeview*/
static void setup_tree_view (GtkWidget *treeview)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Last Name", renderer,
"text", LASTNAME, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("First Name", renderer,
"text", FIRSTNAME, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Home Phone", renderer,
"text", HOMEPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Work Phone", renderer,
"text", WORKPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Cell Phone", renderer,
"text", CELLPHONE, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Relationship", renderer,
"text", RELATIONSHIP, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}
/* Add the tree store */
static void
setup_tree_model (GtkWidget *treeview)
{
GtkListStore *store;
GtkWidget *list;
GtkTreeIter iter;
addressbook_get_addresses_from_xml_file("addressbook.xml");
store = gtk_list_store_new (COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, LASTNAME, address->lastname, FIRSTNAME, address->firstname, HOMEPHONE, address->homephone,WORKPHONE, address->workphone, CELLPHONE, address->cellphone, RELATIONSHIP, address->relationship, -1);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, LASTNAME, address->lastname, FIRSTNAME, address->firstname, HOMEPHONE, address->homephone,WORKPHONE, address->workphone, CELLPHONE, address->cellphone, RELATIONSHIP, address->relationship, -1);
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
g_object_unref (store);
}
/*the button to click in order to see the window with the treeview*/
void
on_view_button_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget *view_address_window, *treeview, *modify_button;
GladeXML *xml = NULL;
GNode *contact = NULL;
gchar *parsed[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
xml = glade_xml_new ("imsua.glade","view_address_window", NULL);
view_address_window = glade_xml_get_widget (xml, "view_address_window");
treeview = glade_xml_get_widget (xml, "treeview");
setup_tree_view (treeview);
setup_tree_model(treeview);
}
|