lua: factorize the right way.
[vlc/asuraparaju-public.git] / src / config / dirs_macos.c
blob0a563d84a1ee0e270b0d8b4771dc2b541caa504c
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
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
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.
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.
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 *****************************************************************************/
25 #include <CoreFoundation/CoreFoundation.h>
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
33 #include "../libvlc.h"
34 #include <vlc_charset.h>
35 #include <vlc_configuration.h>
36 #include "configuration.h"
38 static char *configdir = NULL;
40 static pthread_once_t once = PTHREAD_ONCE_INIT;
42 static void init_dirs( void )
44 configdir = config_GetUserDir(VLC_CONFIG_DIR);
47 const char *config_GetConfDir( void )
49 pthread_once(&once, init_dirs);
50 return configdir;
53 char *config_GetDataDirDefault (void)
55 char *datadir;
57 if (asprintf (&datadir, "%s/share", psz_vlcpath) == -1)
58 return NULL;
59 return datadir;
62 const char *config_GetLibDir (void)
64 abort ();
67 static char *config_GetHomeDir (void)
69 const char *home = getenv ("HOME");
71 if (home == NULL)
72 home = "/tmp";
74 return FromLocaleDup (home);
77 static char *getAppDependentDir(vlc_userdir_t type)
79 const char *psz_path;
80 switch (type)
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;
97 // Default fallback
98 const char *name = "org.videolan.vlc";
100 CFBundleRef mainBundle = CFBundleGetMainBundle();
101 if (mainBundle)
103 CFStringRef identifierAsNS = CFBundleGetIdentifier(mainBundle);
104 if (identifierAsNS)
106 char identifier[256];
107 Boolean ret = CFStringGetCString(identifierAsNS, identifier, sizeof(identifier), kCFStringEncodingUTF8);
108 if (ret)
109 name = identifier;
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);
119 return psz_dir;
122 char *config_GetUserDir (vlc_userdir_t type)
124 const char *psz_path;
125 switch (type)
127 case VLC_CONFIG_DIR:
128 case VLC_TEMPLATES_DIR:
129 case VLC_DATA_DIR:
130 case VLC_CACHE_DIR:
131 return getAppDependentDir(type);
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";
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;