930a40c2a65e91361ccfb1b5dbc6756b2acd21a6
[oweals/cde.git] / cde / programs / dtcm / libDtCmP / props.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*******************************************************************************
24 **
25 **  props.c
26 **
27 **  $TOG: props.c /main/7 1998/08/11 16:33:35 mgreess $
28 **
29 **  RESTRICTED CONFIDENTIAL INFORMATION:
30 **
31 **  The information in this document is subject to special
32 **  restrictions in a confidential disclosure agreement between
33 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
35 **  Sun's specific written approval.  This document and all copies
36 **  and derivative works thereof must be returned or destroyed at
37 **  Sun's request.
38 **
39 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40 **
41 *******************************************************************************/
42
43 /*                                                                      *
44  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
45  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
46  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
47  * (c) Copyright 1993, 1994 Novell, Inc.                                *
48  */
49
50 #ifndef lint
51 static char sccsid[] = "@(#)props.c 1.13 94/11/07 Copyr 1991 Sun Microsystems, Inc.";
52 #endif
53
54 #include <EUSCompat.h>
55 #include <sys/param.h>
56 #include <nl_types.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include "props.h"
61 #include "util.h"
62
63 /*******************************************************************************
64 **
65 **  Constants
66 **
67 *******************************************************************************/
68 static const char       *UPGRADED       = "Upgraded";
69 static const char       *CLASS_NAME     = "deskset";
70 static const char       *APP_NAME       = "calendar";
71 static const char       *RC_FILENAME    = "/.cm.rc";
72 static const char       *DS_FILENAME    = "/.desksetdefaults";
73 static const char       *OW_FILENAME    = "/lib/app-defaults/Deskset";
74 static const char       *X_FILENAME     = "/.Xdefaults";
75
76 /*
77 **  List of constant strings in ~/.desksetdefaults which we will pay attention
78 **  to ... if you add a member to this array, be sure to add it to the
79 **  enumerated type in props.h
80 */
81 static const char     *property_names[] = {
82                                 "BegOp",
83                                 "BeepOn",
84                                 "BeepAdvance",
85                                 "BeepUnit",
86                                 "FlashOn",
87                                 "FlashAdvance",
88                                 "FlashUnit",
89                                 "OpenOn",
90                                 "OpenAdvance",
91                                 "OpenUnit",
92                                 "MailOn",
93                                 "MailAdvance",
94                                 "MailUnit",
95                                 "MailTo",
96                                 "UnixOn",
97                                 "UnixAdvance",
98                                 "UnixCommand",
99                                 "DayBegin",
100                                 "DayEnd",
101                                 "CalendarList",
102                                 "DefaultView",
103                                 "DefaultDisplay",
104                                 "PrintDest",
105                                 "PrintPrivacy",
106                                 "PrinterName",
107                                 "PrintOptions",
108                                 "PrintDirName",
109                                 "PrintFileName",
110                                 "PrintRMargin",
111                                 "PrintBMargin",
112                                 "PrintLMargin",
113                                 "PrintTMargin",
114                                 "PrintLHeader",
115                                 "PrintRHeader",
116                                 "PrintLFooter",
117                                 "PrintRFooter",
118                                 "PrintUnit",
119                                 "PrintCopies",
120                                 "DefaultCal",
121                                 "Location",
122                                 "DateOrdering",
123                                 "DateSeparator",
124                                 "Privacy",
125                                 "UseFNS",
126                                 "ApptBegin",
127                                 "ApptDuration",
128                                 "EndOp"
129                         };
130
131 /*
132 **  List of constant strings containing hard-coded defaults.  The program will
133 **  also attempt to read some stuff from the environment if the hard-coded
134 **  default is NULL (example:  $PRINTER)
135 **
136 **  If you add a member to this array, be sure to add it above and to the
137 **  enumerated type in props.h
138 */
139 static const char       *def_props[] = {
140                                 "\0",                   /* BegOp */
141                                 "True",                 /* BeepOn */
142                                 "5",                    /* BeepAdvance */
143                                 "Mins",                 /* BeepUnit */
144                                 "False",                /* FlashOn */
145                                 "5",                    /* FlashAdvance */
146                                 "Mins",                 /* FlashUnit */
147                                 "True",                 /* OpenOn */
148                                 "5",                    /* OpenAdvance */
149                                 "Mins",                 /* OpenUnit */
150                                 "False",                /* MailOn */
151                                 "2",                    /* MainAdvance */
152                                 "Hrs",                  /* MailUnit */
153                                 "\0",                   /* MailTo */
154                                 "False",                /* UnixOn */
155                                 "0",                    /* UnixAdvance */
156                                 "\0",                   /* UnixCommand */
157                                 "7",                    /* DayBegin */
158                                 "19",                   /* DayEnd */
159                                 "\0",                   /* CalendarList */
160                                 "1",                    /* DefaultView */
161                                 "0",                    /* DefaultDisplay */
162                                 "0",                    /* PrintDest */
163                                 "7",                    /* PrintPrivacy */
164                                 "\0",                   /* PrinterName */
165                                 "\0",                   /* PrintOptions */
166                                 "\0",                   /* PrintDirName */
167                                 "calendar.ps",          /* PrintFileName */
168                                 "1.00 in",              /* PrintRMargin */
169                                 "1.00 in",              /* PrintBMargin */
170                                 "1.00 in",              /* PrintLMargin */
171                                 "1.00 in",              /* PrintTMargin */
172                                 "0",            /* PrintLHeader - Date */
173                                 "1",            /* PrintRHeader - User Id */
174                                 "2",            /* PrintLFooter - Page No */
175                                 "3",            /* PrintRFooter - Rpt Type */
176                                 "1",                    /* PrintUnit */
177                                 "1",                    /* PrintCopies */
178                                 "\0",                   /* DefaultCal */
179                                 "\0",                   /* CalendarLocation */
180                                 "0",                    /* DateOrdering */
181                                 "1",                    /* DateSeparator */
182                                 "Show Time And Text",   /* Privacy */
183                                 "False",                /* UseFNS */
184                                 "540",                  /* ApptBegin */
185                                 "60",                   /* ApptDuration */
186                                 "\0"                    /* EndOp */
187                         };
188
189 /*******************************************************************************
190 **
191 **  Functions static to props.c
192 **
193 *******************************************************************************/
194 static void
195 p_free_props_entries(Props_entry *e) {
196         if (!e)
197                 return;
198         if (e->next)
199                 p_free_props_entries(e->next);
200
201         if (e->property_name)
202                 free(e->property_name);
203         if (e->property_value)
204                 free(e->property_value);
205         free(e);
206 }
207
208 static Props_entry*
209 p_get_props_entry(Props *p, Props_op op) {
210         Props_op        idx = CP_BEGOP + 1;
211         Props_entry     *step = p->p_list;
212
213         while(step && (idx < op)) {
214                 step = step->next;
215                 ++idx;
216         }
217         return step;
218 }
219
220 /*******************************************************************************
221 **
222 **  Functions external to props.c
223 **
224 *******************************************************************************/
225 /*
226 **  Moving .cm.rc properties to .desksetdefaults
227 */
228 extern boolean_t
229 cal_convert_cmrc(Props *p) {
230         char            *c_ptr, fn[MAXPATHLEN], *val;
231         Props_op        op;
232         Resource        *cm_rdb = NULL;
233
234         if (!p->rdb)
235                 return B_FALSE;
236         if (get_resource(p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
237                 (char *)UPGRADED, NULL))
238                 return B_TRUE;
239
240         /*
241         **  If we're here, the Upgraded resource hasn't been set, so read
242         **  the old .cm.rc file then write it to .desksetdefaults.
243         */
244         if (getenv("HOME") != NULL)
245                 sprintf(fn, "%s%s", getenv("HOME"), RC_FILENAME);
246         else
247                 return B_TRUE;
248
249         if (!load_resources(&cm_rdb, fn))
250                 return B_TRUE;
251
252         for (op = CP_BEGOP + 1; op < CP_ENDOP; op++) {
253                 if (!(val = get_resource(cm_rdb, (char *)CLASS_NAME,
254                         (char *)APP_NAME, (char *)property_names[op], NULL)))
255                         continue;
256
257                 set_resource(&p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
258                         (char *)property_names[op], val);
259         }
260         set_resource(&p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
261                 (char *)UPGRADED, "True");
262         save_props(p);
263         free_resources(cm_rdb);
264
265         return B_TRUE;
266 }
267
268 extern char*
269 get_char_prop(Props *p, Props_op op) {
270         Props_entry     *step = p_get_props_entry(p, op);
271
272         if (!step || !step->property_value || step->property_value == '\0')
273                 return get_char_prop_default(op);
274         return (step->property_value);
275 }
276
277 extern char*
278 get_char_prop_default(Props_op op) {
279         char            *val, *ptr, *user, *host;
280
281         switch(op) {
282         case CP_MAILTO:
283                 user = (char *)cm_get_uname();
284                 host = (char *)cm_get_local_host();
285                 val = (char *)ckalloc(cm_strlen(user) + cm_strlen(host) + 2);
286                 sprintf(val, "%s@%s", user, host);
287                 break;
288         case CP_PRINTERNAME:
289                 val = cm_def_printer();
290                 break;
291         case CP_PRINTDIRNAME:
292                 if (getenv("HOME") != NULL)
293                         val = (char *)cm_strdup(getenv("HOME"));
294                 else
295                         val = (char *)cm_strdup("/");
296                 break;
297         case CP_DEFAULTCAL:
298                 user = (char *)cm_get_uname();
299                 host = (char *)cm_get_local_host();
300                 val = (char *)ckalloc(cm_strlen(user) + cm_strlen(host) + 2);
301                 sprintf(val, "%s@%s", user, host);
302                 break;
303         case CP_CALLOC:
304                 val = (char *)cm_strdup(cm_get_local_host());
305                 break;
306         default:
307                 val = (char *)cm_strdup((char *)def_props[op]);
308                 break;
309         }
310
311         return val;
312 }
313
314 extern int
315 get_int_prop(Props *p, Props_op op) {
316         Props_entry     *step = p_get_props_entry(p, op);
317
318         if (!step || !step->property_value || step->property_value == '\0')
319                 return (atoi(get_char_prop_default(op)));
320         return (atoi(step->property_value));
321 }
322
323 extern void
324 props_clean_up(Props *p) {
325         p_free_props_entries(p->p_list);
326         free_resources(p->rdb);
327 }
328
329 static void
330 init_props(void)
331 {
332 #define LIBDTCM_CAT "libdtcm"
333     static int          is_inited = 0;
334     nl_catd             libdtcm_catd;
335     const char          *dflt, *str;
336     OrderingType        ordering;
337
338     if (is_inited) return;
339
340     is_inited = 1;
341
342     libdtcm_catd = catopen(LIBDTCM_CAT, NL_CAT_LOCALE);
343     if ((nl_catd) -1 == libdtcm_catd) return;
344
345     dflt = def_props[CP_DATEORDERING];
346     str = catgets(libdtcm_catd, 1, 1, dflt);
347     def_props[CP_DATEORDERING] = strdup(str);
348
349     dflt = def_props[CP_DEFAULTDISP];
350     str = catgets(libdtcm_catd, 1, 2, dflt);
351     def_props[CP_DEFAULTDISP] = strdup(str);
352 }
353
354 extern boolean_t
355 read_props(Props *p) {
356         char            *resource, buf[MAXPATHLEN];
357         char            *home, *ow_home, *x_env, *ds_def;
358         Props_op        op;
359         Resource        *other_rdb = NULL;
360         Props_entry     *entry_ptr, *last = NULL;
361
362         init_props();
363
364         if (getenv("HOME") != NULL)
365                 home = getenv("HOME");
366         else
367                 home = "/";
368
369         ow_home = getenv("OPENWINHOME");
370         x_env = getenv("XENVIRONMENT");
371         ds_def = getenv("DESKSETDEFAULTS");
372
373         if (p->rdb)
374                 free_resources(p->rdb);
375         p->rdb = NULL;
376         if (ds_def)
377                 sprintf(buf, "%s", ds_def);
378         else
379                 sprintf(buf, "%s%s", home, DS_FILENAME);
380
381         load_resources(&p->rdb, buf);
382
383         if (ow_home) {
384                 sprintf(buf, "%s%s", ow_home, OW_FILENAME);
385                 load_resources(&other_rdb, buf);
386         }
387         if (home) {
388                 sprintf(buf, "%s%s", home, X_FILENAME);
389                 load_resources(&other_rdb, buf);
390         }
391         if (x_env) {
392                 sprintf(buf, "%s%s", x_env, X_FILENAME);
393                 load_resources(&other_rdb, buf);
394         }
395
396         p_free_props_entries(p->p_list);
397         for (op = CP_BEGOP + 1; op < CP_ENDOP; op++) {
398                 entry_ptr = (Props_entry *)ckalloc(sizeof(Props_entry));
399                 entry_ptr->property_name = (char *)
400                         cm_strdup((char *)property_names[op]);
401                 entry_ptr->update = B_FALSE;
402                 if (last)
403                         last->next = entry_ptr;
404                 else
405                         p->p_list = entry_ptr;
406                 entry_ptr->next = NULL;
407                 last = entry_ptr;
408
409                 resource = (char *)get_resource(p->rdb, (char *)CLASS_NAME,
410                         (char *)APP_NAME, entry_ptr->property_name, NULL);
411                 if (!resource || *resource == '\0')
412                         resource = (char *)get_resource(other_rdb,
413                                 (char *)CLASS_NAME, (char *)APP_NAME,
414                                 entry_ptr->property_name, NULL);
415                 if (!resource || *resource == '\0')
416                         entry_ptr->property_value = get_char_prop_default(op);
417                 else
418                         entry_ptr->property_value = (char *)cm_strdup(resource);
419         }
420         free_resources(other_rdb);
421         return B_TRUE;
422 }
423
424 extern boolean_t
425 save_props(Props *p)
426 {
427         char            buf[MAXPATHLEN], *ds_def = getenv("DESKSETDEFAULTS");
428         Props_entry     *p_ptr = p->p_list;
429
430         while(p_ptr) {
431                 if (p_ptr->update && p_ptr->property_value)
432                         set_resource(&p->rdb, (char *)CLASS_NAME,
433                                 (char *)APP_NAME, p_ptr->property_name,
434                                 p_ptr->property_value);
435                 p_ptr = p_ptr->next;
436         }
437
438         if (ds_def)
439                 snprintf(buf, MAXPATHLEN, "%s", ds_def);
440         else
441                 if (getenv("HOME") != NULL)
442                     snprintf(buf, MAXPATHLEN, "%s%s",
443                              getenv("HOME"), DS_FILENAME);
444                 else
445                     snprintf(buf, MAXPATHLEN, "/%s", DS_FILENAME);
446
447         return (save_resources(p->rdb, buf));
448 }        
449
450 extern boolean_t
451 set_char_prop(Props *p, Props_op op, char *value) {
452         Props_entry     *step = p_get_props_entry(p, op);
453
454         if (!step)
455                 return B_FALSE;
456
457         if (step->property_value)
458                 free(step->property_value);
459         step->property_value = (char *)cm_strdup(value);
460         step->update = B_TRUE;
461         return B_TRUE;
462 }
463
464 extern boolean_t
465 set_int_prop(Props *p, Props_op op, int value) {
466         char buf[MAXNAMELEN];
467
468         sprintf(buf, "%d", value);
469         return (set_char_prop(p, op, buf));
470 }