docbook.tcl, instant: finish remaining help generation issues with tcl
[oweals/cde.git] / cde / programs / dtpdm / PdmXp.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 /* $XConsortium: PdmXp.c /main/4 1996/08/12 18:43:03 cde-hp $ */
24 /*
25  * dtpdm/PdmXp.c
26  */
27 /*
28  * (c) Copyright 1996 Digital Equipment Corporation.
29  * (c) Copyright 1996 Hewlett-Packard Company.
30  * (c) Copyright 1996 International Business Machines Corp.
31  * (c) Copyright 1996 Sun Microsystems, Inc.
32  * (c) Copyright 1996 Novell, Inc. 
33  * (c) Copyright 1996 FUJITSU LIMITED.
34  * (c) Copyright 1996 Hitachi.
35  */
36 #include <stdio.h>
37 #include <sys/stat.h>
38 #include "PdmXp.h"
39
40 #include <X11/Intrinsic.h>
41
42 typedef enum {
43     PDMXP_JOB, PDMXP_DOC, PDMXP_PRINTER, PDMXP_SERVER,
44     PDMXP_BAD_POOL /* should always be last in list */
45 } PdmXpPoolIndex;
46
47
48 /*
49  * static function declarations
50  */
51 static const char* PdmXpGetQualifier(PdmXp* me);
52 static char* PdmXpBuildResourceName(PdmXp* me, PdmOid id_att);
53 #if 0 && defined(PRINTING_SUPPORTED)
54 static XrmDatabase PdmXpLoadPool(PdmXp* me, XPAttributes type);
55 #endif /* PRINTING_SUPPORTED */
56
57
58 /*
59  * ------------------------------------------------------------------------
60  * Name: PdmXpNew
61  *
62  * Description:
63  *
64  *     Creates a new PdmXp instance structure.
65  *
66  * Return value:
67  *
68  *     The new PdmXp instance structure.
69  *
70  */
71 PdmXp*
72 PdmXpNew(void)
73 {
74     return (PdmXp*)XtCalloc(1, sizeof(PdmXp));
75 }
76
77 /*
78  * ------------------------------------------------------------------------
79  * Name: PdmXpDelete
80  *
81  * Description:
82  *
83  *     Closes an existing Xp connection, and frees the passed
84  *     PdmXp instance structure.
85  *
86  * Return value:
87  *
88  *     None.
89  *
90  */
91 void
92 PdmXpDelete(PdmXp* me)
93 {
94     PdmXpClose(me);
95     XtFree((char*)me);
96 }
97
98 /*
99  * ------------------------------------------------------------------------
100  * Name: PdmXpOpen
101  *
102  * Description:
103  *
104  *     This function opens the passed print display specifier and sets
105  *     the passed print context on the newly opened print display.
106  *
107  * Return value:
108  *
109  *     If successful, the print display pointer is returned. If unable to
110  *     open the display, or if the display does not support the Xp
111  *     extension, NULL is returned.
112  *
113  */
114 Display*
115 PdmXpOpen(PdmXp* me,
116           char* display_spec,
117           char* context_str)
118 {
119     /*
120      * only maintain one connection
121      */
122     PdmXpClose(me);
123     /*
124      * open the passed display spec
125      */
126     me->display = XOpenDisplay(display_spec);
127     if(me->display)
128     {
129         int error_base;
130         int event_base;
131         /*
132          * check to see if the display is a print server
133          */
134 #if 0 && defined(PRINTING_SUPPORTED)
135         if(XpQueryExtension(me->display, &event_base, &error_base))
136         {
137             /*
138              * set the passed print context on the print display
139              */
140             me->context = strtoul(context_str, (char**)NULL, 0);
141             /*
142              * load the resource DB qualifier
143              */
144             PdmXpGetQualifier(me);
145         }
146         else
147         {
148 #endif /* PRINTING_SUPPORTED */
149             XCloseDisplay(me->display);
150             me->display = (Display*)NULL;
151 #if 0 && defined(PRINTING_SUPPORTED)
152         }
153 #endif /* PRINTING_SUPPORTED */
154     }
155
156     return me->display;
157 }
158
159 /*
160  * ------------------------------------------------------------------------
161  * Name: PdmXpClose
162  *
163  * Description:
164  *
165  *     Closes the print display.
166  *
167  * Return value:
168  *
169  *     None.
170  *
171  */
172 void
173 PdmXpClose(PdmXp* me)
174 {
175     if(me->display)
176     {
177         int i;
178         
179         for(i = 0; i < PDMXP_POOL_COUNT; i++)
180         {
181             if(me->pool[i] != (XrmDatabase)NULL)
182             {
183                 XrmDestroyDatabase(me->pool[i]);
184                 me->pool[i] = (XrmDatabase)NULL;
185             }
186         }
187         XCloseDisplay(me->display);
188         me->display = NULL;
189 #if 0 && defined(PRINTING_SUPPORTED)
190         me->context = (XPContext)NULL;
191 #endif /* PRINTING_SUPPORTED */
192     }
193 }
194
195 /*
196  * ------------------------------------------------------------------------
197  * Name: PdmXpLoadPool
198  *
199  * Description:
200  *
201  *     
202  *
203  * Return value:
204  *
205  *     
206  *
207  */
208 #if 0 && defined(PRINTING_SUPPORTED)
209 static XrmDatabase
210 PdmXpLoadPool(PdmXp* me, XPAttributes type)
211 {
212     PdmXpPoolIndex i;
213     /*
214      * determine the index into the pool array based on the Xp pool type
215      */
216     switch(type)
217     {
218     case XPJobAttr:
219         i = PDMXP_JOB;
220         break;
221     case XPDocAttr:
222         i = PDMXP_DOC;
223         break;
224     case XPPrinterAttr:
225         i = PDMXP_PRINTER;
226         break;
227     case XPServerAttr:
228         i = PDMXP_SERVER;
229         break;
230     default:
231         return (XrmDatabase)NULL;
232         break;
233     }
234     /*
235      * get the attributes from the X print server
236      */
237     if(me->pool[i] == (XrmDatabase)NULL)
238     {
239         XTextProperty text_prop;
240         char** list;
241         int count;
242         
243         text_prop.value = (unsigned char*)
244             XpGetAttributes(me->display, me->context, type);
245         text_prop.encoding = XInternAtom(me->display, "COMPOUND_TEXT", False);
246         text_prop.format = 8;
247         text_prop.nitems = strlen((char*)text_prop.value);
248         if(Success ==
249            XmbTextPropertyToTextList(me->display, &text_prop, &list, &count))
250         {
251             if(count > 0)
252                 me->pool[i] = XrmGetStringDatabase(list[0]);
253
254             XFreeStringList(list);          
255         }
256     }
257
258     return me->pool[i];
259 }
260 #endif /* PRINTING_SUPPORTED */
261
262 /*
263  * ------------------------------------------------------------------------
264  * Name: PdmXpGetQualifier
265  *
266  * Description:
267  *
268  *     
269  *
270  * Return value:
271  *
272  *
273  */
274 static const char*
275 PdmXpGetQualifier(PdmXp* me)
276 {
277     if(me->qualifier == (char*)NULL)
278     {
279 #if 0 && defined(PRINTING_SUPPORTED)
280         if(PdmXpLoadPool(me, XPPrinterAttr) != (XrmDatabase)NULL)
281         {
282             char* str_type;
283             XrmValue value;
284             
285             if(XrmGetResource(me->pool[PDMXP_PRINTER],
286                               "qualifier", "qualifier", &str_type, &value))
287             {
288                 me->qualifier = XtNewString((char*)value.addr);
289                 me->qualifier_len = strlen(me->qualifier);
290             }
291         }
292 #endif /* PRINTING_SUPPORTED */
293     }
294     return me->qualifier;
295 }
296
297
298
299 /*
300  * ------------------------------------------------------------------------
301  * Name: PdmXpBuildResourceName
302  *
303  * Description:
304  *
305  *     
306  *
307  * Return value:
308  *
309  *     A new fully-qualified resource name. It is the caller's
310  *     responsibility to free the returned string by calling XtFree.
311  *
312  */
313 static char*
314 PdmXpBuildResourceName(PdmXp* me, PdmOid id_att)
315 {
316     char* ptr;
317     char* res_name;
318     int oid_str_len;
319     /*
320      * allocate memory for the resource name
321      */
322     oid_str_len = PdmOidStringLength(id_att);
323     ptr = res_name =
324         XtMalloc(me->qualifier_len + 1 + oid_str_len + 1);
325     /*
326      * build the resource name from the printer name and the string value
327      * for the passed attribute id
328      */
329     strncpy(ptr, me->qualifier, me->qualifier_len);
330     ptr += me->qualifier_len;
331     *ptr = '.';
332     ptr += 1;
333     strncpy(ptr, PdmOidString(id_att), oid_str_len);
334     ptr += oid_str_len;
335     *ptr = '\0';
336     /*
337      * return
338      */
339     return res_name;
340 }
341
342
343 /*
344  * ------------------------------------------------------------------------
345  * Name: PdmXpGetValue
346  *
347  * Description:
348  *
349  *     
350  *
351  * Return value:
352  *
353  *     pdmoid_none if the attribute value is not found.
354  *
355  */
356 #if 0 && defined(PRINTING_SUPPORTED)
357 PdmOid
358 PdmXpGetValue(PdmXp* me,
359               XPAttributes type,
360               PdmOid id_att)
361 {
362     const char* value;
363     
364     value = PdmXpGetStringValue(me, type, id_att);
365
366     return PdmOidFromString(value);
367 }
368 #endif /* PRINTING_SUPPORTED */
369
370 /*
371  * ------------------------------------------------------------------------
372  * Name: PdmXpGetStringValue
373  *
374  * Description:
375  *
376  *     
377  *
378  * Return value:
379  *
380  *     NULL if the attribute value is not found, or if the resource
381  *     representation type is not a string.
382  *
383  */
384 #if 0 && defined(PRINTING_SUPPORTED)
385 const char*
386 PdmXpGetStringValue(PdmXp* me,
387                     XPAttributes type,
388                     PdmOid id_att)
389 {
390     char* res_name;
391     char* str_type;
392     XrmValue value;
393     Bool found;
394     
395     XrmDatabase pool;
396     
397     pool = PdmXpLoadPool(me, type);
398     if(pool == (XrmDatabase)NULL)
399         return (const char*)NULL;
400
401     res_name = PdmXpBuildResourceName(me, id_att);
402     found = XrmGetResource(pool, res_name, res_name, &str_type, &value);
403     XtFree(res_name);
404     
405     /*
406      * return
407      */
408     if(found)
409         return (const char*)value.addr;
410     else
411         return (const char*)NULL;
412 }
413 #endif /* PRINTING_SUPPORTED */
414
415 /*
416  * ------------------------------------------------------------------------
417  * Name: PdmXpSetValue
418  *
419  * Description:
420  *
421  *     
422  *
423  * Return value:
424  *
425  *     
426  *
427  */
428 #if 0 && defined(PRINTING_SUPPORTED)
429 void
430 PdmXpSetValue(PdmXp* me,
431               XPAttributes type,
432               PdmOid id_att,
433               PdmOid id_val)
434 {
435     PdmXpSetStringValue(me, type, id_att, PdmOidString(id_val));
436 }
437 #endif /* PRINTING_SUPPORTED */
438
439 /*
440  * ------------------------------------------------------------------------
441  * Name: PdmXpSetStringValue
442  *
443  * Description:
444  *
445  *     
446  *
447  * Return value:
448  *
449  *     
450  *
451  */
452 #if 0 && defined(PRINTING_SUPPORTED)
453 void
454 PdmXpSetStringValue(PdmXp* me,
455                     XPAttributes type,
456                     PdmOid id_att,
457                     const char* str_val)
458 {
459     char* res_name;
460    
461     XrmDatabase pool;
462     
463     pool = PdmXpLoadPool(me, type);
464     if(pool == (XrmDatabase)NULL)
465         return;
466
467     res_name = PdmXpBuildResourceName(me, id_att);
468     XrmPutStringResource(&pool, res_name, (char*)str_val);
469     XtFree(res_name);
470 }
471 #endif /* PRINTING_SUPPORTED */
472
473
474 /*
475  * ------------------------------------------------------------------------
476  * Name: PdmXpUpdateAttributes
477  *
478  * Description:
479  *
480  *     
481  *
482  * Return value:
483  *
484  *     
485  *
486  */
487 void
488 PdmXpUpdateAttributes(PdmXp* me)
489 {
490 #if 0 && defined(PRINTING_SUPPORTED)
491     char fname[L_tmpnam];
492     
493     if(tmpnam(fname))
494     {
495         int i;
496         XrmDatabase pool;
497         XPAttributes type;
498         FILE* fp;
499         struct stat stbuf;
500         int retlen;
501         char* data = NULL;
502         int data_size = 0;
503         XTextProperty text_prop;
504
505         for(i = 0; i < PDMXP_POOL_COUNT; i++)
506         {
507             if(me->pool[i] != (XrmDatabase)NULL)
508             {
509                 switch(i)
510                 {
511                 case PDMXP_JOB:
512                     type = XPJobAttr;
513                     break;
514                 case PDMXP_DOC:
515                     type = XPDocAttr;
516                     break;
517                 default:
518                     continue;
519                 }
520                 /*
521                  * write out the attribute pool to a file Xrm DB
522                  */
523                 XrmPutFileDatabase(me->pool[i], fname);
524                 /*
525                  * open the new file Xrm DB
526                  */
527                 if(fp = fopen(fname, "r"))
528                 {
529                     /*
530                      * read the file to create a string Xrm DB
531                      */
532                     fstat(fileno(fp), &stbuf);
533                     if(stbuf.st_size + 1 > data_size)
534                     {
535                         data_size = stbuf.st_size + 1;
536                         data = XtRealloc(data, data_size);
537                     }
538                     retlen = read(fileno(fp), data, stbuf.st_size);
539                     fclose(fp);
540                     unlink(fname);
541                     data[retlen] = '\0';
542                     /*
543                      * convert to compund text
544                      */
545                     if(Success == 
546                        XmbTextListToTextProperty(me->display,
547                                                  &data, 1,
548                                                  XCompoundTextStyle,
549                                                  &text_prop))
550                     {
551                         /*
552                          * use the string Xrm DB to update the Xp server
553                          */
554                         XpSetAttributes(me->display, me->context,
555                                         type, (char*)text_prop.value,
556                                         XPAttrMerge);
557                         if(text_prop.value)
558                             XFree(text_prop.value);
559                     }
560                 }
561             }
562
563         }
564         XtFree(data);
565     }
566 #endif /* PRINTING_SUPPORTED */
567 }