kdeprint Library API Documentation

kprinter.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include <config.h>
00021 
00022 #include "kprinter.h"
00023 #include "kprinterimpl.h"
00024 #include "kprintdialog.h"
00025 #include "kprintpreview.h"
00026 #include "kmfactory.h"
00027 #include "kmuimanager.h"
00028 #include "kmmanager.h"
00029 #include "driver.h"
00030 
00031 #include <qpaintdevicemetrics.h>
00032 #include <qfile.h>
00033 #include <qtl.h>
00034 #include <qdir.h>
00035 #include <qguardedptr.h>
00036 #include <kapplication.h>
00037 #include <kstandarddirs.h>
00038 #include <kglobal.h>
00039 #include <kconfig.h>
00040 #include <krun.h>
00041 #include <knotifyclient.h>
00042 #include <kdebug.h>
00043 #include <klocale.h>
00044 #include <kprocess.h>
00045 #include <klibloader.h>
00046 #include <kmessagebox.h>
00047 
00048 static void dumpOptions(const QMap<QString,QString>& opts);
00049 static void reportError(KPrinter*);
00050 
00051 //**************************************************************************************
00052 // KPrinterWrapper class
00053 //**************************************************************************************
00054 
00055 class KPrinterWrapper : public QPrinter
00056 {
00057 friend class KPrinter;
00058 public:
00059     KPrinterWrapper(KPrinter*, PrinterMode m = ScreenResolution);
00060     ~KPrinterWrapper();
00061 protected:
00062     virtual bool cmd(int, QPainter*, QPDevCmdParam*);
00063     virtual int metric(int) const;
00064     int qprinterMetric(int) const;
00065 private:
00066     KPrinter    *m_printer;
00067 };
00068 
00069 KPrinterWrapper::KPrinterWrapper(KPrinter *prt, QPrinter::PrinterMode m)
00070 : QPrinter(m), m_printer(prt)
00071 {
00072 }
00073 
00074 KPrinterWrapper::~KPrinterWrapper()
00075 {
00076 }
00077 
00078 bool KPrinterWrapper::cmd(int c, QPainter *painter, QPDevCmdParam *p)
00079 {
00080     return QPrinter::cmd(c,painter,p);
00081 }
00082 
00083 int KPrinterWrapper::metric(int m) const
00084 {
00085     return m_printer->metric(m);
00086 }
00087 
00088 int KPrinterWrapper::qprinterMetric(int m) const
00089 {
00090     return QPrinter::metric(m);
00091 }
00092 
00093 //**************************************************************************************
00094 // KPrinterPrivate class
00095 //**************************************************************************************
00096 
00097 class KPrinterPrivate
00098 {
00099 public:
00100     QGuardedPtr<KPrinterImpl>   m_impl;
00101     bool        m_restore;
00102     bool        m_previewonly;
00103     WId     m_parentId;
00104     QString     m_docfilename;
00105     QString m_docdirectory;
00106     KPrinterWrapper     *m_wrapper;
00107     QMap<QString,QString>   m_options;
00108     QString         m_tmpbuffer;
00109     QString         m_printername;
00110     QString         m_searchname;
00111     QString         m_errormsg;
00112     bool            m_ready;
00113     int     m_pagenumber;
00114     DrPageSize *m_pagesize;
00115     bool m_useprinterres;
00116     int m_defaultres;
00117 };
00118 
00119 //**************************************************************************************
00120 // KPrinter class
00121 //**************************************************************************************
00122 
00123 KPrinter::KPrinter(bool restore, QPrinter::PrinterMode m)
00124 : QPaintDevice(QInternal::Printer|QInternal::ExternalDevice)
00125 {
00126     init(restore, m);
00127 }
00128 
00129 KPrinter::~KPrinter()
00130 {
00131     // delete Wrapper object
00132     delete d->m_wrapper;
00133 
00134     // save current options
00135     if (d->m_restore)
00136         saveSettings();
00137 
00138     // delete private data (along any data allocated internally)
00139     delete d->m_pagesize;
00140     delete d;
00141 }
00142 
00143 void KPrinter::init(bool restore, QPrinter::PrinterMode m)
00144 {
00145     // Private data initialization
00146     d = new KPrinterPrivate;
00147     d->m_impl = KMFactory::self()->printerImplementation();
00148     d->m_restore = restore;
00149     d->m_previewonly = false;
00150     d->m_parentId = 0;
00151     d->m_pagesize = 0;
00152 
00153     // initialize QPrinter wrapper
00154     d->m_wrapper = new KPrinterWrapper(this, m);
00155 
00156     // other initialization
00157     d->m_tmpbuffer = d->m_impl->tempFile();
00158     d->m_ready = false;
00159     d->m_defaultres = d->m_wrapper->resolution();
00160     d->m_useprinterres = false;
00161 
00162     // reload options from implementation (static object)
00163     if (d->m_restore)
00164         loadSettings();
00165 }
00166 
00167 void KPrinter::loadSettings()
00168 {
00169     d->m_options = d->m_impl->loadOptions();
00170 
00171     // load the last printer used in the current process (if any)
00172     // and remove the corresponding entry in the option map, as it
00173     // is not needed anymore
00174     setSearchName(option("kde-searchname"));
00175     d->m_options.remove("kde-searchname");
00176 
00177     KConfig *conf = KGlobal::config(), *pconf = KMFactory::self()->printConfig();
00178     conf->setGroup("KPrinter Settings");
00179     pconf->setGroup("General");
00180 
00181     // load latest used printer from config file, if required in the options
00182     if (searchName().isEmpty() && pconf->readBoolEntry("UseLast", true))
00183         setSearchName(conf->readEntry("Printer"));
00184 
00185     // latest used print command
00186     setOption("kde-printcommand",conf->readPathEntry("PrintCommand"));
00187 
00188     // latest used document directory
00189     setDocDirectory( conf->readPathEntry( "DocDirectory" ) );
00190     setDocFileName( "print" );
00191 }
00192 
00193 void KPrinter::saveSettings()
00194 {
00195     if (d->m_impl)
00196     {
00197         setOption("kde-searchname", searchName());
00198         d->m_impl->saveOptions(d->m_options);
00199     }
00200 
00201     // save latest used printer to config file
00202     KConfig *conf = KGlobal::config();
00203     conf->setGroup("KPrinter Settings");
00204     conf->writeEntry("Printer",searchName());
00205     // latest used print command
00206     conf->writePathEntry("PrintCommand",option("kde-printcommand"));
00207 
00208     // latest used document directory
00209     if ( d->m_docdirectory.isEmpty() )
00210     {
00211         KURL url( outputFileName() );
00212         if ( url.isValid() )
00213             conf->writePathEntry( "DocDirectory", url.directory() );
00214     }
00215     else
00216         conf->writePathEntry( "DocDirectory", d->m_docdirectory );
00217 }
00218 
00219 bool KPrinter::setup(QWidget *parent, const QString& caption, bool forceExpand)
00220 {
00221     if (!kapp->authorize("print/dialog"))
00222     {
00223         autoConfigure(QString::null, parent);
00224         return true; // Just print it
00225     }
00226 
00227     if (parent)
00228         d->m_parentId = parent->winId();
00229 
00230     KPrintDialog    *dlg = KPrintDialog::printerDialog(this, parent, caption, forceExpand);
00231     bool    state = false;
00232     if (dlg)
00233     {
00234         state = dlg->exec();
00235         delete dlg;
00236     }
00237     return state;
00238 }
00239 
00240 void KPrinter::addStandardPage(int p)
00241 {
00242     KMFactory::self()->settings()->standardDialogPages |= p;
00243 }
00244 
00245 void KPrinter::removeStandardPage(int p)
00246 {
00247     KMFactory::self()->settings()->standardDialogPages &= (~p);
00248 }
00249 
00250 void KPrinter::addDialogPage(KPrintDialogPage *page)
00251 {
00252     KMFactory::self()->uiManager()->addPrintDialogPage(page);
00253 }
00254 
00255 void KPrinter::setPageSelection(PageSelectionType t)
00256 {
00257     KMFactory::self()->settings()->pageSelection = t;
00258 }
00259 
00260 KPrinter::PageSelectionType KPrinter::pageSelection()
00261 {
00262     return (PageSelectionType)KMFactory::self()->settings()->pageSelection;
00263 }
00264 
00265 void KPrinter::setApplicationType(ApplicationType t)
00266 {
00267     KMFactory::self()->settings()->application = t;
00268 }
00269 
00270 KPrinter::ApplicationType KPrinter::applicationType()
00271 {
00272     return (ApplicationType)KMFactory::self()->settings()->application;
00273 }
00274 
00275 bool KPrinter::cmd(int c, QPainter *painter, QPDevCmdParam *p)
00276 {
00277     bool value(true);
00278     if (c == QPaintDevice::PdcBegin)
00279     {
00280         d->m_impl->statusMessage(i18n("Initialization..."), this);
00281         d->m_pagenumber = 1;
00282         preparePrinting();
00283         d->m_impl->statusMessage(i18n("Generating print data: page %1").arg(d->m_pagenumber), this);
00284     }
00285     value = d->m_wrapper->cmd(c,painter,p);
00286     if (c == QPaintDevice::PdcEnd)
00287     {
00288         // this call should take care of everything (preview, output-to-file, filtering, ...)
00289         value = value && printFiles(QStringList(d->m_wrapper->outputFileName()),true);
00290         // reset "ready" state
00291         finishPrinting();
00292     }
00293     return value;
00294 }
00295 
00296 void KPrinter::translateQtOptions()
00297 {
00298     d->m_wrapper->setCreator(creator());
00299     d->m_wrapper->setDocName(docName());
00300     d->m_wrapper->setFullPage(fullPage());
00301     d->m_wrapper->setColorMode((QPrinter::ColorMode)colorMode());
00302     d->m_wrapper->setOrientation((QPrinter::Orientation)orientation());
00303     if ( !option( "kde-printsize" ).isEmpty() )
00304         d->m_wrapper->setPageSize( ( QPrinter::PageSize )option( "kde-printsize" ).toInt() );
00305     else
00306         d->m_wrapper->setPageSize((QPrinter::PageSize)pageSize());
00307     d->m_wrapper->setOutputToFile(true);
00308     d->m_wrapper->setOutputFileName(d->m_tmpbuffer);
00309     d->m_wrapper->setNumCopies(option("kde-qtcopies").isEmpty() ? 1 : option("kde-qtcopies").toInt());
00310     if (!option("kde-margin-top").isEmpty())
00311     {
00318         int res = resolution();
00319         d->m_wrapper->setMargins(
00320                 ( int )( ( option("kde-margin-top").toFloat() * res + 71 ) / 72 ),
00321                 ( int )( ( option("kde-margin-left").toFloat() * res + 71 ) / 72 ),
00322                 ( int )( ( option("kde-margin-bottom").toFloat() * res + 71 ) / 72 ),
00323                 ( int )( ( option("kde-margin-right").toFloat() * res + 71 ) / 72 ) );
00324     }
00325     else if ( d->m_pagesize != NULL )
00326     {
00327         int res = resolution();
00328         DrPageSize *ps = d->m_pagesize;
00329         int top = ( int )( ps->topMargin() * res + 71 ) / 72;
00330         int left = ( int )( ps->leftMargin() * res + 71 ) / 72;
00331         int bottom = ( int )( ps->bottomMargin() * res + 71 ) / 72;
00332         int right = ( int )( ps->rightMargin() * res + 71 ) / 72;
00333         if ( !fullPage() )
00334         {
00335             // Printers can often print very close to the edges (PPD files say ImageArea==PaperDimension).
00336             // But that doesn't mean it looks good. Apps which use setFullPage(false) assume that
00337             // KPrinter will give them reasonable margins, so let's QMAX with defaults from Qt in that case.
00338             // Keep this in sync with KPMarginPage::initPageSize
00339             unsigned int it, il, ib, ir;
00340             d->m_wrapper->margins( &it, &il, &ib, &ir );
00341             top = QMAX( top, (int)it );
00342             left = QMAX( left, (int)il );
00343             bottom = QMAX( bottom, (int)ib );
00344             right = QMAX( right, (int)ir );
00345         }
00346         d->m_wrapper->setMargins( top, left, bottom, right );
00347     }
00348     /*else
00349     {
00350         int res = d->m_wrapper->resolution();
00351         d->m_wrapper->setMargins( res/3, res/2, res/3, res/2 );
00352     }*/
00353     // for special printers, copies are handled by Qt
00354     if (option("kde-isspecial") == "1")
00355         d->m_wrapper->setNumCopies(numCopies());
00356 }
00357 
00358 bool KPrinter::printFiles(const QStringList& l, bool flag, bool startviewer)
00359 {
00360     QStringList files(l);
00361     bool        status(true);
00362 
00363     // First apply possible filters, and update "remove" flag if filters has
00364     // been applied (result == 0, means nothing happened).
00365     int fresult = d->m_impl->filterFiles(this, files, flag);
00366     if (fresult == -1)
00367     {
00368         reportError(this);
00369         status = false;
00370     }
00371     else if (fresult == 1)
00372         flag = true;
00373 
00374     if (status)
00375     {
00376         // Automatic conversion to format supported by print system
00377         fresult = d->m_impl->autoConvertFiles(this, files, flag);
00378         if (fresult == -1)
00379         {
00380             reportError(this);
00381             status = false;
00382         }
00383         else if (fresult == 1)
00384             flag = true;
00385     }
00386 
00387     // Continue if status is OK (filtering succeeded) and no output-to-file
00388     if (status && files.count() > 0)
00389     {
00390         // Show preview if needed (only possible for a single file !), and stop
00391         // if the user requested it. Force preview if preview-only mode has been set: it
00392         // then use by default the first file in the list.
00393         if (((files.count() != 1 || option("kde-preview") != "1") && !d->m_previewonly) || doPreview(files[0]))
00394         {
00395             // check if printing has been prepared (it may be not prepared if the KPrinter object is not
00396             // use as a QPaintDevice object)
00397             preparePrinting();
00398 
00399             if (!d->m_impl->printFiles(this, files, flag))
00400             {
00401                 reportError(this);
00402                 status = false;
00403             }
00404             else
00405             {
00406 #if 0           
00407                 if (/* !outputToFile() && */ startviewer)
00408                 {
00409                     QStringList args;
00410                     args << "-d";
00411                     args << printerName();
00412                     args << "--noshow";
00413                     kapp->kdeinitExec("kjobviewer", args);
00414                 }
00415 #endif              
00416             }
00417         }
00418         else if (flag)
00419         // situation: only one file, it has been previewed and printing has been canceled, then
00420         //            we should remove the file ourself
00421         {
00422             QFile::remove(files[0]);
00423         }
00424     }
00425     finishPrinting();
00426     return status;
00427 }
00428 
00429 bool KPrinter::doPreview(const QString& file)
00430 {
00431     d->m_impl->statusMessage(i18n("Previewing..."), this);
00432     d->m_impl->statusMessage(QString::null, this);
00433     return KPrintPreview::preview(file, d->m_previewonly, d->m_parentId);
00434 }
00435 
00436 void KPrinter::preparePrinting()
00437 {
00438     // check if already prepared (-> do nothing)
00439     if (d->m_ready) return;
00440 
00441     // re-initialize error
00442     setErrorMessage(QString::null);
00443 
00444     // re-initialize margins and page size (by default, use Qt mechanism)
00445     setRealPageSize(NULL);
00446 
00447     // print-system-specific setup, only if not printing to file
00448     if (option("kde-isspecial") != "1")
00449         d->m_impl->preparePrinting(this);
00450 
00451     // set the correct resolution, if needed (or reset it)
00452     int res = option( "kde-resolution" ).toInt();
00453     if ( d->m_useprinterres && res > 0 )
00454         d->m_wrapper->setResolution( res );
00455     else
00456         d->m_wrapper->setResolution( d->m_defaultres );
00457 
00458     // standard Qt settings
00459     translateQtOptions();
00460 
00461     d->m_ready = true;
00462 dumpOptions(d->m_options);
00463 }
00464 
00465 void KPrinter::finishPrinting()
00466 {
00467     d->m_ready = false;
00468     // close the status window
00469     d->m_impl->statusMessage(QString::null, this);
00470 }
00471 
00472 QValueList<int> KPrinter::pageList() const
00473 {
00474     QValueList<int> list;
00475     int mp(minPage()), MP(maxPage());
00476     if (mp > 0 && MP > 0 && MP >= mp)
00477     { // do something only if bounds specified
00478         if (option("kde-current") == "1")
00479         { // print only current page
00480             int pp = currentPage();
00481             if (pp >= mp && pp <= MP) list.append(pp);
00482         }
00483         else
00484         {
00485             // process range specification
00486             if (!option("kde-range").isEmpty())
00487             {
00488                 QStringList ranges = QStringList::split(',',option("kde-range"),false);
00489                 for (QStringList::ConstIterator it=ranges.begin();it!=ranges.end();++it)
00490                 {
00491                     int p = (*it).find('-');
00492                     bool    ok;
00493                     if (p == -1)
00494                     {
00495                         int pp = (*it).toInt(&ok);
00496                         if (ok && pp >= mp && pp <= MP)
00497                             list.append(pp);
00498                     }
00499                     else
00500                     {
00501                         int p1(0), p2(0);
00502                         p1 = (*it).left(p).toInt(&ok);
00503                         if (ok) p2 = (*it).right((*it).length()-p-1).toInt(&ok);
00504                         if (ok && p1 <= p2)
00505                         {
00506                             // clip to min/max
00507                             p1 = QMAX(mp,p1);
00508                             p2 = QMIN(MP,p2);
00509                             for (int i=p1;i<=p2;i++)
00510                                 list.append(i);
00511                         }
00512                     }
00513                 }
00514             }
00515             else
00516             { // add all pages between min and max
00517                 for (int i=mp;i<=MP;i++) list.append(i);
00518             }
00519 
00520             // revert the list if needed
00521             if (pageOrder() == LastPageFirst)
00522             {
00523                 for (uint i=0;i<(list.count()/2);i++)
00524                     qSwap(list[i],list[list.count()-1-i]);
00525             }
00526 
00527             // select page set if needed
00528             if (pageSet() != AllPages)
00529             {
00530                 bool    keepEven = (pageSet() == EvenPages);
00531                 for (QValueList<int>::Iterator it=list.begin();it!=list.end();)
00532                     if ((((*it) % 2) != 0 && keepEven) ||
00533                         (((*it) % 2) == 0 && !keepEven)) it = list.remove(it);
00534                     else ++it;
00535             }
00536         }
00537     }
00538     return list;
00539 }
00540 
00541 //**************************************************************************************
00542 // QPrinter interface
00543 //**************************************************************************************
00544 
00545 int KPrinter::numCopies() const
00546 {
00547     bool    ok;
00548     int p = option("kde-copies").toInt(&ok);
00549     return (ok ? p : 1);
00550 }
00551 
00552 QSize KPrinter::margins() const
00553 {
00554     return d->m_wrapper->margins();
00555 }
00556 
00557 void KPrinter::margins( uint *top, uint *left, uint *bottom, uint *right ) const
00558 {
00559     d->m_wrapper->margins( top, left, bottom, right );
00560 }
00561 
00562 int KPrinter::metric(int m) const
00563 {
00564     if (d->m_pagesize == NULL || !option( "kde-printsize" ).isEmpty())
00565         return d->m_wrapper->qprinterMetric(m);
00566 
00567     int val(0);
00568     bool    land = (orientation() == KPrinter::Landscape);
00569     uint    res(d->m_wrapper->resolution()), top = res/2, left = res/2, bottom = res/3, right = res/2;
00570     margins( &top, &left, &bottom, &right );
00571     switch ( m )
00572     {
00573         case QPaintDeviceMetrics::PdmWidth:
00574             val = (land ? ( int )d->m_pagesize->pageHeight() : ( int )d->m_pagesize->pageWidth());
00575             if ( res != 72 )
00576                 val = (val * res + 36) / 72;
00577             if ( !fullPage() )
00578                 val -= ( left + right );
00579             break;
00580         case QPaintDeviceMetrics::PdmHeight:
00581             val = (land ? ( int )d->m_pagesize->pageWidth() : ( int )d->m_pagesize->pageHeight());
00582             if ( res != 72 )
00583                 val = (val * res + 36) / 72;
00584             if ( !fullPage() )
00585                 val -= ( top + bottom );
00586             break;
00587         case QPaintDeviceMetrics::PdmWidthMM:
00588             val = metric( QPaintDeviceMetrics::PdmWidth );
00589             val = (val * 254 + 5*res) / (10*res); // +360 to get the right rounding
00590             break;
00591         case QPaintDeviceMetrics::PdmHeightMM:
00592             val = metric( QPaintDeviceMetrics::PdmHeight );
00593             val = (val * 254 + 5*res) / (10*res);
00594             break;
00595         default:
00596             val = d->m_wrapper->qprinterMetric(m);
00597             break;
00598     }
00599     return val;
00600 }
00601 
00602 void KPrinter::setOrientation(Orientation o)
00603 {
00604     KMFactory::self()->settings()->orientation = o;
00605     setOption("kde-orientation",(o == Landscape ? "Landscape" : "Portrait"));
00606     d->m_impl->broadcastOption("kde-orientation",(o == Landscape ? "Landscape" : "Portrait"));
00607     d->m_impl->broadcastOption( "kde-orientation-fixed", "1" );
00608 }
00609 
00610 void KPrinter::setOption( const QString& key, const QString& value, bool broadcast )
00611 {
00612     setOption( key, value );
00613     if ( broadcast )
00614         d->m_impl->broadcastOption( key, value );
00615 }
00616 
00617 void KPrinter::setPageSize(PageSize s)
00618 {
00619     KMFactory::self()->settings()->pageSize = s;
00620     setOption("kde-pagesize",QString::number((int)s),true);
00621     d->m_impl->broadcastOption( "kde-pagesize-fixed", "1" );
00622 }
00623 
00624 void KPrinter::setOptions(const QMap<QString,QString>& opts)
00625 { // This functions remove all options except those with "kde-..."
00626   // which correspond to externally-sets options (use the value
00627   // from "opts" if specified
00628     QMap<QString,QString>   tmpset = d->m_options;
00629     d->m_options = opts;
00630     // remove some problematic options that may not be overwritten (ugly hack).
00631     // Default values will be used instead, except if the dialog has set new ones.
00632     tmpset.remove("kde-pagesize");
00633     tmpset.remove( "kde-printsize" );
00634     tmpset.remove("kde-orientation");
00635     tmpset.remove("kde-colormode");
00636     tmpset.remove("kde-margin-top");
00637     tmpset.remove("kde-margin-left");
00638     tmpset.remove("kde-margin-bottom");
00639     tmpset.remove("kde-margin-right");
00640     tmpset.remove( "kde-resolution" );
00641     tmpset.remove( "kde-fonts" );
00642     for (QMap<QString,QString>::ConstIterator it=tmpset.begin();it!=tmpset.end();++it)
00643         if (it.key().left(4) == "kde-" && !(d->m_options.contains(it.key())))
00644             d->m_options[it.key()] = it.data();
00645 }
00646 
00647 void KPrinter::initOptions(const QMap<QString,QString>& opts)
00648 { // This function can be used to initialize the KPrinter object just after
00649   // creation to set some options. Non global options will be propagated to
00650   // all listed printers (non-global => start with "kde-...")
00651     for (QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
00652     {
00653         setOption(it.key(), it.data());
00654         if (it.key().left(4) != "kde-")
00655             d->m_impl->broadcastOption(it.key(),it.data());
00656     }
00657 }
00658 
00659 void KPrinter::reload()
00660 {
00661     d->m_impl = KMFactory::self()->printerImplementation();
00662     int global = KMFactory::self()->settings()->orientation;
00663     if (global != -1) setOrientation((KPrinter::Orientation)global);
00664     global = KMFactory::self()->settings()->pageSize;
00665     if (global != -1) setPageSize((KPrinter::PageSize)global);
00666     //initOptions(d->m_options);
00667 }
00668 
00669 bool KPrinter::autoConfigure(const QString& prname, QWidget *parent)
00670 {
00671     KMManager   *mgr = KMManager::self();
00672     KMPrinter   *mprt(0);
00673 
00674     mgr->printerList(false);
00675     if (prname.isEmpty())
00676         mprt = mgr->defaultPrinter();
00677     else
00678         mprt = mgr->findPrinter(prname);
00679 
00680     if (mprt)
00681         return mprt->autoConfigure(this, parent);
00682     else
00683         return false;
00684 }
00685 
00686 //**************************************************************************************
00687 // Util functions
00688 //**************************************************************************************
00689 
00690 void reportError(KPrinter *p)
00691 {
00692     if (!KNotifyClient::event(0,"printerror",i18n("<p><nobr>A print error occurred. Error message received from system:</nobr></p><br>%1").arg(p->errorMessage())))
00693         kdDebug(500) << "could not send notify event" << endl;
00694 }
00695 
00696 KPrinter::PageSize pageNameToPageSize(const QString& _name)
00697 {
00698     QString name = _name.upper();
00699     if (name == "LETTER") return KPrinter::Letter;
00700     else if (name == "LEGAL") return KPrinter::Legal;
00701     else if (name == "A4") return KPrinter::A4;
00702     else if (name == "A3") return KPrinter::A3;
00703     else if (name == "EXECUTIVE") return KPrinter::Executive;
00704     else if (name == "LEDGER") return KPrinter::Ledger;
00705     else if (name == "TABLOID") return KPrinter::Tabloid;
00706     else if (name == "FOLIO") return KPrinter::Folio;
00707     else if (name == "A5") return KPrinter::A5;
00708     else if (name == "A6") return KPrinter::A6;
00709     else if (name == "A7") return KPrinter::A7;
00710     else if (name == "A8") return KPrinter::A8;
00711     else if (name == "A9") return KPrinter::A9;
00712     else if (name == "A2") return KPrinter::A2;
00713     else if (name == "A1") return KPrinter::A1;
00714     else if (name == "A0") return KPrinter::A0;
00715     else if (name == "B0" || name == "B0ISO") return KPrinter::B0;
00716     else if (name == "B1" || name == "B1ISO") return KPrinter::B1;
00717     else if (name == "B2" || name == "B2ISO") return KPrinter::B2;
00718     else if (name == "B3" || name == "B3ISO") return KPrinter::B3;
00719     else if (name == "B4" || name == "B4ISO") return KPrinter::B4;
00720     else if (name == "B5" || name == "B5ISO") return KPrinter::B5;
00721     else if (name == "B6" || name == "B6ISO") return KPrinter::B6;
00722     else if (name == "B7" || name == "B7ISO") return KPrinter::B7;
00723     else if (name == "B8" || name == "B8ISO") return KPrinter::B8;
00724     else if (name == "B9" || name == "B9ISO") return KPrinter::B9;
00725     else if (name == "B10" || name == "B10ISO") return KPrinter::B10;
00726     else if (name == "C5" || name == "C5E" || name == "ENVC5") return KPrinter::C5E;
00727     else if (name == "DL" || name == "DLE" || name == "ENVDL") return KPrinter::DLE;
00728     else if (name == "COMM10" || name == "COM10" || name == "ENV10") return KPrinter::Comm10E;
00729     else return KPrinter::A4;
00730 }
00731 
00732 const char* pageSizeToPageName(KPrinter::PageSize s)
00733 {
00734     switch(s)
00735     {
00736         case KPrinter::Letter: return "Letter";
00737         case KPrinter::Legal: return "Legal";
00738         case KPrinter::A4: return "A4";
00739         case KPrinter::A3: return "A3";
00740         case KPrinter::Executive: return "Executive";
00741         case KPrinter::Ledger: return "Ledger";
00742         case KPrinter::Tabloid: return "Tabloid";
00743         case KPrinter::Folio: return "Folio";
00744         case KPrinter::A5: return "A5";
00745         case KPrinter::A6: return "A6";
00746         case KPrinter::A7: return "A7";
00747         case KPrinter::A8: return "A8";
00748         case KPrinter::A9: return "A9";
00749         case KPrinter::A2: return "A2";
00750         case KPrinter::A1: return "A1";
00751         case KPrinter::A0: return "A0";
00752         case KPrinter::B0: return "B0";
00753         case KPrinter::B1: return "B1";
00754         case KPrinter::B2: return "B2";
00755         case KPrinter::B3: return "B3";
00756         case KPrinter::B4: return "B4";
00757         case KPrinter::B5: return "B5";
00758         case KPrinter::B6: return "B6";
00759         case KPrinter::B7: return "B7";
00760         case KPrinter::B8: return "B8";
00761         case KPrinter::B9: return "B9";
00762         case KPrinter::B10: return "B10";
00763         case KPrinter::C5E: return "C5";
00764         case KPrinter::DLE: return "DL";
00765         case KPrinter::Comm10E: return "Comm10";
00766         default: return "A4";
00767     }
00768 }
00769 
00770 // FIXME: remove for 4.0
00771 QSize rangeToSize( const QString& )
00772 {
00773     kdWarning( 500 ) << "rangeToSize(QString) is obsolete, do not use (no effect)" << endl;
00774     return QSize();
00775 }
00776 
00777 static void dumpOptions(const QMap<QString,QString>& opts)
00778 {
00779     kdDebug(500) << "********************" << endl;
00780     for (QMap<QString,QString>::ConstIterator it=opts.begin(); it!=opts.end(); ++it)
00781         kdDebug(500) << it.key() << " = " << it.data() << endl;
00782 }
00783 
00784 KPrinterImpl* KPrinter::implementation() const
00785 { return d->m_impl; }
00786 
00787 const QString& KPrinter::option(const QString& key) const
00788 { return ((const KPrinterPrivate*)(d))->m_options[key]; }
00789 
00790 void KPrinter::setOption(const QString& key, const QString& value)
00791 { d->m_options[key] = value; }
00792 
00793 QString KPrinter::docName() const
00794 { return option("kde-docname"); }
00795 
00796 void KPrinter::setDocName(const QString& d)
00797 { setOption("kde-docname",d); }
00798 
00799 QString KPrinter::creator() const
00800 { return option("kde-creator"); }
00801 
00802 void KPrinter::setCreator(const QString& d)
00803 { setOption("kde-creator",d); }
00804 
00805 bool KPrinter::fullPage() const
00806 { return (option("kde-fullpage") == "1"); }
00807 
00808 void KPrinter::setFullPage(bool on)
00809 { setOption("kde-fullpage",(on ? "1" : "0")); }
00810 
00811 KPrinter::ColorMode KPrinter::colorMode() const
00812 { return (KPrinter::ColorMode)(option("kde-colormode") == "GrayScale" ? GrayScale : Color); }
00813 
00814 void KPrinter::setColorMode(ColorMode m)
00815 { setOption("kde-colormode",(m == Color ? "Color" : "GrayScale")); }
00816 
00817 void KPrinter::setNumCopies(int n)
00818 { setOption("kde-copies",QString::number(n)); }
00819 
00820 KPrinter::Orientation KPrinter::orientation() const
00821 { return (option("kde-orientation") == "Landscape" ? Landscape : Portrait); }
00822 
00823 KPrinter::PageOrder KPrinter::pageOrder() const
00824 { return (option("kde-pageorder") == "Reverse" ? LastPageFirst : FirstPageFirst); }
00825 
00826 void KPrinter::setPageOrder(PageOrder o)
00827 { setOption("kde-pageorder",(o == LastPageFirst ? "Reverse" : "Forward")); }
00828 
00829 KPrinter::CollateType KPrinter::collate() const
00830 { return (option("kde-collate") == "Collate" ? Collate : Uncollate); }
00831 
00832 void KPrinter::setCollate(CollateType c)
00833 { setOption("kde-collate",(c == Collate ? "Collate" : "Uncollate")); }
00834 
00835 int KPrinter::minPage() const
00836 { return (option("kde-minpage").isEmpty() ? 0 : option("kde-minpage").toInt()); }
00837 
00838 int KPrinter::maxPage() const
00839 { return (option("kde-maxpage").isEmpty() ? 0 : option("kde-maxpage").toInt()); }
00840 
00841 void KPrinter::setMinMax(int m, int M)
00842 { setOption("kde-minpage",QString::number(m)); setOption("kde-maxpage",QString::number(M)); }
00843 
00844 int KPrinter::fromPage() const
00845 { return (option("kde-frompage").isEmpty() ? 0 : option("kde-frompage").toInt()); }
00846 
00847 int KPrinter::toPage() const
00848 { return (option("kde-topage").isEmpty() ? 0 : option("kde-topage").toInt()); }
00849 
00850 void KPrinter::setFromTo(int m, int M)
00851 { setOption("kde-frompage",QString::number(m)); setOption("kde-topage",QString::number(M)); setOption("kde-range",(m>0 && M>0 ? QString("%1-%2").arg(m).arg(M) : QString::fromLatin1(""))); }
00852 
00853 // if no page size defined, use the localized one
00854 KPrinter::PageSize KPrinter::pageSize() const
00855 { return (option("kde-pagesize").isEmpty() ? (PageSize)KGlobal::locale()->pageSize() : (PageSize)option("kde-pagesize").toInt()); }
00856 
00857 KPrinter::PageSetType KPrinter::pageSet() const
00858 { return (option("kde-pageset").isEmpty() ? AllPages : (PageSetType)(option("kde-pageset").toInt())); }
00859 
00860 int KPrinter::currentPage() const
00861 { return (option("kde-currentpage").isEmpty() ? 0 : option("kde-currentpage").toInt()); }
00862 
00863 void KPrinter::setCurrentPage(int p)
00864 { setOption("kde-currentpage",QString::number(p)); }
00865 
00866 QString KPrinter::printerName() const
00867 { return d->m_printername; }
00868 
00869 void KPrinter::setPrinterName(const QString& s)
00870 { d->m_printername = s; }
00871 
00872 QString KPrinter::printProgram() const
00873 { return (option("kde-isspecial") == "1" ? option("kde-special-command") : QString::null); }
00874 
00875 void KPrinter::setPrintProgram(const QString& prg)
00876 {
00877     if (prg.isNull())
00878     {
00879         setOption("kde-isspecial", "0");
00880         d->m_options.remove("kde-special-command");
00881     }
00882     else
00883     {
00884         QString s(prg);
00885         if (s.find("%in") == -1)
00886             s.append(" %in");
00887         setOutputToFile( s.find( "%out" ) != -1 );
00888         setOption("kde-isspecial", "1");
00889         setOption("kde-special-command", s);
00890     }
00891 }
00892 
00893 QString KPrinter::printerSelectionOption() const
00894 { return QString::fromLatin1(""); }
00895 
00896 void KPrinter::setPrinterSelectionOption(const QString&)
00897 {}
00898 
00899 const QMap<QString,QString>& KPrinter::options() const
00900 { return d->m_options; }
00901 
00902 QString KPrinter::searchName() const
00903 { return d->m_searchname; }
00904 
00905 void KPrinter::setSearchName(const QString& s)
00906 { d->m_searchname = s; }
00907 
00908 bool KPrinter::newPage()
00909 {
00910     d->m_pagenumber++;
00911     d->m_impl->statusMessage(i18n("Generating print data: page %1").arg(d->m_pagenumber), this);
00912     return d->m_wrapper->newPage();
00913 }
00914 
00915 QString KPrinter::outputFileName() const
00916 { return option("kde-outputfilename"); }
00917 
00918 void KPrinter::setOutputFileName(const QString& f)
00919 { setOption("kde-outputfilename",f); setOutputToFile(!f.isEmpty()); }
00920 
00921 bool KPrinter::outputToFile() const
00922 { return (option("kde-outputtofile") == "1" || (option("kde-isspecial") == "1" && option("kde-special-command").isEmpty())); }
00923 
00924 void KPrinter::setOutputToFile(bool on)
00925 {
00926     setOption("kde-outputtofile",(on ? "1" : "0"));
00927     if (on)
00928     {
00929         setOption("kde-special-command",QString::null);
00930         setOption("kde-isspecial","1");
00931     }
00932 }
00933 
00934 bool KPrinter::abort()
00935 { return d->m_wrapper->abort(); }
00936 
00937 bool KPrinter::aborted() const
00938 { return d->m_wrapper->aborted(); }
00939 
00940 void KPrinter::setMargins(QSize m)
00941 {
00942     setMargins( m.height(), m.width(), m.height(), m.width() );
00943 }
00944 
00945 void KPrinter::setMargins( uint top, uint left, uint bottom, uint right )
00946 {
00947     d->m_wrapper->setMargins( top, left, bottom, right );
00948     setOption( "kde-margin-top", QString::number( top ), true );
00949     setOption( "kde-margin-left", QString::number( left ), true );
00950     setOption( "kde-margin-bottom", QString::number( bottom ), true );
00951     setOption( "kde-margin-right", QString::number( right ), true );
00952 }
00953 
00954 // FIXME: remove for 4.0
00955 QSize KPrinter::realPageSize() const
00956 {
00957     kdWarning( 500 ) << "KPrinter::realPageSize() is obsolete, do not use" << endl;
00958     if ( d->m_pagesize )
00959         return d->m_pagesize->pageSize();
00960     else
00961         return QSize();
00962 }
00963 
00964 void KPrinter::setRealPageSize(DrPageSize *p)
00965 {
00966     if ( p )
00967     {
00968         kdDebug( 500 ) << "Page size:  width =" << p->pageWidth() << endl;
00969         kdDebug( 500 ) << "Page size: height =" << p->pageHeight() << endl;
00970         kdDebug( 500 ) << "Page size:   left =" << p->leftMargin() << endl;
00971         kdDebug( 500 ) << "Page size:    top =" << p->topMargin() << endl;
00972         kdDebug( 500 ) << "Page size:  right =" << p->rightMargin() << endl;
00973         kdDebug( 500 ) << "Page size: bottom =" << p->bottomMargin() << endl;
00974     }
00975     else
00976         kdDebug( 500 ) << "Resetting page size" << endl;
00977 
00978     /* we copy the page size structure internally
00979      * as the original object is owned by the driver
00980      * that control its destrution */
00981     delete d->m_pagesize;
00982     d->m_pagesize = 0;
00983     if ( p )
00984         d->m_pagesize = new DrPageSize( *p );
00985 }
00986 
00987 // FIXME: remove for 4.0
00988 void KPrinter::setRealPageSize( QSize )
00989 {
00990     kdWarning( 500 ) << "KPrinter::setRealPageSize(QSize) is obsolete, do not use (no effect)" << endl;
00991 }
00992 
00993 // FIXME: remove for 4.0
00994 void KPrinter::setRealDrawableArea( const QRect& )
00995 {
00996     kdWarning( 500 ) << "KPrinter::setRealDrawableArea(QRect) is obsolete, do not use (no effect)" << endl;
00997 }
00998 
00999 // FIXME: remove for 4.0
01000 QRect KPrinter::realDrawableArea() const
01001 {
01002     kdWarning( 500 ) << "KPrinter::realDrawableArea() is obsolete, do not use" << endl;
01003     if ( d->m_pagesize )
01004         return d->m_pagesize->pageRect();
01005     else
01006         return QRect();
01007 }
01008 
01009 QString KPrinter::errorMessage() const
01010 { return d->m_errormsg; }
01011 
01012 void KPrinter::setErrorMessage(const QString& msg)
01013 { d->m_errormsg = msg; }
01014 
01015 /* we're using a builtin member to store this state because we don't
01016  * want to keep it from object to object. So there's no need to use
01017  * the QMap structure to store this
01018  */
01019 void KPrinter::setPreviewOnly(bool on)
01020 { d->m_previewonly = on; }
01021 
01022 bool KPrinter::previewOnly() const
01023 { return d->m_previewonly; }
01024 
01025 void KPrinter::setDocFileName(const QString& s)
01026 { d->m_docfilename = s; }
01027 
01028 QString KPrinter::docFileName() const
01029 { return d->m_docfilename; }
01030 
01031 void KPrinter::setDocDirectory( const QString& s )
01032 { d->m_docdirectory = s; }
01033 
01034 QString KPrinter::docDirectory() const
01035 { return ( d->m_docdirectory.isEmpty() ? QDir::homeDirPath() : d->m_docdirectory ); }
01036 
01037 void KPrinter::setResolution(int dpi)
01038 {
01039     d->m_wrapper->setResolution(dpi);
01040     d->m_defaultres = dpi;
01041 }
01042 
01043 int KPrinter::resolution() const
01044 { return d->m_wrapper->resolution(); }
01045 
01046 void KPrinter::setUsePrinterResolution( bool on )
01047 { d->m_useprinterres = on; }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Jul 20 14:03:16 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003