kmwdrivertest.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 "kmwdrivertest.h"
00021 #include "kmprinter.h"
00022 #include "kmwizard.h"
00023 #include "driver.h"
00024 #include "kmfactory.h"
00025 #include "kmmanager.h"
00026 #include "kmdriverdialog.h"
00027 
00028 #include <qlabel.h>
00029 #include <kpushbutton.h>
00030 #include <qlayout.h>
00031 #include <klocale.h>
00032 #include <kapplication.h>
00033 #include <kmessagebox.h>
00034 #include <kguiitem.h>
00035 #include <kio/netaccess.h>
00036 
00037 KMWDriverTest::KMWDriverTest(QWidget *parent, const char *name)
00038 : KMWizardPage(parent,name)
00039 {
00040     m_ID = KMWizard::DriverTest;
00041     m_title = i18n("Printer Test");
00042     m_nextpage = KMWizard::Name;
00043         m_needsinitonback = true;
00044     m_driver = 0;
00045     m_printer = 0;
00046 
00047     m_manufacturer = new QLabel(this);
00048     m_model = new QLabel(this);
00049     m_driverinfo = new QLabel(this);
00050     m_driverinfo->setTextFormat(Qt::RichText);
00051     QLabel  *l1 = new QLabel(i18n("<b>Manufacturer:</b>"), this);
00052     QLabel  *l2 = new QLabel(i18n("<b>Model:</b>"), this);
00053     QLabel  *l3 = new QLabel(i18n("<b>Description:</b>"), this);
00054 
00055     m_test = new KPushButton(KGuiItem(i18n("&Test"), "kdeprint_testprinter"), this);
00056     m_settings = new KPushButton(KGuiItem(i18n("&Settings"), "configure"), this);
00057 
00058     QLabel  *l0 = new QLabel(this);
00059     l0->setText(i18n("<p>Now you can test the printer before finishing installation. "
00060              "Use the <b>Settings</b> button to configure the printer driver and "
00061              "the <b>Test</b> button to test your configuration. Use the <b>Back</b> "
00062              "button to change the driver (your current configuration will be discarded).</p>"));
00063 
00064     QVBoxLayout *lay1 = new QVBoxLayout(this, 0, 15);
00065     QGridLayout *lay2 = new QGridLayout(0, 3, 3, 0, 0);
00066     QHBoxLayout *lay3 = new QHBoxLayout(0, 0, 10);
00067     lay1->addWidget(l0,0);
00068     lay1->addLayout(lay2,0);
00069     lay1->addLayout(lay3,0);
00070     lay1->addStretch(1);
00071     lay2->setColStretch(2,1);
00072     lay2->addColSpacing(1,10);
00073     lay2->addWidget(l1,0,0);
00074     lay2->addWidget(l2,1,0);
00075     lay2->addWidget(l3,2,0,Qt::AlignLeft|Qt::AlignTop);
00076     lay2->addWidget(m_manufacturer,0,2);
00077     lay2->addWidget(m_model,1,2);
00078     lay2->addWidget(m_driverinfo,2,2);
00079     lay3->addWidget(m_test,0);
00080     lay3->addWidget(m_settings,0);
00081     lay3->addStretch(1);
00082 
00083     connect(m_test,SIGNAL(clicked()),SLOT(slotTest()));
00084     connect(m_settings,SIGNAL(clicked()),SLOT(slotSettings()));
00085 }
00086 
00087 KMWDriverTest::~KMWDriverTest()
00088 {
00089     delete m_driver;
00090 }
00091 
00092 void KMWDriverTest::initPrinter(KMPrinter *p)
00093 {
00094     m_manufacturer->setText(p->manufacturer());
00095     m_model->setText(p->model());
00096     m_driverinfo->setText(p->driverInfo());
00097     m_printer = p;
00098 
00099     delete m_driver;
00100     m_driver = 0;
00101 
00102     QString drfile = p->option("kde-driver");
00103     bool    checkDriver(true);
00104     if (!drfile.isEmpty() && drfile != "raw")
00105     {
00106         m_driver = KMFactory::self()->manager()->loadFileDriver(drfile);
00107         /* remove the temp file if it has been downloaded */
00108         KIO::NetAccess::removeTempFile( drfile );
00109     }
00110     else if (p->dbEntry() != NULL)
00111         m_driver = KMFactory::self()->manager()->loadDbDriver(p->dbEntry());
00112     else
00113         checkDriver = false;
00114 
00115     if (checkDriver && !m_driver)
00116     {
00117         KMessageBox::error(this, i18n("<qt>Unable to load the requested driver:<p>%1</p></qt>").arg(KMManager::self()->errorMsg()));
00118         KMManager::self()->setErrorMsg(QString::null);
00119     }
00120     m_settings->setEnabled((m_driver != 0));
00121 }
00122 
00123 void KMWDriverTest::updatePrinter(KMPrinter *p)
00124 {
00125     // Give the DrMain structure to the driver and don't care about it anymore.
00126     // It will be destroyed either when giving another structure, or when the
00127     // printer object will be destroyed.
00128     p->setDriver(m_driver);
00129     m_driver = 0;
00130 }
00131 
00132 void KMWDriverTest::slotTest()
00133 {
00134     if (!m_printer) return;
00135 
00136     QString name = "tmpprinter_"+KApplication::randomString(8);
00137     // save printer name (can be non empty when modifying a printer)
00138     QString oldname = m_printer->name();
00139 
00140     m_printer->setName(name);
00141     m_printer->setPrinterName(name);
00142     m_printer->setDriver(m_driver);
00143     if (KMFactory::self()->manager()->createPrinter(m_printer))
00144     {
00145         if (KMFactory::self()->manager()->testPrinter(m_printer))
00146             KMessageBox::information(this,"<qt>"+i18n("Test page successfully sent to printer. Wait until printing is complete, then click the OK button."));
00147         else
00148             KMessageBox::error(this,"<qt>"+i18n("Unable to test printer: ")+KMFactory::self()->manager()->errorMsg()+"</qt>");
00149         if (!KMFactory::self()->manager()->removePrinter(m_printer))
00150             KMessageBox::error(this,i18n("Unable to remove temporary printer."));
00151     }
00152     else
00153         KMessageBox::error(this,i18n("Unable to create temporary printer."));
00154 
00155     // restoring old name
00156     m_printer->setName(oldname);
00157     m_printer->setPrinterName(oldname);
00158 
00159     m_driver = m_printer->takeDriver();
00160 }
00161 
00162 void KMWDriverTest::slotSettings()
00163 {
00164     if (m_driver)
00165     {
00166         KMDriverDialog  dlg(this);
00167         dlg.setDriver(m_driver);
00168         dlg.showButtonCancel(false);    // only OK button
00169         dlg.exec();
00170     }
00171 }
00172 #include "kmwdrivertest.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys