File: gprintersettings.cpp

package info (click to toggle)
photoprint 0.3.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,708 kB
  • ctags: 3,996
  • sloc: cpp: 17,543; sh: 8,311; ansic: 4,299; makefile: 324
file content (354 lines) | stat: -rw-r--r-- 8,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * gprintersettings.cpp - Bridges GutenPrint's stp_vars_t and the configuration file.
 * A subclass of ConfigSectionHandler.
 *
 * Copyright (c) 2004 by Alastair M. Robinson
 * Distributed under the terms of the GNU General Public License -
 * see the file named "COPYING" for more details.
 *
 */

#include <iostream>

#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>

#include "stpui_widgets/stputil.h"
#include "gprintersettings.h"

using namespace std;


static void dumpstrings(const stp_parameter_t *desc)
{
	int j,strcount;
	stp_string_list_t *strlist=desc->bounds.str;
	if(strlist)
	{
		strcount=stp_string_list_count(strlist);
		for(j=0;j<strcount;++j)
		{
			stp_param_string_t *p=stp_string_list_param(strlist,j);
			fprintf(stderr,"   %s (%s)\n",p->text,p->name);
		}
	}
	else
		fprintf(stderr,"  No string list!\n");
}


static void dumpvars(stp_vars_t *v)
{
	stp_parameter_list_t params = stp_get_parameter_list(v);
	
	int count = stp_parameter_list_count(params);
	
	for (int i = 0; i < count; i++)
	{
		const stp_parameter_t *p = stp_parameter_list_param(params, i);
//		if((p->p_level<=STP_PARAMETER_LEVEL_ADVANCED4)
//			&& ((p->p_class==STP_PARAMETER_CLASS_FEATURE) || (p->p_class==STP_PARAMETER_CLASS_OUTPUT)))
		{
			stp_parameter_t desc;
			stp_describe_parameter(v,p->name,&desc);
			const char *str;
			int ival;
			double fval;

			fprintf(stderr,"%s, %d, %d, %d, %d ",desc.name,desc.p_type,p->p_type,desc.p_class,desc.p_level);
			
			if(desc.is_active)
			{
				fprintf(stderr,", Active");
			}
			else
			{
				fprintf(stderr,", Inactive");
			}
				switch(desc.p_type)
				{
					case STP_PARAMETER_TYPE_STRING_LIST:
						str=stp_get_string_parameter(v,desc.name);
						fprintf(stderr,", StringList\n");
						dumpstrings(&desc);
						if(str)
							fprintf(stderr,"  Currently set to: %s\n",str);
						break;

					case STP_PARAMETER_TYPE_INT:
						ival=stp_get_int_parameter(v,desc.name);
						fprintf(stderr,", Int\n");
						fprintf(stderr,"  Currently set to: %d\n",ival);
						break;

					case STP_PARAMETER_TYPE_BOOLEAN:
						ival=stp_get_boolean_parameter(v,desc.name);
						fprintf(stderr,", Boolean\n");
						fprintf(stderr,"  Currently set to: %d\n",ival);
						break;

					case STP_PARAMETER_TYPE_DOUBLE:
						fval=stp_get_float_parameter(v,desc.name);
						fprintf(stderr,", Double\n");
						fprintf(stderr,"  Currently set to: %f\n",fval);
						break;

					default:
						fprintf(stderr,", Other\n");
						break;					
			}
		}
	}
	stp_parameter_list_destroy(params);
}


void GPrinterSettings::Dump()
{
	dumpvars(stpvars);
}


GPrinterSettings::GPrinterSettings(PrintOutput &output,ConfigFile *inf,const char *section)
	: ConfigSectionHandler(inf,section), stpvars(NULL), output(output), initialised(false)
{
	cerr << "In GPrinterSetting constructor - about to call stp_init()" << endl;
	stp_init();
	cerr << "Done" << endl;
	stpvars=stp_vars_create();
}


GPrinterSettings::~GPrinterSettings()
{
	if(stpvars)
		stp_vars_destroy(stpvars);
}


void GPrinterSettings::SelectSection()
{
	/* Slight hack here: The printer driver is stored in the [Output] section
	   which is prior to the [Print] section.  Clearing this forces the driver
	   to be set before the printer options are set */

	initialised=false;
}


void GPrinterSettings::ParseString(const char *string)
{
	if(!initialised)
	{
		const char *driver=output.FindString("Driver");
		if(!SetDriver(driver))
			output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
		initialised=true;
	}

	while(*string==' ')
		++string;
	
	char *value;
	char *token;
	value=token=strdup(string);
	int l=strlen(token)-1;
	while((l>0) && ((token[l]=='\n') || (token[l]=='\r')))
		--l;
	token[l+1]=0;

	while(1)
	{
		switch (*value)
		{
			case '\0':
				free(token);
				return;
				break;
			case '=':
				*value='\0';
				++value;
				if(*value)
				{
					stp_parameter_t param;
					stp_describe_parameter(stpvars,token,&param);
					switch(param.p_type)
					{
						case STP_PARAMETER_TYPE_STRING_LIST:
//							cerr << "Setting " << token << " to " << value << endl;
							stp_set_string_parameter(stpvars,token,value);
							break;
	
						case STP_PARAMETER_TYPE_INT:
//							cerr << "Setting " << token << " to " << value << endl;
							stp_set_int_parameter(stpvars,token,atoi(value));
							break;
	
						case STP_PARAMETER_TYPE_BOOLEAN:
//							cerr << "Setting " << token << " to " << value << endl;
							stp_set_boolean_parameter(stpvars,token,atoi(value));
							break;
	
						case STP_PARAMETER_TYPE_DOUBLE:
//							cerr << "Setting " << token << " to " << value << endl;
							stp_set_float_parameter(stpvars,token,atof(value));
							break;
	
						default:
							break;
					}
					stp_parameter_description_destroy(&param);
				}
				free(token);
				return;
				break;
		}
		++value;
	}
}


void GPrinterSettings::SaveSection(FILE *file)
{
	stp_parameter_list_t params = stp_get_parameter_list(stpvars);

	int count = stp_parameter_list_count(params);
	
	for (int i = 0; i < count; i++)
	{
		const stp_parameter_t *p = stp_parameter_list_param(params, i);
		if((p->p_level<=STP_PARAMETER_LEVEL_ADVANCED4))
		{
			stp_parameter_t desc;
			stp_describe_parameter(stpvars,p->name,&desc);
			if(desc.is_active)
			{
				switch(desc.p_type)
				{
					case STP_PARAMETER_TYPE_STRING_LIST:
						{
							if(stp_check_string_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								const char *str=stp_get_string_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || strcmp(str,desc.deflt.str)!=0)
									fprintf(file,"%s=%s\n",desc.name,str);
							}
						}
						break;

					case STP_PARAMETER_TYPE_INT:
						{
							if(stp_check_int_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								int val=stp_get_int_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.integer)
									fprintf(file,"%s=%d\n",desc.name,val);
							}
						}
						break;

					case STP_PARAMETER_TYPE_BOOLEAN:
						{
							if(stp_check_boolean_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								int val=stp_get_boolean_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.boolean)
									fprintf(file,"%s=%d\n",desc.name,val);
							}
						}
						break;

					case STP_PARAMETER_TYPE_DOUBLE:
						{
							if(stp_check_float_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								double val=stp_get_float_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.dbl)
									fprintf(file,"%s=%f\n",desc.name,val);
							}
						}
						break;

					default:
						break;					
				}     
			}
			stp_parameter_description_destroy(&desc);
		}
	}
	stp_parameter_list_destroy(params);
}


// SetDriver
// Returns false if the driver isn't recognised, true otherwise.

bool GPrinterSettings::SetDriver(const char *driver)
{
	bool result=true;
	if(stpvars)
	{
		const char *olddriver=stp_get_driver(stpvars);
		if(!olddriver)
			olddriver="None";
		if(!driver)
			driver=DEFAULT_PRINTER_DRIVER;
		if(strcmp(driver,olddriver))
		{
			cerr << "SetDriver(): Setting driver to " << driver << endl;

			// Work around the non-defaulting of inactive settings...
			const stp_vars_t *defaults=stp_default_settings();
			stp_vars_copy(stpvars,defaults);

			stp_set_driver(stpvars,driver);
			output.SetString("Driver",driver);

			const stp_printer_t *printer=stp_get_printer(stpvars);
			if(printer)
			{
				cerr << "Setting defaults" << endl;
				stp_set_printer_defaults(stpvars,printer);
			}
			else
			{
				cerr << "Unable to get printer - reverting to default driver" << endl;
				output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
				stp_set_driver(stpvars,DEFAULT_PRINTER_DRIVER);
				if((printer=stp_get_printer(stpvars)))
					stp_set_printer_defaults(stpvars,printer);
				else
					cerr << "Still can't get printer!" << endl;
				result=false;
			}
		}
	}
	else
		cerr << "No stp vars!" << endl;
	return(result);
}


void GPrinterSettings::SetPageSize(const char *pagesize)
{
	if(stpvars)
	{
		stp_set_string_parameter(stpvars,"MediaSize",pagesize);
	}
}


void GPrinterSettings::Validate()
{
	stputil_validate_parameters(stpvars);
}


void GPrinterSettings::Reset()
{
	stp_vars_destroy(stpvars);
	stpvars=stp_vars_create();
	cerr << "Created fresh stp_vars" << endl;
	initialised=false;
}