Use a GList for the tunesHosts list. Also fixes a bug causing crash when a host goes offline. diff -X dontdiff -urNp tunesbrowser-0.1.6/daap.c tunesbrowser-dsd/daap.c --- tunesbrowser-0.1.6/daap.c 2004-07-31 15:05:02.000000000 +0100 +++ tunesbrowser-dsd/daap.c 2004-10-24 03:44:57.931629552 +0100 @@ -47,12 +47,9 @@ struct hostTAG char *sharename; int marked; /* used to test if its still in the enum callback */ - - Shost *prev; - Shost *next; }; -Shost *tunesHosts = NULL; +GList *tunesHosts = NULL; Shost *curHost = NULL; int curDBID = 0; @@ -146,13 +143,13 @@ static void free_artists() /* drawing */ static void draw_source() { - Shost *cur = tunesHosts; + GList *cur = g_list_first(tunesHosts); sourcelist_clear(); while (cur) { - sourcelist_append(cur, cur->sharename); - cur = cur->next; + sourcelist_append(cur->data, ((Shost*)cur->data)->sharename); + cur = g_list_next(cur); } } @@ -230,23 +227,25 @@ static void updateDatabases(Shost *host) int addDAAPEnumCB(DAAP_SClient *client, DAAP_SClientHost *host, void *ctx) { Shost *newhost; - Shost **h; + GList *h; /* Check if the host is already in our list */ - for (h = &tunesHosts; *h != NULL; h = &((*h)->next)) { - if ((*h)->daap_host == host) + h = g_list_first(tunesHosts); + while (h) { + Shost *newhost = h->data; + if (newhost->daap_host == host) { - (*h)->marked = 1; + newhost->marked = 1; return 1; } + h = g_list_next(h); } /* If not, add it to the end of the list */ - newhost = malloc(sizeof(Shost)); + newhost = g_malloc0(sizeof(Shost)); DAAP_ClientHost_AddRef(host); - newhost->next = NULL; newhost->daap_host = host; newhost->sharename = NULL; @@ -255,7 +254,7 @@ int addDAAPEnumCB(DAAP_SClient *client, newhost->marked = 1; - *h = newhost; + tunesHosts = g_list_append(tunesHosts, newhost); return 1; } @@ -268,37 +267,36 @@ void init_daap() void init_daap_sources() { /* FIXME should discover */ - Shost *curhost; + GList *hostlist; + GList *todelete = NULL; fprintf(stderr, "init daap sources\n"); - for (curhost = tunesHosts; curhost != NULL; curhost = curhost->next) - curhost->marked = 0; + hostlist = g_list_first(tunesHosts); + while (hostlist) { + if (hostlist->data != NULL) + ((Shost*)hostlist->data)->marked = 0; + hostlist = g_list_next(hostlist); + } DAAP_Client_EnumerateHosts(clientInst, addDAAPEnumCB, NULL); - curhost = tunesHosts; - while (curhost) + hostlist = g_list_first(tunesHosts); + while (hostlist) { - Shost *next = curhost->next; + Shost *curhost = hostlist->data; + char *buf; + int size; + if (curhost->marked == 0) { /* remove it */ fprintf(stderr, "deleting host!\n"); - if (curhost->prev) curhost->prev->next = next; - else tunesHosts = next; - if (next) next->prev = curhost->prev; - DAAP_ClientHost_Release(curhost->daap_host); + todelete = g_list_append(todelete, hostlist); + goto next; } - curhost = next; - } - - for (curhost = tunesHosts; curhost != NULL; curhost = curhost->next) - { - char *buf; - int size; if (curhost->sharename) - continue; + goto next; size = DAAP_ClientHost_GetSharename(curhost->daap_host, NULL, 0); @@ -306,7 +304,21 @@ void init_daap_sources() size = DAAP_ClientHost_GetSharename(curhost->daap_host, buf, size); curhost->sharename = buf; + + next: + hostlist = hostlist->next; + } + + todelete = g_list_first(todelete); + while (todelete) { + GList *node = todelete->data; + DAAP_ClientHost_Release(((Shost*)node)->daap_host); + tunesHosts = g_list_delete_link(tunesHosts, node); + + if (!todelete->next) break; + todelete = g_list_next(todelete); } + g_list_free(todelete); draw_source(); }