Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wapplication.c
blobe9a6cf00b15471565b44f61e97eec69e0e116669
2 #include <unistd.h>
4 #include "WINGsP.h"
6 #include "wconfig.h"
8 #include "X11/Xlocale.h"
10 extern void W_InitNotificationCenter(void);
12 struct W_Application WMApplication;
14 char *_WINGS_progname = NULL;
16 Bool W_ApplicationInitialized(void)
18 return _WINGS_progname != NULL;
21 void WMInitializeApplication(char *applicationName, int *argc, char **argv)
23 int i;
25 assert(argc != NULL);
26 assert(argv != NULL);
27 assert(applicationName != NULL);
29 setlocale(LC_ALL, "");
31 #ifdef I18N
32 if (getenv("NLSPATH"))
33 bindtextdomain("WINGs", getenv("NLSPATH"));
34 else
35 bindtextdomain("WINGs", LOCALEDIR);
36 bind_textdomain_codeset("WINGs", "UTF-8");
37 #endif
39 _WINGS_progname = argv[0];
41 WMApplication.applicationName = wstrdup(applicationName);
42 WMApplication.argc = *argc;
44 WMApplication.argv = wmalloc((*argc + 1) * sizeof(char *));
45 for (i = 0; i < *argc; i++) {
46 WMApplication.argv[i] = wstrdup(argv[i]);
48 WMApplication.argv[i] = NULL;
50 /* initialize notification center */
51 W_InitNotificationCenter();
54 void WMSetResourcePath(char *path)
56 if (WMApplication.resourcePath)
57 wfree(WMApplication.resourcePath);
58 WMApplication.resourcePath = wstrdup(path);
61 char *WMGetApplicationName()
63 return WMApplication.applicationName;
66 static char *checkFile(char *path, char *folder, char *ext, char *resource)
68 char *ret;
69 int extralen;
71 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
72 ret = wmalloc(strlen(path) + strlen(resource) + extralen + 8);
73 strcpy(ret, path);
74 if (folder) {
75 strcat(ret, "/");
76 strcat(ret, folder);
78 if (ext) {
79 strcat(ret, "/");
80 strcat(ret, ext);
82 strcat(ret, "/");
83 strcat(ret, resource);
85 if (access(ret, F_OK) != 0) {
86 wfree(ret);
87 ret = NULL;
90 return ret;
93 char *WMPathForResourceOfType(char *resource, char *ext)
95 char *path = NULL;
96 char *tmp, *appdir;
97 int i;
100 * Paths are searched in this order:
101 * - resourcePath/ext
102 * - argv[0]/ext
103 * - GNUSTEP_USER_ROOT/Applications/ApplicationName.app/ext
104 * - ~/GNUstep/Applications/ApplicationName.app/ext
105 * - GNUSTEP_LOCAL_ROOT/Applications/ApplicationName.app/ext
106 * - /usr/local/GNUstep/Applications/ApplicationName.app/ext
107 * - GNUSTEP_SYSTEM_ROOT/Applications/ApplicationName.app/ext
108 * - /usr/GNUstep/Applications/ApplicationName.app/ext
111 if (WMApplication.resourcePath) {
112 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
113 if (path)
114 return path;
117 if (WMApplication.argv[0]) {
118 tmp = wstrdup(WMApplication.argv[0]);
119 i = strlen(tmp);
120 while (i > 0 && tmp[i] != '/')
121 i--;
122 tmp[i] = 0;
123 if (i > 0) {
124 path = checkFile(tmp, NULL, ext, resource);
125 } else {
126 path = NULL;
128 wfree(tmp);
129 if (path)
130 return path;
133 appdir = wmalloc(strlen(WMApplication.applicationName) + 20);
134 sprintf(appdir, "Applications/%s.app", WMApplication.applicationName);
136 if (getenv("GNUSTEP_USER_ROOT")) {
137 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
138 if (path) {
139 wfree(appdir);
140 return path;
144 tmp = wusergnusteppath();
145 if (tmp) {
146 path = checkFile(tmp, appdir, ext, resource);
147 if (path) {
148 wfree(appdir);
149 return path;
153 if (getenv("GNUSTEP_LOCAL_ROOT")) {
154 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
155 if (path) {
156 wfree(appdir);
157 return path;
161 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
162 if (path) {
163 wfree(appdir);
164 return path;
167 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
168 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
169 if (path) {
170 wfree(appdir);
171 return path;
175 path = checkFile("/usr/GNUstep", appdir, ext, resource);
176 if (path) {
177 wfree(appdir);
178 return path;
181 return NULL;