WPrefs: set default to 1st color in gradient texture
[wmaker-crm.git] / WINGs / wapplication.c
blob90874c06a16b4c70761e7964b39a5942e28ca964
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 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()
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 char *path, *tmp, *appdir;
135 int i;
136 size_t slen;
138 path = tmp = appdir = NULL;
141 * Paths are searched in this order:
142 * - resourcePath/ext
143 * - dirname(argv[0])/ext
144 * - GNUSTEP_USER_ROOT/Applications/ApplicationName.app/ext
145 * - ~/GNUstep/Applications/ApplicationName.app/ext
146 * - GNUSTEP_LOCAL_ROOT/Applications/ApplicationName.app/ext
147 * - /usr/local/GNUstep/Applications/ApplicationName.app/ext
148 * - GNUSTEP_SYSTEM_ROOT/Applications/ApplicationName.app/ext
149 * - /usr/GNUstep/Applications/ApplicationName.app/ext
152 if (WMApplication.resourcePath) {
153 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
154 if (path)
155 goto out;
158 if (WMApplication.argv[0]) {
159 tmp = wstrdup(WMApplication.argv[0]);
160 i = strlen(tmp);
161 while (i > 0 && tmp[i] != '/')
162 i--;
163 tmp[i] = 0;
164 if (i > 0) {
165 path = checkFile(tmp, NULL, ext, resource);
166 } else {
167 path = NULL;
169 goto out;
172 slen = strlen(WMApplication.applicationName) + sizeof("Applications/.app");
173 appdir = wmalloc(slen);
174 if (snprintf(appdir, slen, "Applications/%s.app", WMApplication.applicationName) >= slen)
175 goto out;
177 path = checkFile(getenv("GNUSTEP_USER_ROOT"), appdir, ext, resource);
178 if (path)
179 goto out;
181 path = checkFile(wusergnusteppath(), appdir, ext, resource);
182 if (path)
183 goto out;
185 path = checkFile(getenv("GNUSTEP_LOCAL_ROOT"), appdir, ext, resource);
186 if (path)
187 goto out;
189 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
190 if (path)
191 goto out;
193 path = checkFile(getenv("GNUSTEP_SYSTEM_ROOT"), appdir, ext, resource);
194 if (path)
195 goto out;
197 path = checkFile("/usr/GNUstep", appdir, ext, resource); /* falls through */
199 out:
200 if (tmp)
201 wfree(tmp);
202 if (appdir)
203 wfree(appdir);
205 return path;