PdmXp.c 11 KB

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