kdeprint Library API Documentation

kmmanager.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 "kmmanager.h" 00021 #include "kmprinter.h" 00022 #include "kmdbentry.h" 00023 #include "kmfactory.h" 00024 #include "kmvirtualmanager.h" 00025 #include "kmspecialmanager.h" 00026 #include "printerfilter.h" 00027 #include "kprinter.h" 00028 00029 #include <zlib.h> 00030 #include <qfile.h> 00031 #include <kstandarddirs.h> 00032 #include <kconfig.h> 00033 #include <klocale.h> 00034 #include <kdebug.h> 00035 #include <kapplication.h> 00036 #include <klibloader.h> 00037 #include <unistd.h> 00038 00039 KMManager::KMManager(QObject *parent, const char *name) 00040 : QObject(parent,name) 00041 { 00042 m_printers.setAutoDelete(true); 00043 m_fprinters.setAutoDelete(false); 00044 m_hasmanagement = false; 00045 m_printeroperationmask = 0; 00046 m_serveroperationmask = 0; 00047 m_printerfilter = new PrinterFilter(this); 00048 00049 m_specialmgr = new KMSpecialManager(this); 00050 Q_CHECK_PTR(m_specialmgr); 00051 m_virtualmgr = new KMVirtualManager(this); 00052 Q_CHECK_PTR(m_virtualmgr); 00053 00054 // set default to true to not disturb code that 00055 // hasn't been adapted yet. Otherwise, should be false 00056 m_updatepossible = true; 00057 } 00058 00059 KMManager::~KMManager() 00060 { 00061 } 00062 00063 KMManager* KMManager::self() 00064 { 00065 return KMFactory::self()->manager(); 00066 } 00067 00068 bool KMManager::notImplemented() 00069 { 00070 setErrorMsg(i18n("This operation is not implemented.")); 00071 return false; 00072 } 00073 00074 bool KMManager::modifyPrinter(KMPrinter *oldp, KMPrinter *newp) 00075 { 00076 if (oldp->printerName() != newp->printerName()) 00077 { 00078 // remove the old one, then create the new one. But before removing 00079 // the old one, retrieve its driver so that the new printer has the 00080 // same. 00081 DrMain *driver = loadPrinterDriver(oldp, true); 00082 newp->setDriver(driver); 00083 if (!removePrinter(oldp)) 00084 return false; 00085 } 00086 return createPrinter(newp); 00087 } 00088 00089 bool KMManager::createPrinter(KMPrinter*) 00090 { 00091 return notImplemented(); 00092 } 00093 00094 bool KMManager::removePrinter(KMPrinter*) 00095 { 00096 return notImplemented(); 00097 } 00098 00099 bool KMManager::removePrinter(const QString& name) 00100 { 00101 KMPrinter *p = findPrinter(name); 00102 return (p ? removePrinter(p) : false); 00103 } 00104 00105 bool KMManager::enablePrinter(KMPrinter*, bool) 00106 { 00107 return notImplemented(); 00108 } 00109 00110 bool KMManager::enablePrinter(const QString& name, bool state) 00111 { 00112 KMPrinter *p = findPrinter(name); 00113 return (p ? enablePrinter(p, state) : false); 00114 } 00115 00116 bool KMManager::startPrinter(KMPrinter*, bool) 00117 { 00118 return notImplemented(); 00119 } 00120 00121 bool KMManager::startPrinter(const QString& name, bool state) 00122 { 00123 KMPrinter *p = findPrinter(name); 00124 return (p ? startPrinter(p, state) : false); 00125 } 00126 00127 bool KMManager::upPrinter(KMPrinter *p, bool state) 00128 { 00129 bool result = enablePrinter(p, state); 00130 if (result) 00131 result = startPrinter(p, state); 00132 return result; 00133 } 00134 00135 bool KMManager::completePrinter(KMPrinter*) 00136 { 00137 notImplemented(); 00138 return true; // not false to allow instance config/creation 00139 } 00140 00141 bool KMManager::completePrinterShort(KMPrinter *p) 00142 { 00143 return completePrinter(p); 00144 } 00145 00146 bool KMManager::completePrinter(const QString& name) 00147 { 00148 KMPrinter *p = findPrinter(name); 00149 return (p ? completePrinter(p) : false); 00150 } 00151 00152 bool KMManager::setDefaultPrinter(KMPrinter*) 00153 { 00154 return notImplemented(); 00155 } 00156 00157 bool KMManager::setDefaultPrinter(const QString& name) 00158 { 00159 KMPrinter *p = findPrinter(name); 00160 return (p ? setDefaultPrinter(p) : false); 00161 } 00162 00163 bool KMManager::testPrinter(KMPrinter *prt) 00164 { 00165 // standard Test mechanism 00166 QString testpage = testPage(); 00167 if (testpage.isEmpty()) 00168 { 00169 setErrorMsg(i18n("Unable to locate test page.")); 00170 return false; 00171 } 00172 KPrinter pr; 00173 bool prExist = (findPrinter(prt->printerName()) != 0), result(false); 00174 pr.setPrinterName(prt->printerName()); 00175 pr.setSearchName(prt->name()); 00176 pr.setDocName("KDE Print Test"); 00177 // the printing mechanism may involve some needed operations on the 00178 // printer, so temporary printers (created when testing) should be 00179 // temporarily added to the printer list, then taken out. 00180 if (!prExist) 00181 m_printers.append(prt); 00182 result = pr.printFiles(testpage, false, false); 00183 if (!prExist) 00184 m_printers.take(m_printers.count()-1); 00185 return result; 00186 // return notImplemented(); 00187 } 00188 00189 KMPrinter* KMManager::findPrinter(const QString& name) 00190 { 00191 QPtrListIterator<KMPrinter> it(m_printers); 00192 for (;it.current();++it) 00193 if (it.current()->name() == name) return it.current(); 00194 //setErrorMsg(i18n("%1: printer not found.").arg(name)); 00195 return 0; 00196 } 00197 00198 KMPrinter* KMManager::softDefault() const 00199 { 00200 QPtrListIterator<KMPrinter> it(m_printers); 00201 for (;it.current();++it) 00202 if (it.current()->isSoftDefault()) return it.current(); 00203 return 0; 00204 } 00205 00206 KMPrinter* KMManager::hardDefault() const 00207 { 00208 QPtrListIterator<KMPrinter> it(m_printers); 00209 for (; it.current();++it) 00210 if (it.current()->isHardDefault()) 00211 return it.current(); 00212 return 0; 00213 } 00214 00215 KMPrinter* KMManager::defaultPrinter() 00216 { 00217 printerList(false); 00218 KMPrinter *prt = softDefault(); 00219 if (!prt) 00220 prt = hardDefault(); 00221 return prt; 00222 } 00223 00224 QPtrList<KMPrinter>* KMManager::printerList(bool reload) 00225 { 00226 setErrorMsg(QString::null); 00227 //kdDebug(500) << "Getting printer list: " << reload << endl; 00228 00229 if (reload || m_printers.count() == 0) 00230 { 00231 // reset filter 00232 m_printerfilter->update(); 00233 m_fprinters.clear(); 00234 00235 // first discard all printers 00236 discardAllPrinters(true); 00237 00238 // make sure virtual printers will be reloaded if we don't have 00239 // any printer (for example if settings are wrong) 00240 if (m_printers.count() == 0) 00241 m_virtualmgr->reset(); 00242 00243 // List real printers (in subclasses) 00244 if ( m_updatepossible ) 00245 listPrinters(); 00246 // Listing of special printers has to be done before the instances 00247 // because now special printers can also have instances 00248 m_specialmgr->refresh(); 00249 // list virtual printers (and undiscard virtual printers if necessary) 00250 if ( m_updatepossible ) 00251 m_virtualmgr->refresh(); 00252 00253 // remove discarded printers 00254 for (uint i=0; i<m_printers.count(); i++) 00255 { 00256 KMPrinter *prt = m_printers.at(i); 00257 if (prt->isDiscarded()) 00258 { 00259 m_printers.remove(i); 00260 i--; 00261 } 00262 else if (prt->isSpecial() || m_printerfilter->filter(prt)) 00263 m_fprinters.append(prt); 00264 } 00265 00266 // try to find the default printer from these situations: 00267 // - it already exists from .lpoptions file 00268 // - use the PRINTER variable 00269 if (!softDefault()) 00270 { 00271 KMPrinter *defprinter = findPrinter(QString::fromLatin1(getenv("PRINTER"))); 00272 if (defprinter) 00273 setSoftDefault(defprinter); 00274 } 00275 } 00276 00277 return &m_fprinters; 00278 } 00279 00280 QPtrList<KMPrinter>* KMManager::printerListComplete(bool reload) 00281 { 00282 printerList(reload); 00283 return &m_printers; 00284 } 00285 00286 void KMManager::listPrinters() 00287 { 00288 // do nothing 00289 } 00290 00291 void KMManager::addPrinter(KMPrinter *p) 00292 { 00293 if (p) 00294 { 00295 if (p->name().isEmpty()) 00296 // discard printer with empty name 00297 delete p; 00298 else 00299 { 00300 KMPrinter *other = findPrinter(p->name()); 00301 if (other) 00302 { 00303 other->copy(*p); 00304 delete p; 00305 } 00306 else 00307 { 00308 p->setDiscarded(false); 00309 m_printers.inSort(p); 00310 } 00311 } 00312 } 00313 } 00314 00315 QString KMManager::driverDbCreationProgram() 00316 { 00317 return QString::null; 00318 } 00319 00320 QString KMManager::driverDirectory() 00321 { 00322 return QString::null; 00323 } 00324 00325 DrMain* KMManager::loadPrinterDriver(KMPrinter*, bool) 00326 { 00327 return NULL; 00328 } 00329 00330 DrMain* KMManager::loadDbDriver(KMDBEntry *entry) 00331 { 00332 return loadFileDriver(entry->file); 00333 } 00334 00335 DrMain* KMManager::loadFileDriver(const QString&) 00336 { 00337 return NULL; 00338 } 00339 00340 DrMain* KMManager::loadDriver(KMPrinter *p, bool config) 00341 { 00342 if (p->isSpecial()) 00343 return m_specialmgr->loadDriver(p); 00344 else 00345 return loadPrinterDriver(p, config); 00346 } 00347 00348 bool KMManager::savePrinterDriver(KMPrinter*,DrMain*) 00349 { 00350 return notImplemented(); 00351 } 00352 00353 bool KMManager::uncompressFile(const QString& filename, QString& destname) 00354 { 00355 QFile f(filename); 00356 bool result(true); 00357 destname = QString::null; 00358 if (f.exists() && f.open(IO_ReadOnly)) 00359 { 00360 char buf[1024] = {0}; 00361 f.readBlock(buf,2); 00362 if ((uchar)(buf[0]) == 037 && (uchar)(buf[1]) == 0213) 00363 { 00364 f.close(); 00365 destname = locateLocal("tmp","kdeprint_") + KApplication::randomString(8); 00366 f.setName(destname); 00367 00368 if (f.open(IO_WriteOnly)) 00369 { 00370 gzFile in = gzopen(filename.latin1(),"r"); 00371 int n(0); 00372 while ((n=gzread(in,buf,1024)) > 0) 00373 if (f.writeBlock(buf,n) != n) 00374 break; 00375 if (n != 0) result = false; 00376 gzclose(in); 00377 f.close(); 00378 } 00379 } 00380 } 00381 return result; 00382 } 00383 00384 void KMManager::setHardDefault(KMPrinter *p) 00385 { 00386 QPtrListIterator<KMPrinter> it(m_printers); 00387 for (;it.current();++it) 00388 it.current()->setHardDefault(false); 00389 if (p) p->setHardDefault(true); 00390 } 00391 00392 void KMManager::setSoftDefault(KMPrinter *p) 00393 { 00394 QPtrListIterator<KMPrinter> it(m_printers); 00395 for (;it.current();++it) 00396 { 00397 it.current()->setSoftDefault(false); 00398 it.current()->setOwnSoftDefault(false); 00399 } 00400 if (p) 00401 { 00402 p->setSoftDefault(true); 00403 KMPrinter *pp = findPrinter(p->printerName()); 00404 if (pp) 00405 pp->setOwnSoftDefault(true); 00406 } 00407 } 00408 00409 bool KMManager::restartServer() 00410 { 00411 return notImplemented(); 00412 } 00413 00414 bool KMManager::configureServer(QWidget*) 00415 { 00416 return notImplemented(); 00417 } 00418 00419 QString KMManager::testPage() 00420 { 00421 KConfig *conf = KMFactory::self()->printConfig(); 00422 conf->setGroup("General"); 00423 QString tpage = conf->readPathEntry("TestPage"); 00424 if (tpage.isEmpty()) 00425 tpage = locate("data","kdeprint/testprint.ps"); 00426 return tpage; 00427 } 00428 00429 void KMManager::discardAllPrinters(bool on) 00430 { 00431 QPtrListIterator<KMPrinter> it(m_printers); 00432 for (;it.current();++it) 00433 if (!on || !it.current()->isSpecial()) 00434 it.current()->setDiscarded(on); 00435 } 00436 00437 bool KMManager::validateDbDriver(KMDBEntry*) 00438 { 00439 return true; 00440 } 00441 00442 bool KMManager::createSpecialPrinter(KMPrinter *p) 00443 { 00444 if (p && p->isSpecial()) 00445 { 00446 KMPrinter *old = findPrinter(p->name()); 00447 if (old && !old->isSpecial()) 00448 { 00449 setErrorMsg(i18n("Can't overwrite regular printer with special printer settings.")); 00450 return false; 00451 } 00452 // if the special printer already exists, it will be overwritten 00453 addPrinter(p); 00454 return m_specialmgr->savePrinters(); 00455 } 00456 return false; 00457 } 00458 00459 bool KMManager::removeSpecialPrinter(KMPrinter *p) 00460 { 00461 if (p && p->isSpecial() && m_printers.findRef(p) != -1) 00462 { 00463 m_printers.removeRef(p); 00464 return m_specialmgr->savePrinters(); 00465 } 00466 return false; 00467 } 00468 00469 /* format of output: 00470 Class0, URI0, Desc0, Printer0, Class1, URI1, Desc1, Printer1, ... 00471 00472 Defaults to 3 parallel printers 00473 */ 00474 QStringList KMManager::detectLocalPrinters() 00475 { 00476 QStringList list; 00477 for (int i=0; i<3; i++) 00478 list << QString::null << QString::fromLatin1("parallel:/dev/lp%1").arg(i) << i18n("Parallel Port #%1").arg(i+1) << QString::null; 00479 return list; 00480 } 00481 00482 int KMManager::addPrinterWizard(QWidget *parent) 00483 { 00484 KLibrary *lib = KLibLoader::self()->library("libkdeprint_management_module"); 00485 if (!lib) 00486 setErrorMsg(i18n("Unable to load KDE print management library: %1").arg(KLibLoader::self()->lastErrorMessage())); 00487 else 00488 { 00489 int (*func)(QWidget*) = (int(*)(QWidget*))lib->symbol("add_printer_wizard"); 00490 if (!func) 00491 setErrorMsg(i18n("Unable to find wizard object in management library.")); 00492 else 00493 return func(parent); 00494 } 00495 return (-1); 00496 } 00497 00498 bool KMManager::invokeOptionsDialog(QWidget *parent) 00499 { 00500 KLibrary *lib = KLibLoader::self()->library("libkdeprint_management_module"); 00501 if (!lib) 00502 setErrorMsg(i18n("Unable to load KDE print management library: %1").arg(KLibLoader::self()->lastErrorMessage())); 00503 else 00504 { 00505 bool (*func)(QWidget*) = (bool(*)(QWidget*))lib->symbol("config_dialog"); 00506 if (!func) 00507 setErrorMsg(i18n("Unable to find options dialog in management library.")); 00508 else 00509 return func(parent); 00510 } 00511 return false; 00512 } 00513 00514 void KMManager::createPluginActions(KActionCollection*) 00515 { 00516 } 00517 00518 void KMManager::validatePluginActions(KActionCollection*, KMPrinter*) 00519 { 00520 } 00521 00522 void KMManager::enableFilter(bool on) 00523 { 00524 m_printerfilter->setEnabled(on); 00525 } 00526 00527 bool KMManager::isFilterEnabled() const 00528 { 00529 return m_printerfilter->isEnabled(); 00530 } 00531 00532 QString KMManager::stateInformation() 00533 { 00534 return i18n("No plugin information available"); 00535 } 00536 00537 void KMManager::checkUpdatePossible() 00538 { 00539 m_updatepossible = false; 00540 checkUpdatePossibleInternal(); 00541 } 00542 00543 void KMManager::checkUpdatePossibleInternal() 00544 { 00545 setUpdatePossible( true ); 00546 } 00547 00548 void KMManager::setUpdatePossible( bool value ) 00549 { 00550 if ( value != m_updatepossible ) 00551 m_virtualmgr->reset(); 00552 m_updatepossible = value; 00553 emit updatePossible( m_updatepossible ); 00554 } 00555 00556 #include "kmmanager.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 8 11:15:38 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003