Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wapplication.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
9d2e6ef9 scottc1998-09-29 22:36:29 +00002#include <unistd.h>
3
4#include "WINGsP.h"
5
882b9a8e kojima2001-07-23 20:31:32 +00006#include "wconfig.h"
7
8#include "X11/Xlocale.h"
9
9d2e6ef9 scottc1998-09-29 22:36:29 +000010extern void W_InitNotificationCenter(void);
11
9d2e6ef9 scottc1998-09-29 22:36:29 +000012struct W_Application WMApplication;
13
9d2e6ef9 scottc1998-09-29 22:36:29 +000014char *_WINGS_progname = NULL;
15
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020016Bool W_ApplicationInitialized(void)
9d2e6ef9 scottc1998-09-29 22:36:29 +000017{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020018 return _WINGS_progname != NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +000019}
20
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020021void WMInitializeApplication(char *applicationName, int *argc, char **argv)
9d2e6ef9 scottc1998-09-29 22:36:29 +000022{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020023 int i;
882b9a8e kojima2001-07-23 20:31:32 +000024
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020025 assert(argc != NULL);
26 assert(argv != NULL);
27 assert(applicationName != NULL);
9d2e6ef9 scottc1998-09-29 22:36:29 +000028
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020029 setlocale(LC_ALL, "");
c2ec1cfe dan2002-11-13 15:13:48 +000030
882b9a8e kojima2001-07-23 20:31:32 +000031#ifdef I18N
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020032 if (getenv("NLSPATH"))
33 bindtextdomain("WINGs", getenv("NLSPATH"));
34 else
35 bindtextdomain("WINGs", LOCALEDIR);
36 bind_textdomain_codeset("WINGs", "UTF-8");
882b9a8e kojima2001-07-23 20:31:32 +000037#endif
c2ec1cfe dan2002-11-13 15:13:48 +000038
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020039 _WINGS_progname = argv[0];
c2ec1cfe dan2002-11-13 15:13:48 +000040
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020041 WMApplication.applicationName = wstrdup(applicationName);
42 WMApplication.argc = *argc;
9d2e6ef9 scottc1998-09-29 22:36:29 +000043
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020044 WMApplication.argv = wmalloc((*argc + 1) * sizeof(char *));
45 for (i = 0; i < *argc; i++) {
46 WMApplication.argv[i] = wstrdup(argv[i]);
47 }
48 WMApplication.argv[i] = NULL;
c2ec1cfe dan2002-11-13 15:13:48 +000049
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020050 /* initialize notification center */
51 W_InitNotificationCenter();
9d2e6ef9 scottc1998-09-29 22:36:29 +000052}
53
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020054void WMSetResourcePath(char *path)
9d2e6ef9 scottc1998-09-29 22:36:29 +000055{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 if (WMApplication.resourcePath)
57 wfree(WMApplication.resourcePath);
58 WMApplication.resourcePath = wstrdup(path);
9d2e6ef9 scottc1998-09-29 22:36:29 +000059}
60
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020061char *WMGetApplicationName()
9d2e6ef9 scottc1998-09-29 22:36:29 +000062{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020063 return WMApplication.applicationName;
9d2e6ef9 scottc1998-09-29 22:36:29 +000064}
65
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020066static char *checkFile(char *path, char *folder, char *ext, char *resource)
9d2e6ef9 scottc1998-09-29 22:36:29 +000067{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020068 char *ret;
69 int extralen;
70
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);
77 }
78 if (ext) {
79 strcat(ret, "/");
80 strcat(ret, ext);
81 }
82 strcat(ret, "/");
83 strcat(ret, resource);
84
85 if (access(ret, F_OK) != 0) {
86 wfree(ret);
87 ret = NULL;
88 }
89
90 return ret;
9d2e6ef9 scottc1998-09-29 22:36:29 +000091}
92
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020093char *WMPathForResourceOfType(char *resource, char *ext)
9d2e6ef9 scottc1998-09-29 22:36:29 +000094{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020095 char *path = NULL;
96 char *tmp, *appdir;
97 int i;
98
99 /*
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
109 */
110
111 if (WMApplication.resourcePath) {
112 path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
113 if (path)
114 return path;
115 }
116
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;
127 }
128 wfree(tmp);
129 if (path)
130 return path;
131 }
132
133 appdir = wmalloc(strlen(WMApplication.applicationName) + 20);
134 sprintf(appdir, "Applications/%s.app", WMApplication.applicationName);
135
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;
141 }
142 }
143
144 tmp = wusergnusteppath();
145 if (tmp) {
146 path = checkFile(tmp, appdir, ext, resource);
147 if (path) {
148 wfree(appdir);
149 return path;
150 }
151 }
152
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;
158 }
159 }
160
161 path = checkFile("/usr/local/GNUstep", appdir, ext, resource);
162 if (path) {
163 wfree(appdir);
164 return path;
165 }
166
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;
172 }
173 }
174
175 path = checkFile("/usr/GNUstep", appdir, ext, resource);
176 if (path) {
177 wfree(appdir);
178 return path;
179 }
180
181 return NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000182}