Code

utils: Use singular in time_seconds_to_durationstr() if appropriate.
[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                 struct filelist_entry *entry = filelist_get(filelist, i);
85                 mpd_InfoEntity *entity = entry ? entry->entity : NULL;
86                 char *name = NULL;
88                 if (entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY &&
89                     types & GCMP_TYPE_DIR) {
90                         mpd_Directory *dir = entity->info.directory;
91                         gchar *tmp = utf8_to_locale(dir->path);
92                         gsize size = strlen(tmp)+2;
94                         name = g_malloc(size);
95                         g_strlcpy(name, tmp, size);
96                         g_strlcat(name, "/", size);
97                         g_free(tmp);
98                 } else if (entity &&
99                            entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
100                            types & GCMP_TYPE_FILE) {
101                         mpd_Song *song = entity->info.song;
102                         name = utf8_to_locale(song->file);
103                 } else if (entity &&
104                            entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
105                            types & GCMP_TYPE_PLAYLIST) {
106                         mpd_PlaylistFile *plf = entity->info.playlistFile;
107                         name = utf8_to_locale(plf->path);
108                 }
110                 if (name)
111                         list = g_list_append(list, name);
112         }
114         filelist_free(filelist);
115         return list;
118 char *
119 time_seconds_to_durationstr(unsigned long time_seconds)
121         const char *year = _("year");
122         const char *years = _("years");
123         const char *week = _("week");
124         const char *weeks = _("weeks");
125         const char *day = _("day");
126         const char *days = _("days");
127         char *duration;
128         char *iter;
129         unsigned bytes_written = 0;
130         unsigned length = utf8_width(years) +
131                 utf8_width(weeks) + utf8_width(days) + 32;
133         duration = g_malloc(length);
134         iter = duration;
135         if (time_seconds / 31536000 > 0) {
136                 if (time_seconds / 31536000 == 1)
137                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, year);
138                 else
139                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 31536000, years);
140                 time_seconds %= 31536000;
141                 length -= bytes_written;
142                 iter += bytes_written;
143         }
144         if (time_seconds / 604800 > 0) {
145                 if (time_seconds / 604800 == 1)
146                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, week);
147                 else
148                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 604800, weeks);
149                 time_seconds %= 604800;
150                 length -= bytes_written;
151                 iter += bytes_written;
152         }
153         if (time_seconds / 86400 > 0) {
154                 if (time_seconds / 86400 == 1)
155                         bytes_written = g_snprintf(iter, length, "%d %s, ", 1, day);
156                 else
157                         bytes_written = g_snprintf(iter, length, "%lu %s, ", time_seconds / 86400, days);
158                 time_seconds %= 86400;
159                 length -= bytes_written;
160                 iter += bytes_written;
161         }
162         g_snprintf(iter, length, "%02lu:%02lu:%02lu",
163                         time_seconds / 3600,
164                         time_seconds % 3600 / 60,
165                         time_seconds % 3600 % 60);
166         return duration;