Code

use libmpdclient2
[ncmpc.git] / src / utils.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.
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"
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
29 GList *
30 string_list_free(GList *string_list)
31 {
32         GList *list = g_list_first(string_list);
34         while (list) {
35                 g_free(list->data);
36                 list->data = NULL;
37                 list = list->next;
38         }
40         g_list_free(string_list);
41         return NULL;
42 }
44 GList *
45 string_list_find(GList *string_list, const gchar *str)
46 {
47         GList *list = g_list_first(string_list);
49         while(list) {
50                 if( strcmp(str, (gchar *) list->data) ==  0 )
51                         return list;
52                 list = list->next;
53         }
54         return NULL;
55 }
57 GList *
58 string_list_remove(GList *string_list, const gchar *str)
59 {
60         GList *list = g_list_first(string_list);
62         while(list) {
63                 if( strcmp(str, (gchar *) list->data) ==  0 ) {
64                         g_free(list->data);
65                         list->data = NULL;
66                         return g_list_delete_link(string_list, list);
67                 }
68                 list = list->next;
69         }
70         return list;
71 }
73 /* create a list suitable for GCompletion from path */
74 GList *
75 gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types)
76 {
77         guint i;
78         mpdclient_filelist_t *filelist;
80         if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
81                 return list;
83         for (i = 0; i < filelist_length(filelist); ++i) {
84                 const struct filelist_entry *entry = filelist_get(filelist, i);
85                 const struct mpd_entity *entity = entry ? entry->entity : NULL;
86                 char *name = NULL;
88                 if (entity != NULL &&
89                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
90                     types & GCMP_TYPE_DIR) {
91                         const struct mpd_directory *dir =
92                                 mpd_entity_get_directory(entity);
93                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
94                         gsize size = strlen(tmp)+2;
96                         name = g_malloc(size);
97                         g_strlcpy(name, tmp, size);
98                         g_strlcat(name, "/", size);
99                         g_free(tmp);
100                 } else if (entity != NULL &&
101                            mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
102                            types & GCMP_TYPE_FILE) {
103                         const struct mpd_song *song =
104                                 mpd_entity_get_song(entity);
105                         name = utf8_to_locale(mpd_song_get_uri(song));
106                 } else if (entity != NULL &&
107                            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));
112                 }
114                 if (name)
115                         list = g_list_append(list, name);
116         }
118         filelist_free(filelist);
119         return list;
122 char *
123 time_seconds_to_durationstr(unsigned long time_seconds)
125         const char *year = _("year");
126         const char *years = _("years");
127         const char *week = _("week");
128         const char *weeks = _("weeks");
129         const char *day = _("day");
130         const char *days = _("days");
131         char *duration;
132         char *iter;
133         unsigned bytes_written = 0;
134         unsigned length = utf8_width(years) +
135                 utf8_width(weeks) + utf8_width(days) + 32;
137         duration = g_malloc(length);
138         iter = duration;
139         if (time_seconds / 31536000 > 0) {
140                 if (time_seconds / 31536000 == 1)
141                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, year);
142                 else
143                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, years);
144                 time_seconds %= 31536000;
145                 length -= bytes_written;
146                 iter += bytes_written;
147         }
148         if (time_seconds / 604800 > 0) {
149                 if (time_seconds / 604800 == 1)
150                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, week);
151                 else
152                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, weeks);
153                 time_seconds %= 604800;
154                 length -= bytes_written;
155                 iter += bytes_written;
156         }
157         if (time_seconds / 86400 > 0) {
158                 if (time_seconds / 86400 == 1)
159                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, day);
160                 else
161                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, days);
162                 time_seconds %= 86400;
163                 length -= bytes_written;
164                 iter += bytes_written;
165         }
166         g_snprintf(iter, length, "%02lu:%02lu:%02lu",
167                         time_seconds / 3600,
168                         time_seconds % 3600 / 60,
169                         time_seconds % 3600 % 60);
170         return duration;