From 34ca92e9ed28234b506a158ef53b46eea6ccb78c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Sep 2009 20:36:05 +0200 Subject: [PATCH] screen_utils: moved screen_auth() to screen_client.c screen_client.c is a new library for MPD specific screen functions. --- Makefile.am | 2 ++ src/mpdclient.c | 2 +- src/screen_client.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/screen_client.h | 30 ++++++++++++++++++++++++ src/screen_utils.c | 36 +---------------------------- src/screen_utils.h | 6 +++-- 6 files changed, 94 insertions(+), 38 deletions(-) create mode 100644 src/screen_client.c create mode 100644 src/screen_client.h diff --git a/Makefile.am b/Makefile.am index a39d435..995d01f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,6 +25,7 @@ ncmpc_headers = \ src/screen_list.h \ src/screen_play.h \ src/screen_utils.h \ + src/screen_client.h \ src/list_window.h \ src/colors.h \ src/hscroll.h \ @@ -64,6 +65,7 @@ src_ncmpc_SOURCES = \ src/screen.c \ src/screen_list.c \ src/screen_utils.c \ + src/screen_client.c \ src/screen_play.c \ src/screen_browser.c \ src/screen_file.c \ diff --git a/src/mpdclient.c b/src/mpdclient.c index 1728303..1434d17 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -19,7 +19,7 @@ #include "mpdclient.h" #include "filelist.h" -#include "screen_utils.h" +#include "screen_client.h" #include "config.h" #include "options.h" #include "strfsong.h" diff --git a/src/screen_client.c b/src/screen_client.c new file mode 100644 index 0000000..e4cc7cd --- /dev/null +++ b/src/screen_client.c @@ -0,0 +1,56 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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 "screen_client.h" +#include "screen_utils.h" +#include "mpdclient.h" + +static gint +_screen_auth(struct mpdclient *c, gint recursion) +{ + char *password; + + mpd_connection_clear_error(c->connection); + if (recursion > 2) + return 1; + + password = screen_read_password(NULL, NULL); + if (password == NULL) + return 1; + + mpd_send_password(c->connection, password); + g_free(password); + + mpd_response_finish(c->connection); + mpdclient_update(c); + + if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER && + mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PASSWORD) + return _screen_auth(c, ++recursion); + return 0; +} + +gint +screen_auth(struct mpdclient *c) +{ + gint ret = _screen_auth(c, 0); + mpdclient_update(c); + curs_set(0); + return ret; +} diff --git a/src/screen_client.h b/src/screen_client.h new file mode 100644 index 0000000..73e90d5 --- /dev/null +++ b/src/screen_client.h @@ -0,0 +1,30 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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 NCMPC_SCREEN_CLIENT_H +#define NCMPC_SCREEN_CLIENT_H + +#include + +struct mpdclient; + +gint +screen_auth(struct mpdclient *c); + +#endif diff --git a/src/screen_utils.c b/src/screen_utils.c index d8e56df..9b70369 100644 --- a/src/screen_utils.c +++ b/src/screen_utils.c @@ -98,7 +98,7 @@ screen_getstr(WINDOW *w, const char *prompt) return screen_readln(w, prompt, NULL, NULL, NULL); } -static char * +char * screen_read_password(WINDOW *w, const char *prompt) { char *ret; @@ -124,40 +124,6 @@ screen_read_password(WINDOW *w, const char *prompt) return ret; } -static gint -_screen_auth(struct mpdclient *c, gint recursion) -{ - char *password; - - mpd_connection_clear_error(c->connection); - if (recursion > 2) - return 1; - - password = screen_read_password(NULL, NULL); - if (password == NULL) - return 1; - - mpd_send_password(c->connection, password); - g_free(password); - - mpd_response_finish(c->connection); - mpdclient_update(c); - - if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER && - mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PASSWORD) - return _screen_auth(c, ++recursion); - return 0; -} - -gint -screen_auth(struct mpdclient *c) -{ - gint ret = _screen_auth(c, 0); - mpdclient_update(c); - curs_set(0); - return ret; -} - /* query user for a string and find it in a list window */ int screen_find(list_window_t *lw, diff --git a/src/screen_utils.h b/src/screen_utils.h index 6aacd5d..c878273 100644 --- a/src/screen_utils.h +++ b/src/screen_utils.h @@ -40,6 +40,10 @@ int screen_getch(WINDOW *w, const char *prompt); /* read a string from the status window */ char *screen_getstr(WINDOW *w, const char *prompt); + +char * +screen_read_password(WINDOW *w, const char *prompt); + char *screen_readln(WINDOW *w, const char *prompt, const char *value, GList **history, GCompletion *gcmp); char *screen_readln_masked(WINDOW *w, const char *prompt); @@ -57,8 +61,6 @@ void screen_jump(struct list_window *lw, list_window_callback_fn_t callback_fn, void *callback_data); -gint screen_auth(struct mpdclient *c); - void screen_display_completion_list(GList *list); #ifndef NCMPC_MINI -- 2.30.2