]> git.sesse.net Git - vlc/blob - src/config/dirs_macos.c
Fix prototype warning for config_GetDataDirDefault()
[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     // Default fallback
94     const char *name = "org.videolan.vlc";
95
96     CFBundleRef mainBundle = CFBundleGetMainBundle();
97     if (mainBundle)
98     {
99         CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
100         if (identifierAsNS)
101         {
102             char identifier[256];
103             Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
104             if (ret)
105                 name = identifier;            
106         }
107     }
108
109     char *psz_parent = config_GetHomeDir ();
110     char *psz_dir;
111     if( asprintf( &psz_dir, psz_path, psz_parent, name) == -1 )
112         psz_dir = NULL;
113     free(psz_parent);
114
115     return psz_dir;    
116 }
117
118 char *config_GetUserDir (vlc_userdir_t type)
119 {
120     const char *psz_path;
121     switch (type)
122     {
123         case VLC_CONFIG_DIR:
124         case VLC_TEMPLATES_DIR:
125         case VLC_DATA_DIR:
126         case VLC_CACHE_DIR:
127             return getAppDependentDir(type);
128
129         case VLC_DESKTOP_DIR:
130             psz_path = "%s/Desktop";
131             break;
132         case VLC_DOWNLOAD_DIR:
133             psz_path = "%s/Downloads";
134             break;
135         case VLC_DOCUMENTS_DIR:
136             psz_path = "%s/Documents";
137             break;
138         case VLC_MUSIC_DIR:
139             psz_path = "%s/Music";
140             break;
141         case VLC_PICTURES_DIR:
142             psz_path = "%s/Pictures";
143             break;
144         case VLC_VIDEOS_DIR:
145             psz_path = "%s/Movies";
146             break;
147         case VLC_PUBLICSHARE_DIR:
148             psz_path = "%s/Public";
149             break;
150         case VLC_HOME_DIR:
151         default:
152             psz_path = "%s";
153     }
154     char *psz_parent = config_GetHomeDir ();
155     char *psz_dir;
156     if( asprintf( &psz_dir, psz_path, psz_parent ) == -1 )
157         psz_dir = NULL;
158     free(psz_parent);
159     return psz_dir;
160 }