1/*
2 * "$Id: printer_options.c,v 1.5 2008/06/07 22:47:35 rlk Exp $"
3 *
4 *   Dump the per-printer options for the OpenPrinting database
5 *
6 *   Copyright 2000 Robert Krawitz (rlk@alum.mit.edu)
7 *
8 *   This program is free software; you can redistribute it and/or modify it
9 *   under the terms of the GNU General Public License as published by the Free
10 *   Software Foundation; either version 2 of the License, or (at your option)
11 *   any later version.
12 *
13 *   This program is distributed in the hope that it will be useful, but
14 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 *   for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26#include <stdio.h>
27#include <string.h>
28#include <gutenprint/gutenprint.h>
29#include <gutenprint/gutenprint-intl.h>
30
31int
32main(int argc, char **argv)
33{
34  int i, j, k;
35  int first_arg = 1;
36  stp_string_list_t *printer_list = NULL;
37  stp_parameter_level_t max_level = STP_PARAMETER_LEVEL_ADVANCED4;
38  if (argc > 1 && !strcmp(argv[1], "-s"))
39    {
40      max_level = STP_PARAMETER_LEVEL_BASIC;
41      first_arg++;
42    }
43
44  stp_init();
45
46  if (argc > first_arg)
47    {
48      printer_list = stp_string_list_create();
49      for (i = 1; i < argc; i++)
50	stp_string_list_add_string(printer_list, argv[i], argv[i]);
51    }
52  for (i = 0; i < stp_printer_model_count(); i++)
53    {
54      stp_parameter_list_t params;
55      int nparams;
56      stp_parameter_t desc;
57      const stp_printer_t *printer = stp_get_printer_by_index(i);
58      const char *driver = stp_printer_get_driver(printer);
59      const char *family = stp_printer_get_family(printer);
60      stp_vars_t *pv = stp_vars_create_copy(stp_printer_get_defaults(printer));
61      int tcount = 0;
62      size_t count;
63      int printer_is_color = 0;
64      if (strcmp(family, "ps") == 0 || strcmp(family, "raw") == 0)
65	continue;
66      if (printer_list && !stp_string_list_is_present(printer_list, driver))
67	continue;
68
69      /* Set Job Mode to "Job" as this enables the Duplex option */
70      stp_set_string_parameter(pv, "JobMode", "Job");
71
72      stp_describe_parameter(pv, "PrintingMode", &desc);
73      if (stp_string_list_is_present(desc.bounds.str, "Color"))
74	printer_is_color = 1;
75      stp_parameter_description_destroy(&desc);
76      if (printer_is_color)
77	stp_set_string_parameter(pv, "PrintingMode", "Color");
78      else
79	stp_set_string_parameter(pv, "PrintingMode", "BW");
80      stp_set_string_parameter(pv, "ChannelBitDepth", "8");
81
82      printf("# Printer model %s, long name `%s'\n", driver,
83	     stp_printer_get_long_name(printer));
84
85      printf("$families{'%s'} = '%s';\n", driver, family);
86      printf("$models{'%s'} = '%d';\n", driver, stp_get_model_id(pv));
87
88      params = stp_get_parameter_list(pv);
89      nparams = stp_parameter_list_count(params);
90
91      for (k = 0; k < nparams; k++)
92	{
93	  const stp_parameter_t *p = stp_parameter_list_param(params, k);
94	  if (p->read_only ||
95	      (p->p_level > max_level && strcmp(p->name, "Resolution") != 0) ||
96	      (p->p_class != STP_PARAMETER_CLASS_OUTPUT &&
97	       p->p_class != STP_PARAMETER_CLASS_CORE &&
98	       p->p_class != STP_PARAMETER_CLASS_FEATURE))
99	    continue;
100	  count = 0;
101	  stp_describe_parameter(pv, p->name, &desc);
102	  if (desc.is_active)
103	    {
104	      printf("$longnames{'%s'}{'%s'} = '%s';\n",
105		     driver, desc.name, desc.text);
106	      printf("$param_classes{'%s'}{'%s'} = %d;\n",
107		     driver, desc.name, desc.p_class);
108	      printf("$param_types{'%s'}{'%s'} = %d;\n",
109		     driver, desc.name, desc.p_type);
110	      printf("$param_levels{'%s'}{'%s'} = %d;\n",
111		     driver, desc.name, desc.p_level);
112	      if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST)
113		{
114		  count = stp_string_list_count(desc.bounds.str);
115		  if (count > 0)
116		    {
117		      if (desc.is_mandatory)
118			{
119			  printf("$defaults{'%s'}{'%s'} = '%s';\n",
120				 driver, desc.name, desc.deflt.str);
121			}
122		      else
123			{
124			  printf("$defaults{'%s'}{'%s'} = '%s';\n",
125				 driver, desc.name, "None");
126			  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
127				 driver, desc.name, "None", "None");
128			}
129		      for (j = 0; j < count; j++)
130			{
131			  const stp_param_string_t *param =
132			    stp_string_list_param(desc.bounds.str, j);
133			  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
134				 driver, desc.name, param->name, param->text);
135			  if (strcmp(desc.name, "Resolution") == 0)
136			    {
137			      int x, y;
138			      stp_set_string_parameter(pv, "Resolution",
139						       param->name);
140			      stp_describe_resolution(pv, &x, &y);
141			      if (x > 0 && y > 0)
142				{
143				  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%d';\n",
144					 driver, "x_resolution", param->name, x);
145				  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%d';\n",
146					 driver, "y_resolution", param->name, y);
147				}
148			    }
149			}
150		    }
151		}
152	      else if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN)
153		{
154		  if (desc.is_mandatory)
155		    {
156		      printf("$defaults{'%s'}{'%s'} = '%d';\n",
157			     driver, desc.name, desc.deflt.boolean);
158		    }
159		  else
160		    {
161		      printf("$defaults{'%s'}{'%s'} = '%s';\n",
162			     driver, desc.name, "None");
163		      printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
164			     driver, desc.name, "None", "None");
165		    }
166
167		  printf("$stpdata{'%s'}{'%s'}{'False'} = 'False';\n",
168			 driver, desc.name);
169		  printf("$stpdata{'%s'}{'%s'}{'True'} = 'True';\n",
170			 driver, desc.name);
171		}
172	      else if (desc.p_type == STP_PARAMETER_TYPE_DOUBLE)
173		{
174		  if (desc.bounds.dbl.lower <= desc.deflt.dbl &&
175		      desc.bounds.dbl.upper >= desc.deflt.dbl)
176		    {
177		      printf("$stp_float_values{'%s'}{'MINVAL'}{'%s'} = %.3f;\n",
178			     driver, desc.name, desc.bounds.dbl.lower);
179		      printf("$stp_float_values{'%s'}{'MAXVAL'}{'%s'} = %.3f;\n",
180			     driver, desc.name, desc.bounds.dbl.upper);
181		      printf("$stp_float_values{'%s'}{'DEFVAL'}{'%s'} = %.3f;\n",
182			     driver, desc.name, desc.deflt.dbl);
183		      /* printf("$stp_float_values{'%s'}{'LONG_NAME'}{'%s'} = '%s';\n",
184			 driver, desc.name, gettext(desc.text)); */
185		      printf("$stp_float_values{'%s'}{'CATEGORY'}{'%s'} = '%s';\n",
186			     driver, desc.name, gettext(desc.category));
187		      printf("$stp_float_values{'%s'}{'HELP'}{'%s'} = q(%s);\n",
188			     driver, desc.name, (desc.help ? gettext(desc.help) : "''"));
189		      printf("$stp_float_values{'%s'}{'MANDATORY'}{'%s'} = q(%d);\n",
190			     driver, desc.name, desc.is_mandatory);
191		    }
192		}
193	      else if (desc.p_type == STP_PARAMETER_TYPE_INT)
194		{
195		  if (desc.bounds.integer.lower <= desc.deflt.integer &&
196		      desc.bounds.integer.upper >= desc.deflt.integer)
197		    {
198		      printf("$stp_int_values{'%s'}{'MINVAL'}{'%s'} = %d;\n",
199			     driver, desc.name, desc.bounds.integer.lower);
200		      printf("$stp_int_values{'%s'}{'MAXVAL'}{'%s'} = %d;\n",
201			     driver, desc.name, desc.bounds.integer.upper);
202		      printf("$stp_int_values{'%s'}{'DEFVAL'}{'%s'} = %d;\n",
203			     driver, desc.name, desc.deflt.integer);
204		      /* printf("$stp_int_values{'%s'}{'LONG_NAME'}{'%s'} = '%s';\n",
205			 driver, desc.name, gettext(desc.text)); */
206		      printf("$stp_int_values{'%s'}{'CATEGORY'}{'%s'} = '%s';\n",
207			     driver, desc.name, gettext(desc.category));
208		      printf("$stp_int_values{'%s'}{'HELP'}{'%s'} = q(%s);\n",
209			     driver, desc.name, (desc.help ? gettext(desc.help) : "''"));
210		      printf("$stp_int_values{'%s'}{'MANDATORY'}{'%s'} = q(%d);\n",
211			     driver, desc.name, desc.is_mandatory);
212		    }
213		}
214	      else if (desc.p_type == STP_PARAMETER_TYPE_DIMENSION)
215		{
216		  if (desc.bounds.dimension.lower <= desc.deflt.dimension &&
217		      desc.bounds.dimension.upper >= desc.deflt.dimension)
218		    {
219		      printf("$stp_dimension_values{'%s'}{'MINVAL'}{'%s'} = %d;\n",
220			     driver, desc.name, desc.bounds.dimension.lower);
221		      printf("$stp_dimension_values{'%s'}{'MAXVAL'}{'%s'} = %d;\n",
222			     driver, desc.name, desc.bounds.dimension.upper);
223		      printf("$stp_dimension_values{'%s'}{'DEFVAL'}{'%s'} = %d;\n",
224			     driver, desc.name, desc.deflt.dimension);
225		      /* printf("$stp_dimension_values{'%s'}{'LONG_NAME'}{'%s'} = '%s';\n",
226			 driver, desc.name, gettext(desc.text)); */
227		      printf("$stp_dimension_values{'%s'}{'CATEGORY'}{'%s'} = '%s';\n",
228			     driver, desc.name, gettext(desc.category));
229		      printf("$stp_dimension_values{'%s'}{'HELP'}{'%s'} = q(%s);\n",
230			     driver, desc.name, (desc.help ? gettext(desc.help) : "''"));
231		      printf("$stp_dimension_values{'%s'}{'MANDATORY'}{'%s'} = q(%d);\n",
232			     driver, desc.name, desc.is_mandatory);
233		    }
234		}
235	      tcount += count;
236	    }
237	  stp_parameter_description_destroy(&desc);
238	}
239      stp_parameter_list_destroy(params);
240      if (tcount > 0)
241	{
242	  if (printer_is_color)
243	    {
244	      printf("$defaults{'%s'}{'%s'} = '%s';\n",
245		     driver, "Color", "Color");
246	      printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
247		     driver, "Color", "Color", "Color");
248	      printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
249		     driver, "Color", "RawCMYK", "Raw CMYK");
250	    }
251	  else
252	    printf("$defaults{'%s'}{'%s'} = '%s';\n",
253		   driver, "Color", "Grayscale");
254	  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
255		 driver, "Color", "Grayscale", "Gray Scale");
256	  printf("$stpdata{'%s'}{'%s'}{'%s'} = '%s';\n",
257		 driver, "Color", "BlackAndWhite", "Black and White");
258	}
259      stp_vars_destroy(pv);
260    }
261  return 0;
262}
263