20 #include "foomatic2loader.h"
27 #include <tdelocale.h>
29 void tdeprint_foomatic2scanner_init( TQIODevice* );
30 void tdeprint_foomatic2scanner_terminate();
32 Foomatic2Loader::Foomatic2Loader()
36 Foomatic2Loader::~Foomatic2Loader()
40 bool Foomatic2Loader::read( TQIODevice *d )
44 tdeprint_foomatic2scanner_init( d );
45 if ( tdeprint_foomatic2parse(
this ) != 0 )
47 tdeprint_foomatic2scanner_terminate();
51 bool Foomatic2Loader::readFromFile(
const TQString& filename )
55 if ( f.open( IO_ReadOnly ) )
60 bool Foomatic2Loader::readFromBuffer(
const TQString& buffer )
62 TQCString buf = buffer.utf8();
65 if ( d.open( IO_ReadOnly ) )
70 DrBase* Foomatic2Loader::createValue(
const TQString& name,
const TQStringVariantMap& m )
const
72 DrBase *choice =
new DrBase;
73 choice->setName( name );
74 choice->set(
"text", m.operator[](
"comment" ).toString() );
78 DrBase* Foomatic2Loader::createOption(
const TQStringVariantMap& m )
const
80 TQString type = m.operator[](
"type" ).toString();
84 DrListOption *lopt =
new DrListOption;
85 TQVariant a = m.operator[](
"vals_byname" );
86 TQStringVariantMap::ConstIterator it = a.mapBegin();
87 for ( ; it!=a.mapEnd(); ++it )
89 if ( it.data().type() != TQVariant::Map )
91 DrBase *ch = createValue( it.key(), it.data().toMap() );
93 lopt->addChoice( ch );
97 else if ( type ==
"int" || type ==
"float" )
100 opt =
new DrIntegerOption;
102 opt =
new DrFloatOption;
103 opt->set(
"minval", m.operator[](
"min" ).toString() );
104 opt->set(
"maxval", m.operator[](
"max" ).toString() );
106 else if ( type ==
"bool" )
108 DrBooleanOption *bopt =
new DrBooleanOption;
112 choice->setName(
"0" );
113 choice->set(
"text", m.operator[](
"name_false" ).toString() );
114 bopt->addChoice( choice );
116 choice->setName(
"1" );
117 choice->set(
"text", m.operator[](
"name_true" ).toString() );
118 bopt->addChoice( choice );
121 else if ( type ==
"string" )
123 opt =
new DrStringOption;
127 opt->setName( m.operator[](
"name" ).toString() );
128 opt->set(
"text", m.operator[](
"comment" ).toString() );
129 TQString defval = m.operator[](
"default" ).toString();
130 if ( !defval.isEmpty() )
132 opt->setValueText( defval );
133 opt->set(
"default", defval );
139 DrMain* Foomatic2Loader::buildDriver()
const
141 if ( m_foodata.isEmpty() )
144 TQVariant v = m_foodata.find(
"VAR" ).data();
145 if ( !v.isNull() && v.type() == TQVariant::Map )
147 DrMain *driver =
new DrMain;
148 TQMap<TQString,DrGroup*> groups;
149 driver->set(
"manufacturer", v.mapFind(
"make" ).data().toString() );
150 driver->set(
"model", v.mapFind(
"model" ).data().toString() );
151 driver->set(
"matic_printer", v.mapFind(
"id" ).data().toString() );
152 driver->set(
"matic_driver", v.mapFind(
"driver" ).data().toString() );
153 driver->set(
"text", TQString(
"%1 %2 (%3)" ).arg( driver->get(
"manufacturer" ) ).arg( driver->get(
"model" ) ).arg( driver->get(
"matic_driver" ) ) );
154 if ( m_foodata.contains(
"POSTPIPE" ) )
155 driver->set(
"postpipe", m_foodata.find(
"POSTPIPE" ).data().toString() );
156 v = v.mapFind(
"args" ).data();
157 if ( !v.isNull() && v.type() == TQVariant::List )
159 TQValueList<TQVariant>::ConstIterator it = v.listBegin();
160 for ( ; it!=v.listEnd(); ++it )
162 if ( ( *it ).type() != TQVariant::Map )
164 DrBase *opt = createOption( ( *it ).toMap() );
167 TQString group = DrGroup::groupForOption( opt->name() );
169 if ( !groups.contains( group ) )
172 grp->set(
"text", group );
173 driver->addGroup( grp );
174 groups.insert( group, grp );
177 grp = groups[ group ];
178 grp->addOption( opt );
179 if ( opt->name() ==
"PageSize" )
182 TQVariant choices = ( *it ).mapFind(
"vals_byname" ).data();
183 TQRegExp re(
"(\\d+) +(\\d+)" );
184 if ( choices.type() == TQVariant::Map )
186 TQStringVariantMap::ConstIterator it = choices.mapBegin();
187 for ( ; it!=choices.mapEnd(); ++it )
189 TQString driverval = ( *it ).mapFind(
"driverval" ).data().toString();
190 if ( re.exactMatch( driverval ) )
192 driver->addPageSize(
new DrPageSize( it.key(), re.cap( 1 ).toInt(), re.cap( 2 ).toInt(), 36, 24, 36, 24 ) );
199 kdWarning( 500 ) <<
"Failed to create option: " << ( *it ).toMap()[
"name" ].toString() << endl;
207 DrMain* Foomatic2Loader::modifyDriver( DrMain *driver )
const
209 if ( !m_foodata.isEmpty() )
211 TQValueList<DrBase*> optList;
214 TQVariant V = m_foodata.find(
"VAR" ).data();
215 if ( !V.isNull() && V.type() == TQVariant::Map )
217 TQVariant v = V.mapFind(
"args" ).data();
218 if ( !v.isNull() && v.type() == TQVariant::List )
220 TQValueList<TQVariant>::ConstIterator it = v.listBegin();
221 for ( ; it!=v.listEnd(); ++it )
223 if ( ( *it ).type() != TQVariant::Map )
225 DrBase *opt = createOption( ( *it ).toMap() );
227 optList.append( opt );
229 kdWarning( 500 ) <<
"Failed to create option: " << ( *it ).toMap()[
"name" ].toString() << endl;
234 v = V.mapFind(
"args_byname" ).data();
235 if ( !v.isNull() && v.type() == TQVariant::Map )
237 TQStringVariantMap::ConstIterator it = v.mapBegin();
238 for ( ; it!=v.mapEnd(); ++it )
240 if ( ( *it ).type() != TQVariant::Map )
242 DrBase *opt = createOption( ( *it ).toMap() );
244 optList.append( opt );
246 kdWarning( 500 ) <<
"Failed to create option: " << ( *it ).toMap()[
"name" ].toString() << endl;
252 for ( TQValueList<DrBase*>::ConstIterator it=optList.begin(); it!=optList.end(); ++it )
254 DrBase *opt = ( *it );
257 switch ( opt->type() )
260 case DrBase::Boolean:
268 grp->set(
"text", i18n(
"Adjustments" ) );
269 driver->addGroup( grp );
271 DrBase *oldOpt = driver->findOption( opt->name() );
272 if ( oldOpt && oldOpt->type() == DrBase::List )
274 TQPtrListIterator<DrBase> it( *(
static_cast<DrListOption*
>( oldOpt )->choices() ) );
276 for ( ; it.current(); ++it )
278 fixedvals.append( it.current()->name() );
280 fixedvals.append(
"|" );
282 opt->set(
"fixedvals", fixedvals );
284 driver->removeOptionGlobally( opt->name() );
285 grp->addOption( opt );
295 DrMain* Foomatic2Loader::loadDriver(
const TQString& filename )
297 Foomatic2Loader loader;
298 if ( loader.readFromFile( filename ) )
299 return loader.buildDriver();