Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wapplication.c
1
2 #include <unistd.h>
3
4 #include "WINGsP.h"
5
6 #include "wconfig.h"
7
8 #include "X11/Xlocale.h"
9
10 extern void W_InitNotificationCenter(void);
11
12 struct W_Application WMApplication;
13
14 char *_WINGS_progname = NULL;
15
16 Bool W_ApplicationInitialized(void)
17 {
18         return _WINGS_progname != NULL;
19 }
20
21 void WMInitializeApplication(char *applicationName, int *argc, char **argv)
22 {
23         int i;
24
25         assert(argc != NULL);
26         assert(argv != NULL);
27         assert(applicationName != NULL);
28
29         setlocale(LC_ALL, "");
30
31 #ifdef I18N
32         if (getenv("NLSPATH"))
33                 bindtextdomain("WINGs", getenv("NLSPATH"));
34         else
35                 bindtextdomain("WINGs", LOCALEDIR);
36         bind_textdomain_codeset("WINGs", "UTF-8");
37 #endif
38
39         _WINGS_progname = argv[0];
40
41         WMApplication.applicationName = wstrdup(applicationName);
42         WMApplication.argc = *argc;
43
44         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;
49
50         /* initialize notification center */
51         W_InitNotificationCenter();
52 }
53
54 void WMSetResourcePath(char *path)
55 {
56         if (WMApplication.resourcePath)
57                 wfree(WMApplication.resourcePath);
58         WMApplication.resourcePath = wstrdup(path);
59 }
60
61 char *WMGetApplicationName()
62 {
63         return WMApplication.applicationName;
64 }
65
66 static char *checkFile(char *path, char *folder, char *ext, char *resource)
67 {
68         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;
91 }
92
93 char *WMPathForResourceOfType(char *resource, char *ext)
94 {
95         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;
182 }