Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$

util.c

Go to the documentation of this file.
00001 /*  Audacious - Cross-platform multimedia player
00002  *  Copyright (C) 2005-2008  Audacious development team
00003  *
00004  *  Based on BMP:
00005  *  Copyright (C) 2003-2004  BMP development team.
00006  *
00007  *  Based on XMMS:
00008  *  Copyright (C) 1998-2003  XMMS development team.
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; under version 3 of the License.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program.  If not, see <http://www.gnu.org/licenses>.
00021  *
00022  *  The Audacious team does not consider modular code linking to
00023  *  Audacious or using our public API to be a derived work.
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 *uri = NULL;
00110 
00111     /* try to translate dos path */
00112     convert_dos_path(filename); /* in place replacement */
00113 
00114     // make full path uri here
00115     // case 1: filename is raw full path or uri
00116     if (filename[0] == '/' || strstr(filename, "://")) {
00117         uri = g_filename_to_uri(filename, NULL, NULL);
00118         if(!uri)
00119             uri = g_strdup(filename);
00120     }
00121     // case 2: filename is not raw full path nor uri
00122     // make full path by replacing last part of playlist path with filename.
00123     else
00124     {
00125         const gchar * fslash = strrchr (filename, '/');
00126         const gchar * pslash = strrchr (playlist_name, '/');
00127 
00128         if (pslash)
00129             uri = g_strdup_printf ("%.*s/%s", (gint) (pslash - playlist_name),
00130              playlist_name, fslash ? fslash + 1 : filename);
00131     }
00132 
00133     g_free (filename);
00134     return uri;
00135 }
00136 
00137 /* local files -- not URI's */
00138 gint file_get_mtime (const gchar * filename)
00139 {
00140     struct stat info;
00141 
00142     if (stat (filename, & info))
00143         return -1;
00144 
00145     return info.st_mtime;
00146 }
00147 
00148 void
00149 make_directory(const gchar * path, mode_t mode)
00150 {
00151     if (g_mkdir_with_parents(path, mode) == 0)
00152         return;
00153 
00154     g_printerr(_("Could not create directory (%s): %s\n"), path,
00155                g_strerror(errno));
00156 }
00157 
00158 #define URL_HISTORY_MAX_SIZE 30
00159 
00160 void
00161 util_add_url_history_entry(const gchar * url)
00162 {
00163     if (g_list_find_custom(cfg.url_history, url, (GCompareFunc) strcasecmp))
00164         return;
00165 
00166     cfg.url_history = g_list_prepend(cfg.url_history, g_strdup(url));
00167 
00168     while (g_list_length(cfg.url_history) > URL_HISTORY_MAX_SIZE) {
00169         GList *node = g_list_last(cfg.url_history);
00170         g_free(node->data);
00171         cfg.url_history = g_list_delete_link(cfg.url_history, node);
00172     }
00173 }
00174 
00175 static gboolean plugin_list_func (PluginHandle * plugin, GList * * list)
00176 {
00177     gpointer p_hdr = plugin_get_header(plugin);
00178     g_return_val_if_fail(p_hdr != NULL, TRUE);
00179     *list = g_list_prepend (*list, p_hdr);
00180     return TRUE;
00181 }
00182 
00183 /* Deprecated: This loads all the plugins at once, causing a major slowdown. */
00184 GList * plugin_get_list (gint type)
00185 {
00186     static GList *list[PLUGIN_TYPES] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00187 
00188     if (list[type] == NULL)
00189     {
00190         plugin_for_each (type, (PluginForEachFunc) plugin_list_func, & list[type]);
00191         list[type] = g_list_reverse (list[type]);
00192     }
00193 
00194     return list[type];
00195 }