changed indentation to use spaces only
[wmaker-crm.git] / WINGs / wapplication.c
blob1cf6e08f292df71fe33a4b69d2af24948365bb18
3 #include <unistd.h>
5 #include "WINGsP.h"
7 #include "wconfig.h"
9 #include "X11/Xlocale.h"
12 extern void W_InitNotificationCenter(void);
15 struct W_Application WMApplication;
18 char *_WINGS_progname = NULL;
22 Bool
23 W_ApplicationInitialized(void)
25 return _WINGS_progname!=NULL;
29 void
30 WMInitializeApplication(char *applicationName, int *argc, char **argv)
32 int i;
34 assert(argc!=NULL);
35 assert(argv!=NULL);
36 assert(applicationName!=NULL);
38 /* // TODO: check if to move inside #ifdef I18N */
39 setlocale(LC_ALL, "");
41 #ifdef I18N
42 if (getenv("NLSPATH"))
43 bindtextdomain("WINGs", getenv("NLSPATH"));
44 else
45 bindtextdomain("WINGs", LOCALEDIR);
46 #endif
48 _WINGS_progname = argv[0];
50 WMApplication.applicationName = wstrdup(applicationName);
51 WMApplication.argc = *argc;
53 WMApplication.argv = wmalloc((*argc+1)*sizeof(char*));
54 for (i=0; i<*argc; i++) {
55 WMApplication.argv[i] = wstrdup(argv[i]);
57 WMApplication.argv[i] = NULL;
59 /* initialize notification center */
60 W_InitNotificationCenter();
64 void
65 WMSetResourcePath(char *path)
67 if (WMApplication.resourcePath)
68 wfree(WMApplication.resourcePath);
69 WMApplication.resourcePath = wstrdup(path);
73 char*
74 WMGetApplicationName()
76 return WMApplication.applicationName;
80 static char*
81 checkFile(char *path, char *folder, char *ext, char *resource)
83 char *ret;
84 int extralen;
86 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
87 ret = wmalloc(strlen(path)+strlen(resource)+extralen+8);
88 strcpy(ret, path);
89 if (folder) {
90 strcat(ret, "/");
91 strcat(ret, folder);
93 if (ext) {
94 strcat(ret, "/");
95 strcat(ret, ext);
97 strcat(ret, "/");
98 strcat(ret, resource);
100 if (access(ret, F_OK)!=0) {
101 wfree(ret);
102 ret = NULL;
105 return ret;
109 char*
110 WMPathForResourceOfType(char *resource, char *ext)
112 char *path = NULL;
113 char *tmp, *appdir;
114 int i;
117 * Paths are searched in this order:
118 * - resourcePath/ext
119 * - argv[0]/ext
120 * - GNUSTEP_USER_ROOT/Apps/ApplicationName.app/ext
121 * - ~/GNUstep/Apps/ApplicationName.app/ext
122 * - GNUSTEP_LOCAL_ROOT/Apps/ApplicationName.app/ext
123 * - /usr/local/GNUstep/Apps/ApplicationName.app/ext
124 * - GNUSTEP_SYSTEM_ROOT/Apps/ApplicationName.app/ext
125 * - /usr/GNUstep/Apps/ApplicationName.app/ext
128 if (WMApplication.resourcePath) {
129 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
130 if (path)
131 return path;
134 if (WMApplication.argv[0]) {
135 tmp = wstrdup(WMApplication.argv[0]);
136 i = strlen(tmp);
137 while (i > 0 && tmp[i]!='/')
138 i--;
139 tmp[i] = 0;
140 if (i>0) {
141 path = checkFile(tmp, NULL, ext, resource);
142 } else {
143 path = NULL;
145 wfree(tmp);
146 if (path)
147 return path;
150 appdir = wmalloc(strlen(WMApplication.applicationName)+10);
151 sprintf(appdir, "Apps/%s.app", WMApplication.applicationName);
153 if (getenv("GNUSTEP_USER_ROOT")) {
154 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
155 if (path) {
156 wfree(appdir);
157 return path;
161 tmp = wusergnusteppath();
162 if (tmp) {
163 path = checkFile(tmp, appdir, ext, resource);
164 if (path) {
165 wfree(appdir);
166 return path;
170 if (getenv("GNUSTEP_LOCAL_ROOT")) {
171 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
172 if (path) {
173 wfree(appdir);
174 return path;
178 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
179 if (path) {
180 wfree(appdir);
181 return path;
185 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
186 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
187 if (path) {
188 wfree(appdir);
189 return path;
193 path = checkFile("/usr/GNUstep", appdir, ext, resource);
194 if (path) {
195 wfree(appdir);
196 return path;
199 return NULL;