]> git.sesse.net Git - vlc/blob - src/config/dirs_macos.c
Use var_Inherit* instead of var_CreateGet*.
[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 const char *config_GetLibDir (void)
63 {
64     abort ();
65 }
66
67 static char *config_GetHomeDir (void)
68 {
69     const char *home = getenv ("HOME");
70
71     if (home == NULL)
72         home = "/tmp";
73
74     return FromLocaleDup (home);
75 }
76
77 static char *getAppDependentDir(vlc_userdir_t type)
78 {
79     const char *psz_path;
80     switch (type)
81     {
82         case VLC_CONFIG_DIR:
83             psz_path = "%s/Library/Preferences/%s";
84             break;
85         case VLC_TEMPLATES_DIR:
86         case VLC_DATA_DIR:
87             psz_path = "%s/Library/Application Support/%s";
88             break;
89         case VLC_CACHE_DIR:
90             psz_path = "%s/Library/Caches/%s";
91             break;
92         default:
93             assert(0);
94             break;
95     }
96
97     // Default fallback
98     const char *name = "org.videolan.vlc";
99
100     CFBundleRef mainBundle = CFBundleGetMainBundle();
101     if (mainBundle)
102     {
103         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
104         if (identifierAsNS)
105         {
106             char identifier[256];
107             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
108             if (ret)
109                 name = identifier;            
110         }
111     }
112
113     char *psz_parent = config_GetHomeDir ();
114     char *psz_dir;
115     if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
116         psz_dir = NULL;
117     free(psz_parent);
118
119     return psz_dir;    
120 }
121
122 char *config_GetUserDir (vlc_userdir_t type)
123 {
124     const char *psz_path;
125     switch (type)
126     {
127         case VLC_CONFIG_DIR:
128         case VLC_TEMPLATES_DIR:
129         case VLC_DATA_DIR:
130         case VLC_CACHE_DIR:
131             return getAppDependentDir(type);
132
133         case VLC_DESKTOP_DIR:
134             psz_path = "%s/Desktop";
135             break;
136         case VLC_DOWNLOAD_DIR:
137             psz_path = "%s/Downloads";
138             break;
139         case VLC_DOCUMENTS_DIR:
140             psz_path = "%s/Documents";
141             break;
142         case VLC_MUSIC_DIR:
143             psz_path = "%s/Music";
144             break;
145         case VLC_PICTURES_DIR:
146             psz_path = "%s/Pictures";
147             break;
148         case VLC_VIDEOS_DIR:
149             psz_path = "%s/Movies";
150             break;
151         case VLC_PUBLICSHARE_DIR:
152             psz_path = "%s/Public";
153             break;
154         case VLC_HOME_DIR:
155         default:
156             psz_path = "%s";
157     }
158     char *psz_parent = config_GetHomeDir ();
159     char *psz_dir;
160     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
161         psz_dir = NULL;
162     free(psz_parent);
163     return psz_dir;
164 }