]> git.sesse.net Git - vlc/blob - src/config/dirs_macos.c
config: On Mac OS use the app identifier for app dependent config dirs.
[vlc] / src / config / dirs_macos.c
1 /*****************************************************************************
2  * dirs_macos.c: Mac OS X directories configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * Copyright © 2007-2009 Rémi Denis-Courmont
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <CoreFoundation/CoreFoundation.h>
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include "../libvlc.h"
34 #include <vlc_charset.h>
35 #include <vlc_configuration.h>
36 #include "configuration.h"
37
38 static char *configdir = NULL;
39 static char *datadir = NULL;
40
41 static pthread_once_t once = PTHREAD_ONCE_INIT;
42
43 static void init_dirs( void )
44 {
45     configdir = config_GetUserDir(VLC_CONFIG_DIR);
46     datadir = config_GetUserDir(VLC_DATA_DIR);
47 }
48
49 const char *config_GetConfDir( void )
50 {
51     pthread_once(&once, init_dirs);
52     return configdir;
53 }
54
55 const char *config_GetDataDirDefault (void)
56 {
57     pthread_once(&once, init_dirs);
58     return datadir;
59 }
60
61 static char *config_GetHomeDir (void)
62 {
63     const char *home = getenv ("HOME");
64
65     if (home == NULL)
66         home = "/tmp";
67
68     return FromLocaleDup (home);
69 }
70
71 static char *getAppDependentDir(vlc_userdir_t type)
72 {
73     const char *psz_path;
74     switch (type)
75     {
76         case VLC_CONFIG_DIR:
77             psz_path = "%s/Library/Preferences/%s";
78             break;
79         case VLC_TEMPLATES_DIR:
80         case VLC_DATA_DIR:
81             psz_path = "%s/Library/Application Support/%s";
82             break;
83         case VLC_CACHE_DIR:
84             psz_path = "%s/Library/Caches/%s";
85             break;
86         default:
87             assert(0);
88             break;
89     }
90
91     CFBundleRef mainBundle = CFBundleGetMainBundle();
92     CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
93     const char *identifier = CFStringGetCStringPtr(identifierAsNS, kCFStringEncodingUTF8);
94     
95     char *psz_parent = config_GetHomeDir ();
96     char *psz_dir;
97     if( asprintf( &psz_dir, psz_path, psz_parent, identifier) == -1 )
98         psz_dir = NULL;
99     free(psz_parent);
100
101     return psz_dir;    
102 }
103
104 char *config_GetUserDir (vlc_userdir_t type)
105 {
106     const char *psz_path;
107     switch (type)
108     {
109         case VLC_CONFIG_DIR:
110         case VLC_TEMPLATES_DIR:
111         case VLC_DATA_DIR:
112         case VLC_CACHE_DIR:
113             return getAppDependentDir(type);
114
115         case VLC_DESKTOP_DIR:
116             psz_path = "%s/Desktop";
117             break;
118         case VLC_DOWNLOAD_DIR:
119             psz_path = "%s/Downloads";
120             break;
121         case VLC_DOCUMENTS_DIR:
122             psz_path = "%s/Documents";
123             break;
124         case VLC_MUSIC_DIR:
125             psz_path = "%s/Music";
126             break;
127         case VLC_PICTURES_DIR:
128             psz_path = "%s/Pictures";
129             break;
130         case VLC_VIDEOS_DIR:
131             psz_path = "%s/Movies";
132             break;
133         case VLC_PUBLICSHARE_DIR:
134             psz_path = "%s/Public";
135             break;
136         case VLC_HOME_DIR:
137         default:
138             psz_path = "%s";
139     }
140     char *psz_parent = config_GetHomeDir ();
141     char *psz_dir;
142     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
143         psz_dir = NULL;
144     free(psz_parent);
145     return psz_dir;
146 }