Include screen.h in dialog.h for definition of WScreen
[wmaker-crm.git] / WINGs / wapplication.c
blob07164ec0b42ef98e9d772ed7b877c58d43413948
2 #include <unistd.h>
3 #include <X11/Xlocale.h>
5 #include "WINGsP.h"
6 #include "wconfig.h"
7 #include "userdefaults.h"
10 struct W_Application WMApplication;
12 const char *_WINGS_progname = NULL;
14 Bool W_ApplicationInitialized(void)
16 return _WINGS_progname != NULL;
19 void WMInitializeApplication(const 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 WMReleaseApplication(void) {
53 int i;
56 * We save the configuration on exit, this used to be handled
57 * through an 'atexit' registered function but if application
58 * properly calls WMReleaseApplication then the info to that
59 * will have been freed by us.
61 w_save_defaults_changes();
63 W_ReleaseNotificationCenter();
65 if (WMApplication.applicationName) {
66 wfree(WMApplication.applicationName);
67 WMApplication.applicationName = NULL;
70 if (WMApplication.argv) {
71 for (i = 0; i < WMApplication.argc; i++)
72 wfree(WMApplication.argv[i]);
74 wfree(WMApplication.argv);
75 WMApplication.argv = NULL;
79 void WMSetResourcePath(const char *path)
81 if (WMApplication.resourcePath)
82 wfree(WMApplication.resourcePath);
83 WMApplication.resourcePath = wstrdup(path);
86 char *WMGetApplicationName(void)
88 return WMApplication.applicationName;
91 static char *checkFile(const char *path, const char *folder, const char *ext, const char *resource)
93 char *ret;
94 int extralen;
95 size_t slen;
97 if (!path || !resource)
98 return NULL;
100 extralen = (ext ? strlen(ext) + 1 : 0) + (folder ? strlen(folder) + 1 : 0) + 1;
101 slen = strlen(path) + strlen(resource) + 1 + extralen;
102 ret = wmalloc(slen);
104 if (wstrlcpy(ret, path, slen) >= slen)
105 goto error;
107 if (folder &&
108 (wstrlcat(ret, "/", slen) >= slen ||
109 wstrlcat(ret, folder, slen) >= slen))
110 goto error;
112 if (ext &&
113 (wstrlcat(ret, "/", slen) >= slen ||
114 wstrlcat(ret, ext, slen) >= slen))
115 goto error;
117 if (wstrlcat(ret, "/", slen) >= slen ||
118 wstrlcat(ret, resource, slen) >= slen)
119 goto error;
121 if (access(ret, F_OK) != 0)
122 goto error;
124 return ret;
126 error:
127 if (ret)
128 wfree(ret);
129 return NULL;
132 char *WMPathForResourceOfType(const char *resource, const char *ext)
134 const char *gslocapps, *gssysapps, *gsuserapps;
135 char *path, *appdir;
136 char buffer[PATH_MAX];
137 size_t slen;
139 path = appdir = NULL;
142 * Paths are searched in this order:
143 * - resourcePath/ext
144 * - dirname(argv[0])/ext
145 * - WMAKER_USER_ROOT/Applications/ApplicationName.app/ext
146 * - GNUSTEP_USER_APPS/ApplicationName.app/ext
147 * - GNUSTEP_LOCAL_APPS/ApplicationName.app/ext
148 * - /usr/local/lib/GNUstep/Applications/ApplicationName.app/ext
149 * - GNUSTEP_SYSTEM_APPS/ApplicationName.app/ext
150 * - /usr/lib/GNUstep/Applications/ApplicationName.app/ext
153 if (WMApplication.resourcePath) {
154 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
155 if (path)
156 goto out;
159 if (WMApplication.argv[0]) {
160 char *ptr_slash;
162 ptr_slash = strrchr(WMApplication.argv[0], '/');
163 if (ptr_slash != NULL) {
164 char tmp[ptr_slash - WMApplication.argv[0] + 1];
166 strncpy(tmp, WMApplication.argv[0], sizeof(tmp)-1);
167 tmp[sizeof(tmp) - 1] = '\0';
169 path = checkFile(tmp, NULL, ext, resource);
170 if (path)
171 goto out;
175 snprintf(buffer, sizeof(buffer), "Applications/%s.app", WMApplication.applicationName);
176 path = checkFile(GETENV("WMAKER_USER_ROOT"), buffer, ext, resource);
177 if (path)
178 goto out;
180 slen = strlen(WMApplication.applicationName) + sizeof("/.app");
181 appdir = wmalloc(slen);
182 if (snprintf(appdir, slen, "/%s.app", WMApplication.applicationName) >= slen)
183 goto out;
185 gsuserapps = GETENV("GNUSTEP_USER_APPS");
186 if (!gsuserapps) {
187 snprintf(buffer, sizeof(buffer), "%s/Applications", wusergnusteppath());
188 gsuserapps = buffer;
190 path = checkFile(gsuserapps, appdir, ext, resource);
191 if (path)
192 goto out;
194 gslocapps = GETENV("GNUSTEP_LOCAL_APPS");
195 if (!gslocapps)
196 gslocapps = "/usr/local/lib/GNUstep/Applications";
197 path = checkFile(gslocapps, appdir, ext, resource);
198 if (path)
199 goto out;
201 gssysapps = GETENV("GNUSTEP_SYSTEM_APPS");
202 if (!gssysapps)
203 gssysapps = "/usr/lib/GNUstep/Applications";
204 path = checkFile(gssysapps, appdir, ext, resource);
205 if (path)
206 goto out;
208 path = checkFile("/usr/GNUstep/System/Applications", appdir, ext, resource); /* falls through */
210 out:
211 if (appdir)
212 wfree(appdir);
214 return path;