dirs.c 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*****************************************************************************
 * dirs.c: directories configuration
 *****************************************************************************
 * Copyright (C) 2001-2007 the VideoLAN team
 * Copyright © 2007-2008 Rémi Denis-Courmont
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>

#ifndef _WIN32_IE
# define _WIN32_IE 0x0501
#endif
#include <w32api.h>
#ifndef UNDER_CE
# include <direct.h>
#endif
#include <shlobj.h>

#include "../libvlc.h"
#include <vlc_charset.h>
#include <vlc_configuration.h>
42
#include "config/configuration.h"
43 44 45 46

#include <assert.h>
#include <limits.h>

47
char *config_GetDataDirDefault( void )
48
{
49
    return strdup (psz_vlcpath);
50 51
}

52 53 54 55 56
const char *config_GetLibDir (void)
{
    abort ();
}

57
const char *config_GetConfDir (void)
58
{
59
    static char appdir[PATH_MAX] = "";
60 61
    wchar_t wdir[MAX_PATH];

62 63 64 65
#warning FIXME: thread-safety!
    if (*appdir)
        return appdir;

66 67 68 69 70 71
#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
72
    /* Get the "Application Data" folder for all users */
73
    if( S_OK == SHGetFolderPathW( NULL, CSIDL_COMMON_APPDATA
74 75 76 77
              | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wdir ) )
#endif
    {
        WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
78 79
                             appdir, PATH_MAX, NULL, NULL);
        return appdir;
80 81 82 83
    }
    return NULL;
}

84
static char *config_GetShellDir (int csidl)
85 86 87 88 89 90 91 92 93
{
    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
94
    if (SHGetFolderPathW (NULL, csidl | CSIDL_FLAG_CREATE,
95 96 97 98 99 100 101 102 103
                          NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
#endif
        return FromWide (wdir);
    return NULL;
}

static char *config_GetAppDir (void)
{
    char *psz_dir;
104
    char *psz_parent = config_GetShellDir (CSIDL_APPDATA);
105

106 107
    if (psz_parent == NULL
     ||  asprintf (&psz_dir, "%s\\vlc", psz_parent) == -1)
108
        psz_dir = NULL;
109
    free (psz_parent);
110 111 112
    return psz_dir;
}

113
#warning FIXME Use known folders on Vista and above
114 115 116 117 118
char *config_GetUserDir (vlc_userdir_t type)
{
    switch (type)
    {
        case VLC_HOME_DIR:
119
            return config_GetShellDir (CSIDL_PERSONAL);
120 121
        case VLC_CONFIG_DIR:
        case VLC_DATA_DIR:
122
        case VLC_CACHE_DIR:
123
            return config_GetAppDir ();
124

125 126 127 128 129
        case VLC_DESKTOP_DIR:
        case VLC_DOWNLOAD_DIR:
        case VLC_TEMPLATES_DIR:
        case VLC_PUBLICSHARE_DIR:
        case VLC_DOCUMENTS_DIR:
130
            return config_GetUserDir(VLC_HOME_DIR);
131
        case VLC_MUSIC_DIR:
132
            return config_GetShellDir (CSIDL_MYMUSIC);
133
        case VLC_PICTURES_DIR:
134
            return config_GetShellDir (CSIDL_MYPICTURES);
135
        case VLC_VIDEOS_DIR:
136
            return config_GetShellDir (CSIDL_MYVIDEO);
137 138 139
    }
    assert (0);
}