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 filelist_entry *entry = filelist_get(filelist, i);
88 const struct mpd_entity *entity = entry ? entry->entity : NULL;
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 char *
126 time_seconds_to_durationstr(unsigned long time_seconds)
127 {
128 const char *year = _("year");
129 const char *years = _("years");
130 const char *week = _("week");
131 const char *weeks = _("weeks");
132 const char *day = _("day");
133 const char *days = _("days");
134 char *duration;
135 char *iter;
136 unsigned bytes_written = 0;
137 unsigned length = utf8_width(years) +
138 utf8_width(weeks) + utf8_width(days) + 32;
140 duration = g_malloc(length);
141 iter = duration;
142 if (time_seconds / 31536000 > 0) {
143 if (time_seconds / 31536000 == 1)
144 bytes_written = g_snprintf(iter, length, "%d %s, ", 1, year);
145 else
146 bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, years);
147 time_seconds %= 31536000;
148 length -= bytes_written;
149 iter += bytes_written;
150 }
151 if (time_seconds / 604800 > 0) {
152 if (time_seconds / 604800 == 1)
153 bytes_written = g_snprintf(iter, length, "%d %s, ", 1, week);
154 else
155 bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, weeks);
156 time_seconds %= 604800;
157 length -= bytes_written;
158 iter += bytes_written;
159 }
160 if (time_seconds / 86400 > 0) {
161 if (time_seconds / 86400 == 1)
162 bytes_written = g_snprintf(iter, length, "%d %s, ", 1, day);
163 else
164 bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, days);
165 time_seconds %= 86400;
166 length -= bytes_written;
167 iter += bytes_written;
168 }
169 g_snprintf(iter, length, "%02lu:%02lu:%02lu",
170 time_seconds / 3600,
171 time_seconds % 3600 / 60,
172 time_seconds % 3600 % 60);
173 return duration;
174 }