1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
24 #include "mpdclient.h"
31 string_list_free(GList
*string_list
)
33 GList
*list
= g_list_first(string_list
);
41 g_list_free(string_list
);
46 string_list_find(GList
*string_list
, const gchar
*str
)
48 GList
*list
= g_list_first(string_list
);
51 if( strcmp(str
, (gchar
*) list
->data
) == 0 )
59 string_list_remove(GList
*string_list
, const gchar
*str
)
61 GList
*list
= g_list_first(string_list
);
64 if( strcmp(str
, (gchar
*) list
->data
) == 0 ) {
67 return g_list_delete_link(string_list
, list
);
74 /* create a list suitable for GCompletion from path */
76 gcmp_list_from_path(struct mpdclient
*c
, const gchar
*path
,
77 GList
*list
, gint types
)
79 struct mpd_connection
*connection
;
80 struct mpd_entity
*entity
;
82 connection
= mpdclient_get_connection(c
);
83 if (connection
== NULL
)
86 mpd_send_list_meta(connection
, path
);
88 while ((entity
= mpd_recv_entity(connection
)) != NULL
) {
91 if (mpd_entity_get_type(entity
) == MPD_ENTITY_TYPE_DIRECTORY
&&
92 types
& GCMP_TYPE_DIR
) {
93 const struct mpd_directory
*dir
=
94 mpd_entity_get_directory(entity
);
95 gchar
*tmp
= utf8_to_locale(mpd_directory_get_path(dir
));
96 gsize size
= strlen(tmp
)+2;
98 name
= g_malloc(size
);
99 g_strlcpy(name
, tmp
, size
);
100 g_strlcat(name
, "/", size
);
102 } else if (mpd_entity_get_type(entity
) == MPD_ENTITY_TYPE_SONG
&&
103 types
& GCMP_TYPE_FILE
) {
104 const struct mpd_song
*song
=
105 mpd_entity_get_song(entity
);
106 name
= utf8_to_locale(mpd_song_get_uri(song
));
107 } else if (mpd_entity_get_type(entity
) == MPD_ENTITY_TYPE_PLAYLIST
&&
108 types
& GCMP_TYPE_PLAYLIST
) {
109 const struct mpd_playlist
*playlist
=
110 mpd_entity_get_playlist(entity
);
111 name
= utf8_to_locale(mpd_playlist_get_path(playlist
));
113 mpd_entity_free(entity
);
117 list
= g_list_append(list
, name
);
118 mpd_entity_free(entity
);
125 format_duration_short(char *buffer
, size_t length
, unsigned duration
)
128 g_snprintf(buffer
, length
,
129 "%i:%02i", duration
/ 60, duration
% 60);
131 g_snprintf(buffer
, length
,
132 "%i:%02i:%02i", duration
/ 3600,
133 (duration
% 3600) / 60, duration
% 60);
137 format_duration_long(char *p
, size_t length
, unsigned long duration
)
139 const char *year
= _("year");
140 const char *years
= _("years");
141 const char *week
= _("week");
142 const char *weeks
= _("weeks");
143 const char *day
= _("day");
144 const char *days
= _("days");
145 unsigned bytes_written
= 0;
147 if (duration
/ 31536000 > 0) {
148 if (duration
/ 31536000 == 1)
149 bytes_written
= g_snprintf(p
, length
, "%d %s, ", 1, year
);
151 bytes_written
= g_snprintf(p
, length
, "%lu %s, ", duration
/ 31536000, years
);
152 duration
%= 31536000;
153 length
-= bytes_written
;
156 if (duration
/ 604800 > 0) {
157 if (duration
/ 604800 == 1)
158 bytes_written
= g_snprintf(p
, length
, "%d %s, ", 1, week
);
160 bytes_written
= g_snprintf(p
, length
, "%lu %s, ", duration
/ 604800, weeks
);
162 length
-= bytes_written
;
165 if (duration
/ 86400 > 0) {
166 if (duration
/ 86400 == 1)
167 bytes_written
= g_snprintf(p
, length
, "%d %s, ", 1, day
);
169 bytes_written
= g_snprintf(p
, length
, "%lu %s, ", duration
/ 86400, days
);
171 length
-= bytes_written
;
175 g_snprintf(p
, length
, "%02lu:%02lu:%02lu", duration
/ 3600,
176 duration
% 3600 / 60, duration
% 3600 % 60);