]> git.sesse.net Git - vlc/blob - src/config/dirs_macos.c
config_(Get|Put)(Int|Float): use global configuration lock
[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
40 static pthread_once_t once = PTHREAD_ONCE_INIT;
41
42 static void init_dirs( void )
43 {
44     configdir = config_GetUserDir(VLC_CONFIG_DIR);
45 }
46
47 const char *config_GetConfDir( void )
48 {
49     pthread_once(&once, init_dirs);
50     return configdir;
51 }
52
53 char *config_GetDataDirDefault (void)
54 {
55     char *datadir;
56
57     if (asprintf (&datadir, "%s/share", psz_vlcpath) == -1)
58         return NULL;
59     return datadir;
60 }
61
62 static char *config_GetHomeDir (void)
63 {
64     const char *home = getenv ("HOME");
65
66     if (home == NULL)
67         home = "/tmp";
68
69     return FromLocaleDup (home);
70 }
71
72 static char *getAppDependentDir(vlc_userdir_t type)
73 {
74     const char *psz_path;
75     switch (type)
76     {
77         case VLC_CONFIG_DIR:
78             psz_path = "%s/Library/Preferences/%s";
79             break;
80         case VLC_TEMPLATES_DIR:
81         case VLC_DATA_DIR:
82             psz_path = "%s/Library/Application Support/%s";
83             break;
84         case VLC_CACHE_DIR:
85             psz_path = "%s/Library/Caches/%s";
86             break;
87         default:
88             assert(0);
89             break;
90     }
91
92     // Default fallback
93     const char *name = "org.videolan.vlc";
94
95     CFBundleRef mainBundle = CFBundleGetMainBundle();
96     if (mainBundle)
97     {
98         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
99         if (identifierAsNS)
100         {
101             char identifier[256];
102             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
103             if (ret)
104                 name = identifier;            
105         }
106     }
107
108     char *psz_parent = config_GetHomeDir ();
109     char *psz_dir;
110     if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
111         psz_dir = NULL;
112     free(psz_parent);
113
114     return psz_dir;    
115 }
116
117 char *config_GetUserDir (vlc_userdir_t type)
118 {
119     const char *psz_path;
120     switch (type)
121     {
122         case VLC_CONFIG_DIR:
123         case VLC_TEMPLATES_DIR:
124         case VLC_DATA_DIR:
125         case VLC_CACHE_DIR:
126             return getAppDependentDir(type);
127
128         case VLC_DESKTOP_DIR:
129             psz_path = "%s/Desktop";
130             break;
131         case VLC_DOWNLOAD_DIR:
132             psz_path = "%s/Downloads";
133             break;
134         case VLC_DOCUMENTS_DIR:
135             psz_path = "%s/Documents";
136             break;
137         case VLC_MUSIC_DIR:
138             psz_path = "%s/Music";
139             break;
140         case VLC_PICTURES_DIR:
141             psz_path = "%s/Pictures";
142             break;
143         case VLC_VIDEOS_DIR:
144             psz_path = "%s/Movies";
145             break;
146         case VLC_PUBLICSHARE_DIR:
147             psz_path = "%s/Public";
148             break;
149         case VLC_HOME_DIR:
150         default:
151             psz_path = "%s";
152     }
153     char *psz_parent = config_GetHomeDir ();
154     char *psz_dir;
155     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
156         psz_dir = NULL;
157     free(psz_parent);
158     return psz_dir;
159 }