Commit 08d89300 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Add a directory type parameter to config_GetHomeDir

This will avoid adding plenty of config_GetFoobarDir exports later.
Also make config_GetHomeDir return a heap-allocated string.
parent 7fe6a697
......@@ -217,9 +217,14 @@ VLC_EXPORT( module_config_t *, config_FindConfig,( vlc_object_t *, const char *
VLC_EXPORT(const char *, config_GetDataDir, ( void ) LIBVLC_USED);
VLC_EXPORT(const char *, config_GetConfDir, ( void ) LIBVLC_USED);
VLC_EXPORT(const char *, config_GetHomeDir, ( void ) LIBVLC_USED);
VLC_EXPORT(char *, config_GetUserConfDir, ( void ) LIBVLC_USED);
VLC_EXPORT(char *, config_GetUserDataDir, ( void ) LIBVLC_USED);
typedef enum vlc_userdir {
VLC_HOME_DIR,
} vlc_userdir_t;
VLC_EXPORT(char *, config_GetUserDir, ( vlc_userdir_t ) LIBVLC_USED);
VLC_EXPORT(char *, config_GetCacheDir, ( void ) LIBVLC_USED);
VLC_EXPORT( void, __config_AddIntf, ( vlc_object_t *, const char * ) );
......
......@@ -271,12 +271,15 @@ static void Trigger (access_t *access)
// and there is an off-by-one in the following sprintf().
return;
const char *home = config_GetHomeDir();
char *dir = config_GetUserDir( VLC_HOME_DIR );
if( dir == NULL )
return;
/* Hmm what about the extension?? */
char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
char filename[strlen (dir) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", dir,
t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
free( dir );
msg_Info (access, "dumping media to \"%s\"...", filename);
......
......@@ -951,9 +951,10 @@ char *RealPath( const char *psz_src )
if( psz_dir[0] == '~' )
{
char *dir;
asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
char *home = config_GetUserDir( VLC_HOME_DIR ), *dir;
asprintf( &dir, "%s%s", home, psz_dir + 1 );
free( psz_dir );
free( home );
psz_dir = dir;
}
......
......@@ -1243,11 +1243,11 @@ static int MP4_ReadBox_sample_soun( stream_t *p_stream, MP4_Box_t *p_box )
if( p_box->i_type == FOURCC_drms )
{
p_box->data.p_sample_soun->p_drms =
drms_alloc( config_GetHomeDir() );
if( p_box->data.p_sample_soun->p_drms == NULL )
char *home = config_GetUserDir( VLC_HOME_DIR );
if( home != NULL )
{
p_box->data.p_sample_soun->p_drms = drms_alloc( home );
if( p_box->data.p_sample_soun->p_drms == NULL )
msg_Err( p_stream, "drms_alloc() failed" );
}
}
......@@ -1344,11 +1344,11 @@ int MP4_ReadBox_sample_vide( stream_t *p_stream, MP4_Box_t *p_box )
if( p_box->i_type == FOURCC_drmi )
{
p_box->data.p_sample_vide->p_drms =
drms_alloc( config_GetHomeDir() );
if( p_box->data.p_sample_vide->p_drms == NULL )
char *home = config_GetUserDir( VLC_HOME_DIR );
if( home != NULL )
{
p_box->data.p_sample_vide->p_drms = drms_alloc( home );
if( p_box->data.p_sample_vide->p_drms == NULL )
msg_Err( p_stream, "drms_alloc() failed" );
}
}
......
......@@ -310,7 +310,7 @@ static int Open( vlc_object_t *p_this )
p_sys->psz_current_dir = psz_tmp;
else
{
p_sys->psz_current_dir = strdup( config_GetHomeDir() );
p_sys->psz_current_dir = config_GetUserDir( VLC_HOME_DIR );
free( psz_tmp );
}
......
......@@ -327,7 +327,7 @@ FileConfigControl::FileConfigControl( vlc_object_t *_p_this,
void FileConfigControl::updateField()
{
QString file = QFileDialog::getOpenFileName( NULL,
qtr( "Select File" ), qfu( config_GetHomeDir() ) );
qtr( "Select File" ), QVLCUserDir( VLC_HOME_DIR ) );
if( file.isNull() ) return;
text->setText( toNativeSeparators( file ) );
}
......@@ -361,7 +361,7 @@ void DirectoryConfigControl::updateField()
QString dir = QFileDialog::getExistingDirectory( NULL,
qtr( "Select Directory" ),
text->text().isEmpty() ?
qfu( config_GetHomeDir() ) : text->text(),
QVLCUserDir( VLC_HOME_DIR ) : text->text(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
if( dir.isNull() ) return;
......
......@@ -271,7 +271,7 @@ void UpdateDialog::UpdateOrDownload()
{
QString dest_dir = QFileDialog::getExistingDirectory( this,
qtr( "Select a directory..." ),
qfu( config_GetHomeDir() ) );
QVLCUserDir( VLC_DOWNLOAD_DIR ) );
if( !dest_dir.isEmpty() )
{
......
......@@ -245,7 +245,7 @@ bool MessagesDialog::save()
{
QString saveLogFileName = QFileDialog::getSaveFileName(
this, qtr( "Save log file as..." ),
qfu( config_GetHomeDir() ),
QVLCUserDir( VLC_HOME_DIR ),
qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
if( !saveLogFileName.isNull() )
......
......@@ -267,7 +267,7 @@ bool VLMDialog::exportVLMConf()
{
QString saveVLMConfFileName = QFileDialog::getSaveFileName( this,
qtr( "Save VLM configuration as..." ),
qfu( config_GetHomeDir() ),
QVLCUserDir( VLC_HOME_DIR ),
qtr( "VLM conf (*.vlm);;All (*)" ) );
if( !saveVLMConfFileName.isEmpty() )
......@@ -339,7 +339,7 @@ bool VLMDialog::importVLMConf()
QString openVLMConfFileName = toNativeSeparators(
QFileDialog::getOpenFileName(
this, qtr( "Open VLM configuration..." ),
qfu( config_GetHomeDir() ),
QVLCUserDir( VLC_HOME_DIR ),
qtr( "VLM conf (*.vlm);;All (*)" ) ) );
if( !openVLMConfFileName.isEmpty() )
......
......@@ -453,7 +453,7 @@ static void *Thread( void *obj )
/* Retrieve last known path used in file browsing */
p_intf->p_sys->filepath =
getSettings()->value( "filedialog-path", config_GetHomeDir() ).toString();
getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString();
/* Loads and tries to apply the preferred QStyle */
QString s_style = getSettings()->value( "MainWindow/QtStyle", "" ).toString();
......
......@@ -120,5 +120,14 @@ struct intf_sys_t
#define getSettings() p_intf->p_sys->mainSettings
static inline QString QVLCUserDir( vlc_userdir_t type )
{
char *dir = config_GetUserDir( type );
if( !dir )
abort();
QString res = qfu( dir );
free( dir );
return res;
}
#endif
......@@ -234,10 +234,13 @@ static int Open( vlc_object_t *p_this )
if( !psz_file )
{
#ifdef __APPLE__
if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
char *home = config_GetUserDir(VLC_HOME_DIR);
if( home == NULL
|| asprintf( &psz_file, "%s/"LOG_DIR"/%s", home,
(p_sys->msg.i_mode == MODE_HTML) ? LOG_FILE_HTML
: LOG_FILE_TEXT ) == -1 )
psz_file = NULL;
free(home);
#else
switch( p_sys->msg.i_mode )
{
......
......@@ -126,7 +126,9 @@ static int vlclua_userdatadir( lua_State *L )
static int vlclua_homedir( lua_State *L )
{
lua_pushstring( L, config_GetHomeDir() );
char *home = config_GetUserDir( VLC_HOME_DIR );
lua_pushstring( L, home );
free( home );
return 1;
}
......
......@@ -175,12 +175,9 @@ static int Start( stream_t *s, const char *psz_extension )
psz_extension = "dat";
/* Retreive path */
char *psz_path = var_CreateGetString( s, "input-record-path" );
if( !psz_path || *psz_path == '\0' )
{
free( psz_path );
psz_path = strdup( config_GetHomeDir() );
}
char *psz_path = var_CreateGetNonEmptyString( s, "input-record-path" );
if( !psz_path )
psz_path = config_GetUserDir( VLC_HOME_DIR );
if( !psz_path )
return VLC_ENOMEM;
......
......@@ -193,11 +193,7 @@ static int Create( vlc_object_t *p_this )
p_sys->psz_prefix = var_CreateGetString( p_this, CFG_PREFIX "prefix" );
p_sys->psz_path = var_GetNonEmptyString( p_this, CFG_PREFIX "path" );
if( p_sys->psz_path == NULL )
{
const char *psz_homedir = config_GetHomeDir();
if( psz_homedir )
p_sys->psz_path = strdup( psz_homedir );
}
p_sys->psz_path = config_GetUserDir( VLC_HOME_DIR );
p_filter->pf_video_filter = Filter;
......
......@@ -78,6 +78,7 @@ const char *config_GetDataDir( void )
#endif
}
#if defined (WIN32) || defined(__APPLE__) || defined (SYS_BEOS)
static const char *GetDir( bool b_appdata, bool b_common_appdata )
{
/* FIXME: a full memory page here - quite a waste... */
......@@ -146,6 +147,7 @@ static const char *GetDir( bool b_appdata, bool b_common_appdata )
#endif
return homedir;
}
#endif
/**
* Determines the system configuration directory.
......@@ -171,12 +173,53 @@ const char *config_GetConfDir( void )
#endif
}
/**
* Get the user's home directory
*/
const char *config_GetHomeDir( void )
static char *config_GetHomeDir (void)
{
#ifndef WIN32
/* 1/ Try $HOME */
const char *home = getenv ("HOME");
#if defined(HAVE_GETPWUID_R)
/* 2/ Try /etc/passwd */
char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
if (home == NULL)
{
struct passwd pw, *res;
if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
home = pw.pw_dir;
}
#endif
/* 3/ Desperately try $TMP */
if (home == NULL)
home = getenv( "TMP" );
/* 4/ Beyond hope, hard-code /tmp */
if (home == NULL)
home = "/tmp";
return FromLocaleDup (home);
#else /* WIN32 */
wchar_t wdir[MAX_PATH];
# if defined (UNDER_CE)
/*There are some errors in cegcc headers*/
#undef SHGetSpecialFolderPath
BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1))
# else
if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
# endif
return FromWide (wdir);
return NULL;
#endif
}
char *config_GetUserDir (vlc_userdir_t type)
{
return GetDir (false, false);
char *home = config_GetHomeDir ();
(void)type;
return home;
}
static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
......@@ -194,8 +237,7 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
/* XDG Base Directory Specification - Version 0.6 */
snprintf (var, sizeof (var), "XDG_%s_HOME", xdg_name);
const char *psz_home = getenv (var);
psz_home = psz_home ? FromLocale (psz_home) : NULL;
char *psz_home = FromLocale (getenv (var));
if( psz_home )
{
if( asprintf( &psz_dir, "%s/vlc", psz_home ) == -1 )
......@@ -204,10 +246,11 @@ static char *config_GetFooDir (const char *xdg_name, const char *xdg_default)
return psz_dir;
}
/* Try HOME, then fallback to non-XDG dirs */
psz_home = config_GetHomeDir();
if( asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
psz_home = config_GetUserDir (VLC_HOME_DIR);
if( psz_home == NULL
|| asprintf( &psz_dir, "%s/%s/vlc", psz_home, xdg_default ) == -1 )
psz_dir = NULL;
free (psz_home);
#endif
return psz_dir;
}
......
......@@ -88,9 +88,12 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
{
/* This is the fallback for pre XDG Base Directory
* Specification configs */
char *home = config_GetUserDir(VLC_HOME_DIR);
char *psz_old;
if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
config_GetHomeDir() ) != -1 )
if( home != NULL
&& asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
home ) != -1 )
{
p_stream = utf8_fopen( psz_old, "rt" );
if( p_stream )
......@@ -101,7 +104,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
"VLC will now use %s.", psz_old, psz_filename );
char *psz_readme;
if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README",
config_GetHomeDir() ) != -1 )
home ) != -1 )
{
FILE *p_readme = utf8_fopen( psz_readme, "wt" );
if( p_readme )
......@@ -120,6 +123,7 @@ static FILE *config_OpenConfigFile( vlc_object_t *p_obj )
}
free( psz_old );
}
free( home );
}
#endif
free( psz_filename );
......
......@@ -464,12 +464,9 @@ static int EsOutSetRecord( es_out_t *out, bool b_record )
if( b_record )
{
char *psz_path = var_CreateGetString( p_input, "input-record-path" );
if( !psz_path || *psz_path == '\0' )
{
free( psz_path );
psz_path = strdup( config_GetHomeDir() );
}
char *psz_path = var_CreateGetNonEmptyString( p_input, "input-record-path" );
if( !psz_path )
psz_path = config_GetUserDir(VLC_HOME_DIR);
char *psz_sout = NULL; // TODO conf
......
......@@ -54,7 +54,7 @@ config_GetCacheDir
config_GetConfDir
config_GetDataDir
__config_GetFloat
config_GetHomeDir
config_GetUserDir
__config_GetInt
__config_GetPsz
__config_GetType
......
......@@ -131,67 +131,7 @@ void vout_snapshot_Set(vout_snapshot_t *snap,
/* */
char *vout_snapshot_GetDirectory(void)
{
char *psz_path = NULL;
#if defined(__APPLE__) || defined(SYS_BEOS)
if (asprintf(&psz_path, "%s/Desktop",
config_GetHomeDir()) == -1)
psz_path = NULL;
#elif defined(WIN32) && !defined(UNDER_CE)
/* Get the My Pictures folder path */
char *p_mypicturesdir = NULL;
typedef HRESULT (WINAPI *SHGETFOLDERPATH)(HWND, int, HANDLE, DWORD,
LPWSTR);
#ifndef CSIDL_FLAG_CREATE
# define CSIDL_FLAG_CREATE 0x8000
#endif
#ifndef CSIDL_MYPICTURES
# define CSIDL_MYPICTURES 0x27
#endif
#ifndef SHGFP_TYPE_CURRENT
# define SHGFP_TYPE_CURRENT 0
#endif
HINSTANCE shfolder_dll;
SHGETFOLDERPATH SHGetFolderPath ;
/* load the shfolder dll to retrieve SHGetFolderPath */
if ((shfolder_dll = LoadLibrary(_T("SHFolder.dll"))) != NULL)
{
wchar_t wdir[PATH_MAX];
SHGetFolderPath = (void *)GetProcAddress(shfolder_dll,
_T("SHGetFolderPathW"));
if ((SHGetFolderPath != NULL)
&& SUCCEEDED (SHGetFolderPath (NULL,
CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
NULL, SHGFP_TYPE_CURRENT,
wdir)))
p_mypicturesdir = FromWide (wdir);
FreeLibrary(shfolder_dll);
}
if (p_mypicturesdir == NULL)
psz_path = strdup(config_GetHomeDir());
else
psz_path = p_mypicturesdir;
#else
/* XXX: This saves in the data directory. Shouldn't we try saving
* to psz_homedir/Desktop or something nicer ? */
char *psz_datadir = config_GetUserDataDir();
if (psz_datadir)
{
if (asprintf(&psz_path, "%s", psz_datadir) == -1)
psz_path = NULL;
free(psz_datadir);
}
#endif
return psz_path;
return config_GetUserDir(VLC_HOME_DIR);
}
/* */
int vout_snapshot_SaveImage(char **name, int *sequential,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment