00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "guiprintermanualsetup.h"
00030 #include <printersetupdb.h>
00031 #include "globals.h"
00032
00033
00034 #include <qmessagebox.h>
00035 #include <qlistbox.h>
00036 #include <qlabel.h>
00037 #include <qpushbutton.h>
00038 #include <qfile.h>
00039 #include <qdir.h>
00040 #include <qtextstream.h>
00041 #include <qstring.h>
00042 #include <qdom.h>
00043 #include <qapplication.h>
00044 #include <qcombobox.h>
00045 #include <qlineedit.h>
00046 #include <qmessagebox.h>
00047 #include <qprinter.h>
00048 #include <qregexp.h>
00049 #include <qvalidator.h>
00050 #include <qcursor.h>
00051 #include <qpixmap.h>
00052 #include <qinputdialog.h>
00053
00054
00060 GuiPrinterManualSetup::GuiPrinterManualSetup( QWidget* parent, const char* name )
00061 : GuiPrinterManualSetupBase( parent,name )
00062 {
00063 m_db = new PrinterSetupDb( LPS_DB_DOCUMENT_NAME );
00064 m_translationRecommended = tr( "recommended", "This will be a appended to any recommended printer" );
00065
00066 connect( pushButton_back, SIGNAL(clicked() ), this, SIGNAL( back() ) );
00067 }
00068
00069
00073 GuiPrinterManualSetup::~GuiPrinterManualSetup()
00074 {
00075 if ( m_db )
00076 delete m_db;
00077
00078 qDebug("Destructor GuiPrinterManualSetup finished");
00079 }
00080
00081
00088 void GuiPrinterManualSetup::enableWidgets( int wf )
00089 {
00090
00091 listBox_models->setEnabled( wf & ModelsFlag );
00092 listBox_drivers->setEnabled( wf & DriversFlag );
00093 listBox_manufacturers->setEnabled( wf & ManufacturersFlag );
00094
00095
00096 label_drivers->setEnabled( wf & DriversFlag );
00097 label_models->setEnabled( wf & ModelsFlag );
00098 label_manufacturers->setEnabled( wf & ManufacturersFlag );
00099
00100
00101 comboBox_ports->setEnabled( wf & PortsFlag );
00102
00103
00104 pushButton_driverDetails->setEnabled( wf & DetailsFlag );
00105 pushButton_saveSettings->setEnabled( wf & SaveSettingsFlag );
00106 }
00107
00108
00115 void GuiPrinterManualSetup::on_listBox_manufacturers_highlighted( QListBoxItem * item )
00116 {
00117 if ( !item )
00118 return;
00119
00120 listBox_models->clear();
00121 listBox_drivers->clear();
00122
00123
00124 QDomNodeList models = m_db->getModels( item->text() );
00125
00126
00127 for ( uint i=0; i<models.length(); i++ )
00128 {
00129 QDomElement modelElement = models.item( i ).toElement();
00130
00131 if ( !modelElement.isNull() )
00132 {
00133 listBox_models->insertItem( modelElement.attribute( "name" ) );
00134 }
00135 }
00136
00137
00138 if ( listBox_models->count() <= 0 )
00139 {
00140 label_status->setPaletteBackgroundColor( Qt::red );
00141 label_status->setText( tr( "No model found for this manufacturer." ));
00142 enableWidgets( AllFlags ^ SaveSettingsFlag ^ DriversFlag ^ DetailsFlag ^ ModelsFlag );
00143 }
00144
00145 enableWidgets( AllFlags ^ PortsFlag ^ DriversFlag ^ DetailsFlag ^ SaveSettingsFlag );
00146
00147 label_status->setPaletteBackgroundColor( Qt::yellow );
00148 label_status->setText( tr( "Please choose a model now." ) );
00149 }
00150
00151
00158 void GuiPrinterManualSetup::on_listBox_models_highlighted( QListBoxItem * item )
00159 {
00160
00161 QListBoxItem * selectedManufacturerItem = listBox_manufacturers->selectedItem();
00162
00163
00164 if ( !item || !selectedManufacturerItem )
00165 return;
00166
00167 listBox_drivers->clear();
00168
00169
00170
00171 QDomNodeList drivers = m_db->getDriversByModel( selectedManufacturerItem->text(), item->text() );
00172
00173
00174 for ( uint i=0; i<drivers.length(); i++ )
00175 {
00176 QDomElement driverElement = drivers.item( i ).toElement();
00177
00178 if ( !driverElement.isNull() )
00179 {
00180
00181 listBox_drivers->insertItem( driverElement.attribute( "nickName" ).replace( "recommended", m_translationRecommended ) );
00182
00183 uint count = listBox_drivers->count();
00184
00185
00186 if ( listBox_drivers->text( count - 1 ).contains( m_translationRecommended ) )
00187 {
00188 listBox_drivers->setCurrentItem( count - 1 );
00189 enableWidgets( AllFlags );
00190 }
00191 }
00192 }
00193
00194
00195 if ( listBox_drivers->count() <= 0 )
00196 {
00197 label_status->setPaletteBackgroundColor( Qt::red );
00198 label_status->setText( tr( "No driver found for this model." ));
00199 enableWidgets( AllFlags ^ SaveSettingsFlag ^ DriversFlag ^ DetailsFlag );
00200 }
00201
00202 else if( !listBox_drivers->selectedItem() )
00203 {
00204 enableWidgets( AllFlags ^ SaveSettingsFlag ^ PortsFlag );
00205 listBox_drivers->setCurrentItem( 0 );
00206 }
00207
00208 lineEdit_printername->setText( item->text().replace( " ", "_" ) );
00209 }
00210
00211
00219 void GuiPrinterManualSetup::on_listBox_drivers_highlighted( QListBoxItem * item )
00220 {
00221
00222 if ( !item )
00223 return;
00224
00225 label_status->setPaletteBackgroundColor( Qt::yellow );
00226
00227
00228 if( comboBox_ports->currentText() == "" )
00229 {
00230 enableWidgets(AllFlags ^ SaveSettingsFlag);
00231 label_status->setPaletteBackgroundColor( Qt::yellow );
00232 label_status->setText( tr( "Driver:" ) +" " + item->text() + "<br>" +
00233 tr( "Port:" ) + " " + tr( "Not yet selected." ) );
00234 }
00235
00236 else
00237 {
00238 enableWidgets(AllFlags);
00239 label_status->setPaletteBackgroundColor( Qt::green );
00240 label_status->setText( tr( "Driver:" ) +" " + item->text() + "<br>" +
00241 tr( "Port:" ) + " " + comboBox_ports->currentText() );
00242 }
00243 }
00244
00245
00249 void GuiPrinterManualSetup::on_pushButton_help_clicked()
00250 {
00251 emit helpRequest( "printerManualSetup.html" );
00252 }
00253
00254
00263 void GuiPrinterManualSetup::on_pushButton_saveSettings_clicked()
00264 {
00265 try
00266 {
00267
00268 if ( lineEdit_printername->text() == "" )
00269 throw LpsNotice( tr( "Printer is unnamed. Please type in a name." ) );
00270
00271
00272 QString driverName = listBox_drivers->selectedItem()->text().replace( m_translationRecommended, "recommended" );
00273
00274
00275 QDomElement driver = m_db->getDriverByModel(
00276 listBox_manufacturers->selectedItem()->text(),
00277 listBox_models->selectedItem()->text(),
00278 driverName );
00279
00280
00281 if ( driver.isNull() )
00282 throw LpsError( "The driver element was NULL." );
00283
00284
00285 QString printerName = lineEdit_printername->text();
00286 QString deviceUri = comboBox_ports->currentText();
00287 QString ppdFilePath = driver.attribute( "ppdFilePath" );
00288
00289
00290 installPrinter( this, printerName, deviceUri, ppdFilePath );
00291
00292
00293 testPrintRequest( this, printerName );
00294
00295 label_status->setText( "<p>" + tr( "Your printer has been set up successfully." ) + "</p>");
00296 label_status->setPaletteBackgroundColor( Qt::green );
00297 }
00298 catch ( LpsNotice e )
00299 {
00300 label_status->setPaletteBackgroundColor( Qt::yellow );
00301 label_status->setText( e );
00302 return;
00303 }
00304 catch ( LpsError e )
00305 {
00306 label_status->setPaletteBackgroundColor( Qt::red );
00307 label_status->setText( e );
00308 return;
00309 }
00310
00311 label_status->setText( tr( "<p>Your printer has been configured successfully!</p>" ) );
00312 label_status->setPaletteBackgroundColor( Qt::green );
00313 }
00314
00315
00324 void GuiPrinterManualSetup::show()
00325 {
00326 GuiPrinterManualSetupBase::show();
00327
00328 emit showSmallHelp( "smallPrinterManualSetup.html" );
00329 label_status->setPaletteBackgroundColor( Qt::yellow );
00330 label_status->setText(tr( "Loading database..." ));
00331 setCursor( Qt::WaitCursor );
00332 setCaption( tr( "Manual Printer Setup" ) );
00333 qApp->processEvents();
00334
00335 enableWidgets( NoneFlag | ManufacturersFlag );
00336
00337
00338 QRegExp rx( "[a-zA-Z_0-9]+" );
00339 QValidator * validator = new QRegExpValidator( rx, this );
00340 lineEdit_printername->setValidator( validator );
00341
00342
00343 listBox_drivers->clear();
00344 listBox_models->clear();
00345 listBox_manufacturers->clear();
00346
00347
00348 lineEdit_printername->setText( LPS_PRINTER_NAME );
00349 QString fileName= MP_DIR() + MONEYPENNY_DATA_DIR + LPS_DB_FILE;
00350 QFile file( fileName );
00351
00352 try
00353 {
00354 if ( !file.open( IO_ReadOnly ) )
00355 throw LpsError( tr( "Could not open the database file." ) );
00356 if ( !m_db->setContent( &file ) )
00357 throw LpsError( tr( "Could not set content of xml database." ) );
00358 }
00359 catch ( LpsError e )
00360 {
00361 label_status->setPaletteBackgroundColor( Qt::red );
00362 label_status->setText( e );
00363 qDebug( e.latin1() );
00364 enableWidgets( NoneFlag );
00365 }
00366
00367
00368 QDomNodeList manufacturers = m_db->getManufacturers();
00369 for ( uint i=0; i<manufacturers.length(); i++ )
00370 {
00371 QDomElement manufacturersElement = manufacturers.item( i ).toElement();
00372 if ( !manufacturersElement.isNull() )
00373 {
00374 listBox_manufacturers->insertItem( manufacturersElement.attribute( "name" ) );
00375 }
00376 }
00377
00378
00379 if ( listBox_manufacturers->count() <= 0 )
00380 {
00381 label_status->setPaletteBackgroundColor( Qt::red );
00382 label_status->setText( tr( "No manufacturers found." ) );
00383 enableWidgets( NoneFlag );
00384 }
00385 else
00386 {
00387 label_status->setPaletteBackgroundColor( Qt::yellow );
00388 label_status->setText( tr( "Please select your printer's manufacturer name from the manufacturer list box." ) );
00389 }
00390
00391 readPorts();
00392 unsetCursor();
00393 }
00394
00395
00402 void GuiPrinterManualSetup::on_comboBox_ports_activated( const QString & itemtext )
00403 {
00404
00405 if ( itemtext == "" )
00406 {
00407 enableWidgets( AllFlags ^ SaveSettingsFlag );
00408 }
00409 else
00410 {
00411 enableWidgets( AllFlags );
00412 label_status->setPaletteBackgroundColor( Qt::yellow );
00413 label_status->setText( tr( "To save these settings and create the printer click on Save settings." ) );
00414 }
00415 }
00416
00417
00423 void GuiPrinterManualSetup::on_pushButton_driverDetails_clicked()
00424 {
00425 try
00426 {
00427 showPrinterDriverDetails( listBox_drivers->selectedItem()->text().replace( m_translationRecommended, "recommended" ) );
00428 }
00429 catch ( LpsError e )
00430 {
00431 label_status->setText( e );
00432 label_status->setPaletteBackgroundColor( Qt::yellow );
00433 }
00434 }
00435
00436
00442 void GuiPrinterManualSetup::readPorts()
00443 {
00444 FILE * lpinfo = NULL;
00445 comboBox_ports->clear();
00446
00447 lpinfo = popen( sudo() + "/usr/sbin/lpinfo -v", "r" );
00448
00449 QTextStream* stream = NULL;
00450
00451 try
00452 {
00453 if ( !lpinfo )
00454 {
00455 throw LpsError( tr( "Could not read ports" ) );
00456 }
00457 stream = new QTextStream(lpinfo,IO_ReadOnly);
00458 QString line;
00459
00460 while( !stream->atEnd() )
00461 {
00462 line = QString( stream->readLine() );
00463
00464 if ( !line.contains( "network" ) )
00465 comboBox_ports->insertItem( line.remove( "direct " ) );
00466 }
00467 }
00468 catch ( LpsError e )
00469 {
00470 label_status->setPaletteBackgroundColor( Qt::red );
00471 label_status->setText(e);
00472 qDebug( e.latin1() );
00473 }
00474
00475
00476
00477
00478 if ( stream )
00479 delete stream;
00480 if ( lpinfo )
00481 pclose( lpinfo );
00482
00483 comboBox_ports->setCurrentItem( 0 );
00484 }