You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
8.3 KiB
303 lines
8.3 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001-2003 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include "foomatic2loader.h"
|
||
|
#include "driver.h"
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
|
#include <tqregexp.h>
|
||
|
#include <tqbuffer.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
#include <klocale.h>
|
||
|
|
||
13 years ago
|
void tdeprint_foomatic2scanner_init( TQIODevice* );
|
||
|
void tdeprint_foomatic2scanner_terminate();
|
||
15 years ago
|
|
||
|
Foomatic2Loader::Foomatic2Loader()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
Foomatic2Loader::~Foomatic2Loader()
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
bool Foomatic2Loader::read( TQIODevice *d )
|
||
15 years ago
|
{
|
||
|
bool result = true;
|
||
|
m_foodata.clear();
|
||
13 years ago
|
tdeprint_foomatic2scanner_init( d );
|
||
|
if ( tdeprint_foomatic2parse( this ) != 0 )
|
||
15 years ago
|
result = false;
|
||
13 years ago
|
tdeprint_foomatic2scanner_terminate();
|
||
15 years ago
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool Foomatic2Loader::readFromFile( const TQString& filename )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQFile f( filename );
|
||
15 years ago
|
m_foodata.clear();
|
||
|
if ( f.open( IO_ReadOnly ) )
|
||
14 years ago
|
return read( TQT_TQIODEVICE(&f) );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool Foomatic2Loader::readFromBuffer( const TQString& buffer )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString buf = buffer.utf8();
|
||
|
TQBuffer d( buf );
|
||
15 years ago
|
m_foodata.clear();
|
||
|
if ( d.open( IO_ReadOnly ) )
|
||
14 years ago
|
return read( TQT_TQIODEVICE(&d) );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
DrBase* Foomatic2Loader::createValue( const TQString& name, const TQMap<TQString,TQVariant>& m ) const
|
||
15 years ago
|
{
|
||
|
DrBase *choice = new DrBase;
|
||
|
choice->setName( name );
|
||
|
choice->set( "text", m.operator[]( "comment" ).toString() );
|
||
|
return choice;
|
||
|
}
|
||
|
|
||
15 years ago
|
DrBase* Foomatic2Loader::createOption( const TQMap<TQString,TQVariant>& m ) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString type = m.operator[]( "type" ).toString();
|
||
15 years ago
|
DrBase *opt = NULL;
|
||
|
if ( type == "enum" )
|
||
|
{
|
||
|
DrListOption *lopt = new DrListOption;
|
||
15 years ago
|
TQVariant a = m.operator[]( "vals_byname" );
|
||
|
TQMap<TQString,TQVariant>::ConstIterator it = a.mapBegin();
|
||
15 years ago
|
for ( ; it!=a.mapEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
if ( it.data().type() != TQVariant::Map )
|
||
15 years ago
|
continue;
|
||
|
DrBase *ch = createValue( it.key(), it.data().toMap() );
|
||
|
if ( ch )
|
||
|
lopt->addChoice( ch );
|
||
|
}
|
||
|
opt = lopt;
|
||
|
}
|
||
|
else if ( type == "int" || type == "float" )
|
||
|
{
|
||
|
if ( type == "int" )
|
||
|
opt = new DrIntegerOption;
|
||
|
else
|
||
|
opt = new DrFloatOption;
|
||
|
opt->set( "minval", m.operator[]( "min" ).toString() );
|
||
|
opt->set( "maxval", m.operator[]( "max" ).toString() );
|
||
|
}
|
||
|
else if ( type == "bool" )
|
||
|
{
|
||
|
DrBooleanOption *bopt = new DrBooleanOption;
|
||
|
DrBase *choice;
|
||
|
// choice 1
|
||
|
choice = new DrBase;
|
||
|
choice->setName( "0" );
|
||
|
choice->set( "text", m.operator[]( "name_false" ).toString() );
|
||
|
bopt->addChoice( choice );
|
||
|
choice = new DrBase;
|
||
|
choice->setName( "1" );
|
||
|
choice->set( "text", m.operator[]( "name_true" ).toString() );
|
||
|
bopt->addChoice( choice );
|
||
|
opt = bopt;
|
||
|
}
|
||
|
else if ( type == "string" )
|
||
|
{
|
||
|
opt = new DrStringOption;
|
||
|
}
|
||
|
if ( opt )
|
||
|
{
|
||
|
opt->setName( m.operator[]( "name" ).toString() );
|
||
|
opt->set( "text", m.operator[]( "comment" ).toString() );
|
||
15 years ago
|
TQString defval = m.operator[]( "default" ).toString();
|
||
15 years ago
|
if ( !defval.isEmpty() )
|
||
|
{
|
||
|
opt->setValueText( defval );
|
||
|
opt->set( "default", defval );
|
||
|
}
|
||
|
}
|
||
|
return opt;
|
||
|
}
|
||
|
|
||
|
DrMain* Foomatic2Loader::buildDriver() const
|
||
|
{
|
||
|
if ( m_foodata.isEmpty() )
|
||
|
return NULL;
|
||
|
|
||
14 years ago
|
TQVariant v = m_foodata.find( "VAR" ).data();
|
||
15 years ago
|
if ( !v.isNull() && v.type() == TQVariant::Map )
|
||
15 years ago
|
{
|
||
|
DrMain *driver = new DrMain;
|
||
15 years ago
|
TQMap<TQString,DrGroup*> groups;
|
||
15 years ago
|
driver->set( "manufacturer", v.mapFind( "make" ).data().toString() );
|
||
|
driver->set( "model", v.mapFind( "model" ).data().toString() );
|
||
|
driver->set( "matic_printer", v.mapFind( "id" ).data().toString() );
|
||
|
driver->set( "matic_driver", v.mapFind( "driver" ).data().toString() );
|
||
15 years ago
|
driver->set( "text", TQString( "%1 %2 (%3)" ).arg( driver->get( "manufacturer" ) ).arg( driver->get( "model" ) ).arg( driver->get( "matic_driver" ) ) );
|
||
14 years ago
|
if ( m_foodata.contains( "POSTPIPE" ) )
|
||
|
driver->set( "postpipe", m_foodata.find( "POSTPIPE" ).data().toString() );
|
||
15 years ago
|
v = v.mapFind( "args" ).data();
|
||
15 years ago
|
if ( !v.isNull() && v.type() == TQVariant::List )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<TQVariant>::ConstIterator it = v.listBegin();
|
||
15 years ago
|
for ( ; it!=v.listEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
if ( ( *it ).type() != TQVariant::Map )
|
||
15 years ago
|
continue;
|
||
|
DrBase *opt = createOption( ( *it ).toMap() );
|
||
|
if ( opt )
|
||
|
{
|
||
15 years ago
|
TQString group = DrGroup::groupForOption( opt->name() );
|
||
15 years ago
|
DrGroup *grp = NULL;
|
||
14 years ago
|
if ( !groups.contains( group ) )
|
||
15 years ago
|
{
|
||
|
grp = new DrGroup;
|
||
|
grp->set( "text", group );
|
||
|
driver->addGroup( grp );
|
||
|
groups.insert( group, grp );
|
||
|
}
|
||
|
else
|
||
|
grp = groups[ group ];
|
||
|
grp->addOption( opt );
|
||
|
if ( opt->name() == "PageSize" )
|
||
|
{
|
||
|
// try to add the corresponding page sizes
|
||
15 years ago
|
TQVariant choices = ( *it ).mapFind( "vals_byname" ).data();
|
||
|
TQRegExp re( "(\\d+) +(\\d+)" );
|
||
|
if ( choices.type() == TQVariant::Map )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQMap<TQString,TQVariant>::ConstIterator it = choices.mapBegin();
|
||
15 years ago
|
for ( ; it!=choices.mapEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
TQString driverval = ( *it ).mapFind( "driverval" ).data().toString();
|
||
15 years ago
|
if ( re.exactMatch( driverval ) )
|
||
|
{
|
||
|
driver->addPageSize( new DrPageSize( it.key(), re.cap( 1 ).toInt(), re.cap( 2 ).toInt(), 36, 24, 36, 24 ) );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
|
||
|
}
|
||
|
}
|
||
|
return driver;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
DrMain* Foomatic2Loader::modifyDriver( DrMain *driver ) const
|
||
|
{
|
||
|
if ( !m_foodata.isEmpty() )
|
||
|
{
|
||
15 years ago
|
TQValueList<DrBase*> optList;
|
||
15 years ago
|
DrGroup *grp = NULL;
|
||
|
|
||
14 years ago
|
TQVariant V = m_foodata.find( "VAR" ).data();
|
||
15 years ago
|
if ( !V.isNull() && V.type() == TQVariant::Map )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQVariant v = V.mapFind( "args" ).data();
|
||
|
if ( !v.isNull() && v.type() == TQVariant::List )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<TQVariant>::ConstIterator it = v.listBegin();
|
||
15 years ago
|
for ( ; it!=v.listEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
if ( ( *it ).type() != TQVariant::Map )
|
||
15 years ago
|
continue;
|
||
|
DrBase *opt = createOption( ( *it ).toMap() );
|
||
|
if ( opt )
|
||
|
optList.append( opt );
|
||
|
else
|
||
|
kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
v = V.mapFind( "args_byname" ).data();
|
||
15 years ago
|
if ( !v.isNull() && v.type() == TQVariant::Map )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQMap<TQString,TQVariant>::ConstIterator it = v.mapBegin();
|
||
15 years ago
|
for ( ; it!=v.mapEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
if ( ( *it ).type() != TQVariant::Map )
|
||
15 years ago
|
continue;
|
||
|
DrBase *opt = createOption( ( *it ).toMap() );
|
||
|
if ( opt )
|
||
|
optList.append( opt );
|
||
|
else
|
||
|
kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
for ( TQValueList<DrBase*>::ConstIterator it=optList.begin(); it!=optList.end(); ++it )
|
||
15 years ago
|
{
|
||
|
DrBase *opt = ( *it );
|
||
|
if ( opt )
|
||
|
{
|
||
|
switch ( opt->type() )
|
||
|
{
|
||
|
case DrBase::List:
|
||
|
case DrBase::Boolean:
|
||
|
delete opt;
|
||
|
break;
|
||
|
default:
|
||
|
{
|
||
|
if ( !grp )
|
||
|
{
|
||
|
grp = new DrGroup;
|
||
|
grp->set( "text", i18n( "Adjustments" ) );
|
||
|
driver->addGroup( grp );
|
||
|
}
|
||
14 years ago
|
DrBase *oldOpt = driver->findOption( opt->name() );
|
||
15 years ago
|
if ( oldOpt && oldOpt->type() == DrBase::List )
|
||
|
{
|
||
15 years ago
|
TQPtrListIterator<DrBase> it( *( static_cast<DrListOption*>( oldOpt )->choices() ) );
|
||
|
TQString fixedvals;
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
{
|
||
|
fixedvals.append( it.current()->name() );
|
||
|
if ( !it.atLast() )
|
||
|
fixedvals.append( "|" );
|
||
|
}
|
||
|
opt->set( "fixedvals", fixedvals );
|
||
|
}
|
||
|
driver->removeOptionGlobally( opt->name() );
|
||
|
grp->addOption( opt );
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return driver;
|
||
|
}
|
||
|
|
||
15 years ago
|
DrMain* Foomatic2Loader::loadDriver( const TQString& filename )
|
||
15 years ago
|
{
|
||
|
Foomatic2Loader loader;
|
||
|
if ( loader.readFromFile( filename ) )
|
||
|
return loader.buildDriver();
|
||
|
else
|
||
|
return NULL;
|
||
|
}
|