]> git.sesse.net Git - vlc/commitdiff
Add a directory type parameter to config_GetHomeDir
authorRémi Denis-Courmont <remi@remlab.net>
Mon, 24 Aug 2009 18:25:12 +0000 (21:25 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Mon, 24 Aug 2009 18:27:31 +0000 (21:27 +0300)
This will avoid adding plenty of config_GetFoobarDir exports later.
Also make config_GetHomeDir return a heap-allocated string.

20 files changed:
include/vlc_configuration.h
modules/access_filter/dump.c
modules/control/http/util.c
modules/demux/mp4/libmp4.c
modules/gui/ncurses.c
modules/gui/qt4/components/preferences_widgets.cpp
modules/gui/qt4/dialogs/help.cpp
modules/gui/qt4/dialogs/messages.cpp
modules/gui/qt4/dialogs/vlm.cpp
modules/gui/qt4/qt4.cpp
modules/gui/qt4/qt4.hpp
modules/misc/logger.c
modules/misc/lua/libs/misc.c
modules/stream_filter/record.c
modules/video_filter/scene.c
src/config/dirs.c
src/config/file.c
src/input/es_out.c
src/libvlccore.sym
src/video_output/snapshot.c

index 1c2c48fe1cdbeef1b3b5ad9687f58fcf9338d0dc..e5df206260b961dca6ceadc36702a045fbd27ed8 100644 (file)
@@ -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 * ) );
index 33a30422ad573cbd1ea8530d90e9035be15d94e5..fcfb8cabe7b62e59adadba963f920aafdeb7efa1 100644 (file)
@@ -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);
 
index bb78bb7f4a5c04c4f0d2c0838b73f04a0570a2f1..7cd4af7de3ba89ad9d778cf51220124e3a327766 100644 (file)
@@ -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;
     }
 
index c4e9db15d871fbda98f66af4568fce503d2a07fb..8df00de92b855ac0054c8dc2b6c43ee238bbb799 100644 (file)
@@ -1243,12 +1243,12 @@ 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 )
         {
-            msg_Err( p_stream, "drms_alloc() failed" );
+            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,12 +1344,12 @@ 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 )
         {
-            msg_Err( p_stream, "drms_alloc() failed" );
+            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" );
         }
     }
 
index 04934d2fff772e4a48f511e72ac52952590fa18e..67afea88991a953f8e2d594ba8a08b6580a4a825 100644 (file)
@@ -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 );
     }
 
index 2b6c99248f0f50caeda154daab9f3bbef5b176d5..a96e4379fe30a60093b66e6a2865ed1ed5ee2edc 100644 (file)
@@ -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;
index 13d5531eeac2fc62d0f7d26e20cd7031f970bbed..edcba5253c8bbfb57ee5e93810f13b15107063c8 100644 (file)
@@ -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() )
         {
index a2371df3c6eb021eda8b946787c6647e4594db23..f491b6e7478d2dcfd4a1eea4be3b5bdcd57a0c44 100644 (file)
@@ -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() )
index 31b2a5be7844123e3644c607253a883771cd80ce..8a706dc2f6e60e342776e062ddb036e708340748 100644 (file)
@@ -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() )
index a6b44c3394216fccb75abe9f114832a1138c4d47..e1128ac55c42691ec7076fc20f67dfa8284f8092 100644 (file)
@@ -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();
index 6bde27156959e2756d58fe83ba01014ac090cabe..c93754a4a1b794f974d8bdb493273e03ff9a8cbc 100644 (file)
@@ -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
index 9a8a9d020e76ec0df99b6bdbe91672714b83929a..db842d13ac0edf03affe8a0d0e8dd115674c768b 100644 (file)
@@ -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 )
             {
index 7aab9ec9d223921843ac8fc965a9adc3ac6e7491..1bbc1b0aad6a969705fcd1552cb4fca6761cdf4e 100644 (file)
@@ -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;
 }
 
index 51fd62370e75ad058cd7784eb0317e0ec49e09be..dee348ac01571b55a23064427c3895c645892368 100644 (file)
@@ -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;
index dfd3047f13139f1c0eaaa1220d57cb1520bb6b52..6a0d6581bdefc0dcee65db0e34dd6386d9ad8c22 100644 (file)
@@ -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;
 
index c8cfc822d1fded55ee43757ff99d67dc94871c3e..a6314baa90ee6a683f8e0509e89a44dd0cce5275 100644 (file)
@@ -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;
 }
index b3153ea7e1237319e24bc028fb81f2be244ebbcd..e1162bdec244c005e47ab86e73432515b84f753a 100644 (file)
@@ -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 );
index 9ca862851b58033d119906623ecdb9440475fa0b..4f97a92d64501d71d05ece2a01e5e70ae8b5f69b 100644 (file)
@@ -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
 
index 5d40e4af7ccc5b377857432661c0d6d6b35404a5..4f567a9525af01f3d1172bf4ddf0937c61b3c7ac 100644 (file)
@@ -54,7 +54,7 @@ config_GetCacheDir
 config_GetConfDir
 config_GetDataDir
 __config_GetFloat
-config_GetHomeDir
+config_GetUserDir
 __config_GetInt
 __config_GetPsz
 __config_GetType
index f6beb19a7995858e03db1090851c138d56a8a183..1866cfd7ae11c2d63ec781782c3e89fdbe041b2d 100644 (file)
@@ -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,