]> git.sesse.net Git - vlc/blob - src/config/dirs_win.c
d80aecd4136c422fc3e5c576678b131f077c72d3
[vlc] / src / config / dirs_win.c
1 /*****************************************************************************
2  * dirs.c: directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * Copyright © 2007-2008 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #ifndef _WIN32_IE
31 # define _WIN32_IE 0x0501
32 #endif
33 #include <w32api.h>
34 #ifndef UNDER_CE
35 # include <direct.h>
36 #endif
37 #include <shlobj.h>
38
39 #include "../libvlc.h"
40 #include <vlc_charset.h>
41 #include <vlc_configuration.h>
42
43 #include <assert.h>
44 #include <limits.h>
45
46 const char *config_GetDataDir( void )
47 {
48     static char path[PATH_MAX] = "";
49 #warning FIXME: thread-safety!
50
51     if( *path == '\0' )
52         strlcpy (path, psz_vlcpath, sizeof (path));
53     return path;
54 }
55
56 static const char *GetDir( bool b_common )
57 {
58     wchar_t wdir[MAX_PATH];
59
60 #if defined (UNDER_CE)
61     /*There are some errors in cegcc headers*/
62 #undef SHGetSpecialFolderPath
63     BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
64     if( SHGetSpecialFolderPath( NULL, wdir, CSIDL_APPDATA, 1 ) )
65 #else
66     /* Get the "Application Data" folder for the current user */
67     if( S_OK == SHGetFolderPathW( NULL, (b_common ? CSIDL_COMMON_APPDATA
68                                                   : CSIDL_APPDATA)
69               | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wdir ) )
70 #endif
71     {
72         static char appdir[PATH_MAX] = "";
73         static char comappdir[PATH_MAX] = "";
74         WideCharToMultiByte (CP_UTF8, 0, wdir, -1,
75                              b_common ? comappdir : appdir,
76                              PATH_MAX, NULL, NULL);
77         return b_common ? comappdir : appdir;
78     }
79     return NULL;
80 }
81
82 const char *config_GetConfDir( void )
83 {
84     return GetDir( true );
85 }
86
87 static char *config_GetHomeDir (void)
88 {
89     wchar_t wdir[MAX_PATH];
90
91 #if defined (UNDER_CE)
92     /*There are some errors in cegcc headers*/
93 #undef SHGetSpecialFolderPath
94     BOOL WINAPI SHGetSpecialFolderPath(HWND,LPWSTR,int,BOOL);
95     if (SHGetSpecialFolderPath (NULL, wdir, CSIDL_APPDATA, 1))
96 #else
97     if (SHGetFolderPathW (NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
98                           NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
99 #endif
100         return FromWide (wdir);
101     return NULL;
102 }
103
104 static char *config_GetAppDir (void)
105 {
106     char *psz_dir;
107     const char *psz_parent = GetDir (false);
108
109     if( asprintf( &psz_dir, "%s\\vlc", psz_parent ) == -1 )
110         psz_dir = NULL;
111     return psz_dir;
112 }
113
114 char *config_GetCacheDir( void )
115 {
116     return config_GetAppDir ();
117 }
118
119 char *config_GetUserDir (vlc_userdir_t type)
120 {
121     switch (type)
122     {
123         case VLC_HOME_DIR:
124             return config_GetHomeDir ();
125         case VLC_CONFIG_DIR:
126             return config_GetAppDir ();
127         case VLC_DATA_DIR:
128             return config_GetAppDir ();
129         case VLC_DESKTOP_DIR:
130         case VLC_DOWNLOAD_DIR:
131         case VLC_TEMPLATES_DIR:
132         case VLC_PUBLICSHARE_DIR:
133         case VLC_DOCUMENTS_DIR:
134         case VLC_MUSIC_DIR:
135         case VLC_PICTURES_DIR:
136         case VLC_VIDEOS_DIR:
137             return config_GetUserDir (VLC_HOME_DIR);
138     }
139     assert (0);
140 }