fixed bugs in configure.in, removed
[wmaker-crm.git] / WINGs / wapplication.c
blob59bf63cae1db189dcd4a86f004bbee52cc96cb21
3 #include <unistd.h>
5 #include "WINGsP.h"
8 extern void W_InitNotificationCenter(void);
11 struct W_Application WMApplication;
14 char *_WINGS_progname = NULL;
18 Bool
19 W_ApplicationInitialized(void)
21 return _WINGS_progname!=NULL;
25 void
26 WMInitializeApplication(char *applicationName, int *argc, char **argv)
28 int i;
30 assert(argc!=NULL);
31 assert(argv!=NULL);
32 assert(applicationName!=NULL);
34 _WINGS_progname = argv[0];
36 WMApplication.applicationName = wstrdup(applicationName);
37 WMApplication.argc = *argc;
39 WMApplication.argv = wmalloc((*argc+1)*sizeof(char*));
40 for (i=0; i<*argc; i++) {
41 WMApplication.argv[i] = wstrdup(argv[i]);
43 WMApplication.argv[i] = NULL;
45 /* initialize notification center */
46 W_InitNotificationCenter();
50 void
51 WMSetResourcePath(char *path)
53 if (WMApplication.resourcePath)
54 wfree(WMApplication.resourcePath);
55 WMApplication.resourcePath = wstrdup(path);
59 char*
60 WMGetApplicationName()
62 return WMApplication.applicationName;
66 static char*
67 checkFile(char *path, char *folder, char *ext, char *resource)
69 char *ret;
70 int extralen;
72 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
73 ret = wmalloc(strlen(path)+strlen(resource)+extralen+8);
74 strcpy(ret, path);
75 if (folder) {
76 strcat(ret, "/");
77 strcat(ret, folder);
79 if (ext) {
80 strcat(ret, "/");
81 strcat(ret, ext);
83 strcat(ret, "/");
84 strcat(ret, resource);
86 if (access(ret, F_OK)!=0) {
87 wfree(ret);
88 ret = NULL;
91 return ret;
95 char*
96 WMPathForResourceOfType(char *resource, char *ext)
98 char *path = NULL;
99 char *tmp, *appdir;
100 int i;
103 * Paths are searched in this order:
104 * - resourcePath/ext
105 * - argv[0]/ext
106 * - GNUSTEP_USER_ROOT/Apps/ApplicationName.app/ext
107 * - ~/GNUstep/Apps/ApplicationName.app/ext
108 * - GNUSTEP_LOCAL_ROOT/Apps/ApplicationName.app/ext
109 * - /usr/local/GNUstep/Apps/ApplicationName.app/ext
110 * - GNUSTEP_SYSTEM_ROOT/Apps/ApplicationName.app/ext
111 * - /usr/GNUstep/Apps/ApplicationName.app/ext
114 if (WMApplication.resourcePath) {
115 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
116 if (path)
117 return path;
120 if (WMApplication.argv[0]) {
121 tmp = wstrdup(WMApplication.argv[0]);
122 i = strlen(tmp);
123 while (i > 0 && tmp[i]!='/')
124 i--;
125 tmp[i] = 0;
126 if (i>0) {
127 path = checkFile(tmp, NULL, ext, resource);
128 } else {
129 path = NULL;
131 wfree(tmp);
132 if (path)
133 return path;
136 appdir = wmalloc(strlen(WMApplication.applicationName)+10);
137 sprintf(appdir, "Apps/%s.app", WMApplication.applicationName);
139 if (getenv("GNUSTEP_USER_ROOT")) {
140 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
141 if (path) {
142 wfree(appdir);
143 return path;
147 tmp = wusergnusteppath();
148 if (tmp) {
149 path = checkFile(tmp, appdir, ext, resource);
150 if (path) {
151 wfree(appdir);
152 return path;
156 if (getenv("GNUSTEP_LOCAL_ROOT")) {
157 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
158 if (path) {
159 wfree(appdir);
160 return path;
164 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
165 if (path) {
166 wfree(appdir);
167 return path;
171 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
172 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
173 if (path) {
174 wfree(appdir);
175 return path;
179 path = checkFile("/usr/GNUstep", appdir, ext, resource);
180 if (path) {
181 wfree(appdir);
182 return path;
185 return NULL;