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