kmwlpd.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include <config.h>
00021 #include "kmwlpd.h"
00022 #include "kmwizard.h"
00023 #include "kmprinter.h"
00024 
00025 #include <kurl.h>
00026 #include <klocale.h>
00027 #include <qlabel.h>
00028 #include <kdebug.h>
00029 #include <qlineedit.h>
00030 #include <kmessagebox.h>
00031 #include <kextsock.h>
00032 
00033 static bool checkLpdQueue(const char *host, const char *queue);
00034 
00035 
00036 
00037 KMWLpd::KMWLpd(QWidget *parent, const char *name)
00038 : KMWInfoBase(2,parent,name)
00039 {
00040     m_ID = KMWizard::LPD;
00041     m_title = i18n("LPD Queue Information");
00042     m_nextpage = KMWizard::Driver;
00043 
00044     setInfo(i18n("<p>Enter the information concerning the remote LPD queue. "
00045              "This wizard will check them before continuing.</p>"));
00046     setLabel(0,i18n("Host:"));
00047     setLabel(1,i18n("Queue:"));
00048 }
00049 
00050 bool KMWLpd::isValid(QString& msg)
00051 {
00052     if (text(0).isEmpty() || text(1).isEmpty())
00053     {
00054         msg = i18n("Some information is missing!");
00055         return false;
00056     }
00057 
00058     
00059     if (!checkLpdQueue(text(0).latin1(),text(1).latin1()))
00060     {
00061         if (KMessageBox::warningYesNo(this, i18n("Can't find queue %1 on server %2! Do you want to continue anyway?").arg(text(1)).arg(text(0))) == KMessageBox::No)
00062             return false;
00063     }
00064     return true;
00065 }
00066 
00067 void KMWLpd::updatePrinter(KMPrinter *p)
00068 {
00069     QString dev = QString::fromLatin1("lpd://%1/%2").arg(text(0)).arg(text(1));
00070     p->setDevice(dev);
00071 }
00072 
00073 
00074 
00075 bool checkLpdQueue(const char *host, const char *queue)
00076 {
00077     KExtendedSocket sock(host, "printer", KExtendedSocket::streamSocket);
00078     sock.setBlockingMode(true);
00079     if (sock.connect() != 0)
00080         return false;
00081 
00082     char    res[64] = {0};
00083     snprintf(res,64,"%c%s\n",(char)4,queue);
00084     if (sock.writeBlock(res, strlen(res)) != (Q_LONG)(strlen(res)))
00085         return false;
00086 
00087     char    buf[1024] = {0};
00088     int n, tot(1);
00089     while ((n = sock.readBlock(res, 63)) > 0)
00090     {
00091         res[n] = 0;
00092         tot += n;
00093         if (tot >= 1024)
00094             break;
00095         else
00096             strcat(buf, res);
00097     }
00098     sock.close();
00099     if (strlen(buf) == 0 || strstr(buf, "unknown printer") != NULL)
00100         return false;
00101     return true;
00102 }
 
This file is part of the documentation for kdeprint Library Version 3.2.0.