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

globalsprintersetup.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2006 by Konrad Wilhelm Kleine
00003  *   konrad@plothe-kleine.de
00004  *
00005  *   This file contains the implementation of globally used types, functions
00006  *   and constants for the Linux Printer Setup project.
00007  *
00008  *   PLEASE NOTICE: If not stated otherwise, whenever we speak of 'printer
00009  *                  drivers' or simply 'drivers', we mean the PPD files
00010  *                  and not the actual drivers.
00011  *
00012  *   STATUS: File has been reviewed and commented
00013  *
00014  *   $Id: globalsprintersetup.cpp,v 1.5 2006/04/23 16:06:22 hbci Exp $
00015  *
00016  *   This program is free software; you can redistribute it and/or modify
00017  *   it under the terms of the GNU General Public License as published by
00018  *   the Free Software Foundation; either version 2 of the License, or
00019  *   (at your option) any later version.
00020  *
00021  *   This program is distributed in the hope that it will be useful,
00022  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *   GNU General Public License for more details.
00025  *
00026  *   You should have received a copy of the GNU General Public License
00027  *   along with this program; if not, write to the Free Software Foundation,
00028  *   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029  ***************************************************************************/
00030 
00031 // App includes
00032 #include "globalsprintersetup.h"
00033 
00034 // MoneyPenny includes
00035 #include "globals.h"
00036 
00037 // Qt includes
00038 #include <qstring.h>
00039 #include <qtextstream.h>
00040 #include <qmessagebox.h>
00041 #include <qlistbox.h>
00042 #include <qobject.h>
00043 #include <qcursor.h>
00044 #include <qinputdialog.h>
00045 
00046 // Other includes
00047 #include <stdlib.h>
00048 using namespace std;
00049 
00050 namespace LinuxPrinterSetup
00051 {
00052 
00053 
00057 PrinterDevice::PrinterDevice()
00058 {
00059 }
00060 
00061 
00070 PrinterDevice::PrinterDevice( const QString & nativeManufacturer,
00071                               const QString & nativeProduct,
00072                               ConnectionType connectionType,
00073                               const QDomNodeList & drivers)
00074 {
00075         this->nativeManufacturer  = nativeManufacturer;
00076         this->nativeProduct       = nativeProduct;
00077         this->connectionType      = connectionType;
00078         this->drivers             = drivers;
00079 }
00080 
00081 
00087 LpsNotice::LpsNotice( const char * str ) : QString(str)
00088 {
00089 }
00090 
00091 
00098 LpsNotice::LpsNotice( const QString & str ) : QString(str)
00099 {
00100 }
00101 
00102 
00108 LpsError::LpsError( const char * str ) : QString(str)
00109 {
00110 }
00111 
00112 
00119 LpsError::LpsError( const QString & str ) : QString(str)
00120 {
00121 }
00122 
00123 
00136 QString sudo()
00137 {
00138   bool    okPressed;
00139 
00140   QString result      = "sudo ";
00141 
00142   if ( !USE_LIVE_CD_FEATURES )
00143   {
00144     //
00145     // Since we are not operating under Knoppix, we ask the user for the root password
00146     //
00147     QString rootPassword =  QInputDialog::getText(
00148                               QObject::tr("Information"),
00149                               QObject::tr("Please enter the root password to execute the printer setup command."),
00150                               QLineEdit::Password,
00151                               0,
00152                               &okPressed
00153                             );
00154 
00155     if ( okPressed )
00156     {
00157       result  = QString("echo ") + rootPassword + " | sudo -S ";
00158     }
00159   }
00160 
00161   return result;
00162 }
00163 
00164 
00165 //
00166 //====================================================================================
00167 //
00168 // Shared functions
00169 //
00170 // The following functions are used by both printer setups, the automatic and manual.
00171 // Therefore, they are outsourced from the concrete classes and made friends.
00172 //
00173 //====================================================================================
00174 //
00175 
00176 
00187 void showPrinterDriverDetails( const QString & driverName )
00188 {
00189   QString       xmlFileName = driverName.section("/", 1, 1);
00190   FILE        * pipe        = NULL;
00191   QTextStream * reader      = NULL;
00192 
00193   try
00194   {
00200 
00201     QString xmlFilePath = QString(LPS_DRIVER_DETAILS_DIR + xmlFileName + ".xml").remove(" (recommended)");
00202     QString params      = "--nonet --stringparam commentTitle " + QObject::tr("Comment");
00203     QString cmd         = "xsltproc "+params+" "+MP_DIR() + MONEYPENNY_DATA_DIR + LPS_DRIVER_STYLESHEET_PATH+" " + xmlFilePath;
00204     pipe                = popen(cmd.latin1(), "r");
00205 
00206     if ( !pipe )
00207     {
00208       throw "Error during xsltproc execution! Could not execute process:<br />" + cmd;
00209     }
00210 
00211     reader          = new QTextStream(pipe, IO_ReadOnly);
00212 
00213     QString details = reader->read().replace("&gt;", ">").replace("&lt;", "<");
00214 
00215     QMessageBox::information(0, QObject::tr("Driver Details"), details);
00216   }
00217   catch ( LpsError e )
00218   {
00219     //
00220     // NEVER change the order of these deletions/closings!!!
00221     //
00222     if (reader)
00223       delete reader;
00224     if (pipe)
00225       pclose(pipe);
00226 
00227     throw e;
00228   }
00229 
00230   //
00231   // NEVER change the order of these deletions/closings!!!
00232   //
00233   if (reader)
00234     delete reader;
00235   if (pipe)
00236     pclose(pipe);
00237 }
00238 
00239 
00252 void testPrintRequest( QWidget * widget, const QString & printerName )
00253 {
00254   //
00255   // Ask the user if a test print is requested
00256   //
00257   QString question = QObject::tr("Your printer has been configured successfully.") + "<br>" +
00258                      QObject::tr("Would you like to print a testpage?");
00259 
00260   int answer = QMessageBox::question(widget,
00261                                      LPS_APP_NAME,
00262                                      question,
00263                                      QMessageBox::Yes,
00264                                      QMessageBox::No,
00265                                      QMessageBox::NoButton);
00266 
00267   if ( answer == QMessageBox::Yes )
00268   {
00269     //
00270     // Build the path to test print file
00271     //
00272     QString testFile = MP_DIR() + MONEYPENNY_DATA_DIR + "/" + LPS_TESTPAGE_PS_SRC;
00273     QString cmdLpr = "lpr -P "+printerName+" -C "LPS_TESTPAGE_JOBNAME" "+testFile;
00274     qDebug("Printing Testpage: %s", cmdLpr.latin1());
00275 
00276     //
00277     // Now print the test page.
00278     // Due to system() call, the UI is blocked.
00279     //
00280     widget->setCursor(Qt::WaitCursor);
00281     if ( system(cmdLpr) != 0 )
00282     {
00283       widget->unsetCursor();
00284       throw LpsError("Error during lpr execution! Could not execute process: " + cmdLpr);
00285     }
00286     widget->unsetCursor();
00287 
00288     //
00289     // Open preview window
00290     //
00291     QDialog dlg;
00292     dlg.setFixedSize(LPS_TESTPAGE_WIDTH, LPS_TESTPAGE_HEIGHT);
00293     QPixmap pxm(MP_DIR() + MONEYPENNY_DATA_DIR + "/" + LPS_TESTPAGE_IMG_SRC);
00294     pxm.resize(LPS_TESTPAGE_WIDTH, LPS_TESTPAGE_HEIGHT);
00295     dlg.setPaletteBackgroundPixmap(pxm);
00296     dlg.setCaption(QObject::tr("Testprint Preview"));
00297     dlg.exec();
00298   }
00299 }
00300 
00301 
00312 void installPrinter( QWidget * widget,
00313                      const QString & printerName,
00314                      const QString & deviceUri,
00315                      const QString & ppdFilePath )
00316 {
00317   //
00321   //
00322   QString cmdLpadmin = sudo() + LPS_LPADMIN_PATH "lpadmin -p " + QString(printerName).replace(" ", "_") + " -E -v " + deviceUri + " -m " + ppdFilePath + "";
00323 
00324   //
00325   // Install the printer.
00326   // Due to system() call, the UI is blocked.
00327   //
00328   widget->setCursor(Qt::WaitCursor);
00329   if (  system(cmdLpadmin) != 0 )
00330   {
00331     widget->unsetCursor();
00332     qDebug("GuiPrinterManualSetup::on_pushButton_saveSettings_clicked: Failed command: %s", cmdLpadmin.latin1());
00333     throw LpsError("Error during lpadmin execution! Could not execute process: " + cmdLpadmin);
00334   }
00335 
00336   system("sync");
00337 
00338   widget->unsetCursor();
00339 }
00340 
00341 };


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)