From d1c31cf66398447b5ba0d0a60de5b92ad162675c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 19 Sep 2008 16:23:31 +0200 Subject: [PATCH] filelist: provide more functions for working with a filelist Avoid direct accesses to the filelist struct, provide an API for that. --- src/filelist.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/filelist.h | 34 +++++++++++++++++++++++++++ src/mpdclient.c | 32 +++++++------------------ src/screen_artist.c | 13 ++++------ src/screen_browser.c | 13 +++++----- src/screen_file.c | 12 +++++----- src/screen_search.c | 22 +++++++---------- 7 files changed, 124 insertions(+), 58 deletions(-) diff --git a/src/filelist.c b/src/filelist.c index 55b4cfa..bfc4c12 100644 --- a/src/filelist.c +++ b/src/filelist.c @@ -23,6 +23,19 @@ #include #include +struct filelist * +filelist_new(const char *path) +{ + struct filelist *filelist = g_malloc(sizeof(*filelist)); + + filelist->path = g_strdup(path); + filelist->length = 0; + filelist->updated = FALSE; + filelist->list = NULL; + + return filelist; +} + void filelist_free(struct filelist *filelist) { @@ -46,6 +59,49 @@ filelist_free(struct filelist *filelist) g_free(filelist); } +struct filelist_entry * +filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity) +{ + struct filelist_entry *entry = g_malloc(sizeof(*entry)); + + entry->flags = 0; + entry->entity = entity; + + filelist->list = g_list_append(filelist->list, entry); + filelist->length++; + + return entry; +} + +struct filelist_entry * +filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity) +{ + struct filelist_entry *entry = g_malloc(sizeof(*entry)); + + entry->flags = 0; + entry->entity = entity; + + filelist->list = g_list_insert(filelist->list, entry, 0); + filelist->length++; + + return entry; +} + +void +filelist_move(struct filelist *filelist, struct filelist *from) +{ + filelist->list = g_list_concat(filelist->list, from->list); + filelist->length += from->length; + from->list = NULL; + from->length = 0; +} + +void +filelist_sort(struct filelist *filelist, GCompareFunc compare_func) +{ + filelist->list = g_list_sort(filelist->list, compare_func); +} + struct filelist_entry * filelist_find_song(struct filelist *fl, const struct mpd_song *song) { diff --git a/src/filelist.h b/src/filelist.h index 3b33eb4..e2425d7 100644 --- a/src/filelist.h +++ b/src/filelist.h @@ -43,9 +43,43 @@ typedef struct filelist { GList *list; } mpdclient_filelist_t; +struct filelist * +filelist_new(const char *path); + void filelist_free(struct filelist *filelist); +static inline guint +filelist_length(const struct filelist *filelist) +{ + return filelist->length; +} + +static inline gboolean +filelist_is_empty(const struct filelist *filelist) +{ + return filelist_length(filelist) == 0; +} + +static inline struct filelist_entry * +filelist_get(const struct filelist *filelist, guint i) +{ + return (struct filelist_entry*) + g_list_nth_data(filelist->list, i); +} + +struct filelist_entry * +filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity); + +struct filelist_entry * +filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity); + +void +filelist_move(struct filelist *filelist, struct filelist *from); + +void +filelist_sort(struct filelist *filelist, GCompareFunc compare_func); + struct filelist_entry * filelist_find_song(struct filelist *flist, const struct mpd_song *song); diff --git a/src/mpdclient.c b/src/mpdclient.c index 5a630fb..2024821 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -746,21 +746,13 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path) D("mpdclient_filelist_get(%s)\n", path); mpd_sendLsInfoCommand(c->connection, path_utf8); - filelist = g_malloc0(sizeof(mpdclient_filelist_t)); - if (path && path[0] && strcmp(path, "/")) { + filelist = filelist_new(path); + if (path && path[0] && strcmp(path, "/")) /* add a dummy entry for ./.. */ - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); - entry->entity = NULL; - filelist->list = g_list_append(filelist->list, entry); - filelist->length++; - } + filelist_append(filelist, NULL); while ((entity=mpd_getNextInfoEntity(c->connection))) { - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); - - entry->entity = entity; - filelist->list = g_list_append(filelist->list, entry); - filelist->length++; + filelist_append(filelist, entity); if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) { has_dirs_only = FALSE; @@ -771,13 +763,12 @@ mpdclient_filelist_get(mpdclient_t *c, const gchar *path) mpdclient_finish_command(c); g_free(path_utf8); - filelist->path = g_strdup(path); filelist->updated = TRUE; // If there are only directory entities in the filelist, we sort it if (has_dirs_only) { D("mpdclient_filelist_get: only dirs; sorting!\n"); - filelist->list = g_list_sort(filelist->list, compare_filelistentry_dir); + filelist_sort(filelist, compare_filelistentry_dir); } return filelist; @@ -797,15 +788,10 @@ mpdclient_filelist_search_utf8(mpdclient_t *c, mpd_sendFindCommand(c->connection, table, filter_utf8); else mpd_sendSearchCommand(c->connection, table, filter_utf8); - filelist = g_malloc0(sizeof(mpdclient_filelist_t)); + filelist = filelist_new(NULL); - while ((entity=mpd_getNextInfoEntity(c->connection))) { - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); - - entry->entity = entity; - filelist->list = g_list_append(filelist->list, entry); - filelist->length++; - } + while ((entity=mpd_getNextInfoEntity(c->connection))) + filelist_append(filelist, entity); if (mpdclient_finish_command(c)) { filelist_free(filelist); @@ -854,7 +840,7 @@ mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl) { GList *list = g_list_first(fl->list); - if (fl->list == NULL || fl->length < 1) + if (filelist_is_empty(fl)) return 0; mpd_sendCommandListBegin(c->connection); diff --git a/src/screen_artist.c b/src/screen_artist.c index 61f05cc..71be6f4 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -108,8 +108,6 @@ update_metalist(mpdclient_t *c, char *m_artist, char *m_album) if (m_album) { /* retreive songs... */ - filelist_entry_t *entry; - artist = m_artist; album = m_album; if (album[0] == 0) { @@ -124,11 +122,8 @@ update_metalist(mpdclient_t *c, char *m_artist, char *m_album) MPD_TABLE_ALBUM, album); /* add a dummy entry for ".." */ - entry = g_malloc0(sizeof(filelist_entry_t)); - entry->entity = NULL; - browser.filelist->list = g_list_insert(browser.filelist->list, - entry, 0); - browser.filelist->length++; + filelist_prepend(browser.filelist, NULL); + /* install playlist callback and fix highlights */ sync_highlights(c, browser.filelist); mpdclient_install_playlist_callback(c, playlist_changed_callback); @@ -425,7 +420,7 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) case CMD_LIST_RFIND_NEXT: if (browser.filelist) return screen_find(screen, - browser.lw, browser.filelist->length, + browser.lw, filelist_length(browser.filelist), cmd, browser_lw_callback, browser.filelist); else if (metalist) @@ -443,7 +438,7 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) } if (browser.filelist) - return list_window_cmd(browser.lw, browser.filelist->length, cmd); + return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd); else if (metalist) return list_window_cmd(browser.lw, metalist_length, cmd); diff --git a/src/screen_browser.c b/src/screen_browser.c index 47fda6a..40c6399 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -125,7 +125,8 @@ browser_lw_callback(unsigned idx, int *highlight, void *data) filelist_entry_t *entry; mpd_InfoEntity *entity; - if( (entry=(filelist_entry_t *)g_list_nth_data(fl->list,idx))==NULL ) + entry = filelist_get(fl, idx); + if (entry == NULL) return NULL; entity = entry->entity; @@ -204,7 +205,7 @@ browser_change_directory(struct screen_browser *browser, mpdclient_t *c, filelist_free(browser->filelist); browser->filelist = mpdclient_filelist_get(c, path); sync_highlights(c, browser->filelist); - list_window_check_selected(browser->lw, browser->filelist->length); + list_window_check_selected(browser->lw, filelist_length(browser->filelist)); g_free(path); return 0; } @@ -254,8 +255,8 @@ browser_handle_enter(struct screen_browser *browser, mpdclient_t *c) if (browser->filelist == NULL) return -1; - entry = (filelist_entry_t *) g_list_nth_data(browser->filelist->list, - browser->lw->selected); + + entry = filelist_get(browser->filelist, browser->lw->selected); if( entry==NULL ) return -1; @@ -382,7 +383,7 @@ browser_handle_select(struct screen_browser *browser, mpdclient_t *c) if (browser->filelist == NULL) return -1; - entry = g_list_nth_data(browser->filelist->list, browser->lw->selected); + entry = filelist_get(browser->filelist, browser->lw->selected); if (entry == NULL || entry->entity == NULL) return -1; @@ -419,7 +420,7 @@ browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c) int length; if (browser->filelist) - length = browser->filelist->length; + length = filelist_length(browser->filelist); else length = 0; diff --git a/src/screen_file.c b/src/screen_file.c index 3df1a3f..9dd5900 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -46,7 +46,7 @@ file_changed_callback(mpdclient_t *c, mpd_unused int event, D("screen_file.c> filelist_callback() [%d]\n", event); browser.filelist = mpdclient_filelist_update(c, browser.filelist); sync_highlights(c, browser.filelist); - list_window_check_selected(browser.lw, browser.filelist->length); + list_window_check_selected(browser.lw, filelist_length(browser.filelist)); } /* the playlist have been updated -> fix highlights */ @@ -62,7 +62,7 @@ handle_save(screen_t *screen, mpdclient_t *c) filelist_entry_t *entry; char *defaultname = NULL; - entry = g_list_nth_data(browser.filelist->list, browser.lw->selected); + entry = filelist_get(browser.filelist, browser.lw->selected); if( entry && entry->entity ) { mpd_InfoEntity *entity = entry->entity; if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) { @@ -83,7 +83,7 @@ handle_delete(screen_t *screen, mpdclient_t *c) char *str, *buf; int key; - entry = g_list_nth_data(browser.filelist->list,browser. lw->selected); + entry = filelist_get(browser.filelist, browser.lw->selected); if( entry==NULL || entry->entity==NULL ) return -1; @@ -223,7 +223,7 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) browser.lw->repaint = 1; browser.filelist = mpdclient_filelist_update(c, browser.filelist); list_window_check_selected(browser.lw, - browser.filelist->length); + filelist_length(browser.filelist)); screen_status_printf(_("Screen updated!")); return 1; case CMD_DB_UPDATE: @@ -250,7 +250,7 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) case CMD_LIST_FIND_NEXT: case CMD_LIST_RFIND_NEXT: return screen_find(screen, - browser.lw, browser.filelist->length, + browser.lw, filelist_length(browser.filelist), cmd, browser_lw_callback, browser.filelist); case CMD_MOUSE_EVENT: @@ -259,7 +259,7 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) break; } - return list_window_cmd(browser.lw, browser.filelist->length, cmd); + return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd); } const struct screen_functions screen_browse = { diff --git a/src/screen_search.c b/src/screen_search.c index 822729a..cf91921 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -189,9 +189,8 @@ filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table, list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE, local_pattern); - list->length += list2->length; - list->list = g_list_concat(list->list, list2->list); - list->list = g_list_sort(list->list, compare_filelistentry_format); + filelist_move(list, list2); + filelist_sort(list, compare_filelistentry_format); list->updated = TRUE; } else list = mpdclient_filelist_search(c, FALSE, table, local_pattern); @@ -283,13 +282,8 @@ search_advanced_query(char *query, mpdclient_t *c) fl = g_malloc0(sizeof(mpdclient_filelist_t)); - while ((entity=mpd_getNextInfoEntity(c->connection))) { - filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t)); - - entry->entity = entity; - fl->list = g_list_append(fl->list, (gpointer) entry); - fl->length++; - } + while ((entity=mpd_getNextInfoEntity(c->connection))) + filelist_append(fl, entity); if (mpdclient_finish_command(c) && fl) filelist_free(fl); @@ -338,7 +332,7 @@ search_new(screen_t *screen, mpdclient_t *c) sync_highlights(c, browser.filelist); mpdclient_install_playlist_callback(c, playlist_changed_callback); - list_window_check_selected(browser.lw, browser.filelist->length); + list_window_check_selected(browser.lw, filelist_length(browser.filelist)); } @@ -447,7 +441,7 @@ search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) cmd = CMD_LIST_NEXT; } /* call list_window_cmd to go to the next item */ - return list_window_cmd(browser.lw, browser.filelist->length, cmd); + return list_window_cmd(browser.lw, filelist_length(browser.filelist), cmd); case CMD_SELECT_ALL: browser_handle_select_all(&browser, c); @@ -487,7 +481,7 @@ search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) case CMD_LIST_RFIND_NEXT: if (browser.filelist) return screen_find(screen, - browser.lw, browser.filelist->length, + browser.lw, filelist_length(browser.filelist), cmd, browser_lw_callback, browser.filelist); else @@ -499,7 +493,7 @@ search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) default: if (browser.filelist) return list_window_cmd(browser.lw, - browser.filelist->length, cmd); + filelist_length(browser.filelist), cmd); } return 0; -- 2.30.2