Improve dockapp recognition
[wmaker-crm.git] / WINGs / wapplication.c
blob620e378bae7e5a92af77cc117f866f23e451a1b8
2 #include <unistd.h>
3 #include <X11/Xlocale.h>
5 #include "WINGsP.h"
6 #include "wconfig.h"
8 extern void W_InitNotificationCenter(void);
10 struct W_Application WMApplication;
12 char *_WINGS_progname = NULL;
14 Bool W_ApplicationInitialized(void)
16 return _WINGS_progname != NULL;
19 void WMInitializeApplication(char *applicationName, int *argc, char **argv)
21 int i;
23 assert(argc != NULL);
24 assert(argv != NULL);
25 assert(applicationName != NULL);
27 setlocale(LC_ALL, "");
29 #ifdef I18N
30 if (getenv("NLSPATH"))
31 bindtextdomain("WINGs", getenv("NLSPATH"));
32 else
33 bindtextdomain("WINGs", LOCALEDIR);
34 bind_textdomain_codeset("WINGs", "UTF-8");
35 #endif
37 _WINGS_progname = argv[0];
39 WMApplication.applicationName = wstrdup(applicationName);
40 WMApplication.argc = *argc;
42 WMApplication.argv = wmalloc((*argc + 1) * sizeof(char *));
43 for (i = 0; i < *argc; i++) {
44 WMApplication.argv[i] = wstrdup(argv[i]);
46 WMApplication.argv[i] = NULL;
48 /* initialize notification center */
49 W_InitNotificationCenter();
52 void WMSetResourcePath(char *path)
54 if (WMApplication.resourcePath)
55 wfree(WMApplication.resourcePath);
56 WMApplication.resourcePath = wstrdup(path);
59 char *WMGetApplicationName()
61 return WMApplication.applicationName;
64 static char *checkFile(char *path, char *folder, char *ext, char *resource)
66 char *ret;
67 int extralen;
69 extralen = (ext ? strlen(ext) : 0) + (folder ? strlen(folder) : 0) + 4;
70 ret = wmalloc(strlen(path) + strlen(resource) + extralen + 8);
71 strcpy(ret, path);
72 if (folder) {
73 strcat(ret, "/");
74 strcat(ret, folder);
76 if (ext) {
77 strcat(ret, "/");
78 strcat(ret, ext);
80 strcat(ret, "/");
81 strcat(ret, resource);
83 if (access(ret, F_OK) != 0) {
84 wfree(ret);
85 ret = NULL;
88 return ret;
91 char *WMPathForResourceOfType(char *resource, char *ext)
93 char *path = NULL;
94 char *tmp, *appdir;
95 int i;
98 * Paths are searched in this order:
99 * - resourcePath/ext
100 * - argv[0]/ext
101 * - GNUSTEP_USER_ROOT/Applications/ApplicationName.app/ext
102 * - ~/GNUstep/Applications/ApplicationName.app/ext
103 * - GNUSTEP_LOCAL_ROOT/Applications/ApplicationName.app/ext
104 * - /usr/local/GNUstep/Applications/ApplicationName.app/ext
105 * - GNUSTEP_SYSTEM_ROOT/Applications/ApplicationName.app/ext
106 * - /usr/GNUstep/Applications/ApplicationName.app/ext
109 if (WMApplication.resourcePath) {
110 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
111 if (path)
112 return path;
115 if (WMApplication.argv[0]) {
116 tmp = wstrdup(WMApplication.argv[0]);
117 i = strlen(tmp);
118 while (i > 0 && tmp[i] != '/')
119 i--;
120 tmp[i] = 0;
121 if (i > 0) {
122 path = checkFile(tmp, NULL, ext, resource);
123 } else {
124 path = NULL;
126 wfree(tmp);
127 if (path)
128 return path;
131 appdir = wmalloc(strlen(WMApplication.applicationName) + 20);
132 sprintf(appdir, "Applications/%s.app", WMApplication.applicationName);
134 if (getenv("GNUSTEP_USER_ROOT")) {
135 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
136 if (path) {
137 wfree(appdir);
138 return path;
142 tmp = wusergnusteppath();
143 if (tmp) {
144 path = checkFile(tmp, appdir, ext, resource);
145 if (path) {
146 wfree(appdir);
147 return path;
151 if (getenv("GNUSTEP_LOCAL_ROOT")) {
152 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
153 if (path) {
154 wfree(appdir);
155 return path;
159 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
160 if (path) {
161 wfree(appdir);
162 return path;
165 if (getenv("GNUSTEP_SYSTEM_ROOT")) {
166 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
167 if (path) {
168 wfree(appdir);
169 return path;
173 path = checkFile("/usr/GNUstep", appdir, ext, resource);
174 if (path) {
175 wfree(appdir);
176 return path;
179 return NULL;