summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e3b42ca)
raw | patch | inline | side by side (parent: e3b42ca)
author | Max Kellermann <max@duempel.org> | |
Fri, 19 Sep 2008 14:23:31 +0000 (16:23 +0200) | ||
committer | Max 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.
diff --git a/src/filelist.c b/src/filelist.c
index 55b4cfabf07e9f8f1a1cb0ab8e1c055845940962..bfc4c12b5faa7157935d0751f6cbac567437fea1 100644 (file)
--- a/src/filelist.c
+++ b/src/filelist.c
#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)
{
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 3b33eb44960fd75eb29fb6caf4d6222d0daf91c1..e2425d787e2be8515632ab40dfdb0535d8ffffc0 100644 (file)
--- a/src/filelist.h
+++ b/src/filelist.h
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 5a630fb2bd2a3b67c3033b00b3df0a69467c3ac1..20248215f3c566b48ebf220dabc9c4f112825638 100644 (file)
--- a/src/mpdclient.c
+++ b/src/mpdclient.c
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;
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;
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);
{
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 61f05cc51d26f2a85eec56a8f352688888065b73..71be6f408603f67596307b2ac3cad59b47f2e786 100644 (file)
--- a/src/screen_artist.c
+++ b/src/screen_artist.c
if (m_album) {
/* retreive songs... */
- filelist_entry_t *entry;
-
artist = m_artist;
album = m_album;
if (album[0] == 0) {
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);
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)
}
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 47fda6abe4da7c88bfe14117ae31ba283a56064f..40c63998062d32b48d6d29edefe29a8566a3c1b4 100644 (file)
--- a/src/screen_browser.c
+++ b/src/screen_browser.c
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;
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;
}
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;
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;
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 3df1a3fdc4cfd62b2fd5dfdccea7bdbb7cf9e9ce..9dd59001cc84ec3ad0bf348fa1e092527397cae6 100644 (file)
--- a/src/screen_file.c
+++ b/src/screen_file.c
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 */
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 ) {
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;
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:
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:
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 822729ad43c7fd961eb6667948ec2c70fd96f4ba..cf919218aca4e1b811e506cef958d5b9736ca0ad 100644 (file)
--- a/src/screen_search.c
+++ b/src/screen_search.c
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);
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);
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));
}
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);
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
default:
if (browser.filelist)
return list_window_cmd(browser.lw,
- browser.filelist->length, cmd);
+ filelist_length(browser.filelist), cmd);
}
return 0;