From aab54ebefbbb5e81e8feca6dfd08ea463df88e7c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 21 Mar 2017 22:29:59 +0100 Subject: [PATCH] utils: move gcmp_list_from_path() to db_completion.c --- Makefile.am | 1 + src/db_completion.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ src/db_completion.h | 40 +++++++++++++++++++++++++ src/save_playlist.c | 1 + src/screen_queue.c | 1 + src/utils.c | 52 --------------------------------- src/utils.h | 13 --------- 7 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 src/db_completion.c create mode 100644 src/db_completion.h diff --git a/Makefile.am b/Makefile.am index a72ecab..a70f478 100644 --- a/Makefile.am +++ b/Makefile.am @@ -93,6 +93,7 @@ endif if NCMPC_MINI else src_ncmpc_SOURCES += \ + src/db_completion.c src/db_completion.h \ src/xterm_title.c src/xterm_title.h \ src/hscroll.c src/hscroll.h \ src/match.c src/match.h \ diff --git a/src/db_completion.c b/src/db_completion.c new file mode 100644 index 0000000..739e145 --- /dev/null +++ b/src/db_completion.c @@ -0,0 +1,71 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2017 The Music Player Daemon Project + * Project homepage: http://musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "db_completion.h" +#include "charset.h" +#include "mpdclient.h" + +#include + +GList * +gcmp_list_from_path(struct mpdclient *c, const gchar *path, + GList *list, gint types) +{ + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return list; + + mpd_send_list_meta(connection, path); + + struct mpd_entity *entity; + while ((entity = mpd_recv_entity(connection)) != NULL) { + char *name; + + if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY && + types & GCMP_TYPE_DIR) { + const struct mpd_directory *dir = + mpd_entity_get_directory(entity); + gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir)); + gsize size = strlen(tmp)+2; + + name = g_malloc(size); + g_strlcpy(name, tmp, size); + g_strlcat(name, "/", size); + g_free(tmp); + } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG && + types & GCMP_TYPE_FILE) { + const struct mpd_song *song = + mpd_entity_get_song(entity); + name = utf8_to_locale(mpd_song_get_uri(song)); + } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST && + types & GCMP_TYPE_PLAYLIST) { + const struct mpd_playlist *playlist = + mpd_entity_get_playlist(entity); + name = utf8_to_locale(mpd_playlist_get_path(playlist)); + } else { + mpd_entity_free(entity); + continue; + } + + list = g_list_append(list, name); + mpd_entity_free(entity); + } + + return list; +} diff --git a/src/db_completion.h b/src/db_completion.h new file mode 100644 index 0000000..57a8b1f --- /dev/null +++ b/src/db_completion.h @@ -0,0 +1,40 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2017 The Music Player Daemon Project + * Project homepage: http://musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef DB_COMPLETION_H +#define DB_COMPLETION_H + +#include + +struct mpdclient; + +#define GCMP_TYPE_DIR (0x01 << 0) +#define GCMP_TYPE_FILE (0x01 << 1) +#define GCMP_TYPE_PLAYLIST (0x01 << 2) +#define GCMP_TYPE_RFILE (GCMP_TYPE_DIR | GCMP_TYPE_FILE) +#define GCMP_TYPE_RPLAYLIST (GCMP_TYPE_DIR | GCMP_TYPE_PLAYLIST) + +/** + * Create a list suitable for GCompletion from path. + */ +GList * +gcmp_list_from_path(struct mpdclient *c, const gchar *path, + GList *list, gint types); + +#endif diff --git a/src/save_playlist.c b/src/save_playlist.c index 118d11a..ed2134d 100644 --- a/src/save_playlist.c +++ b/src/save_playlist.c @@ -18,6 +18,7 @@ */ #include "save_playlist.h" +#include "db_completion.h" #include "screen_status.h" #include "config.h" #include "i18n.h" diff --git a/src/screen_queue.c b/src/screen_queue.c index 3d20da3..d98445d 100644 --- a/src/screen_queue.c +++ b/src/screen_queue.c @@ -36,6 +36,7 @@ #include "screen_utils.h" #include "screen_song.h" #include "screen_lyrics.h" +#include "db_completion.h" #include "Compiler.h" #ifndef NCMPC_MINI diff --git a/src/utils.c b/src/utils.c index 3f327f4..8f867ad 100644 --- a/src/utils.c +++ b/src/utils.c @@ -18,12 +18,8 @@ */ #include "utils.h" -#include "options.h" -#include "charset.h" #include "i18n.h" -#include "mpdclient.h" -#include #include #include @@ -63,54 +59,6 @@ string_list_remove(GList *string_list, const gchar *str) return list; } -/* create a list suitable for GCompletion from path */ -GList * -gcmp_list_from_path(struct mpdclient *c, const gchar *path, - GList *list, gint types) -{ - struct mpd_connection *connection = mpdclient_get_connection(c); - if (connection == NULL) - return list; - - mpd_send_list_meta(connection, path); - - struct mpd_entity *entity; - while ((entity = mpd_recv_entity(connection)) != NULL) { - char *name; - - if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY && - types & GCMP_TYPE_DIR) { - const struct mpd_directory *dir = - mpd_entity_get_directory(entity); - gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir)); - gsize size = strlen(tmp)+2; - - name = g_malloc(size); - g_strlcpy(name, tmp, size); - g_strlcat(name, "/", size); - g_free(tmp); - } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG && - types & GCMP_TYPE_FILE) { - const struct mpd_song *song = - mpd_entity_get_song(entity); - name = utf8_to_locale(mpd_song_get_uri(song)); - } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST && - types & GCMP_TYPE_PLAYLIST) { - const struct mpd_playlist *playlist = - mpd_entity_get_playlist(entity); - name = utf8_to_locale(mpd_playlist_get_path(playlist)); - } else { - mpd_entity_free(entity); - continue; - } - - list = g_list_append(list, name); - mpd_entity_free(entity); - } - - return list; -} - void format_duration_short(char *buffer, size_t length, unsigned duration) { diff --git a/src/utils.h b/src/utils.h index 46f5ba5..ecd68bb 100644 --- a/src/utils.h +++ b/src/utils.h @@ -22,24 +22,11 @@ #include -struct mpdclient; - /* functions for lists containing strings */ GList *string_list_free(GList *string_list); GList *string_list_find(GList *string_list, const gchar *str); GList *string_list_remove(GList *string_list, const gchar *str); -/* create a string list from path - used for completion */ -#define GCMP_TYPE_DIR (0x01 << 0) -#define GCMP_TYPE_FILE (0x01 << 1) -#define GCMP_TYPE_PLAYLIST (0x01 << 2) -#define GCMP_TYPE_RFILE (GCMP_TYPE_DIR | GCMP_TYPE_FILE) -#define GCMP_TYPE_RPLAYLIST (GCMP_TYPE_DIR | GCMP_TYPE_PLAYLIST) - -GList * -gcmp_list_from_path(struct mpdclient *c, const gchar *path, - GList *list, gint types); - void format_duration_short(char *buffer, size_t length, unsigned duration); -- 2.30.2