22 #include "kmcupsmanager.h"
23 #include "kmprinter.h"
24 #include "ipprequest.h"
25 #include "cupsinfos.h"
27 #include "kmfactory.h"
28 #include "kmdbentry.h"
29 #include "cupsaddsmb2.h"
30 #include "ippreportdlg.h"
31 #include "kpipeprocess.h"
33 #include "foomatic2loader.h"
34 #include "ppdloader.h"
37 #include <tqtextstream.h>
41 #include <tqdatetime.h>
44 #include <tdeapplication.h>
45 #include <tdelocale.h>
46 #include <tdeconfig.h>
47 #include <kstandarddirs.h>
48 #include <tdesocketbase.h>
49 #include <klibloader.h>
50 #include <tdemessagebox.h>
51 #include <tdeaction.h>
52 #include <kdialogbase.h>
53 #include <kextendedsocket.h>
54 #include <tdeprocess.h>
55 #include <kbufferedsocket.h>
56 #include <kfilterdev.h>
57 #include <cups/cups.h>
61 #define ppdi18n(s) i18n(TQString::fromLocal8Bit(s).utf8())
63 static void extractMaticData(TQString& buf,
const TQString& filename);
64 static TQString printerURI(KMPrinter *p,
bool useExistingURI);
65 static TQString downloadDriver(KMPrinter *p);
67 static int trials = 5;
71 KMCupsManager::KMCupsManager(TQObject *parent,
const char *name,
const TQStringList & )
72 : KMManager(parent,name)
81 setHasManagement(
true);
82 setPrinterOperationMask(KMManager::PrinterAll);
83 setServerOperationMask(KMManager::ServerAll);
88 setenv(
"LANG",
"en_US.UTF-8", 1);
91 KMCupsManager::~KMCupsManager()
96 TQString KMCupsManager::driverDbCreationProgram()
98 return TQString(__TDE_BINDIR).append(TQString::fromLatin1(
"/make_driver_db_cups"));
101 TQString KMCupsManager::driverDirectory()
103 TQString d = cupsInstallDir();
105 #if defined(__OpenBSD__) || defined(__FreeBSD__)
111 d.append(
"/share/cups/model");
113 #if defined(__OpenBSD__) || defined(__FreeBSD__)
114 d.append(
":/usr/local/share/foomatic/db/source");
116 d.append(
":/usr/share/foomatic/db/source");
118 d.append(
":/usr/lib/cups/driver/foomatic-db-compressed-ppds");
123 TQString KMCupsManager::cupsInstallDir()
125 TDEConfig *conf= KMFactory::self()->printConfig();
126 conf->setGroup(
"CUPS");
127 TQString dir = conf->readPathEntry(
"InstallDir");
131 void KMCupsManager::reportIppError(IppRequest *req)
133 setErrorMsg(req->statusMessage());
136 bool KMCupsManager::createPrinter(KMPrinter *p)
138 bool isclass = p->isClass(
false), result(
false);
142 uri = printerURI(p,
false);
143 req.addURI(IPP_TAG_OPERATION,
"printer-uri",uri);
145 p->setUri(KURL(uri));
149 req.setOperation(CUPS_ADD_CLASS);
150 TQStringList members = p->members(), uris;
152 s = TQString::fromLocal8Bit(
"ipp://%1/printers/").arg(CupsInfos::self()->hostaddr());
153 for (TQStringList::ConstIterator it=members.begin(); it!=members.end(); ++it)
154 uris.append(s+(*it));
155 req.addURI(IPP_TAG_PRINTER,
"member-uris",uris);
159 req.setOperation(CUPS_ADD_PRINTER);
162 KMPrinter *otherP = findPrinter(p->printerName());
163 if (!otherP || otherP->device() != p->device())
170 req.addURI(IPP_TAG_PRINTER,
"device-uri",p->device());
172 if (!p->option(
"kde-banners").isEmpty())
174 TQStringList bans = TQStringList::split(
',',p->option(
"kde-banners"),
false);
175 while (bans.count() < 2)
177 req.addName(IPP_TAG_PRINTER,
"job-sheets-default",bans);
179 req.addInteger(IPP_TAG_PRINTER,
"job-quota-period",p->option(
"job-quota-period").toInt());
180 req.addInteger(IPP_TAG_PRINTER,
"job-k-limit",p->option(
"job-k-limit").toInt());
181 req.addInteger(IPP_TAG_PRINTER,
"job-page-limit",p->option(
"job-page-limit").toInt());
182 if (!p->option(
"requesting-user-name-denied").isEmpty())
183 req.addName(IPP_TAG_PRINTER,
"requesting-user-name-denied",TQStringList::split(
",",p->option(
"requesting-user-name-denied"),
false));
184 else if (!p->option(
"requesting-user-name-allowed").isEmpty())
185 req.addName(IPP_TAG_PRINTER,
"requesting-user-name-allowed",TQStringList::split(
",",p->option(
"requesting-user-name-allowed"),
false));
187 req.addName(IPP_TAG_PRINTER,
"requesting-user-name-allowed",TQString::fromLatin1(
"all"));
189 req.addText(IPP_TAG_PRINTER,
"printer-info",p->description());
190 req.addText(IPP_TAG_PRINTER,
"printer-location",p->location());
192 if (req.doRequest(
"/admin/"))
196 result = savePrinterDriver(p,p->driver());
200 else reportIppError(&req);
205 bool KMCupsManager::removePrinter(KMPrinter *p)
207 bool result = setPrinterState(p,CUPS_DELETE_PRINTER);
211 bool KMCupsManager::enablePrinter(KMPrinter *p,
bool state)
213 return setPrinterState(p, (state ? CUPS_ACCEPT_JOBS : CUPS_REJECT_JOBS));
216 bool KMCupsManager::startPrinter(KMPrinter *p,
bool state)
218 return setPrinterState(p, (state ? IPP_RESUME_PRINTER : IPP_PAUSE_PRINTER));
221 bool KMCupsManager::setDefaultPrinter(KMPrinter *p)
223 return setPrinterState(p,CUPS_SET_DEFAULT);
226 bool KMCupsManager::setPrinterState(KMPrinter *p,
int state)
231 req.setOperation(state);
232 uri = printerURI(p,
true);
233 req.addURI(IPP_TAG_OPERATION,
"printer-uri",uri);
234 if (req.doRequest(
"/admin/"))
236 reportIppError(&req);
240 bool KMCupsManager::completePrinter(KMPrinter *p)
242 if (completePrinterShort(p))
245 TQString ppdname = downloadDriver(p);
246 ppd_file_t *ppd = (ppdname.isEmpty() ? NULL : ppdOpenFile(ppdname.local8Bit()));
253 entry.manufacturer = ppd->manufacturer;
254 entry.model = ppd->shortnickname;
255 entry.modelname = ppd->modelname;
257 entry.validate(
false);
259 p->setManufacturer(entry.manufacturer);
260 p->setModel(entry.model);
261 p->setDriverInfo(TQString::fromLocal8Bit(ppd->nickname));
264 if (!ppdname.isEmpty())
265 TQFile::remove(ppdname);
272 bool KMCupsManager::completePrinterShort(KMPrinter *p)
278 req.setOperation(IPP_GET_PRINTER_ATTRIBUTES);
279 uri = printerURI(p,
true);
280 req.addURI(IPP_TAG_OPERATION,
"printer-uri",uri);
322 keys.append(
"printer-info");
323 keys.append(
"printer-make-and-model");
324 keys.append(
"job-sheets-default");
325 keys.append(
"job-sheets-supported");
326 keys.append(
"job-quota-period");
327 keys.append(
"job-k-limit");
328 keys.append(
"job-page-limit");
329 keys.append(
"requesting-user-name-allowed");
330 keys.append(
"requesting-user-name-denied");
331 if (p->isClass(
true))
333 keys.append(
"member-uris");
334 keys.append(
"member-names");
337 keys.append(
"device-uri");
338 req.addKeyword(IPP_TAG_OPERATION,
"requested-attributes",keys);
340 if (req.doRequest(
"/printers/"))
343 if (req.text(
"printer-info",value)) p->setDescription(value);
346 if (req.text(
"printer-make-and-model",value)) p->setDriverInfo(value);
347 if (req.uri(
"device-uri",value))
353 p->setDevice( value );
367 if (req.name(
"member-names",values))
368 p->setMembers(values);
370 req.name(
"job-sheets-default",values);
371 while (values.count() < 2) values.append(
"none");
372 p->setOption(
"kde-banners",values.join(TQString::fromLatin1(
",")));
373 if (req.name(
"job-sheets-supported",values)) p->setOption(
"kde-banners-supported",values.join(TQString::fromLatin1(
",")));
377 if (req.integer(
"job-quota-period",ival)) p->setOption(
"job-quota-period",TQString::number(ival));
378 if (req.integer(
"job-k-limit",ival)) p->setOption(
"job-k-limit",TQString::number(ival));
379 if (req.integer(
"job-page-limit",ival)) p->setOption(
"job-page-limit",TQString::number(ival));
382 if (req.name(
"requesting-user-name-allowed",values) && values.count() > 0)
384 p->removeOption(
"requesting-user-name-denied");
385 p->setOption(
"requesting-user-name-allowed",values.join(
","));
387 if (req.name(
"requesting-user-name-denied",values) && values.count() > 0)
389 p->removeOption(
"requesting-user-name-allowed");
390 p->setOption(
"requesting-user-name-denied",values.join(
","));
396 reportIppError(&req);
400 bool KMCupsManager::testPrinter(KMPrinter *p)
402 return KMManager::testPrinter(p);
427 void KMCupsManager::listPrinters()
429 loadServerPrinters();
432 void KMCupsManager::loadServerPrinters()
438 req.setOperation(CUPS_GET_PRINTERS);
439 keys.append(
"printer-name");
440 keys.append(
"printer-type");
441 keys.append(
"printer-state");
443 keys.append(
"printer-location");
444 keys.append(
"printer-uri-supported");
445 keys.append(
"printer-is-accepting-jobs");
446 req.addKeyword(IPP_TAG_OPERATION,
"requested-attributes",keys);
449 req.addName(IPP_TAG_OPERATION,
"requesting-user-name", TQString(cupsUser()));
451 if (req.doRequest(
"/printers/"))
453 processRequest(&req);
457 req.setOperation(CUPS_GET_CLASSES);
458 req.addKeyword(IPP_TAG_OPERATION,
"requested-attributes",keys);
460 if (req.doRequest(
"/classes/"))
462 processRequest(&req);
466 req.setOperation(CUPS_GET_DEFAULT);
467 req.addKeyword(IPP_TAG_OPERATION,
"requested-attributes",TQString::fromLatin1(
"printer-name"));
468 if (req.doRequest(
"/printers/"))
470 TQString s = TQString::null;
471 req.name(
"printer-name",s);
472 setHardDefault(findPrinter(s));
484 reportIppError(&req);
487 void KMCupsManager::processRequest(IppRequest* req)
489 ipp_attribute_t *attr = req->first();
490 ipp_attribute_t *nextAttr;
491 KMPrinter *printer =
new KMPrinter();
495 TQString attrname(ippGetName(attr));
496 if (attrname ==
"printer-name")
498 TQString value = TQString::fromLocal8Bit(ippGetString(attr, 0, NULL));
499 printer->setName(value);
500 printer->setPrinterName(value);
502 else if (attrname ==
"printer-type")
504 int value = ippGetInteger(attr, 0);
506 printer->addType(((value & CUPS_PRINTER_CLASS) || (value & CUPS_PRINTER_IMPLICIT) ? KMPrinter::Class : KMPrinter::Printer));
507 if ((value & CUPS_PRINTER_REMOTE)) printer->addType(KMPrinter::Remote);
508 if ((value & CUPS_PRINTER_IMPLICIT)) printer->addType(KMPrinter::Implicit);
511 printer->setPrinterCap( ( value & CUPS_PRINTER_OPTIONS ) >> 2 );
513 else if (attrname ==
"printer-state")
515 switch (ippGetInteger(attr, 0))
517 case IPP_PRINTER_IDLE: printer->setState(KMPrinter::Idle);
break;
518 case IPP_PRINTER_PROCESSING: printer->setState(KMPrinter::Processing);
break;
519 case IPP_PRINTER_STOPPED: printer->setState(KMPrinter::Stopped);
break;
522 else if (attrname ==
"printer-uri-supported")
524 printer->setUri(KURL(ippGetString(attr, 0, NULL)));
526 else if (attrname ==
"printer-location")
528 printer->setLocation(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
530 else if (attrname ==
"printer-is-accepting-jobs")
532 printer->setAcceptJobs(ippGetBoolean(attr, 0));
535 nextAttr = ippNextAttribute(req->request());
536 if (attrname.isEmpty() || (!nextAttr))
539 printer =
new KMPrinter();
543 TQString attrname(attr->name);
544 if (attrname ==
"printer-name")
546 TQString value = TQString::fromLocal8Bit(attr->values[0].string.text);
547 printer->setName(value);
548 printer->setPrinterName(value);
550 else if (attrname ==
"printer-type")
552 int value = attr->values[0].integer;
554 printer->addType(((value & CUPS_PRINTER_CLASS) || (value & CUPS_PRINTER_IMPLICIT) ? KMPrinter::Class : KMPrinter::Printer));
555 if ((value & CUPS_PRINTER_REMOTE)) printer->addType(KMPrinter::Remote);
556 if ((value & CUPS_PRINTER_IMPLICIT)) printer->addType(KMPrinter::Implicit);
559 printer->setPrinterCap( ( value & CUPS_PRINTER_OPTIONS ) >> 2 );
561 else if (attrname ==
"printer-state")
563 switch (attr->values[0].integer)
565 case IPP_PRINTER_IDLE: printer->setState(KMPrinter::Idle);
break;
566 case IPP_PRINTER_PROCESSING: printer->setState(KMPrinter::Processing);
break;
567 case IPP_PRINTER_STOPPED: printer->setState(KMPrinter::Stopped);
break;
570 else if (attrname ==
"printer-uri-supported")
572 printer->setUri(KURL(attr->values[0].string.text));
574 else if (attrname ==
"printer-location")
576 printer->setLocation(TQString::fromLocal8Bit(attr->values[0].string.text));
578 else if (attrname ==
"printer-is-accepting-jobs")
580 printer->setAcceptJobs(attr->values[0].boolean);
582 if (attrname.isEmpty() || attr == req->last())
585 printer =
new KMPrinter();
593 DrMain* KMCupsManager::loadPrinterDriver(KMPrinter *p,
bool)
598 if (p->isClass(
true))
600 KMPrinter *first_class_member = NULL;
602 first_class_member = findPrinter(p->members().first());
604 if (first_class_member == NULL)
611 p = first_class_member;
615 TQString fname = downloadDriver(p);
617 if (!fname.isEmpty())
619 driver = loadDriverFile(fname);
621 driver->set(
"temporary",fname);
627 DrMain* KMCupsManager::loadFileDriver(
const TQString& filename)
629 if (filename.startsWith(
"ppd:"))
630 return loadDriverFile(filename.mid(4));
631 else if (filename.startsWith(
"compressed-ppd:"))
632 return loadDriverFile(filename);
633 else if (filename.startsWith(
"foomatic/"))
634 return loadMaticDriver(filename);
636 return loadDriverFile(filename);
639 DrMain* KMCupsManager::loadMaticDriver(
const TQString& drname)
641 TQStringList comps = TQStringList::split(
'/', drname,
false);
642 TQString tmpFile = locateLocal(
"tmp",
"foomatic_" + kapp->randomString(8));
643 #if defined(__OpenBSD__) || defined(__FreeBSD__)
644 TQString PATH = getenv(
"PATH") + TQString::fromLatin1(
":/usr/local/bin:/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local/sbin");
646 TQString PATH = getenv(
"PATH") + TQString::fromLatin1(
":/usr/sbin:/usr/local/sbin:/opt/sbin:/opt/local/sbin");
648 TQString exe = TDEStandardDirs::findExe(
"foomatic-datafile", PATH);
651 setErrorMsg(i18n(
"Unable to find the executable foomatic-datafile "
652 "in your PATH. Check that Foomatic is correctly installed."));
658 TQString cmd = TDEProcess::quote(exe);
659 cmd +=
" -t cups -d ";
660 cmd += TDEProcess::quote(comps[2]);
662 cmd += TDEProcess::quote(comps[1]);
663 if (in.open(cmd) && out.open(IO_WriteOnly))
665 TQTextStream tin(&in), tout(&out);
669 line = tin.readLine();
670 tout << line << endl;
675 DrMain *driver = loadDriverFile(tmpFile);
678 driver->set(
"template", tmpFile);
679 driver->set(
"temporary", tmpFile);
683 setErrorMsg(i18n(
"Unable to create the Foomatic driver [%1,%2]. "
684 "Either that driver does not exist, or you don't have "
685 "the required permissions to perform that operation.").arg(comps[1]).arg(comps[2]));
686 TQFile::remove(tmpFile);
690 DrMain* KMCupsManager::loadDriverFile(
const TQString& fname)
692 if ((fname.startsWith(
"compressed-ppd:")) || TQFile::exists(fname))
695 DrMain *driver = PPDLoader::loadDriver( fname, &msg );
698 driver->set(
"template", fname );
708 void KMCupsManager::saveDriverFile(DrMain *driver,
const TQString& filename)
710 kdDebug( 500 ) <<
"Saving PPD file with template=" << driver->get(
"template" ) << endl;
711 TQString templateFile = driver->get(
"template" );
712 if (templateFile.startsWith(
"compressed-ppd:")) {
713 templateFile = driver->get(
"temporary-cppd" );
715 TQIODevice *in = KFilterDev::deviceForFile( templateFile );
716 TQFile out(filename);
717 if (in && in->open(IO_ReadOnly) && out.open(IO_WriteOnly))
719 TQTextStream tin(in), tout(&out);
720 TQString line, keyword;
721 bool isnumeric(
false);
726 line = tin.readLine();
727 if (line.startsWith(
"*% COMDATA #"))
730 if ((p=line.find(
"'name'")) != -1)
732 p = line.find(
'\'',p+6)+1;
733 q = line.find(
'\'',p);
734 keyword = line.mid(p,q-p);
735 opt = driver->findOption(keyword);
736 if (opt && (opt->type() == DrBase::Integer || opt->type() == DrBase::Float))
749 else if ((p=line.find(
"'default'")) != -1 && !keyword.isEmpty() && opt && isnumeric)
751 TQString prefix = line.left(p+9);
752 tout << prefix <<
" => '" << opt->valueText() <<
'\'';
753 if (line.find(
',',p) != -1)
758 tout << line << endl;
760 else if (line.startsWith(
"*Default"))
762 int p = line.find(
':',8);
763 keyword = line.mid(8,p-8);
765 if ( keyword ==
"PageRegion" || keyword ==
"ImageableArea" || keyword ==
"PaperDimension" )
766 bopt = driver->findOption( TQString::fromLatin1(
"PageSize" ) );
768 bopt = driver->findOption( keyword );
770 switch (bopt->type())
773 case DrBase::Boolean:
775 DrListOption *opt =
static_cast<DrListOption*
>(bopt);
776 if (opt && opt->currentChoice())
777 tout <<
"*Default" << keyword <<
": " << opt->currentChoice()->name() << endl;
779 tout << line << endl;
782 case DrBase::Integer:
784 DrIntegerOption *opt =
static_cast<DrIntegerOption*
>(bopt);
785 tout <<
"*Default" << keyword <<
": " << opt->fixedVal() << endl;
790 DrFloatOption *opt =
static_cast<DrFloatOption*
>(bopt);
791 tout <<
"*Default" << keyword <<
": " << opt->fixedVal() << endl;
795 tout << line << endl;
799 tout << line << endl;
802 tout << line << endl;
808 bool KMCupsManager::savePrinterDriver(KMPrinter *p, DrMain *d)
810 TQString tmpfilename = locateLocal(
"tmp",
"print_") + kapp->randomString(8);
813 saveDriverFile(d,tmpfilename);
820 req.setOperation(CUPS_ADD_PRINTER);
821 uri = printerURI(p,
true);
822 req.addURI(IPP_TAG_OPERATION,
"printer-uri",uri);
823 result = req.doFileRequest(
"/admin/",tmpfilename);
826 TQFile::remove(tmpfilename);
829 reportIppError(&req);
833 void* KMCupsManager::loadCupsdConfFunction(
const char *name)
837 m_cupsdconf = KLibLoader::self()->library(
"cupsdconf");
840 setErrorMsg(i18n(
"Library cupsdconf not found. Check your installation."));
844 void* func = m_cupsdconf->symbol(name);
846 setErrorMsg(i18n(
"Symbol %1 not found in cupsdconf library.").arg(name));
850 void KMCupsManager::unloadCupsdConf()
854 KLibLoader::self()->unloadLibrary(
"libcupsdconf");
859 bool KMCupsManager::restartServer()
862 bool (*f1)(TQString&) = (
bool(*)(TQString&))loadCupsdConfFunction(
"restartServer");
867 if (!result) setErrorMsg(msg);
873 bool KMCupsManager::configureServer(TQWidget *parent)
876 bool (*f2)(TQWidget*, TQString&) = (
bool(*)(TQWidget*, TQString&))loadCupsdConfFunction(
"configureServer");
880 result = f2(parent, msg);
888 TQStringList KMCupsManager::detectLocalPrinters()
892 ipp_attribute_t *nextAttr;
893 req.setOperation(CUPS_GET_DEVICES);
894 if (req.doRequest(
"/"))
896 TQString desc, uri, printer, cl;
897 ipp_attribute_t *attr = req.first();
901 TQString attrname(ippGetName(attr));
902 if (attrname ==
"device-info") desc = ippGetString(attr, 0, NULL);
903 else if (attrname ==
"device-make-and-model") printer = ippGetString(attr, 0, NULL);
904 else if (attrname ==
"device-uri") uri = ippGetString(attr, 0, NULL);
905 else if ( attrname ==
"device-class" ) cl = ippGetString(attr, 0, NULL);
906 nextAttr = ippNextAttribute(req.request());
907 if (attrname.isEmpty() || (!nextAttr))
911 if (printer ==
"Unknown") printer = TQString::null;
912 list << cl << uri << desc << printer;
914 uri = desc = printer = cl = TQString::null;
918 TQString attrname(attr->name);
919 if (attrname ==
"device-info") desc = attr->values[0].string.text;
920 else if (attrname ==
"device-make-and-model") printer = attr->values[0].string.text;
921 else if (attrname ==
"device-uri") uri = attr->values[0].string.text;
922 else if ( attrname ==
"device-class" ) cl = attr->values[ 0 ].string.text;
923 if (attrname.isEmpty() || attr == req.last())
927 if (printer ==
"Unknown") printer = TQString::null;
928 list << cl << uri << desc << printer;
930 uri = desc = printer = cl = TQString::null;
939 void KMCupsManager::createPluginActions(TDEActionCollection *coll)
941 TDEAction *act =
new TDEAction(i18n(
"&Export Driver..."),
"tdeprint_uploadsmb", 0,
this, TQ_SLOT(exportDriver()), coll,
"plugin_export_driver");
942 act->setGroup(
"plugin");
943 act =
new TDEAction(i18n(
"&Printer IPP Report"),
"tdeprint_report", 0,
this, TQ_SLOT(printerIppReport()), coll,
"plugin_printer_ipp_report");
944 act->setGroup(
"plugin");
947 void KMCupsManager::validatePluginActions(TDEActionCollection *coll, KMPrinter *pr)
950 m_currentprinter = pr;
951 coll->action(
"plugin_export_driver")->setEnabled(pr && pr->isLocal() && !pr->isClass(
true) && !pr->isSpecial());
952 coll->action(
"plugin_printer_ipp_report")->setEnabled(pr && !pr->isSpecial());
955 void KMCupsManager::exportDriver()
957 if (m_currentprinter && m_currentprinter->isLocal() &&
958 !m_currentprinter->isClass(
true) && !m_currentprinter->isSpecial())
960 TQString path = cupsInstallDir();
961 if (path.isEmpty()) {
962 #if defined(__OpenBSD__) || defined(__FreeBSD__)
963 path =
"/usr/local/share/cups";
965 path =
"/usr/share/cups";
968 path +=
"/share/cups";
970 CupsAddSmb::exportDest(m_currentprinter->printerName(), path);
974 void KMCupsManager::printerIppReport()
976 if (m_currentprinter && !m_currentprinter->isSpecial())
981 req.setOperation(IPP_GET_PRINTER_ATTRIBUTES);
982 uri = printerURI(m_currentprinter,
true);
983 req.addURI(IPP_TAG_OPERATION,
"printer-uri",uri);
992 if (req.doRequest(
"/printers/"))
994 ippReport(req, IPP_TAG_PRINTER, i18n(
"IPP Report for %1").arg(m_currentprinter->printerName()));
998 KMessageBox::error(0,
"<p>"+i18n(
"Unable to retrieve printer information. Error received:")+
"</p>"+req.statusMessage());
1003 void KMCupsManager::ippReport(IppRequest& req,
int group,
const TQString& caption)
1005 IppReportDlg::report(&req, group, caption);
1008 TQString KMCupsManager::stateInformation()
1010 return TQString(
"%1: %2")
1011 .arg(i18n(
"Server"))
1012 .arg(CupsInfos::self()->host()[0] !=
'/' ?
1013 TQString(TQString(
"%1:%2").arg(CupsInfos::self()->host()).arg(CupsInfos::self()->port()))
1014 : CupsInfos::self()->host());
1017 void KMCupsManager::checkUpdatePossibleInternal()
1019 kdDebug(500) <<
"Checking for update possible" << endl;
1021 m_socket =
new KNetwork::TDEBufferedSocket;
1022 m_socket->setTimeout( 1500 );
1023 connect( m_socket, TQ_SIGNAL( connected(
const KResolverEntry&) ),
1024 TQ_SLOT( slotConnectionSuccess() ) );
1025 connect( m_socket, TQ_SIGNAL( gotError(
int ) ), TQ_SLOT( slotConnectionFailed(
int ) ) );
1028 TQTimer::singleShot( 1,
this, TQ_SLOT( slotAsyncConnect() ) );
1031 void KMCupsManager::slotConnectionSuccess()
1033 kdDebug(500) <<
"Connection success, trying to send a request..." << endl;
1037 req.setOperation( CUPS_GET_PRINTERS );
1038 req.addKeyword( IPP_TAG_OPERATION,
"requested-attributes", TQString::fromLatin1(
"printer-name" ) );
1039 if ( req.doRequest(
"/printers/" ) )
1040 setUpdatePossible(
true );
1043 kdDebug(500) <<
"Unable to get printer list" << endl;
1047 TQTimer::singleShot( 1000,
this, TQ_SLOT( slotAsyncConnect() ) );
1051 setErrorMsg( i18n(
"Connection to CUPS server failed. Check that the CUPS server is correctly installed and running. "
1052 "Error: %1." ).arg( i18n(
"the IPP request failed for an unknown reason" ) ) );
1053 setUpdatePossible(
false );
1058 void KMCupsManager::slotAsyncConnect()
1060 kdDebug(500) <<
"Starting async connect to " << CupsInfos::self()->hostaddr() << endl;
1062 if (CupsInfos::self()->host().startsWith(
"/"))
1063 m_socket->connect( TQString(), CupsInfos::self()->host());
1065 m_socket->connectToHost( CupsInfos::self()->host(), CupsInfos::self()->port() );
1068 void KMCupsManager::slotConnectionFailed(
int errcode )
1070 kdDebug(500) <<
"Connection failed trials=" << trials << endl;
1077 TQTimer::singleShot( 1000,
this, TQ_SLOT( slotAsyncConnect() ) );
1084 case KNetwork::TDESocketBase::ConnectionRefused:
1085 case KNetwork::TDESocketBase::ConnectionTimedOut:
1086 einfo = i18n(
"connection refused") + TQString(
" (%1)").arg(errcode);
1088 case KNetwork::TDESocketBase::LookupFailure:
1089 einfo = i18n(
"host not found") + TQString(
" (%1)").arg(errcode);
1091 case KNetwork::TDESocketBase::WouldBlock:
1093 einfo = i18n(
"read failed (%1)").arg(errcode);
1097 setErrorMsg( i18n(
"Connection to CUPS server failed. Check that the CUPS server is correctly installed and running. "
1098 "Error: %2: %1." ).arg( einfo, CupsInfos::self()->host()));
1099 setUpdatePossible(
false );
1102 void KMCupsManager::hostPingSlot() {
1103 m_hostSuccess =
true;
1104 m_lookupDone =
true;
1107 void KMCupsManager::hostPingFailedSlot() {
1108 m_hostSuccess =
false;
1109 m_lookupDone =
true;
1114 static void extractMaticData(TQString& buf,
const TQString& filename)
1117 if (f.exists() && f.open(IO_ReadOnly))
1123 line = t.readLine();
1124 if (line.startsWith(
"*% COMDATA #"))
1125 buf.append(line.right(line.length()-12)).append(
'\n');
1130 static TQString printerURI(KMPrinter *p,
bool use)
1133 if (use && !p->uri().isEmpty())
1134 uri = p->uri().prettyURL();
1136 uri = TQString(
"ipp://%1/%3/%2").arg(CupsInfos::self()->hostaddr()).arg(p->printerName()).arg((p->isClass(
false) ?
"classes" :
"printers"));
1140 static TQString downloadDriver(KMPrinter *p)
1142 TQString driverfile, prname = p->printerName();
1143 bool changed(
false);
1162 driverfile = cupsGetPPD(prname.local8Bit());
1167 cupsSetServer(CupsInfos::self()->host().local8Bit());
1168 ippSetPort(CupsInfos::self()->port());
1174 #include "kmcupsmanager.moc"