kdeprint Library API Documentation

foomatic2loader.cpp

00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2001-2003 Michael Goffioul <kdeprint@swing.be> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 * Boston, MA 02111-1307, USA. 00018 **/ 00019 00020 #include "foomatic2loader.h" 00021 #include "driver.h" 00022 00023 #include <qfile.h> 00024 #include <qregexp.h> 00025 #include <qbuffer.h> 00026 #include <kdebug.h> 00027 #include <klocale.h> 00028 00029 void kdeprint_foomatic2scanner_init( QIODevice* ); 00030 void kdeprint_foomatic2scanner_terminate(); 00031 00032 Foomatic2Loader::Foomatic2Loader() 00033 { 00034 } 00035 00036 Foomatic2Loader::~Foomatic2Loader() 00037 { 00038 } 00039 00040 bool Foomatic2Loader::read( QIODevice *d ) 00041 { 00042 bool result = true; 00043 m_foodata.clear(); 00044 kdeprint_foomatic2scanner_init( d ); 00045 if ( kdeprint_foomatic2parse( this ) != 0 ) 00046 result = false; 00047 kdeprint_foomatic2scanner_terminate(); 00048 return result; 00049 } 00050 00051 bool Foomatic2Loader::readFromFile( const QString& filename ) 00052 { 00053 QFile f( filename ); 00054 m_foodata.clear(); 00055 if ( f.open( IO_ReadOnly ) ) 00056 return read( &f ); 00057 return false; 00058 } 00059 00060 bool Foomatic2Loader::readFromBuffer( const QString& buffer ) 00061 { 00062 QCString buf = buffer.utf8(); 00063 QBuffer d( buf ); 00064 m_foodata.clear(); 00065 if ( d.open( IO_ReadOnly ) ) 00066 return read( &d ); 00067 return false; 00068 } 00069 00070 DrBase* Foomatic2Loader::createValue( const QString& name, const QMap<QString,QVariant>& m ) const 00071 { 00072 DrBase *choice = new DrBase; 00073 choice->setName( name ); 00074 choice->set( "text", m.operator[]( "comment" ).toString() ); 00075 return choice; 00076 } 00077 00078 DrBase* Foomatic2Loader::createOption( const QMap<QString,QVariant>& m ) const 00079 { 00080 QString type = m.operator[]( "type" ).toString(); 00081 DrBase *opt = NULL; 00082 if ( type == "enum" ) 00083 { 00084 DrListOption *lopt = new DrListOption; 00085 QVariant a = m.operator[]( "vals_byname" ); 00086 QMap<QString,QVariant>::ConstIterator it = a.mapBegin(); 00087 for ( ; it!=a.mapEnd(); ++it ) 00088 { 00089 if ( it.data().type() != QVariant::Map ) 00090 continue; 00091 DrBase *ch = createValue( it.key(), it.data().toMap() ); 00092 if ( ch ) 00093 lopt->addChoice( ch ); 00094 } 00095 opt = lopt; 00096 } 00097 else if ( type == "int" || type == "float" ) 00098 { 00099 if ( type == "int" ) 00100 opt = new DrIntegerOption; 00101 else 00102 opt = new DrFloatOption; 00103 opt->set( "minval", m.operator[]( "min" ).toString() ); 00104 opt->set( "maxval", m.operator[]( "max" ).toString() ); 00105 } 00106 else if ( type == "bool" ) 00107 { 00108 DrBooleanOption *bopt = new DrBooleanOption; 00109 DrBase *choice; 00110 // choice 1 00111 choice = new DrBase; 00112 choice->setName( "0" ); 00113 choice->set( "text", m.operator[]( "name_false" ).toString() ); 00114 bopt->addChoice( choice ); 00115 choice = new DrBase; 00116 choice->setName( "1" ); 00117 choice->set( "text", m.operator[]( "name_true" ).toString() ); 00118 bopt->addChoice( choice ); 00119 opt = bopt; 00120 } 00121 else if ( type == "string" ) 00122 { 00123 opt = new DrStringOption; 00124 } 00125 if ( opt ) 00126 { 00127 opt->setName( m.operator[]( "name" ).toString() ); 00128 opt->set( "text", m.operator[]( "comment" ).toString() ); 00129 QString defval = m.operator[]( "default" ).toString(); 00130 if ( !defval.isEmpty() ) 00131 { 00132 opt->setValueText( defval ); 00133 opt->set( "default", defval ); 00134 } 00135 } 00136 return opt; 00137 } 00138 00139 DrMain* Foomatic2Loader::buildDriver() const 00140 { 00141 if ( m_foodata.isEmpty() ) 00142 return NULL; 00143 00144 QVariant v = m_foodata.find( "VAR" ).data(); 00145 if ( !v.isNull() && v.type() == QVariant::Map ) 00146 { 00147 DrMain *driver = new DrMain; 00148 QMap<QString,DrGroup*> groups; 00149 driver->set( "manufacturer", v.mapFind( "make" ).data().toString() ); 00150 driver->set( "model", v.mapFind( "model" ).data().toString() ); 00151 driver->set( "matic_printer", v.mapFind( "id" ).data().toString() ); 00152 driver->set( "matic_driver", v.mapFind( "driver" ).data().toString() ); 00153 driver->set( "text", QString( "%1 %2 (%3)" ).arg( driver->get( "manufacturer" ) ).arg( driver->get( "model" ) ).arg( driver->get( "matic_driver" ) ) ); 00154 if ( m_foodata.contains( "POSTPIPE" ) ) 00155 driver->set( "postpipe", m_foodata.find( "POSTPIPE" ).data().toString() ); 00156 v = v.mapFind( "args" ).data(); 00157 if ( !v.isNull() && v.type() == QVariant::List ) 00158 { 00159 QValueList<QVariant>::ConstIterator it = v.listBegin(); 00160 for ( ; it!=v.listEnd(); ++it ) 00161 { 00162 if ( ( *it ).type() != QVariant::Map ) 00163 continue; 00164 DrBase *opt = createOption( ( *it ).toMap() ); 00165 if ( opt ) 00166 { 00167 QString group = DrGroup::groupForOption( opt->name() ); 00168 DrGroup *grp = NULL; 00169 if ( !groups.contains( group ) ) 00170 { 00171 grp = new DrGroup; 00172 grp->set( "text", group ); 00173 driver->addGroup( grp ); 00174 groups.insert( group, grp ); 00175 } 00176 else 00177 grp = groups[ group ]; 00178 grp->addOption( opt ); 00179 if ( opt->name() == "PageSize" ) 00180 { 00181 // try to add the corresponding page sizes 00182 QVariant choices = ( *it ).mapFind( "vals_byname" ).data(); 00183 QRegExp re( "(\\d+) +(\\d+)" ); 00184 if ( choices.type() == QVariant::Map ) 00185 { 00186 QMap<QString,QVariant>::ConstIterator it = choices.mapBegin(); 00187 for ( ; it!=choices.mapEnd(); ++it ) 00188 { 00189 QString driverval = ( *it ).mapFind( "driverval" ).data().toString(); 00190 if ( re.exactMatch( driverval ) ) 00191 { 00192 driver->addPageSize( new DrPageSize( it.key(), re.cap( 1 ).toInt(), re.cap( 2 ).toInt(), 36, 24, 36, 24 ) ); 00193 } 00194 } 00195 } 00196 } 00197 } 00198 else 00199 kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl; 00200 } 00201 } 00202 return driver; 00203 } 00204 return NULL; 00205 } 00206 00207 DrMain* Foomatic2Loader::modifyDriver( DrMain *driver ) const 00208 { 00209 if ( !m_foodata.isEmpty() ) 00210 { 00211 QValueList<DrBase*> optList; 00212 DrGroup *grp = NULL; 00213 00214 QVariant V = m_foodata.find( "VAR" ).data(); 00215 if ( !V.isNull() && V.type() == QVariant::Map ) 00216 { 00217 QVariant v = V.mapFind( "args" ).data(); 00218 if ( !v.isNull() && v.type() == QVariant::List ) 00219 { 00220 QValueList<QVariant>::ConstIterator it = v.listBegin(); 00221 for ( ; it!=v.listEnd(); ++it ) 00222 { 00223 if ( ( *it ).type() != QVariant::Map ) 00224 continue; 00225 DrBase *opt = createOption( ( *it ).toMap() ); 00226 if ( opt ) 00227 optList.append( opt ); 00228 else 00229 kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl; 00230 } 00231 } 00232 else 00233 { 00234 v = V.mapFind( "args_byname" ).data(); 00235 if ( !v.isNull() && v.type() == QVariant::Map ) 00236 { 00237 QMap<QString,QVariant>::ConstIterator it = v.mapBegin(); 00238 for ( ; it!=v.mapEnd(); ++it ) 00239 { 00240 if ( ( *it ).type() != QVariant::Map ) 00241 continue; 00242 DrBase *opt = createOption( ( *it ).toMap() ); 00243 if ( opt ) 00244 optList.append( opt ); 00245 else 00246 kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl; 00247 } 00248 } 00249 } 00250 } 00251 00252 for ( QValueList<DrBase*>::ConstIterator it=optList.begin(); it!=optList.end(); ++it ) 00253 { 00254 DrBase *opt = ( *it ); 00255 if ( opt ) 00256 { 00257 switch ( opt->type() ) 00258 { 00259 case DrBase::List: 00260 case DrBase::Boolean: 00261 delete opt; 00262 break; 00263 default: 00264 { 00265 if ( !grp ) 00266 { 00267 grp = new DrGroup; 00268 grp->set( "text", i18n( "Adjustments" ) ); 00269 driver->addGroup( grp ); 00270 } 00271 DrBase *oldOpt = driver->findOption( opt->name() ); 00272 if ( oldOpt && oldOpt->type() == DrBase::List ) 00273 { 00274 QPtrListIterator<DrBase> it( *( static_cast<DrListOption*>( oldOpt )->choices() ) ); 00275 QString fixedvals; 00276 for ( ; it.current(); ++it ) 00277 { 00278 fixedvals.append( it.current()->name() ); 00279 if ( !it.atLast() ) 00280 fixedvals.append( "|" ); 00281 } 00282 opt->set( "fixedvals", fixedvals ); 00283 } 00284 driver->removeOptionGlobally( opt->name() ); 00285 grp->addOption( opt ); 00286 break; 00287 } 00288 } 00289 } 00290 } 00291 } 00292 return driver; 00293 } 00294 00295 DrMain* Foomatic2Loader::loadDriver( const QString& filename ) 00296 { 00297 Foomatic2Loader loader; 00298 if ( loader.readFromFile( filename ) ) 00299 return loader.buildDriver(); 00300 else 00301 return NULL; 00302 }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 23:26:33 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003