File: wapplication.c

package info (click to toggle)
wmaker 0.96.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,416 kB
  • sloc: ansic: 99,483; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (216 lines) | stat: -rw-r--r-- 4,938 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

#include <unistd.h>
#include <X11/Xlocale.h>

#include "WINGsP.h"
#include "wconfig.h"
#include "userdefaults.h"


struct W_Application WMApplication;

const char *_WINGS_progname = NULL;

Bool W_ApplicationInitialized(void)
{
	return _WINGS_progname != NULL;
}

void WMInitializeApplication(const char *applicationName, int *argc, char **argv)
{
	int i;

	assert(argc != NULL);
	assert(argv != NULL);
	assert(applicationName != NULL);

	setlocale(LC_ALL, "");

#ifdef I18N
	if (getenv("NLSPATH"))
		bindtextdomain("WINGs", getenv("NLSPATH"));
	else
		bindtextdomain("WINGs", LOCALEDIR);
	bind_textdomain_codeset("WINGs", "UTF-8");
#endif

	_WINGS_progname = argv[0];

	WMApplication.applicationName = wstrdup(applicationName);
	WMApplication.argc = *argc;

	WMApplication.argv = wmalloc((*argc + 1) * sizeof(char *));
	for (i = 0; i < *argc; i++) {
		WMApplication.argv[i] = wstrdup(argv[i]);
	}
	WMApplication.argv[i] = NULL;

	/* initialize notification center */
	W_InitNotificationCenter();
}

void WMReleaseApplication(void) {
	int i;

	/*
	 * We save the configuration on exit, this used to be handled
	 * through an 'atexit' registered function but if application
	 * properly calls WMReleaseApplication then the info to that
	 * will have been freed by us.
	 */
	w_save_defaults_changes();

	W_ReleaseNotificationCenter();

	if (WMApplication.applicationName) {
		wfree(WMApplication.applicationName);
		WMApplication.applicationName = NULL;
	}

	if (WMApplication.argv) {
		for (i = 0; i < WMApplication.argc; i++)
			wfree(WMApplication.argv[i]);

		wfree(WMApplication.argv);
		WMApplication.argv = NULL;
	}
}

void WMSetResourcePath(const char *path)
{
	if (WMApplication.resourcePath)
		wfree(WMApplication.resourcePath);
	WMApplication.resourcePath = wstrdup(path);
}

char *WMGetApplicationName(void)
{
	return WMApplication.applicationName;
}

static char *checkFile(const char *path, const char *folder, const char *ext, const char *resource)
{
	char *ret;
	int extralen;
	size_t slen;

	if (!path || !resource)
		return NULL;

	extralen = (ext ? strlen(ext) + 1 : 0) + (folder ? strlen(folder) + 1 : 0) + 1;
	slen = strlen(path) + strlen(resource) + 1 + extralen;
	ret = wmalloc(slen);

	if (wstrlcpy(ret, path, slen) >= slen)
		goto error;

	if (folder &&
		(wstrlcat(ret, "/", slen) >= slen ||
		 wstrlcat(ret, folder, slen) >= slen))
			goto error;

	if (ext &&
		(wstrlcat(ret, "/", slen) >= slen ||
		 wstrlcat(ret, ext, slen) >= slen))
			goto error;

	if (wstrlcat(ret, "/", slen) >= slen ||
	    wstrlcat(ret, resource, slen) >= slen)
		goto error;

	if (access(ret, F_OK) != 0)
		goto error;

	return ret;

error:
	if (ret)
		wfree(ret);
	return NULL;
}

char *WMPathForResourceOfType(const char *resource, const char *ext)
{
	const char *gslocapps, *gssysapps, *gsuserapps;
	char *path, *appdir;
	char buffer[PATH_MAX];
	size_t slen;

	path = appdir = NULL;

	/*
	 * Paths are searched in this order:
	 * - resourcePath/ext
	 * - dirname(argv[0])/ext
	 * - WMAKER_USER_ROOT/Applications/ApplicationName.app/ext
	 * - GNUSTEP_USER_APPS/ApplicationName.app/ext
	 * - GNUSTEP_LOCAL_APPS/ApplicationName.app/ext
	 * - /usr/local/lib/GNUstep/Applications/ApplicationName.app/ext
	 * - GNUSTEP_SYSTEM_APPS/ApplicationName.app/ext
	 * - /usr/lib/GNUstep/Applications/ApplicationName.app/ext
	 */

	if (WMApplication.resourcePath) {
		path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
		if (path)
			goto out;
	}

	if (WMApplication.argv[0]) {
		char *ptr_slash;

		ptr_slash = strrchr(WMApplication.argv[0], '/');
		if (ptr_slash != NULL) {
			char tmp[ptr_slash - WMApplication.argv[0] + 1];

			strncpy(tmp, WMApplication.argv[0], sizeof(tmp)-1);
			tmp[sizeof(tmp) - 1] = '\0';

			path = checkFile(tmp, NULL, ext, resource);
			if (path)
				goto out;
		}
	}

	snprintf(buffer, sizeof(buffer), "Applications/%s.app", WMApplication.applicationName);
	path = checkFile(GETENV("WMAKER_USER_ROOT"), buffer, ext, resource);
	if (path)
		goto out;

	slen = strlen(WMApplication.applicationName) + sizeof("/.app");
	appdir = wmalloc(slen);
	if (snprintf(appdir, slen, "/%s.app", WMApplication.applicationName) >= slen)
		goto out;

	gsuserapps = GETENV("GNUSTEP_USER_APPS");
	if (!gsuserapps) {
		snprintf(buffer, sizeof(buffer), "%s/Applications", wusergnusteppath());
		gsuserapps = buffer;
	}
	path = checkFile(gsuserapps, appdir, ext, resource);
	if (path)
		goto out;

	gslocapps = GETENV("GNUSTEP_LOCAL_APPS");
	if (!gslocapps)
		gslocapps = "/usr/local/lib/GNUstep/Applications";
	path = checkFile(gslocapps, appdir, ext, resource);
	if (path)
		goto out;

	gssysapps = GETENV("GNUSTEP_SYSTEM_APPS");
	if (!gssysapps)
		gssysapps = "/usr/lib/GNUstep/Applications";
	path = checkFile(gssysapps, appdir, ext, resource);
	if (path)
		goto out;

	path = checkFile("/usr/GNUstep/System/Applications", appdir, ext, resource); /* falls through */

out:
	if (appdir)
		wfree(appdir);

	return path;

}