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 *****************************************************************************/
34 #include <vlc_common.h>
42 #include "../libvlc.h"
43 #include <vlc_charset.h>
44 #include <vlc_configuration.h>
45 #include "config/configuration.h"
51 #include <winstring.h>
52 #include <windows.storage.h>
55 #ifndef CSIDL_LOCAL_APPDATA
56 # define CSIDL_LOCAL_APPDATA 0x001C
59 static HRESULT
WinRTSHGetFolderPath(HWND hwnd
, int csidl
, HANDLE hToken
, DWORD dwFlags
, LPWSTR pszPath
)
65 IStorageFolder
*folder
;
67 if (dwFlags
!= SHGFP_TYPE_CURRENT
)
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
);
85 hr
= RoGetActivationFactory(hClassName
, &IID_IApplicationDataStatics
, (void**)&appDataStatics
);
90 if (!appDataStatics
) {
95 hr
= IApplicationDataStatics_get_Current(appDataStatics
, &appData
);
105 hr
= IApplicationData_get_LocalFolder(appData
, &folder
);
108 WindowsDeleteString(hClassName
);
110 IApplicationDataStatics_Release(appDataStatics
);
112 IApplicationData_Release(appData
);
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
);
126 hr
= RoGetActivationFactory(hClassName
, &IID_IKnownFoldersStatics
, (void**)&knownFoldersStatics
);
131 if (!knownFoldersStatics
) {
138 hr
= IKnownFoldersStatics_get_DocumentsLibrary(knownFoldersStatics
, &folder
);
141 hr
= IKnownFoldersStatics_get_MusicLibrary(knownFoldersStatics
, &folder
);
143 case CSIDL_MYPICTURES
:
144 hr
= IKnownFoldersStatics_get_PicturesLibrary(knownFoldersStatics
, &folder
);
147 hr
= IKnownFoldersStatics_get_VideosLibrary(knownFoldersStatics
, &folder
);
154 WindowsDeleteString(hClassName
);
155 if (knownFoldersStatics
)
156 IKnownFoldersStatics_Release(knownFoldersStatics
);
159 if( SUCCEEDED(hr
) && folder
!= NULL
)
162 IStorageItem
*item
= NULL
;
164 hr
= IStorageFolder_QueryInterface(folder
, &IID_IStorageItem
, (void**)&item
);
167 hr
= IStorageItem_get_Path(item
, &path
);
170 pszPathTemp
= WindowsGetStringRawBuffer(path
, NULL
);
171 wcscpy(pszPath
, pszPathTemp
);
173 WindowsDeleteString(path
);
174 IStorageFolder_Release(folder
);
176 IStorageItem_Release(item
);
181 #define SHGetFolderPathW WinRTSHGetFolderPath
184 char *config_GetLibDir (void)
189 /* Get our full path */
190 MEMORY_BASIC_INFORMATION mbi
;
191 if (!VirtualQuery (config_GetLibDir
, &mbi
, sizeof(mbi
)))
194 wchar_t wpath
[MAX_PATH
];
195 if (!GetModuleFileName ((HMODULE
) mbi
.AllocationBase
, wpath
, MAX_PATH
))
198 wchar_t *file
= wcsrchr (wpath
, L
'\\');
203 return FromWide (wpath
);
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
)
221 case VLC_PKG_DATA_DIR
:
222 dir
= config_GetDataDir();
224 case VLC_PKG_LIB_DIR
:
225 dir
= config_GetLibDir();
227 case VLC_SYSDATA_DIR
:
230 dir
= config_GetSysPath(VLC_PKG_DATA_DIR
, "locale");
233 vlc_assert_unreachable();
236 if (filename
== NULL
|| unlikely(dir
== NULL
))
240 if (unlikely(asprintf(&path
, "%s/%s", dir
, filename
) == -1))
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
);
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
, '\\');
266 _tcscpy (lastDir
+ 1, TEXT("portable"));
267 DWORD attrib
= GetFileAttributes (path
);
268 if (attrib
!= INVALID_FILE_ATTRIBUTES
&&
269 (attrib
& FILE_ATTRIBUTE_DIRECTORY
))
276 char *psz_parent
= config_GetShellDir (CSIDL_APPDATA
);
278 if (psz_parent
== NULL
279 || asprintf (&psz_dir
, "%s\\vlc", psz_parent
) == -1)
285 #warning FIXME Use known folders on Vista and above
286 char *config_GetUserDir (vlc_userdir_t type
)
291 return config_GetShellDir (CSIDL_PERSONAL
);
293 case VLC_USERDATA_DIR
:
294 return config_GetAppDir ();
296 #if !VLC_WINSTORE_APP
297 return config_GetAppDir ();
299 return config_GetShellDir (CSIDL_LOCAL_APPDATA
);
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
);
309 return config_GetShellDir (CSIDL_MYMUSIC
);
310 case VLC_PICTURES_DIR
:
311 return config_GetShellDir (CSIDL_MYPICTURES
);
313 return config_GetShellDir (CSIDL_MYVIDEO
);
315 vlc_assert_unreachable ();