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., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, 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 <tqfile.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(TQObject *parent, const char *name) 00040 : TQObject(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 TQString& 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 TQString& 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 TQString& 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 TQString& 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 TQString& 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 TQString 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 TQString& name) 00190 { 00191 TQPtrListIterator<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 TQPtrListIterator<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 TQPtrListIterator<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 TQPtrList<KMPrinter>* KMManager::printerList(bool reload) 00225 { 00226 setErrorMsg(TQString::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(TQString::fromLatin1(getenv("PRINTER"))); 00272 if (defprinter) 00273 setSoftDefault(defprinter); 00274 } 00275 emit printerListUpdated(); 00276 } 00277 00278 return &m_fprinters; 00279 } 00280 00281 TQPtrList<KMPrinter>* KMManager::printerListComplete(bool reload) 00282 { 00283 printerList(reload); 00284 return &m_printers; 00285 } 00286 00287 void KMManager::listPrinters() 00288 { 00289 // do nothing 00290 } 00291 00292 void KMManager::addPrinter(KMPrinter *p) 00293 { 00294 if (p) 00295 { 00296 if (p->name().isEmpty()) 00297 // discard printer with empty name 00298 delete p; 00299 else 00300 { 00301 KMPrinter *other = findPrinter(p->name()); 00302 if (other) 00303 { 00304 other->copy(*p); 00305 delete p; 00306 } 00307 else 00308 { 00309 p->setDiscarded(false); 00310 m_printers.inSort(p); 00311 } 00312 } 00313 } 00314 } 00315 00316 TQString KMManager::driverDbCreationProgram() 00317 { 00318 return TQString::null; 00319 } 00320 00321 TQString KMManager::driverDirectory() 00322 { 00323 return TQString::null; 00324 } 00325 00326 DrMain* KMManager::loadPrinterDriver(KMPrinter*, bool) 00327 { 00328 return NULL; 00329 } 00330 00331 DrMain* KMManager::loadDbDriver(KMDBEntry *entry) 00332 { 00333 return loadFileDriver(entry->file); 00334 } 00335 00336 DrMain* KMManager::loadFileDriver(const TQString&) 00337 { 00338 return NULL; 00339 } 00340 00341 DrMain* KMManager::loadDriver(KMPrinter *p, bool config) 00342 { 00343 if (p->isSpecial()) 00344 return m_specialmgr->loadDriver(p); 00345 else 00346 return loadPrinterDriver(p, config); 00347 } 00348 00349 bool KMManager::savePrinterDriver(KMPrinter*,DrMain*) 00350 { 00351 return notImplemented(); 00352 } 00353 00354 bool KMManager::uncompressFile(const TQString& filename, TQString& destname) 00355 { 00356 TQFile f(filename); 00357 bool result(true); 00358 destname = TQString::null; 00359 if (f.exists() && f.open(IO_ReadOnly)) 00360 { 00361 char buf[1024] = {0}; 00362 f.readBlock(buf,2); 00363 if ((uchar)(buf[0]) == 037 && (uchar)(buf[1]) == 0213) 00364 { 00365 f.close(); 00366 destname = locateLocal("tmp","kdeprint_") + KApplication::randomString(8); 00367 f.setName(destname); 00368 00369 if (f.open(IO_WriteOnly)) 00370 { 00371 gzFile in = gzopen(filename.latin1(),"r"); 00372 int n(0); 00373 while ((n=gzread(in,buf,1024)) > 0) 00374 if (f.writeBlock(buf,n) != n) 00375 break; 00376 if (n != 0) result = false; 00377 gzclose(in); 00378 f.close(); 00379 } 00380 } 00381 } 00382 return result; 00383 } 00384 00385 void KMManager::setHardDefault(KMPrinter *p) 00386 { 00387 TQPtrListIterator<KMPrinter> it(m_printers); 00388 for (;it.current();++it) 00389 it.current()->setHardDefault(false); 00390 if (p) p->setHardDefault(true); 00391 } 00392 00393 void KMManager::setSoftDefault(KMPrinter *p) 00394 { 00395 TQPtrListIterator<KMPrinter> it(m_printers); 00396 for (;it.current();++it) 00397 { 00398 it.current()->setSoftDefault(false); 00399 it.current()->setOwnSoftDefault(false); 00400 } 00401 if (p) 00402 { 00403 p->setSoftDefault(true); 00404 KMPrinter *pp = findPrinter(p->printerName()); 00405 if (pp) 00406 pp->setOwnSoftDefault(true); 00407 } 00408 } 00409 00410 bool KMManager::restartServer() 00411 { 00412 return notImplemented(); 00413 } 00414 00415 bool KMManager::configureServer(TQWidget*) 00416 { 00417 return notImplemented(); 00418 } 00419 00420 TQString KMManager::testPage() 00421 { 00422 KConfig *conf = KMFactory::self()->printConfig(); 00423 conf->setGroup("General"); 00424 QString tpage = conf->readPathEntry("TestPage"); 00425 if (tpage.isEmpty()) 00426 tpage = locate("data","kdeprint/testprint.ps"); 00427 return tpage; 00428 } 00429 00430 void KMManager::discardAllPrinters(bool on) 00431 { 00432 TQPtrListIterator<KMPrinter> it(m_printers); 00433 for (;it.current();++it) 00434 if (!on || !it.current()->isSpecial()) 00435 it.current()->setDiscarded(on); 00436 } 00437 00438 bool KMManager::validateDbDriver(KMDBEntry*) 00439 { 00440 return true; 00441 } 00442 00443 bool KMManager::createSpecialPrinter(KMPrinter *p) 00444 { 00445 if (p && p->isSpecial()) 00446 { 00447 KMPrinter *old = findPrinter(p->name()); 00448 if (old && !old->isSpecial()) 00449 { 00450 setErrorMsg(i18n("Can't overwrite regular printer with special printer settings.")); 00451 return false; 00452 } 00453 // if the special printer already exists, it will be overwritten 00454 addPrinter(p); 00455 return m_specialmgr->savePrinters(); 00456 } 00457 return false; 00458 } 00459 00460 bool KMManager::removeSpecialPrinter(KMPrinter *p) 00461 { 00462 if (p && p->isSpecial() && m_printers.findRef(p) != -1) 00463 { 00464 m_printers.removeRef(p); 00465 return m_specialmgr->savePrinters(); 00466 } 00467 return false; 00468 } 00469 00470 /* format of output: 00471 Class0, URI0, Desc0, Printer0, Class1, URI1, Desc1, Printer1, ... 00472 00473 Defaults to 3 parallel printers 00474 */ 00475 TQStringList KMManager::detectLocalPrinters() 00476 { 00477 TQStringList list; 00478 for (int i=0; i<3; i++) 00479 list << TQString() << TQString::fromLatin1("parallel:/dev/lp%1").arg(i) << i18n("Parallel Port #%1").arg(i+1) << TQString(); 00480 return list; 00481 } 00482 00483 int KMManager::addPrinterWizard(TQWidget *parent) 00484 { 00485 KLibrary *lib = KLibLoader::self()->library("libkdeprint_management_module"); 00486 if (!lib) 00487 setErrorMsg(i18n("Unable to load KDE print management library: %1").arg(KLibLoader::self()->lastErrorMessage())); 00488 else 00489 { 00490 int (*func)(TQWidget*) = (int(*)(TQWidget*))lib->symbol("add_printer_wizard"); 00491 if (!func) 00492 setErrorMsg(i18n("Unable to find wizard object in management library.")); 00493 else 00494 return func(parent); 00495 } 00496 return (-1); 00497 } 00498 00499 bool KMManager::invokeOptionsDialog(TQWidget *parent) 00500 { 00501 KLibrary *lib = KLibLoader::self()->library("libkdeprint_management_module"); 00502 if (!lib) 00503 setErrorMsg(i18n("Unable to load KDE print management library: %1").arg(KLibLoader::self()->lastErrorMessage())); 00504 else 00505 { 00506 bool (*func)(TQWidget*) = (bool(*)(TQWidget*))lib->symbol("config_dialog"); 00507 if (!func) 00508 setErrorMsg(i18n("Unable to find options dialog in management library.")); 00509 else 00510 return func(parent); 00511 } 00512 return false; 00513 } 00514 00515 void KMManager::createPluginActions(KActionCollection*) 00516 { 00517 } 00518 00519 void KMManager::validatePluginActions(KActionCollection*, KMPrinter*) 00520 { 00521 } 00522 00523 void KMManager::enableFilter(bool on) 00524 { 00525 m_printerfilter->setEnabled(on); 00526 } 00527 00528 bool KMManager::isFilterEnabled() const 00529 { 00530 return m_printerfilter->isEnabled(); 00531 } 00532 00533 TQString KMManager::stateInformation() 00534 { 00535 return i18n("No plugin information available"); 00536 } 00537 00538 void KMManager::checkUpdatePossible() 00539 { 00540 m_updatepossible = false; 00541 checkUpdatePossibleInternal(); 00542 } 00543 00544 void KMManager::checkUpdatePossibleInternal() 00545 { 00546 setUpdatePossible( true ); 00547 } 00548 00549 void KMManager::setUpdatePossible( bool value ) 00550 { 00551 if ( value != m_updatepossible ) 00552 m_virtualmgr->reset(); 00553 m_updatepossible = value; 00554 emit updatePossible( m_updatepossible ); 00555 } 00556 00557 #include "kmmanager.moc"