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.
532 lines
13 KiB
532 lines
13 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 "ppdloader.h"
|
||
|
#include "foomatic2loader.h"
|
||
|
#include "driver.h"
|
||
|
|
||
|
#include <kfilterdev.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <klocale.h>
|
||
15 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
#include <math.h>
|
||
|
|
||
13 years ago
|
void tdeprint_ppdscanner_init( TQIODevice* );
|
||
|
void tdeprint_ppdscanner_terminate( bool deleteIt = true );
|
||
|
int tdeprint_ppdscanner_numberoflines();
|
||
15 years ago
|
|
||
15 years ago
|
static TQString processLocaleString( const TQString& s )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString res;
|
||
15 years ago
|
uint pos = 0;
|
||
|
while ( pos < s.length() )
|
||
|
{
|
||
15 years ago
|
TQChar c = s[ pos++ ];
|
||
15 years ago
|
if ( c == '<' )
|
||
|
{
|
||
|
bool flag = false;
|
||
|
uint hc = 0;
|
||
|
while ( pos < s.length() )
|
||
|
{
|
||
15 years ago
|
TQChar cc = s[ pos++ ];
|
||
15 years ago
|
uint _hc = 0;
|
||
|
if ( cc == '>' )
|
||
|
break;
|
||
|
else if ( cc.isDigit() )
|
||
|
_hc = cc.digitValue();
|
||
|
else
|
||
|
_hc = cc.lower().latin1() - 'a' + 10;
|
||
|
if ( flag )
|
||
|
{
|
||
|
hc |= _hc;
|
||
15 years ago
|
res.append( TQChar( hc ) );
|
||
15 years ago
|
hc = 0;
|
||
|
}
|
||
|
else
|
||
|
hc = ( _hc << 4 );
|
||
|
flag = !flag;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
res.append( c );
|
||
|
}
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
15 years ago
|
static TQValueList<float> splitNumberString( const TQString& _s )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString s = _s.simplifyWhiteSpace();
|
||
|
TQValueList<float> l;
|
||
15 years ago
|
int p1 = 1, p2 = 0;
|
||
|
while ( true )
|
||
|
{
|
||
13 years ago
|
p2 = s.find( ' ', p1 );
|
||
15 years ago
|
if ( p2 != -1 )
|
||
|
{
|
||
|
l.append( s.mid( p1, p2-p1 ).toFloat() );
|
||
|
p1 = p2+1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// ignore the final quote
|
||
|
l.append( s.mid( p1, s.length() - p1 - 1 ).toFloat() );
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return l;
|
||
|
}
|
||
|
|
||
|
struct PS_private
|
||
|
{
|
||
15 years ago
|
TQString name;
|
||
15 years ago
|
struct
|
||
|
{
|
||
|
float width, height;
|
||
|
} size;
|
||
|
struct
|
||
|
{
|
||
|
float left, bottom, right, top;
|
||
|
} area;
|
||
|
};
|
||
|
|
||
|
PPDLoader::PPDLoader()
|
||
|
{
|
||
|
m_option = 0;
|
||
|
m_ps.setAutoDelete( true );
|
||
|
}
|
||
|
|
||
|
PPDLoader::~PPDLoader()
|
||
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
DrMain* PPDLoader::readFromFile( const TQString& filename )
|
||
15 years ago
|
{
|
||
|
// Initialization
|
||
|
m_groups.clear();
|
||
|
m_option = NULL;
|
||
|
m_fonts.clear();
|
||
|
// Open driver file
|
||
15 years ago
|
TQIODevice *d = KFilterDev::deviceForFile( filename );
|
||
15 years ago
|
if ( d && d->open( IO_ReadOnly ) )
|
||
|
{
|
||
|
DrMain *driver = new DrMain;
|
||
|
bool result = true;
|
||
|
|
||
|
m_groups.push( driver );
|
||
13 years ago
|
tdeprint_ppdscanner_init( d );
|
||
|
if ( tdeprint_ppdparse( this ) != 0 )
|
||
15 years ago
|
result = false;
|
||
13 years ago
|
tdeprint_ppdscanner_terminate( true );
|
||
15 years ago
|
|
||
|
if ( result )
|
||
|
{
|
||
|
if ( m_groups.size() > 1 )
|
||
|
kdWarning( 500 ) << "PPD syntax error, GROUP specification not correctly closed" << endl;
|
||
|
if ( driver->has( "foodata" ) )
|
||
|
{
|
||
|
Foomatic2Loader loader;
|
||
|
if ( loader.readFromBuffer( driver->get( "foodata" ) ) )
|
||
|
{
|
||
|
driver = loader.modifyDriver( driver );
|
||
|
}
|
||
|
else
|
||
|
kdWarning( 500 ) << "PPD syntax error, Foomatic data read failed" << endl;
|
||
|
}
|
||
|
processPageSizes( driver );
|
||
|
if ( !m_fonts.isEmpty() )
|
||
|
driver->set( "fonts", m_fonts.join( "," ) );
|
||
|
return driver;
|
||
|
}
|
||
|
else
|
||
|
kdWarning( 500 ) << "PPD syntax error, PPD parse failed" << endl;
|
||
|
delete driver;
|
||
|
m_ps.clear();
|
||
|
}
|
||
|
else
|
||
|
kdWarning( 500 ) << "PPD read error, unable to open device for file " << filename << endl;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
DrMain* PPDLoader::loadDriver( const TQString& filename, TQString* msg )
|
||
15 years ago
|
{
|
||
|
PPDLoader loader;
|
||
|
DrMain *driver = loader.readFromFile( filename );
|
||
|
if ( !driver && msg )
|
||
13 years ago
|
*msg = filename + i18n( "(line %1): " ).arg( tdeprint_ppdscanner_numberoflines() ) + loader.errorMsg();
|
||
15 years ago
|
return driver;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::openUi( const TQString& name, const TQString& desc, const TQString& type )
|
||
15 years ago
|
{
|
||
|
if ( m_option )
|
||
|
{
|
||
|
qWarning( "PPD syntax error, UI specification not correctly closed" );
|
||
|
endUi( m_option->name() );
|
||
|
}
|
||
|
|
||
|
if ( type == "PickOne" || type == "PickMany" )
|
||
|
m_option = new DrListOption;
|
||
|
else if ( type == "Boolean" )
|
||
|
m_option = new DrBooleanOption;
|
||
|
else
|
||
|
return false;
|
||
|
if ( name[ 0 ] == '*' )
|
||
|
m_option->setName( name.mid( 1 ) );
|
||
|
else
|
||
|
m_option->setName( name );
|
||
|
if ( desc.isEmpty() )
|
||
|
m_option->set( "text", m_option->name() );
|
||
|
else
|
||
|
m_option->set( "text", processLocaleString( desc ) );
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::endUi( const TQString& name )
|
||
15 years ago
|
{
|
||
|
if ( m_option && ( m_option->name() == name || m_option->name() == name.mid( 1 ) ) )
|
||
|
{
|
||
|
if ( m_option->name() == "PageRegion" )
|
||
|
delete m_option;
|
||
|
else
|
||
|
{
|
||
15 years ago
|
TQString defval = m_option->get( "default" );
|
||
15 years ago
|
DrGroup *grp = 0;
|
||
|
if ( !defval.isEmpty() )
|
||
|
m_option->setValueText( defval );
|
||
|
if ( m_groups.size() == 1 )
|
||
|
{
|
||
|
// we don't have any group defined, create the
|
||
|
// most adapted one.
|
||
14 years ago
|
grp = findOrCreateGroupForOption( m_option->name() );
|
||
15 years ago
|
}
|
||
|
else
|
||
|
grp = m_groups.top();
|
||
|
grp->addOption( m_option );
|
||
13 years ago
|
if ( grp->get( "text" ).contains( "install", false ) )
|
||
15 years ago
|
m_option->set( "fixed", "1" );
|
||
|
}
|
||
|
m_option = 0;
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::openGroup( const TQString& name, const TQString& desc )
|
||
15 years ago
|
{
|
||
|
DrGroup *grp = new DrGroup;
|
||
|
grp->setName( name );
|
||
|
if ( desc.isEmpty() )
|
||
|
grp->set( "text", name );
|
||
|
else
|
||
|
grp->set( "text", processLocaleString( desc ) );
|
||
|
m_groups.top()->addGroup( grp );
|
||
|
m_groups.push( grp );
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::endGroup( const TQString& name )
|
||
15 years ago
|
{
|
||
|
if ( m_groups.size() > 1 && m_groups.top()->name() == name )
|
||
|
{
|
||
|
m_groups.pop();
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putStatement( const TQString& keyword, const TQString& name, const TQString& desc, const TQStringList& values )
|
||
15 years ago
|
{
|
||
|
if ( m_option )
|
||
|
{
|
||
|
if ( !name.isEmpty() && m_option->name() == keyword )
|
||
|
{
|
||
|
if ( m_option->type() >= DrBase::List )
|
||
|
{
|
||
|
DrBase *ch = new DrBase;
|
||
|
ch->setName( name );
|
||
|
if ( desc.isEmpty() )
|
||
|
ch->set( "text", name );
|
||
|
else
|
||
|
ch->set( "text", processLocaleString( desc ) );
|
||
|
static_cast<DrListOption*>( m_option )->addChoice( ch );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
15 years ago
|
TQString fv = m_option->get( "fixedvals" );
|
||
15 years ago
|
if ( fv.isEmpty() )
|
||
|
fv = name;
|
||
|
else
|
||
|
fv.append( "|" + name );
|
||
|
m_option->set( "fixedvals", fv );
|
||
|
}
|
||
|
}
|
||
|
else if ( keyword == "FoomaticRIPOption" && name == m_option->name()
|
||
|
&& values.size() > 1 )
|
||
|
{
|
||
15 years ago
|
TQString type = values[ 0 ];
|
||
15 years ago
|
if ( type == "float" || type == "int" )
|
||
|
{
|
||
|
DrBase *opt = 0;
|
||
|
if ( type == "float" )
|
||
|
opt = new DrFloatOption;
|
||
|
else
|
||
|
opt = new DrIntegerOption;
|
||
|
opt->setName( m_option->name() );
|
||
|
opt->set( "text", m_option->get( "text" ) );
|
||
|
opt->set( "default", m_option->get( "default" ) );
|
||
|
if ( m_option->type() == DrBase::List )
|
||
|
{
|
||
15 years ago
|
TQStringList vals;
|
||
|
TQPtrListIterator<DrBase> it( *( static_cast<DrListOption*>( m_option )->choices() ) );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
vals.append( it.current()->name() );
|
||
|
opt->set( "fixedvals", vals.join( "|" ) );
|
||
|
}
|
||
|
delete m_option;
|
||
|
m_option = opt;
|
||
|
}
|
||
|
// FIXME: support other option types
|
||
|
}
|
||
|
else if ( keyword == "FoomaticRIPOptionRange" && name == m_option->name()
|
||
|
&& values.size() >= 2 && ( m_option->type() == DrBase::Float || m_option->type() == DrBase::Integer ) )
|
||
|
{
|
||
|
m_option->set( "minval", values[ 0 ] );
|
||
|
m_option->set( "maxval", values[ 1 ] );
|
||
|
}
|
||
|
}
|
||
|
else if ( keyword == "Font" && m_groups.size() > 0 )
|
||
|
{
|
||
|
m_fonts << name;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putStatement2( const TQString& keyword, const TQString& value )
|
||
15 years ago
|
{
|
||
|
if ( !m_option && m_groups.size() == 1 )
|
||
|
{
|
||
|
DrGroup *driver = m_groups.top();
|
||
|
if ( keyword == "NickName" )
|
||
|
{
|
||
|
driver->set( "text", value );
|
||
|
driver->set( "description", value );
|
||
|
}
|
||
|
else if ( keyword == "Manufacturer" )
|
||
|
driver->set( "manufacturer", value );
|
||
|
else if ( keyword == "ShortNickName" )
|
||
|
driver->set( "model", value );
|
||
|
else if ( keyword == "ColorDevice" )
|
||
|
driver->set( "colordevice", value == "True" ? "1" : "0" );
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putDefault( const TQString& keyword, const TQString& value )
|
||
15 years ago
|
{
|
||
|
if ( keyword == "Resolution" && m_groups.size() > 0 )
|
||
|
{
|
||
|
// Store default resolution as it could be fed back
|
||
|
// to the application. And default resolution can
|
||
|
// occur outside a OpenUI/CloseUI pair.
|
||
|
m_groups[ 0 ]->set( "resolution", value );
|
||
|
}
|
||
|
|
||
|
if ( m_option && m_option->name() == keyword )
|
||
|
{
|
||
|
m_option->set( "default", value );
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
return false;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putConstraint( const TQString& opt1, const TQString& opt2, const TQString& ch1, const TQString& ch2 )
|
||
15 years ago
|
{
|
||
|
if ( !m_option && m_groups.size() == 1 )
|
||
|
{
|
||
|
DrMain *driver = static_cast<DrMain*>( m_groups.top() );
|
||
|
driver->addConstraint( new DrConstraint( opt1, opt2, ch1, ch2 ) );
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putFooData( const TQString& data )
|
||
15 years ago
|
{
|
||
|
if ( !m_option && m_groups.size() == 1 )
|
||
|
{
|
||
|
m_groups.top()->set( "foodata", m_groups.top()->get( "foodata" ) + data + "\n" );
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putFooProcessedData( const TQVariant& var )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQMap<TQString,TQVariant>::ConstIterator it = var.mapFind( "args_byname" );
|
||
15 years ago
|
if ( it != var.mapEnd() )
|
||
|
{
|
||
15 years ago
|
TQVariant opts = it.data();
|
||
15 years ago
|
for ( it = opts.mapBegin(); it != opts.mapEnd(); ++it )
|
||
|
{
|
||
15 years ago
|
TQMap<TQString,TQVariant> opt = it.data().toMap();
|
||
|
TQString type = opt[ "type" ].toString();
|
||
15 years ago
|
if ( type == "float" || type == "int" )
|
||
|
{
|
||
|
DrBase *o;
|
||
|
if ( type == "float" )
|
||
|
o = new DrFloatOption;
|
||
|
else
|
||
|
o = new DrIntegerOption;
|
||
|
o->setName( opt[ "name" ].toString() );
|
||
|
o->set( "text", opt[ "comment" ].toString() );
|
||
|
o->set( "minval", opt[ "min" ].toString() );
|
||
|
o->set( "maxval", opt[ "max" ].toString() );
|
||
|
o->set( "default", opt[ "default" ].toString() );
|
||
|
o->setValueText( o->get( "default" ) );
|
||
|
|
||
|
DrGroup *grp = 0;
|
||
14 years ago
|
DrBase *old = m_groups.top()->findOption( o->name(), &grp );
|
||
15 years ago
|
if ( old )
|
||
|
{
|
||
|
if ( old->type() == DrBase::List )
|
||
|
{
|
||
15 years ago
|
TQStringList vals;
|
||
|
TQPtrListIterator<DrBase> it( *( static_cast<DrListOption*>( old )->choices() ) );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
vals.append( it.current()->name() );
|
||
|
o->set( "fixedvals", vals.join( "|" ) );
|
||
|
}
|
||
|
grp->removeOption( o->name() );
|
||
|
grp->addOption( o );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
qWarning( "Option %s not found in original PPD file", o->name().latin1() );
|
||
|
delete o;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putPaperDimension( const TQString& name, const TQString& s )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<float> l = splitNumberString( s );
|
||
15 years ago
|
|
||
13 years ago
|
PS_private *ps = m_ps.find( name );
|
||
15 years ago
|
if ( !ps )
|
||
|
{
|
||
|
ps = new PS_private;
|
||
|
ps->name = name;
|
||
|
m_ps.insert( name, ps );
|
||
|
}
|
||
|
ps->size.width = l[ 0 ];
|
||
|
ps->size.height = l[ 1 ];
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool PPDLoader::putImageableArea( const TQString& name, const TQString& s )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<float> l = splitNumberString( s );
|
||
15 years ago
|
|
||
13 years ago
|
PS_private *ps = m_ps.find( name );
|
||
15 years ago
|
if ( !ps )
|
||
|
{
|
||
|
ps = new PS_private;
|
||
|
ps->name = name;
|
||
|
m_ps.insert( name, ps );
|
||
|
}
|
||
|
ps->area.left = l[ 0 ];
|
||
|
ps->area.bottom = l[ 1 ];
|
||
|
ps->area.right = l[ 2 ];
|
||
|
ps->area.top = l[ 3 ];
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
DrGroup* PPDLoader::findOrCreateGroupForOption( const TQString& optname )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString grpname;
|
||
15 years ago
|
if ( optname == "PageSize" ||
|
||
|
optname == "InputSlot" ||
|
||
|
optname == "ManualFeed" ||
|
||
|
optname == "MediaType" ||
|
||
|
optname == "MediaColor" ||
|
||
|
optname == "MediaWeight" )
|
||
|
grpname = "General";
|
||
|
else if ( optname.startsWith( "stp" ) ||
|
||
|
optname == "Cyan" ||
|
||
|
optname == "Yellow" ||
|
||
|
optname == "Magenta" ||
|
||
|
optname == "Density" ||
|
||
|
optname == "Contrast" )
|
||
|
grpname = "Adjustments";
|
||
|
else if ( optname.startsWith( "JCL" ) )
|
||
|
grpname = "JCL";
|
||
|
else
|
||
|
grpname = "Others";
|
||
|
|
||
|
DrGroup *grp = 0;
|
||
15 years ago
|
for ( TQPtrListIterator<DrGroup> it( m_groups[ 0 ]->groups() ); it.current(); ++it )
|
||
15 years ago
|
if ( it.current()->name() == grpname )
|
||
|
{
|
||
|
grp = it.current();
|
||
|
break;
|
||
|
}
|
||
|
if ( !grp )
|
||
|
{
|
||
|
grp = new DrGroup;
|
||
|
grp->setName( grpname );
|
||
|
grp->set( "text", grpname );
|
||
|
m_groups[ 0 ]->addGroup( grp );
|
||
|
}
|
||
|
return grp;
|
||
|
}
|
||
|
|
||
|
void PPDLoader::processPageSizes( DrMain *driver )
|
||
|
{
|
||
15 years ago
|
TQDictIterator<PS_private> it( m_ps );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
{
|
||
|
//qDebug( "ADDING PAGESIZE: %16s, Size = ( %.2f, %.2f ), Area = ( %.2f, %.2f, %.2f, %.2f )", it.current()->name.latin1(),
|
||
|
// it.current()->size.width, it.current()->size.height,
|
||
|
// it.current()->area.left, it.current()->area.bottom,
|
||
|
// it.current()->area.right, it.current()->area.top );
|
||
|
driver->addPageSize( new DrPageSize( it.current()->name,
|
||
|
( int )it.current()->size.width, ( int )it.current()->size.height,
|
||
|
( int )it.current()->area.left, ( int )it.current()->area.bottom,
|
||
|
( int )ceil( it.current()->size.width - it.current()->area.right ),
|
||
|
( int )ceil( it.current()->size.height - it.current()->area.top ) ) );
|
||
|
}
|
||
|
m_ps.clear();
|
||
|
}
|
||
|
|
||
15 years ago
|
void PPDLoader::setErrorMsg( const TQString& msg )
|
||
15 years ago
|
{
|
||
|
m_errormsg = msg;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString PPDLoader::errorMsg() const
|
||
15 years ago
|
{
|
||
|
return m_errormsg;
|
||
|
}
|