00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include "config.h"
00028 #endif
00029
00030
00031 #include <glib.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <ctype.h>
00035
00036 #include <errno.h>
00037
00038 #ifdef HAVE_FTS_H
00039 # include <sys/types.h>
00040 # include <sys/stat.h>
00041 # include <fts.h>
00042 #endif
00043
00044 #include <libaudcore/audstrings.h>
00045
00046 #include "audconfig.h"
00047 #include "debug.h"
00048 #include "i18n.h"
00049 #include "misc.h"
00050 #include "plugins.h"
00051 #include "util.h"
00052
00053 gboolean
00054 dir_foreach(const gchar * path, DirForeachFunc function,
00055 gpointer user_data, GError ** error)
00056 {
00057 GError *error_out = NULL;
00058 GDir *dir;
00059 const gchar *entry;
00060 gchar *entry_fullpath;
00061
00062 if (!(dir = g_dir_open(path, 0, &error_out))) {
00063 g_propagate_error(error, error_out);
00064 return FALSE;
00065 }
00066
00067 while ((entry = g_dir_read_name(dir))) {
00068 entry_fullpath = g_build_filename(path, entry, NULL);
00069
00070 if ((*function) (entry_fullpath, entry, user_data)) {
00071 g_free(entry_fullpath);
00072 break;
00073 }
00074
00075 g_free(entry_fullpath);
00076 }
00077
00078 g_dir_close(dir);
00079
00080 return TRUE;
00081 }
00082
00091 gchar*
00092 util_get_localdir(void)
00093 {
00094 gchar *datadir;
00095 gchar *tmp;
00096
00097 if ( (tmp = getenv("XDG_CONFIG_HOME")) == NULL )
00098 datadir = g_build_filename( g_get_home_dir() , ".config" , "audacious" , NULL );
00099 else
00100 datadir = g_build_filename( tmp , "audacious" , NULL );
00101
00102 return datadir;
00103 }
00104
00105
00106 gchar * construct_uri (const gchar * string, const gchar * playlist_name)
00107 {
00108 gchar *filename = g_strdup(string);
00109 gchar *tmp, *path;
00110 gchar *uri = NULL;
00111
00112
00113 convert_dos_path(filename);
00114
00115
00116
00117 if (filename[0] == '/' || strstr(filename, "://")) {
00118 uri = g_filename_to_uri(filename, NULL, NULL);
00119 if(!uri)
00120 uri = g_strdup(filename);
00121 g_free(filename);
00122 }
00123
00124
00125 else if (playlist_name[0] == '/' || strstr(playlist_name, "://")) {
00126 path = g_filename_from_uri(playlist_name, NULL, NULL);
00127 if (!path)
00128 path = g_strdup(playlist_name);
00129 tmp = strrchr(path, '/'); *tmp = '\0';
00130 tmp = g_build_filename(path, filename, NULL);
00131 g_free(path); g_free(filename);
00132 uri = g_filename_to_uri(tmp, NULL, NULL);
00133 g_free(tmp);
00134 }
00135
00136
00137 else {
00138 g_free(filename);
00139 uri = NULL;
00140 }
00141
00142 AUDDBG("uri=%s\n", uri);
00143 return uri;
00144 }
00145
00146
00147 gint file_get_mtime (const gchar * filename)
00148 {
00149 struct stat info;
00150
00151 if (stat (filename, & info))
00152 return -1;
00153
00154 return info.st_mtime;
00155 }
00156
00157 void
00158 make_directory(const gchar * path, mode_t mode)
00159 {
00160 if (g_mkdir_with_parents(path, mode) == 0)
00161 return;
00162
00163 g_printerr(_("Could not create directory (%s): %s\n"), path,
00164 g_strerror(errno));
00165 }
00166
00167 #define URL_HISTORY_MAX_SIZE 30
00168
00169 void
00170 util_add_url_history_entry(const gchar * url)
00171 {
00172 if (g_list_find_custom(cfg.url_history, url, (GCompareFunc) strcasecmp))
00173 return;
00174
00175 cfg.url_history = g_list_prepend(cfg.url_history, g_strdup(url));
00176
00177 while (g_list_length(cfg.url_history) > URL_HISTORY_MAX_SIZE) {
00178 GList *node = g_list_last(cfg.url_history);
00179 g_free(node->data);
00180 cfg.url_history = g_list_delete_link(cfg.url_history, node);
00181 }
00182 }
00183
00184 static gboolean plugin_list_func (PluginHandle * plugin, GList * * list)
00185 {
00186 gpointer p_hdr = plugin_get_header(plugin);
00187 g_return_val_if_fail(p_hdr != NULL, TRUE);
00188 *list = g_list_prepend (*list, p_hdr);
00189 return TRUE;
00190 }
00191
00192
00193 GList * plugin_get_list (gint type)
00194 {
00195 static GList *list[PLUGIN_TYPES] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00196
00197 if (list[type] == NULL)
00198 {
00199 plugin_for_each (type, (PluginForEachFunc) plugin_list_func, & list[type]);
00200 list[type] = g_list_reverse (list[type]);
00201 }
00202
00203 return list[type];
00204 }