dtcm: Resolve CID 87408
[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 librararies 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 "props.h"
60 #include "util.h"
61
62 /*******************************************************************************
63 **
64 **  Constants
65 **
66 *******************************************************************************/
67 static const char       *UPGRADED       = "Upgraded";
68 static const char       *CLASS_NAME     = "deskset";
69 static const char       *APP_NAME       = "calendar";
70 static const char       *RC_FILENAME    = "/.cm.rc";
71 static const char       *DS_FILENAME    = "/.desksetdefaults";
72 static const char       *OW_FILENAME    = "/lib/app-defaults/Deskset";
73 static const char       *X_FILENAME     = "/.Xdefaults";
74
75 /*
76 **  List of constant strings in ~/.desksetdefaults which we will pay attention
77 **  to ... if you add a member to this array, be sure to add it to the
78 **  enumerated type in props.h
79 */
80 static const char     *property_names[] = {
81                                 "BegOp",
82                                 "BeepOn",
83                                 "BeepAdvance",
84                                 "BeepUnit",
85                                 "FlashOn",
86                                 "FlashAdvance",
87                                 "FlashUnit",
88                                 "OpenOn",
89                                 "OpenAdvance",
90                                 "OpenUnit",
91                                 "MailOn",
92                                 "MailAdvance",
93                                 "MailUnit",
94                                 "MailTo",
95                                 "UnixOn",
96                                 "UnixAdvance",
97                                 "UnixCommand",
98                                 "DayBegin",
99                                 "DayEnd",
100                                 "CalendarList",
101                                 "DefaultView",
102                                 "DefaultDisplay",
103                                 "PrintDest",
104                                 "PrintPrivacy",
105                                 "PrinterName",
106                                 "PrintOptions",
107                                 "PrintDirName",
108                                 "PrintFileName",
109                                 "PrintRMargin",
110                                 "PrintBMargin",
111                                 "PrintLMargin",
112                                 "PrintTMargin",
113                                 "PrintLHeader",
114                                 "PrintRHeader",
115                                 "PrintLFooter",
116                                 "PrintRFooter",
117                                 "PrintUnit",
118                                 "PrintCopies",
119                                 "DefaultCal",
120                                 "Location",
121                                 "DateOrdering",
122                                 "DateSeparator",
123                                 "Privacy",
124                                 "UseFNS",
125                                 "ApptBegin",
126                                 "ApptDuration",
127                                 "EndOp"
128                         };
129
130 /*
131 **  List of constant strings containing hard-coded defaults.  The program will
132 **  also attempt to read some stuff from the environment if the hard-coded
133 **  default is NULL (example:  $PRINTER)
134 **
135 **  If you add a member to this array, be sure to add it above and to the
136 **  enumerated type in props.h
137 */
138 static const char       *def_props[] = {
139                                 "\0",                   /* BegOp */
140                                 "True",                 /* BeepOn */
141                                 "5",                    /* BeepAdvance */
142                                 "Mins",                 /* BeepUnit */
143                                 "False",                /* FlashOn */
144                                 "5",                    /* FlashAdvance */
145                                 "Mins",                 /* FlashUnit */
146                                 "True",                 /* OpenOn */
147                                 "5",                    /* OpenAdvance */
148                                 "Mins",                 /* OpenUnit */
149                                 "False",                /* MailOn */
150                                 "2",                    /* MainAdvance */
151                                 "Hrs",                  /* MailUnit */
152                                 "\0",                   /* MailTo */
153                                 "False",                /* UnixOn */
154                                 "0",                    /* UnixAdvance */
155                                 "\0",                   /* UnixCommand */
156                                 "7",                    /* DayBegin */
157                                 "19",                   /* DayEnd */
158                                 "\0",                   /* CalendarList */
159                                 "1",                    /* DefaultView */
160                                 "0",                    /* DefaultDisplay */
161                                 "0",                    /* PrintDest */
162                                 "7",                    /* PrintPrivacy */
163                                 "\0",                   /* PrinterName */
164                                 "\0",                   /* PrintOptions */
165                                 "\0",                   /* PrintDirName */
166                                 "calendar.ps",          /* PrintFileName */
167                                 "1.00 in",              /* PrintRMargin */
168                                 "1.00 in",              /* PrintBMargin */
169                                 "1.00 in",              /* PrintLMargin */
170                                 "1.00 in",              /* PrintTMargin */
171                                 "0",            /* PrintLHeader - Date */
172                                 "1",            /* PrintRHeader - User Id */
173                                 "2",            /* PrintLFooter - Page No */
174                                 "3",            /* PrintRFooter - Rpt Type */
175                                 "1",                    /* PrintUnit */
176                                 "1",                    /* PrintCopies */
177                                 "\0",                   /* DefaultCal */
178                                 "\0",                   /* CalendarLocation */
179                                 "0",                    /* DateOrdering */
180                                 "1",                    /* DateSeparator */
181                                 "Show Time And Text",   /* Privacy */
182                                 "False",                /* UseFNS */
183                                 "540",                  /* ApptBegin */
184                                 "60",                   /* ApptDuration */
185                                 "\0"                    /* EndOp */
186                         };
187
188 /*******************************************************************************
189 **
190 **  Functions static to props.c
191 **
192 *******************************************************************************/
193 static void
194 p_free_props_entries(Props_entry *e) {
195         if (!e)
196                 return;
197         if (e->next)
198                 p_free_props_entries(e->next);
199
200         if (e->property_name)
201                 free(e->property_name);
202         if (e->property_value)
203                 free(e->property_value);
204         free(e);
205 }
206
207 static Props_entry*
208 p_get_props_entry(Props *p, Props_op op) {
209         Props_op        idx = CP_BEGOP + 1;
210         Props_entry     *step = p->p_list;
211
212         while(step && (idx < op)) {
213                 step = step->next;
214                 ++idx;
215         }
216         return step;
217 }
218
219 /*******************************************************************************
220 **
221 **  Functions external to props.c
222 **
223 *******************************************************************************/
224 /*
225 **  Moving .cm.rc properties to .desksetdefaults
226 */
227 extern boolean_t
228 cal_convert_cmrc(Props *p) {
229         char            *c_ptr, fn[MAXPATHLEN], *val;
230         Props_op        op;
231         Resource        *cm_rdb = NULL;
232
233         if (!p->rdb)
234                 return B_FALSE;
235         if (get_resource(p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
236                 (char *)UPGRADED, NULL))
237                 return B_TRUE;
238
239         /*
240         **  If we're here, the Upgraded resource hasn't been set, so read
241         **  the old .cm.rc file then write it to .desksetdefaults.
242         */
243         if (getenv("HOME") != NULL)
244                 sprintf(fn, "%s%s", getenv("HOME"), RC_FILENAME);
245         else
246                 return B_TRUE;
247
248         if (!load_resources(&cm_rdb, fn))
249                 return B_TRUE;
250
251         for (op = CP_BEGOP + 1; op < CP_ENDOP; op++) {
252                 if (!(val = get_resource(cm_rdb, (char *)CLASS_NAME,
253                         (char *)APP_NAME, (char *)property_names[op], NULL)))
254                         continue;
255
256                 set_resource(&p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
257                         (char *)property_names[op], val);
258         }
259         set_resource(&p->rdb, (char *)CLASS_NAME, (char *)APP_NAME,
260                 (char *)UPGRADED, "True");
261         save_props(p);
262         free_resources(cm_rdb);
263
264         return B_TRUE;
265 }
266
267 extern char*
268 get_char_prop(Props *p, Props_op op) {
269         Props_entry     *step = p_get_props_entry(p, op);
270
271         if (!step || !step->property_value || step->property_value == '\0')
272                 return get_char_prop_default(op);
273         return (step->property_value);
274 }
275
276 extern char*
277 get_char_prop_default(Props_op op) {
278         char            *val, *ptr, *user, *host;
279
280         switch(op) {
281         case CP_MAILTO:
282                 user = (char *)cm_get_uname();
283                 host = (char *)cm_get_local_host();
284                 val = (char *)ckalloc(cm_strlen(user) + cm_strlen(host) + 2);
285                 sprintf(val, "%s@%s", user, host);
286                 break;
287         case CP_PRINTERNAME:
288                 val = cm_def_printer();
289                 break;
290         case CP_PRINTDIRNAME:
291                 if (getenv("HOME") != NULL)
292                         val = (char *)cm_strdup(getenv("HOME"));
293                 else
294                         val = (char *)cm_strdup("/");
295                 break;
296         case CP_DEFAULTCAL:
297                 user = (char *)cm_get_uname();
298                 host = (char *)cm_get_local_host();
299                 val = (char *)ckalloc(cm_strlen(user) + cm_strlen(host) + 2);
300                 sprintf(val, "%s@%s", user, host);
301                 break;
302         case CP_CALLOC:
303                 val = (char *)cm_strdup(cm_get_local_host());
304                 break;
305         default:
306                 val = (char *)cm_strdup((char *)def_props[op]);
307                 break;
308         }
309
310         return val;
311 }
312
313 extern int
314 get_int_prop(Props *p, Props_op op) {
315         Props_entry     *step = p_get_props_entry(p, op);
316
317         if (!step || !step->property_value || step->property_value == '\0')
318                 return (atoi(get_char_prop_default(op)));
319         return (atoi(step->property_value));
320 }
321
322 extern void
323 props_clean_up(Props *p) {
324         p_free_props_entries(p->p_list);
325         free_resources(p->rdb);
326 }
327
328 static void
329 init_props()
330 {
331 #define LIBDTCM_CAT "libdtcm"
332     static int          is_inited = 0;
333     nl_catd             libdtcm_catd;
334     const char          *dflt, *str;
335     OrderingType        ordering;
336
337     if (is_inited) return;
338
339     is_inited = 1;
340
341     libdtcm_catd = catopen(LIBDTCM_CAT, NL_CAT_LOCALE);
342     if ((nl_catd) -1 == libdtcm_catd) return;
343
344     dflt = def_props[CP_DATEORDERING];
345     str = catgets(libdtcm_catd, 1, 1, dflt);
346     def_props[CP_DATEORDERING] = strdup(str);
347
348     dflt = def_props[CP_DEFAULTDISP];
349     str = catgets(libdtcm_catd, 1, 2, dflt);
350     def_props[CP_DEFAULTDISP] = strdup(str);
351 }
352
353 extern boolean_t
354 read_props(Props *p) {
355         char            *resource, buf[MAXPATHLEN];
356         char            *home, *ow_home, *x_env, *ds_def;
357         Props_op        op;
358         Resource        *other_rdb = NULL;
359         Props_entry     *entry_ptr, *last = NULL;
360
361         init_props();
362
363         if (getenv("HOME") != NULL)
364                 home = getenv("HOME");
365         else
366                 home = "/";
367
368         ow_home = getenv("OPENWINHOME");
369         x_env = getenv("XENVIRONMENT");
370         ds_def = getenv("DESKSETDEFAULTS");
371
372         if (p->rdb)
373                 free_resources(p->rdb);
374         p->rdb = NULL;
375         if (ds_def)
376                 sprintf(buf, "%s", ds_def);
377         else
378                 sprintf(buf, "%s%s", home, DS_FILENAME);
379
380         load_resources(&p->rdb, buf);
381
382         if (ow_home) {
383                 sprintf(buf, "%s%s", ow_home, OW_FILENAME);
384                 load_resources(&other_rdb, buf);
385         }
386         if (home) {
387                 sprintf(buf, "%s%s", home, X_FILENAME);
388                 load_resources(&other_rdb, buf);
389         }
390         if (x_env) {
391                 sprintf(buf, "%s%s", x_env, X_FILENAME);
392                 load_resources(&other_rdb, buf);
393         }
394
395         p_free_props_entries(p->p_list);
396         for (op = CP_BEGOP + 1; op < CP_ENDOP; op++) {
397                 entry_ptr = (Props_entry *)ckalloc(sizeof(Props_entry));
398                 entry_ptr->property_name = (char *)
399                         cm_strdup((char *)property_names[op]);
400                 entry_ptr->update = B_FALSE;
401                 if (last)
402                         last->next = entry_ptr;
403                 else
404                         p->p_list = entry_ptr;
405                 entry_ptr->next = NULL;
406                 last = entry_ptr;
407
408                 resource = (char *)get_resource(p->rdb, (char *)CLASS_NAME,
409                         (char *)APP_NAME, entry_ptr->property_name, NULL);
410                 if (!resource || *resource == '\0')
411                         resource = (char *)get_resource(other_rdb,
412                                 (char *)CLASS_NAME, (char *)APP_NAME,
413                                 entry_ptr->property_name, NULL);
414                 if (!resource || *resource == '\0')
415                         entry_ptr->property_value = get_char_prop_default(op);
416                 else
417                         entry_ptr->property_value = (char *)cm_strdup(resource);
418         }
419         free_resources(other_rdb);
420         return B_TRUE;
421 }
422
423 extern boolean_t
424 save_props(Props *p)
425 {
426         char            buf[MAXPATHLEN], *ds_def = getenv("DESKSETDEFAULTS");
427         Props_entry     *p_ptr = p->p_list;
428
429         while(p_ptr) {
430                 if (p_ptr->update && p_ptr->property_value)
431                         set_resource(&p->rdb, (char *)CLASS_NAME,
432                                 (char *)APP_NAME, p_ptr->property_name,
433                                 p_ptr->property_value);
434                 p_ptr = p_ptr->next;
435         }
436
437         if (ds_def)
438                 sprintf(buf, "%s", ds_def);
439         else
440                 if (getenv("HOME") != NULL)
441                         sprintf(buf, "%s%s", getenv("HOME"), DS_FILENAME);
442                 else
443                         sprintf(buf, "/%s", DS_FILENAME);
444
445         return (save_resources(p->rdb, buf));
446 }        
447
448 extern boolean_t
449 set_char_prop(Props *p, Props_op op, char *value) {
450         Props_entry     *step = p_get_props_entry(p, op);
451
452         if (!step)
453                 return B_FALSE;
454
455         if (step->property_value)
456                 free(step->property_value);
457         step->property_value = (char *)cm_strdup(value);
458         step->update = B_TRUE;
459         return B_TRUE;
460 }
461
462 extern boolean_t
463 set_int_prop(Props *p, Props_op op, int value) {
464         char buf[MAXNAMELEN];
465
466         sprintf(buf, "%d", value);
467         return (set_char_prop(p, op, buf));
468 }