• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
foomatic2loader.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001-2003 Michael Goffioul <tdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "foomatic2loader.h"
21 #include "driver.h"
22 
23 #include <tqfile.h>
24 #include <tqregexp.h>
25 #include <tqbuffer.h>
26 #include <kdebug.h>
27 #include <tdelocale.h>
28 
29 void tdeprint_foomatic2scanner_init( TQIODevice* );
30 void tdeprint_foomatic2scanner_terminate();
31 
32 Foomatic2Loader::Foomatic2Loader()
33 {
34 }
35 
36 Foomatic2Loader::~Foomatic2Loader()
37 {
38 }
39 
40 bool Foomatic2Loader::read( TQIODevice *d )
41 {
42  bool result = true;
43  m_foodata.clear();
44  tdeprint_foomatic2scanner_init( d );
45  if ( tdeprint_foomatic2parse( this ) != 0 )
46  result = false;
47  tdeprint_foomatic2scanner_terminate();
48  return result;
49 }
50 
51 bool Foomatic2Loader::readFromFile( const TQString& filename )
52 {
53  TQFile f( filename );
54  m_foodata.clear();
55  if ( f.open( IO_ReadOnly ) )
56  return read( &f );
57  return false;
58 }
59 
60 bool Foomatic2Loader::readFromBuffer( const TQString& buffer )
61 {
62  TQCString buf = buffer.utf8();
63  TQBuffer d( buf );
64  m_foodata.clear();
65  if ( d.open( IO_ReadOnly ) )
66  return read( &d );
67  return false;
68 }
69 
70 DrBase* Foomatic2Loader::createValue( const TQString& name, const TQStringVariantMap& m ) const
71 {
72  DrBase *choice = new DrBase;
73  choice->setName( name );
74  choice->set( "text", m.operator[]( "comment" ).toString() );
75  return choice;
76 }
77 
78 DrBase* Foomatic2Loader::createOption( const TQStringVariantMap& m ) const
79 {
80  TQString type = m.operator[]( "type" ).toString();
81  DrBase *opt = NULL;
82  if ( type == "enum" )
83  {
84  DrListOption *lopt = new DrListOption;
85  TQVariant a = m.operator[]( "vals_byname" );
86  TQStringVariantMap::ConstIterator it = a.mapBegin();
87  for ( ; it!=a.mapEnd(); ++it )
88  {
89  if ( it.data().type() != TQVariant::Map )
90  continue;
91  DrBase *ch = createValue( it.key(), it.data().toMap() );
92  if ( ch )
93  lopt->addChoice( ch );
94  }
95  opt = lopt;
96  }
97  else if ( type == "int" || type == "float" )
98  {
99  if ( type == "int" )
100  opt = new DrIntegerOption;
101  else
102  opt = new DrFloatOption;
103  opt->set( "minval", m.operator[]( "min" ).toString() );
104  opt->set( "maxval", m.operator[]( "max" ).toString() );
105  }
106  else if ( type == "bool" )
107  {
108  DrBooleanOption *bopt = new DrBooleanOption;
109  DrBase *choice;
110  // choice 1
111  choice = new DrBase;
112  choice->setName( "0" );
113  choice->set( "text", m.operator[]( "name_false" ).toString() );
114  bopt->addChoice( choice );
115  choice = new DrBase;
116  choice->setName( "1" );
117  choice->set( "text", m.operator[]( "name_true" ).toString() );
118  bopt->addChoice( choice );
119  opt = bopt;
120  }
121  else if ( type == "string" )
122  {
123  opt = new DrStringOption;
124  }
125  if ( opt )
126  {
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() )
131  {
132  opt->setValueText( defval );
133  opt->set( "default", defval );
134  }
135  }
136  return opt;
137 }
138 
139 DrMain* Foomatic2Loader::buildDriver() const
140 {
141  if ( m_foodata.isEmpty() )
142  return NULL;
143 
144  TQVariant v = m_foodata.find( "VAR" ).data();
145  if ( !v.isNull() && v.type() == TQVariant::Map )
146  {
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 )
158  {
159  TQValueList<TQVariant>::ConstIterator it = v.listBegin();
160  for ( ; it!=v.listEnd(); ++it )
161  {
162  if ( ( *it ).type() != TQVariant::Map )
163  continue;
164  DrBase *opt = createOption( ( *it ).toMap() );
165  if ( opt )
166  {
167  TQString group = DrGroup::groupForOption( opt->name() );
168  DrGroup *grp = NULL;
169  if ( !groups.contains( group ) )
170  {
171  grp = new DrGroup;
172  grp->set( "text", group );
173  driver->addGroup( grp );
174  groups.insert( group, grp );
175  }
176  else
177  grp = groups[ group ];
178  grp->addOption( opt );
179  if ( opt->name() == "PageSize" )
180  {
181  // try to add the corresponding page sizes
182  TQVariant choices = ( *it ).mapFind( "vals_byname" ).data();
183  TQRegExp re( "(\\d+) +(\\d+)" );
184  if ( choices.type() == TQVariant::Map )
185  {
186  TQStringVariantMap::ConstIterator it = choices.mapBegin();
187  for ( ; it!=choices.mapEnd(); ++it )
188  {
189  TQString driverval = ( *it ).mapFind( "driverval" ).data().toString();
190  if ( re.exactMatch( driverval ) )
191  {
192  driver->addPageSize( new DrPageSize( it.key(), re.cap( 1 ).toInt(), re.cap( 2 ).toInt(), 36, 24, 36, 24 ) );
193  }
194  }
195  }
196  }
197  }
198  else
199  kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
200  }
201  }
202  return driver;
203  }
204  return NULL;
205 }
206 
207 DrMain* Foomatic2Loader::modifyDriver( DrMain *driver ) const
208 {
209  if ( !m_foodata.isEmpty() )
210  {
211  TQValueList<DrBase*> optList;
212  DrGroup *grp = NULL;
213 
214  TQVariant V = m_foodata.find( "VAR" ).data();
215  if ( !V.isNull() && V.type() == TQVariant::Map )
216  {
217  TQVariant v = V.mapFind( "args" ).data();
218  if ( !v.isNull() && v.type() == TQVariant::List )
219  {
220  TQValueList<TQVariant>::ConstIterator it = v.listBegin();
221  for ( ; it!=v.listEnd(); ++it )
222  {
223  if ( ( *it ).type() != TQVariant::Map )
224  continue;
225  DrBase *opt = createOption( ( *it ).toMap() );
226  if ( opt )
227  optList.append( opt );
228  else
229  kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
230  }
231  }
232  else
233  {
234  v = V.mapFind( "args_byname" ).data();
235  if ( !v.isNull() && v.type() == TQVariant::Map )
236  {
237  TQStringVariantMap::ConstIterator it = v.mapBegin();
238  for ( ; it!=v.mapEnd(); ++it )
239  {
240  if ( ( *it ).type() != TQVariant::Map )
241  continue;
242  DrBase *opt = createOption( ( *it ).toMap() );
243  if ( opt )
244  optList.append( opt );
245  else
246  kdWarning( 500 ) << "Failed to create option: " << ( *it ).toMap()[ "name" ].toString() << endl;
247  }
248  }
249  }
250  }
251 
252  for ( TQValueList<DrBase*>::ConstIterator it=optList.begin(); it!=optList.end(); ++it )
253  {
254  DrBase *opt = ( *it );
255  if ( opt )
256  {
257  switch ( opt->type() )
258  {
259  case DrBase::List:
260  case DrBase::Boolean:
261  delete opt;
262  break;
263  default:
264  {
265  if ( !grp )
266  {
267  grp = new DrGroup;
268  grp->set( "text", i18n( "Adjustments" ) );
269  driver->addGroup( grp );
270  }
271  DrBase *oldOpt = driver->findOption( opt->name() );
272  if ( oldOpt && oldOpt->type() == DrBase::List )
273  {
274  TQPtrListIterator<DrBase> it( *( static_cast<DrListOption*>( oldOpt )->choices() ) );
275  TQString fixedvals;
276  for ( ; it.current(); ++it )
277  {
278  fixedvals.append( it.current()->name() );
279  if ( !it.atLast() )
280  fixedvals.append( "|" );
281  }
282  opt->set( "fixedvals", fixedvals );
283  }
284  driver->removeOptionGlobally( opt->name() );
285  grp->addOption( opt );
286  break;
287  }
288  }
289  }
290  }
291  }
292  return driver;
293 }
294 
295 DrMain* Foomatic2Loader::loadDriver( const TQString& filename )
296 {
297  Foomatic2Loader loader;
298  if ( loader.readFromFile( filename ) )
299  return loader.buildDriver();
300  else
301  return NULL;
302 }

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.1
This website is maintained by Timothy Pearson.