1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "utils.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "i18n.h"
24 #include "mpdclient.h"
25 #include "filelist.h"
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <string.h>
31 GList *
32 string_list_free(GList *string_list)
33 {
34 GList *list = g_list_first(string_list);
36 while (list) {
37 g_free(list->data);
38 list->data = NULL;
39 list = list->next;
40 }
42 g_list_free(string_list);
43 return NULL;
44 }
46 GList *
47 string_list_find(GList *string_list, const gchar *str)
48 {
49 GList *list = g_list_first(string_list);
51 while(list) {
52 if( strcmp(str, (gchar *) list->data) == 0 )
53 return list;
54 list = list->next;
55 }
56 return NULL;
57 }
59 GList *
60 string_list_remove(GList *string_list, const gchar *str)
61 {
62 GList *list = g_list_first(string_list);
64 while(list) {
65 if( strcmp(str, (gchar *) list->data) == 0 ) {
66 g_free(list->data);
67 list->data = NULL;
68 return g_list_delete_link(string_list, list);
69 }
70 list = list->next;
71 }
72 return list;
73 }
75 /* create a list suitable for GCompletion from path */
76 GList *
77 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
78 GList *list, gint types)
79 {
80 guint i;
81 struct filelist *filelist;
83 if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
84 return list;
86 for (i = 0; i < filelist_length(filelist); ++i) {
87 const struct mpd_entity *entity =
88 filelist_get(filelist, i)->entity;
89 char *name = NULL;
91 if (entity != NULL &&
92 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
93 types & GCMP_TYPE_DIR) {
94 const struct mpd_directory *dir =
95 mpd_entity_get_directory(entity);
96 gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
97 gsize size = strlen(tmp)+2;
99 name = g_malloc(size);
100 g_strlcpy(name, tmp, size);
101 g_strlcat(name, "/", size);
102 g_free(tmp);
103 } else if (entity != NULL &&
104 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
105 types & GCMP_TYPE_FILE) {
106 const struct mpd_song *song =
107 mpd_entity_get_song(entity);
108 name = utf8_to_locale(mpd_song_get_uri(song));
109 } else if (entity != NULL &&
110 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
111 types & GCMP_TYPE_PLAYLIST) {
112 const struct mpd_playlist *playlist =
113 mpd_entity_get_playlist(entity);
114 name = utf8_to_locale(mpd_playlist_get_path(playlist));
115 }
117 if (name)
118 list = g_list_append(list, name);
119 }
121 filelist_free(filelist);
122 return list;
123 }
125 void
126 format_duration_short(char *buffer, size_t length, unsigned duration)
127 {
128 if (duration < 3600)
129 g_snprintf(buffer, length,
130 "%i:%02i", duration / 60, duration % 60);
131 else
132 g_snprintf(buffer, length,
133 "%i:%02i:%02i", duration / 3600,
134 (duration % 3600) / 60, duration % 60);
135 }
137 void
138 format_duration_long(char *p, size_t length, unsigned long duration)
139 {
140 const char *year = _("year");
141 const char *years = _("years");
142 const char *week = _("week");
143 const char *weeks = _("weeks");
144 const char *day = _("day");
145 const char *days = _("days");
146 unsigned bytes_written = 0;
148 if (duration / 31536000 > 0) {
149 if (duration / 31536000 == 1)
150 bytes_written = g_snprintf(p, length, "%d %s, ", 1, year);
151 else
152 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, years);
153 duration %= 31536000;
154 length -= bytes_written;
155 p += bytes_written;
156 }
157 if (duration / 604800 > 0) {
158 if (duration / 604800 == 1)
159 bytes_written = g_snprintf(p, length, "%d %s, ", 1, week);
160 else
161 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 604800, weeks);
162 duration %= 604800;
163 length -= bytes_written;
164 p += bytes_written;
165 }
166 if (duration / 86400 > 0) {
167 if (duration / 86400 == 1)
168 bytes_written = g_snprintf(p, length, "%d %s, ", 1, day);
169 else
170 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 86400, days);
171 duration %= 86400;
172 length -= bytes_written;
173 p += bytes_written;
174 }
176 g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
177 duration % 3600 / 60, duration % 3600 % 60);
178 }