Code

mpdclient: use the "idle" command to reduce CPU and network usage
[ncmpc.git] / src / screen_client.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "screen_client.h"
21 #include "screen_utils.h"
22 #include "screen_message.h"
23 #include "mpdclient.h"
24 #include "i18n.h"
25 #include "charset.h"
27 static bool
28 _screen_auth(struct mpdclient *c, gint recursion)
29 {
30         struct mpd_connection *connection;
31         char *password;
33         connection = mpdclient_get_connection(c);
35         mpd_connection_clear_error(connection);
36         if (recursion > 2)
37                 return false;
39         password = screen_read_password(NULL);
40         if (password == NULL)
41                 return false;
43         mpd_send_password(connection, password);
44         g_free(password);
46         mpd_response_finish(connection);
47         mpdclient_update(c);
49         if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
50             mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_PASSWORD)
51                 return  _screen_auth(c, ++recursion);
53         return true;
54 }
56 bool
57 screen_auth(struct mpdclient *c)
58 {
59         return _screen_auth(c, 0);
60 }
62 void
63 mpdclient_ui_error(const char *message_utf8)
64 {
65         char *message_locale = utf8_to_locale(message_utf8);
66         screen_status_printf("%s", message_locale);
67         g_free(message_locale);
69         screen_bell();
70         doupdate();
71 }
73 void
74 screen_database_update(struct mpdclient *c, const char *path)
75 {
76         struct mpd_connection *connection;
77         unsigned id;
79         assert(c != NULL);
80         assert(mpdclient_is_connected(c));
82         connection = mpdclient_get_connection(c);
84         id = mpd_run_update(connection, path);
85         if (id == 0) {
86                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
87                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_UPDATE_ALREADY &&
88                     mpd_connection_clear_error(connection))
89                         screen_status_printf(_("Database update running..."));
90                 else
91                         mpdclient_handle_error(c);
92                 return;
93         }
95         /* set update_id to make sure the browse callback gets called
96            even if the update has finished before status is updated */
97         c->update_id = id;
99         if (path != NULL && *path != 0) {
100                 char *path_locale = utf8_to_locale(path);
101                 screen_status_printf(_("Database update of %s started"), path);
102                 g_free(path_locale);
103         } else
104                 screen_status_message(_("Database update started"));