Update Serbian translation from master branch
[wmaker-crm.git] / util / getstyle.c
bloba29358929501b6007140aefc59a2e1cdf3b187a3
1 /* getstyle.c - outputs style related options from WindowMaker to stdout
3 * WindowMaker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifdef __GLIBC__
23 #define _GNU_SOURCE /* getopt_long */
24 #endif
26 #include "config.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <libgen.h>
35 #include <limits.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <unistd.h>
43 #ifdef HAVE_STDNORETURN
44 #include <stdnoreturn.h>
45 #endif
47 #include <WINGs/WUtil.h>
49 #include "common.h"
51 #ifndef PATH_MAX
52 #define PATH_MAX 1024
53 #endif
55 #include "../src/wconfig.h"
58 #define THEME_SUBPATH "/" PACKAGE_TARNAME "/Themes/"
59 #define THEME_EXTDIR ".themed/"
61 /* table of style related options */
62 static char *options[] = {
63 "TitleJustify",
64 "ClipTitleFont",
65 "WindowTitleFont",
66 "MenuTitleFont",
67 "MenuTextFont",
68 "IconTitleFont",
69 "LargeDisplayFont",
70 "HighlightColor",
71 "HighlightTextColor",
72 "ClipTitleColor",
73 "CClipTitleColor",
74 "FTitleColor",
75 "PTitleColor",
76 "UTitleColor",
77 "FTitleBack",
78 "PTitleBack",
79 "UTitleBack",
80 "ResizebarBack",
81 "MenuTitleColor",
82 "MenuTextColor",
83 "MenuDisabledColor",
84 "MenuTitleBack",
85 "MenuTextBack",
86 "IconBack",
87 "IconTitleColor",
88 "IconTitleBack",
89 "FrameBorderWidth",
90 "FrameBorderColor",
91 "FrameFocusedBorderColor",
92 "FrameSelectedBorderColor",
93 "MenuStyle",
94 "WindowTitleExtendSpace",
95 "MenuTitleExtendSpace",
96 "MenuTextExtendSpace",
97 NULL
100 /* table of theme related options */
101 static char *theme_options[] = {
102 "WorkspaceBack",
103 "NormalCursor",
104 "ArrowCursor",
105 "MoveCursor",
106 "ResizeCursor",
107 "TopLeftResizeCursor",
108 "TopRightResizeCursor",
109 "BottomLeftResizeCursor",
110 "BottomRightResizeCursor",
111 "VerticalResizeCursor",
112 "HorizontalResizeCursor",
113 "WaitCursor",
114 "QuestionCursor",
115 "TextCursor",
116 "SelectCursor",
117 NULL
120 /* table of style related fonts */
122 static char *font_options[] = {
123 "ClipTitleFont",
124 "WindowTitleFont",
125 "MenuTitleFont",
126 "MenuTextFont",
127 "IconTitleFont",
128 "LargeDisplayFont",
129 NULL
132 static const char *prog_name;
134 WMPropList *PixmapPath = NULL;
136 char *ThemePath = NULL;
139 static noreturn void print_help(int print_usage, int exitval)
141 printf("Usage: %s [-t] [-p] [-h] [-v] [file]\n", prog_name);
142 if (print_usage) {
143 puts("Retrieves style/theme configuration and outputs to "
144 "~/" GSUSER_SUBDIR "/" USERDATA_SUBDIR THEME_SUBPATH "file" THEME_EXTDIR "style"
145 " or to stdout");
146 puts("");
147 puts(" -h, --help display this help and exit");
148 puts(" -v, --version output version information and exit");
149 puts(" -t, --theme-options output theme related options when producing a style file");
150 puts(" -p, --pack produce output as a theme pack");
152 exit(exitval);
155 static Bool isFontOption(const char *option)
157 int i;
159 for (i = 0; font_options[i] != NULL; i++) {
160 if (strcasecmp(option, font_options[i]) == 0) {
161 return True;
165 return False;
168 static void findCopyFile(const char *dir, const char *file)
170 char *fullPath;
172 fullPath = wfindfileinarray(PixmapPath, file);
173 if (!fullPath) {
174 wwarning("Could not find file %s", file);
175 if (ThemePath)
176 (void)wrmdirhier(ThemePath);
177 return;
179 wcopy_file(dir, fullPath, file);
180 wfree(fullPath);
183 static void makeThemePack(WMPropList * style, const char *themeName)
185 WMPropList *keys;
186 WMPropList *key;
187 WMPropList *value;
188 int i;
189 size_t themeNameLen;
190 char *themeDir;
191 const char *user_library;
193 user_library = wuserdatapath();
194 if (user_library == NULL)
195 return;
196 themeNameLen = strlen(user_library) + sizeof(THEME_SUBPATH) + strlen(themeName) + sizeof(THEME_EXTDIR) + 1;
197 themeDir = wmalloc(themeNameLen);
198 snprintf(themeDir, themeNameLen,
199 "%s" THEME_SUBPATH "%s" THEME_EXTDIR,
200 user_library, themeName);
201 ThemePath = themeDir;
203 if (!wmkdirhier(themeDir)) {
204 wwarning("Could not make theme dir %s\n", themeDir);
205 return;
208 keys = WMGetPLDictionaryKeys(style);
210 for (i = 0; i < WMGetPropListItemCount(keys); i++) {
211 key = WMGetFromPLArray(keys, i);
213 value = WMGetFromPLDictionary(style, key);
214 if (value && WMIsPLArray(value) && WMGetPropListItemCount(value) > 2) {
215 WMPropList *type;
216 char *t;
218 type = WMGetFromPLArray(value, 0);
219 t = WMGetFromPLString(type);
220 if (t == NULL)
221 continue;
223 if (strcasecmp(t, "tpixmap") == 0 ||
224 strcasecmp(t, "spixmap") == 0 ||
225 strcasecmp(t, "cpixmap") == 0 ||
226 strcasecmp(t, "mpixmap") == 0 ||
227 strcasecmp(t, "tdgradient") == 0 ||
228 strcasecmp(t, "tvgradient") == 0 ||
229 strcasecmp(t, "thgradient") == 0) {
231 WMPropList *file;
232 char *p;
233 char *newPath;
235 file = WMGetFromPLArray(value, 1);
237 p = strrchr(WMGetFromPLString(file), '/');
238 if (p) {
239 newPath = wstrdup(p + 1);
241 wcopy_file(themeDir, WMGetFromPLString(file), newPath);
243 WMDeleteFromPLArray(value, 1);
244 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
245 free(newPath);
246 } else {
247 findCopyFile(themeDir, WMGetFromPLString(file));
249 } else if (strcasecmp(t, "bitmap") == 0) {
251 WMPropList *file;
252 char *p;
253 char *newPath;
255 file = WMGetFromPLArray(value, 1);
257 p = strrchr(WMGetFromPLString(file), '/');
258 if (p) {
259 newPath = wstrdup(p + 1);
261 wcopy_file(themeDir, WMGetFromPLString(file), newPath);
263 WMDeleteFromPLArray(value, 1);
264 WMInsertInPLArray(value, 1, WMCreatePLString(newPath));
265 free(newPath);
266 } else {
267 findCopyFile(themeDir, WMGetFromPLString(file));
270 file = WMGetFromPLArray(value, 2);
272 p = strrchr(WMGetFromPLString(file), '/');
273 if (p) {
274 newPath = wstrdup(p + 1);
276 wcopy_file(themeDir, WMGetFromPLString(file), newPath);
278 WMDeleteFromPLArray(value, 2);
279 WMInsertInPLArray(value, 2, WMCreatePLString(newPath));
280 free(newPath);
281 } else {
282 findCopyFile(themeDir, WMGetFromPLString(file));
287 WMReleasePropList(keys);
290 int main(int argc, char **argv)
292 WMPropList *prop, *style, *key, *val;
293 char *path;
294 int i, ch, theme_too = 0, make_pack = 0;
295 char *style_file = NULL;
297 struct option longopts[] = {
298 { "pack", no_argument, NULL, 'p' },
299 { "theme-options", no_argument, NULL, 't' },
300 { "version", no_argument, NULL, 'v' },
301 { "help", no_argument, NULL, 'h' },
302 { NULL, 0, NULL, 0 }
305 prog_name = argv[0];
306 while ((ch = getopt_long(argc, argv, "ptvh", longopts, NULL)) != -1)
307 switch(ch) {
308 case 'v':
309 printf("%s (Window Maker %s)\n", prog_name, VERSION);
310 return 0;
311 /* NOTREACHED */
312 case 'h':
313 print_help(1, 0);
314 /* NOTREACHED */
315 case 'p':
316 make_pack = 1;
317 theme_too = 1;
318 break;
319 case 't':
320 theme_too = 1;
321 case 0:
322 break;
323 default:
324 print_help(0, 1);
325 /* NOTREACHED */
328 /* At most one non-option ARGV-element is accepted (the theme name) */
329 if (argc - optind > 1)
330 print_help(0, 1);
332 if (argc - optind == 1)
333 style_file = argv[argc - 1];
335 if (make_pack && !style_file) {
336 printf("%s: you must supply a name for the theme pack\n", prog_name);
337 return 1;
340 WMPLSetCaseSensitive(False);
342 path = wdefaultspathfordomain("WindowMaker");
344 prop = WMReadPropListFromFile(path);
345 if (!prop) {
346 printf("%s: could not load WindowMaker configuration file \"%s\".\n", prog_name, path);
347 return 1;
350 /* get global value */
351 path = wglobaldefaultspathfordomain("WindowMaker");
353 val = WMReadPropListFromFile(path);
354 if (val) {
355 WMMergePLDictionaries(val, prop, True);
356 WMReleasePropList(prop);
357 prop = val;
360 style = WMCreatePLDictionary(NULL, NULL);
362 for (i = 0; options[i] != NULL; i++) {
363 key = WMCreatePLString(options[i]);
365 val = WMGetFromPLDictionary(prop, key);
366 if (val) {
367 WMRetainPropList(val);
368 if (isFontOption(options[i])) {
369 char *newfont, *oldfont;
371 oldfont = WMGetFromPLString(val);
372 newfont = convertFont(oldfont, False);
373 /* newfont is a reference to old if conversion is not needed */
374 if (newfont != oldfont) {
375 WMReleasePropList(val);
376 val = WMCreatePLString(newfont);
377 wfree(newfont);
380 WMPutInPLDictionary(style, key, val);
381 WMReleasePropList(val);
383 WMReleasePropList(key);
386 val = WMGetFromPLDictionary(prop, WMCreatePLString("PixmapPath"));
387 if (val)
388 PixmapPath = val;
390 if (theme_too) {
391 for (i = 0; theme_options[i] != NULL; i++) {
392 key = WMCreatePLString(theme_options[i]);
394 val = WMGetFromPLDictionary(prop, key);
395 if (val)
396 WMPutInPLDictionary(style, key, val);
400 if (make_pack) {
401 char *path;
403 makeThemePack(style, style_file);
405 path = wmalloc(strlen(ThemePath) + 32);
406 strcpy(path, ThemePath);
407 strcat(path, "/style");
408 WMWritePropListToFile(style, path);
409 wfree(path);
410 } else {
411 if (style_file) {
412 WMWritePropListToFile(style, style_file);
413 } else {
414 puts(WMGetPropListDescription(style, True));
417 return 0;