config: add localedir
[vlc.git] / src / win32 / dirs.c
blob5489da6286e8fedd5e3662af72581abeeb4c703e
1 /*****************************************************************************
2 * dirs.c: directories configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2010 VLC authors and VideoLAN
5 * Copyright © 2007-2012 Rémi Denis-Courmont
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #define COBJMACROS
29 #define INITGUID
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
34 #include <vlc_common.h>
36 #ifdef __MINGW32__
37 # include <w32api.h>
38 #endif
39 #include <direct.h>
40 #include <shlobj.h>
42 #include "../libvlc.h"
43 #include <vlc_charset.h>
44 #include <vlc_configuration.h>
45 #include "config/configuration.h"
47 #include <assert.h>
48 #include <limits.h>
50 #if VLC_WINSTORE_APP
51 #include <winstring.h>
52 #include <windows.storage.h>
53 #include <roapi.h>
55 #ifndef CSIDL_LOCAL_APPDATA
56 # define CSIDL_LOCAL_APPDATA 0x001C
57 #endif
59 static HRESULT WinRTSHGetFolderPath(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
61 VLC_UNUSED(hwnd);
62 VLC_UNUSED(hToken);
64 HRESULT hr;
65 IStorageFolder *folder;
67 if (dwFlags != SHGFP_TYPE_CURRENT)
68 return E_NOTIMPL;
70 folder = NULL;
71 csidl &= ~0xFF00; /* CSIDL_FLAG_MASK */
73 if (csidl == CSIDL_APPDATA) {
74 IApplicationDataStatics *appDataStatics = NULL;
75 IApplicationData *appData = NULL;
76 static const WCHAR *className = L"Windows.Storage.ApplicationData";
77 const UINT32 clen = wcslen(className);
79 HSTRING hClassName = NULL;
80 HSTRING_HEADER header;
81 hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
82 if (FAILED(hr))
83 goto end_appdata;
85 hr = RoGetActivationFactory(hClassName, &IID_IApplicationDataStatics, (void**)&appDataStatics);
87 if (FAILED(hr))
88 goto end_appdata;
90 if (!appDataStatics) {
91 hr = E_FAIL;
92 goto end_appdata;
95 hr = IApplicationDataStatics_get_Current(appDataStatics, &appData);
97 if (FAILED(hr))
98 goto end_appdata;
100 if (!appData) {
101 hr = E_FAIL;
102 goto end_appdata;
105 hr = IApplicationData_get_LocalFolder(appData, &folder);
107 end_appdata:
108 WindowsDeleteString(hClassName);
109 if (appDataStatics)
110 IApplicationDataStatics_Release(appDataStatics);
111 if (appData)
112 IApplicationData_Release(appData);
114 else
116 IKnownFoldersStatics *knownFoldersStatics = NULL;
117 static const WCHAR *className = L"Windows.Storage.KnownFolders";
118 const UINT32 clen = wcslen(className);
120 HSTRING hClassName = NULL;
121 HSTRING_HEADER header;
122 hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
123 if (FAILED(hr))
124 goto end_other;
126 hr = RoGetActivationFactory(hClassName, &IID_IKnownFoldersStatics, (void**)&knownFoldersStatics);
128 if (FAILED(hr))
129 goto end_other;
131 if (!knownFoldersStatics) {
132 hr = E_FAIL;
133 goto end_other;
136 switch (csidl) {
137 case CSIDL_PERSONAL:
138 hr = IKnownFoldersStatics_get_DocumentsLibrary(knownFoldersStatics, &folder);
139 break;
140 case CSIDL_MYMUSIC:
141 hr = IKnownFoldersStatics_get_MusicLibrary(knownFoldersStatics, &folder);
142 break;
143 case CSIDL_MYPICTURES:
144 hr = IKnownFoldersStatics_get_PicturesLibrary(knownFoldersStatics, &folder);
145 break;
146 case CSIDL_MYVIDEO:
147 hr = IKnownFoldersStatics_get_VideosLibrary(knownFoldersStatics, &folder);
148 break;
149 default:
150 hr = E_NOTIMPL;
153 end_other:
154 WindowsDeleteString(hClassName);
155 if (knownFoldersStatics)
156 IKnownFoldersStatics_Release(knownFoldersStatics);
159 if( SUCCEEDED(hr) && folder != NULL )
161 HSTRING path = NULL;
162 IStorageItem *item = NULL;
163 PCWSTR pszPathTemp;
164 hr = IStorageFolder_QueryInterface(folder, &IID_IStorageItem, (void**)&item);
165 if (FAILED(hr))
166 goto end_folder;
167 hr = IStorageItem_get_Path(item, &path);
168 if (FAILED(hr))
169 goto end_folder;
170 pszPathTemp = WindowsGetStringRawBuffer(path, NULL);
171 wcscpy(pszPath, pszPathTemp);
172 end_folder:
173 WindowsDeleteString(path);
174 IStorageFolder_Release(folder);
175 if (item)
176 IStorageItem_Release(item);
179 return hr;
181 #define SHGetFolderPathW WinRTSHGetFolderPath
182 #endif
184 char *config_GetLibDir (void)
186 #if VLC_WINSTORE_APP
187 return NULL;
188 #else
189 /* Get our full path */
190 MEMORY_BASIC_INFORMATION mbi;
191 if (!VirtualQuery (config_GetLibDir, &mbi, sizeof(mbi)))
192 goto error;
194 wchar_t wpath[MAX_PATH];
195 if (!GetModuleFileName ((HMODULE) mbi.AllocationBase, wpath, MAX_PATH))
196 goto error;
198 wchar_t *file = wcsrchr (wpath, L'\\');
199 if (file == NULL)
200 goto error;
201 *file = L'\0';
203 return FromWide (wpath);
204 error:
205 abort ();
206 #endif
209 static char *config_GetDataDir(void)
211 const char *path = getenv ("VLC_DATA_PATH");
212 return (path != NULL) ? strdup (path) : config_GetLibDir ();
215 char *config_GetSysPath(vlc_sysdir_t type, const char *filename)
217 char *dir = NULL;
219 switch (type)
221 case VLC_PKG_DATA_DIR:
222 dir = config_GetDataDir();
223 break;
224 case VLC_PKG_LIB_DIR:
225 dir = config_GetLibDir();
226 break;
227 case VLC_SYSDATA_DIR:
228 break;
229 case VLC_LOCALE_DIR:
230 dir = config_GetSysPath(VLC_PKG_DATA_DIR, "locale");
231 break;
232 default:
233 vlc_assert_unreachable();
236 if (filename == NULL || unlikely(dir == NULL))
237 return dir;
239 char *path;
240 if (unlikely(asprintf(&path, "%s/%s", dir, filename) == -1))
241 path = NULL;
242 free(dir);
243 return path;
246 static char *config_GetShellDir (int csidl)
248 wchar_t wdir[MAX_PATH];
250 if (SHGetFolderPathW (NULL, csidl | CSIDL_FLAG_CREATE,
251 NULL, SHGFP_TYPE_CURRENT, wdir ) == S_OK)
252 return FromWide (wdir);
253 return NULL;
256 static char *config_GetAppDir (void)
258 #if !VLC_WINSTORE_APP
259 /* if portable directory exists, use it */
260 TCHAR path[MAX_PATH];
261 if (GetModuleFileName (NULL, path, MAX_PATH))
263 TCHAR *lastDir = _tcsrchr (path, '\\');
264 if (lastDir)
266 _tcscpy (lastDir + 1, TEXT("portable"));
267 DWORD attrib = GetFileAttributes (path);
268 if (attrib != INVALID_FILE_ATTRIBUTES &&
269 (attrib & FILE_ATTRIBUTE_DIRECTORY))
270 return FromT (path);
273 #endif
275 char *psz_dir;
276 char *psz_parent = config_GetShellDir (CSIDL_APPDATA);
278 if (psz_parent == NULL
279 || asprintf (&psz_dir, "%s\\vlc", psz_parent) == -1)
280 psz_dir = NULL;
281 free (psz_parent);
282 return psz_dir;
285 #warning FIXME Use known folders on Vista and above
286 char *config_GetUserDir (vlc_userdir_t type)
288 switch (type)
290 case VLC_HOME_DIR:
291 return config_GetShellDir (CSIDL_PERSONAL);
292 case VLC_CONFIG_DIR:
293 case VLC_USERDATA_DIR:
294 return config_GetAppDir ();
295 case VLC_CACHE_DIR:
296 #if !VLC_WINSTORE_APP
297 return config_GetAppDir ();
298 #else
299 return config_GetShellDir (CSIDL_LOCAL_APPDATA);
300 #endif
302 case VLC_DESKTOP_DIR:
303 case VLC_DOWNLOAD_DIR:
304 case VLC_TEMPLATES_DIR:
305 case VLC_PUBLICSHARE_DIR:
306 case VLC_DOCUMENTS_DIR:
307 return config_GetUserDir(VLC_HOME_DIR);
308 case VLC_MUSIC_DIR:
309 return config_GetShellDir (CSIDL_MYMUSIC);
310 case VLC_PICTURES_DIR:
311 return config_GetShellDir (CSIDL_MYPICTURES);
312 case VLC_VIDEOS_DIR:
313 return config_GetShellDir (CSIDL_MYVIDEO);
315 vlc_assert_unreachable ();