Code

filelist: provide more functions for working with a filelist
authorMax Kellermann <max@duempel.org>
Fri, 19 Sep 2008 14:23:31 +0000 (16:23 +0200)
committerMax Kellermann <max@duempel.org>
Fri, 19 Sep 2008 14:23:31 +0000 (16:23 +0200)
Avoid direct accesses to the filelist struct, provide an API for that.

src/filelist.c
src/filelist.h
src/mpdclient.c
src/screen_artist.c
src/screen_browser.c
src/screen_file.c
src/screen_search.c

index 55b4cfabf07e9f8f1a1cb0ab8e1c055845940962..bfc4c12b5faa7157935d0751f6cbac567437fea1 100644 (file)
 #include <string.h>
 #include <assert.h>
 
+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)
 {
index 3b33eb44960fd75eb29fb6caf4d6222d0daf91c1..e2425d787e2be8515632ab40dfdb0535d8ffffc0 100644 (file)
@@ -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);
 
index 5a630fb2bd2a3b67c3033b00b3df0a69467c3ac1..20248215f3c566b48ebf220dabc9c4f112825638 100644 (file)
@@ -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);
index 61f05cc51d26f2a85eec56a8f352688888065b73..71be6f408603f67596307b2ac3cad59b47f2e786 100644 (file)
@@ -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);
 
index 47fda6abe4da7c88bfe14117ae31ba283a56064f..40c63998062d32b48d6d29edefe29a8566a3c1b4 100644 (file)
@@ -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;
 
index 3df1a3fdc4cfd62b2fd5dfdccea7bdbb7cf9e9ce..9dd59001cc84ec3ad0bf348fa1e092527397cae6 100644 (file)
@@ -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 = {
index 822729ad43c7fd961eb6667948ec2c70fd96f4ba..cf919218aca4e1b811e506cef958d5b9736ca0ad 100644 (file)
@@ -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;