Main Page · Namespace List · Alphabetical List · Class List · File List · Namespace Members · Class Members · File Members · Related Pages

guiprintermanualsetup.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2006 by Jens Lukowski
00003  *   jens.lukowski@fh-swf.de
00004  *
00005  *   This file contains the implementation of the Printersetup GUI
00006  *
00007  *   PLEASE NOTICE: If not stated otherwise, whenever we speak of 'printer
00008  *                  drivers' or simply 'drivers', we mean the PPD files
00009  *                  and not the actual drivers.
00010  *
00011  *   $Id: guiprintermanualsetup.cpp,v 1.9 2006/04/18 19:35:03 hbci Exp $
00012  *
00013  *   This program is free software; you can redistribute it and/or modify
00014  *   it under the terms of the GNU General Public License as published by
00015  *   the Free Software Foundation; either version 2 of the License, or
00016  *   (at your option) any later version.
00017  *
00018  *   This program is distributed in the hope that it will be useful,
00019  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *   GNU General Public License for more details.
00022  *
00023  *   You should have received a copy of the GNU General Public License
00024  *   along with this program; if not, write to the Free Software Foundation,
00025  *   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00026  ***************************************************************************/
00027 
00028 // App includes
00029 #include "guiprintermanualsetup.h"
00030 #include <printersetupdb.h>
00031 #include "globals.h"
00032 
00033 // Qt includes
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   // list boxes
00091   listBox_models->setEnabled(             wf & ModelsFlag );
00092   listBox_drivers->setEnabled(            wf & DriversFlag );
00093   listBox_manufacturers->setEnabled(      wf & ManufacturersFlag );
00094 
00095   // labels
00096   label_drivers->setEnabled(              wf & DriversFlag );
00097   label_models->setEnabled(               wf & ModelsFlag );
00098   label_manufacturers->setEnabled(        wf & ManufacturersFlag );
00099 
00100   // combo boxes
00101   comboBox_ports->setEnabled(             wf & PortsFlag );
00102 
00103   // push buttons
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 ) // If the manufacturer is not highlighted correctly
00118     return;
00119 
00120   listBox_models->clear();
00121   listBox_drivers->clear();
00122 
00123   // Get the Models to the highlightet manufacturer from the db
00124   QDomNodeList models = m_db->getModels( item->text() );
00125 
00126   // Fill the listBox_models
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   // No models in listBox_models?
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   // Get the manufacturer out of the listBox_manufacturers
00161   QListBoxItem * selectedManufacturerItem = listBox_manufacturers->selectedItem();
00162 
00163   // Is the model item correctly highlighted
00164   if ( !item || !selectedManufacturerItem )
00165     return;
00166 
00167   listBox_drivers->clear();
00168 
00169 
00170   // Get the drivers from the database
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       //replace "recommended" for the user. This must be undone later for selecting the driver.
00181       listBox_drivers->insertItem( driverElement.attribute( "nickName" ).replace( "recommended", m_translationRecommended ) );
00182 
00183       uint count = listBox_drivers->count();
00184 
00185       // If the driver is recommended, highlight it!
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   // No driver in drivers list box?
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   // No item selected? Then select the first one!
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   // Is the driver item correctly highlighted (prevents clicking in nirvana)
00222   if ( !item )
00223     return;
00224 
00225   label_status->setPaletteBackgroundColor( Qt::yellow );
00226 
00227   // No port selected!
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   // Port selcted
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     // If no printername is given throw exception.
00268     if ( lineEdit_printername->text() == "" )
00269       throw LpsNotice( tr( "Printer is unnamed. Please type in a name." ) );
00270 
00271     //undo the translation, because the driver name is untranslated in list.
00272     QString driverName = listBox_drivers->selectedItem()->text().replace( m_translationRecommended, "recommended" );
00273 
00274     // Get driver element
00275     QDomElement driver = m_db->getDriverByModel(
00276                            listBox_manufacturers->selectedItem()->text(),
00277                            listBox_models->selectedItem()->text(),
00278                            driverName );
00279 
00280     // If no driver element is null throw exception.
00281     if ( driver.isNull() )
00282       throw LpsError( "The driver element was NULL." );
00283 
00284     // Build command to be executed and run it
00285     QString printerName = lineEdit_printername->text();
00286     QString deviceUri   = comboBox_ports->currentText();
00287     QString ppdFilePath = driver.attribute( "ppdFilePath" ); // -p
00288 
00289     // Run lpadmin to install printer
00290     installPrinter( this, printerName, deviceUri, ppdFilePath );
00291 
00292     // Ask the user if a testpage shall be printed
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(); // Superclass Constructor
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   // Only valid characters for printername
00338   QRegExp rx( "[a-zA-Z_0-9]+" );
00339   QValidator * validator = new QRegExpValidator( rx, this );
00340   lineEdit_printername->setValidator( validator );
00341 
00342   // Clear all list boxes
00343   listBox_drivers->clear();
00344   listBox_models->clear();
00345   listBox_manufacturers->clear();
00346 
00347   // Set the default printername
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   // Fill the manufacturers list box
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   // No manufacturers in listBox_manufacturers?
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   // Is no port selected / activated?
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   // Never change the order of these deletions/closings!!!
00477   //
00478   if ( stream )
00479     delete stream;
00480   if ( lpinfo )
00481     pclose( lpinfo );
00482 
00483   comboBox_ports->setCurrentItem( 0 );
00484 }


To get more information, please visit our project site hosted at SourceForge.net Logo
To support this project, please click on this image: Support This Project

Copyright © 2005-2006 Linux Printer Setup Documentation generated by Doxygen 1.4.4 LPS 1.0 (Disclaimer)