kmwippprinter.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 "kmwippprinter.h"
00021 #include "kmwizard.h"
00022 #include "kmprinter.h"
00023 #include "ipprequest.h"
00024 #include "kmcupsmanager.h"
00025 #include "networkscanner.h"
00026 
00027 #include <klistview.h>
00028 #include <qheader.h>
00029 #include <kpushbutton.h>
00030 #include <qlineedit.h>
00031 #include <qlabel.h>
00032 #include <kmessagebox.h>
00033 #include <qtextview.h>
00034 #include <qlayout.h>
00035 #include <qregexp.h>
00036 #include <kseparator.h>
00037 #include <klocale.h>
00038 #include <kiconloader.h>
00039 #include <kguiitem.h>
00040 #include <kurl.h>
00041 
00042 KMWIppPrinter::KMWIppPrinter(QWidget *parent, const char *name)
00043 : KMWizardPage(parent,name)
00044 {
00045     m_title = i18n("IPP Printer Information");
00046     m_ID = KMWizard::Custom+1;
00047     m_nextpage = KMWizard::Driver;
00048 
00049     m_list = new KListView(this);
00050     m_list->addColumn("");
00051     m_list->header()->hide();
00052     m_list->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00053     m_list->setLineWidth(1);
00054 
00055     QLabel  *l1 = new QLabel(i18n("&Printer URI:"),this);
00056 
00057     m_uri = new QLineEdit(this);
00058 
00059     l1->setBuddy(m_uri);
00060 
00061     m_info = new QTextView(this);
00062     m_info->setPaper(colorGroup().background());
00063     m_info->setMinimumHeight(100);
00064     m_info->setText(i18n("<p>Either enter the printer URI directly, or use the network scanning facility.</p>"));
00065     m_ippreport = new KPushButton(KGuiItem(i18n("&IPP Report"), "kdeprint_report"), this);
00066     m_ippreport->setEnabled(false);
00067 
00068     m_scanner = new NetworkScanner( 631, this );
00069 
00070     KSeparator* sep = new KSeparator( KSeparator::HLine, this);
00071     sep->setFixedHeight(20);
00072 
00073     connect(m_list,SIGNAL(selectionChanged(QListViewItem*)),SLOT(slotPrinterSelected(QListViewItem*)));
00074     connect( m_scanner, SIGNAL( scanStarted() ), SLOT( slotScanStarted() ) );
00075     connect( m_scanner, SIGNAL( scanFinished() ), SLOT( slotScanFinished() ) );
00076     connect( m_scanner, SIGNAL( scanStarted() ), parent, SLOT( disableWizard() ) );
00077     connect( m_scanner, SIGNAL( scanFinished() ), parent, SLOT( enableWizard() ) );
00078     connect(m_ippreport, SIGNAL(clicked()), SLOT(slotIppReport()));
00079 
00080     // layout
00081     QHBoxLayout *lay3 = new QHBoxLayout(this, 0, 10);
00082     QVBoxLayout *lay2 = new QVBoxLayout(0, 0, 0);
00083     QHBoxLayout *lay4 = new QHBoxLayout(0, 0, 0);
00084 
00085     lay3->addWidget(m_list,1);
00086     lay3->addLayout(lay2,1);
00087     lay2->addWidget(l1);
00088     lay2->addWidget(m_uri);
00089     lay2->addSpacing(10);
00090     lay2->addWidget(m_info, 1);
00091     lay2->addSpacing(5);
00092     lay2->addLayout(lay4);
00093     lay4->addStretch(1);
00094     lay4->addWidget(m_ippreport);
00095     lay2->addWidget(sep);
00096     lay2->addWidget( m_scanner );
00097 }
00098 
00099 KMWIppPrinter::~KMWIppPrinter()
00100 {
00101 }
00102 
00103 void KMWIppPrinter::updatePrinter(KMPrinter *p)
00104 {
00105     p->setDevice(m_uri->text());
00106 }
00107 
00108 bool KMWIppPrinter::isValid(QString& msg)
00109 {
00110     if (m_uri->text().isEmpty())
00111     {
00112         msg = i18n("You must enter a printer URI.");
00113         return false;
00114     }
00115 
00116     KURL uri( m_uri->text() );
00117     if (!m_scanner->checkPrinter(uri.host(),(uri.port()==0?631:uri.port())))
00118     {
00119         msg = i18n("No printer found at this address/port.");
00120         return false;
00121     }
00122     return true;
00123 }
00124 
00125 void KMWIppPrinter::slotScanStarted()
00126 {
00127     m_list->clear();
00128 }
00129 
00130 void KMWIppPrinter::slotScanFinished()
00131 {
00132     m_ippreport->setEnabled(false);
00133     const QPtrList<NetworkScanner::SocketInfo>  *list = m_scanner->printerList();
00134     QPtrListIterator<NetworkScanner::SocketInfo>    it(*list);
00135     for (;it.current();++it)
00136     {
00137         QString name;
00138         if (it.current()->Name.isEmpty())
00139             name = i18n("Unknown host - 1 is the IP", "<Unknown> (%1)").arg(it.current()->IP);
00140         else
00141         name = it.current()->Name;
00142         QListViewItem   *item = new QListViewItem(m_list,name,it.current()->IP,QString::number(it.current()->Port));
00143         item->setPixmap(0,SmallIcon("kdeprint_printer"));
00144     }
00145 }
00146 
00147 void KMWIppPrinter::slotPrinterSelected(QListViewItem *item)
00148 {
00149     m_ippreport->setEnabled(item != 0);
00150     if (!item) return;
00151 
00152     // trying to get printer attributes
00153     IppRequest  req;
00154     QString     uri;
00155     QStringList keys;
00156 
00157     req.setOperation(IPP_GET_PRINTER_ATTRIBUTES);
00158     req.setHost(item->text(1));
00159     req.setPort(item->text(2).toInt());
00160     uri = QString::fromLatin1("ipp://%1:%2/ipp").arg(item->text(1)).arg(item->text(2));
00161     req.addURI(IPP_TAG_OPERATION,"printer-uri",uri);
00162     keys.append("printer-name");
00163     keys.append("printer-state");
00164     keys.append("printer-info");
00165     keys.append("printer-uri-supported");
00166     keys.append("printer-make-and-model");
00167     keys.append("printer-location");
00168     req.addKeyword(IPP_TAG_OPERATION,"requested-attributes",keys);
00169     if (req.doRequest("/ipp/") && (req.status() == IPP_OK || req.status() == IPP_OK_SUBST || req.status() == IPP_OK_CONFLICT))
00170     {
00171         QString value, txt;
00172         int     state;
00173         if (req.name("printer-name",value)) txt.append(i18n("<b>Name</b>: %1<br>").arg(value));
00174         if (req.text("printer-location",value) && !value.isEmpty()) txt.append(i18n("<b>Location</b>: %1<br>").arg(value));
00175         if (req.text("printer-info",value) && !value.isEmpty()) txt.append(i18n("<b>Description</b>: %1<br>").arg(value.replace(QRegExp(";"),"<br>")));
00176         if (req.uri("printer-uri-supported",value))
00177         {
00178             if (value[0] == '/')
00179                 value.prepend(QString::fromLatin1("ipp://%1:%2").arg(item->text(1)).arg(item->text(2)));
00180             m_uri->setText(value);
00181         }
00182         if (req.text("printer-make-and-model",value) && !value.isEmpty()) txt.append(i18n("<b>Model</b>: %1<br>").arg(value));
00183         if (req.enumvalue("printer-state",state))
00184         {
00185             switch (state)
00186             {
00187                 case IPP_PRINTER_IDLE: value = i18n("Idle"); break;
00188                 case IPP_PRINTER_STOPPED: value = i18n("Stopped"); break;
00189                 case IPP_PRINTER_PROCESSING: value = i18n("Processing..."); break;
00190                 default: value = i18n("Unknown State", "Unknown"); break;
00191             }
00192             txt.append(i18n("<b>State</b>: %1<br>").arg(value));
00193         }
00194         m_info->setText(txt);
00195     }
00196     else
00197     {
00198         m_uri->setText(uri);
00199         m_info->setText(i18n("Unable to retrieve printer info. Printer answered:<br><br>%1").arg(ippErrorString((ipp_status_t)req.status())));
00200     }
00201 }
00202 
00203 void KMWIppPrinter::slotIppReport()
00204 {
00205     IppRequest  req;
00206     QString uri("ipp://%1:%2/ipp");
00207     QListViewItem   *item = m_list->currentItem();
00208 
00209     if (item)
00210     {
00211         req.setOperation(IPP_GET_PRINTER_ATTRIBUTES);
00212         req.setHost(item->text(1));
00213         req.setPort(item->text(2).toInt());
00214         uri = uri.arg(item->text(1)).arg(item->text(2));
00215         req.addURI(IPP_TAG_OPERATION, "printer-uri", uri);
00216         if (req.doRequest("/ipp/"))
00217         {
00218             QString caption = i18n("IPP Report for %1").arg(item->text(0));
00219             static_cast<KMCupsManager*>(KMManager::self())->ippReport(req, IPP_TAG_PRINTER, caption);
00220         }
00221         else
00222             KMessageBox::error(this, i18n("Unable to generate report. IPP request failed with message: "
00223                                           "%1 (0x%2).").arg(ippErrorString((ipp_status_t)req.status())).arg(req.status(),0,16));
00224     }
00225 }
00226 
00227 #include "kmwippprinter.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys