escpwidget.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 "escpwidget.h"
00021 
00022 #include <qpushbutton.h>
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qcheckbox.h>
00026 #include <qaccel.h>
00027 #include <kdemacros.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <kstandarddirs.h>
00031 #include <kiconloader.h>
00032 #include <kdialogbase.h>
00033 #include <klibloader.h>
00034 #include <kseparator.h>
00035 #include <kdebug.h>
00036 
00037 class EscpFactory : public KLibFactory
00038 {
00039 public:
00040     EscpFactory(QObject *parent = 0, const char *name = 0) : KLibFactory(parent, name) {}
00041 protected:
00042     QObject* createObject(QObject *parent = 0, const char *name = 0, const char * className = "QObject", const QStringList& args = QStringList())
00043     {
00044                Q_UNUSED(className);
00045         KDialogBase *dlg = new KDialogBase(static_cast<QWidget*>(parent), name, true, i18n("EPSON InkJet Printer Utilities"), KDialogBase::Close);
00046         EscpWidget  *w = new EscpWidget(dlg);
00047         if (args.count() > 0)
00048             w->setDevice(args[0]);
00049         if (args.count() > 1)
00050             w->setPrinterName(args[1]);
00051         dlg->setMainWidget(w);
00052         return dlg;
00053     }
00054 };
00055 
00056 extern "C"
00057 {
00058     void* init_kdeprint_tool_escputil() KDE_EXPORT;
00059     void* init_kdeprint_tool_escputil()
00060     {
00061         return new EscpFactory;
00062     }
00063 }
00064 
00065 EscpWidget::EscpWidget(QWidget *parent, const char *name)
00066 : QWidget(parent, name)
00067 {
00068     m_hasoutput = false;
00069 
00070     connect(&m_proc, SIGNAL(processExited(KProcess*)), SLOT(slotProcessExited(KProcess*)));
00071     connect(&m_proc, SIGNAL(receivedStdout(KProcess*,char*,int)), SLOT(slotReceivedStdout(KProcess*,char*,int)));
00072     connect(&m_proc, SIGNAL(receivedStderr(KProcess*,char*,int)), SLOT(slotReceivedStderr(KProcess*,char*,int)));
00073 
00074     QPushButton *cleanbtn = new QPushButton(this, "-c");
00075     cleanbtn->setPixmap(DesktopIcon("exec"));
00076     QPushButton *nozzlebtn = new QPushButton(this, "-n");
00077     nozzlebtn->setPixmap(DesktopIcon("exec"));
00078     QPushButton *alignbtn = new QPushButton(this, "-a");
00079     alignbtn->setPixmap(DesktopIcon("exec"));
00080     QPushButton *inkbtn = new QPushButton(this, "-i");
00081     inkbtn->setPixmap(DesktopIcon("kdeprint_inklevel"));
00082     QPushButton *identbtn = new QPushButton(this, "-d");
00083     identbtn->setPixmap(DesktopIcon("exec"));
00084 
00085     QFont   f(font());
00086     f.setBold(true);
00087     m_printer = new QLabel(this);
00088     m_printer->setFont(f);
00089     m_device = new QLabel(this);
00090     m_device->setFont(f);
00091     m_useraw = new QCheckBox(i18n("&Use direct connection (might need root permissions)"), this);
00092 
00093     connect(cleanbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00094     connect(nozzlebtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00095     connect(alignbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00096     connect(inkbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00097     connect(identbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00098 
00099     QLabel  *printerlab = new QLabel(i18n("Printer:"), this);
00100     printerlab->setAlignment(AlignRight|AlignVCenter);
00101     QLabel  *devicelab = new QLabel(i18n("Device:"), this);
00102     devicelab->setAlignment(AlignRight|AlignVCenter);
00103     QLabel  *cleanlab = new QLabel(i18n("Clea&n print head"), this);
00104     QLabel  *nozzlelab = new QLabel(i18n("&Print a nozzle test pattern"), this);
00105     QLabel  *alignlab = new QLabel(i18n("&Align print head"), this);
00106     QLabel  *inklab = new QLabel(i18n("&Ink level"), this);
00107     QLabel  *identlab = new QLabel(i18n("P&rinter identification"), this);
00108 
00109     cleanlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00110     nozzlelab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00111     alignlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00112     inklab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00113     identlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00114 
00115     cleanbtn->setAccel(QAccel::shortcutKey(cleanlab->text()));
00116     nozzlebtn->setAccel(QAccel::shortcutKey(nozzlelab->text()));
00117     alignbtn->setAccel(QAccel::shortcutKey(alignlab->text()));
00118     inkbtn->setAccel(QAccel::shortcutKey(inklab->text()));
00119     identbtn->setAccel(QAccel::shortcutKey(identlab->text()));
00120 
00121     KSeparator  *sep = new KSeparator(this);
00122     sep->setFixedHeight(10);
00123 
00124     QGridLayout *l0 = new QGridLayout(this, 8, 2, 10, 10);
00125     QGridLayout *l1 = new QGridLayout(0, 2, 2, 0, 5);
00126     l0->addMultiCellLayout(l1, 0, 0, 0, 1);
00127     l1->addWidget(printerlab, 0, 0);
00128     l1->addWidget(devicelab, 1, 0);
00129     l1->addWidget(m_printer, 0, 1);
00130     l1->addWidget(m_device, 1, 1);
00131     l1->setColStretch(1, 1);
00132     l0->addMultiCellWidget(sep, 1, 1, 0, 1);
00133     l0->addWidget(cleanbtn, 2, 0);
00134     l0->addWidget(nozzlebtn, 3, 0);
00135     l0->addWidget(alignbtn, 4, 0);
00136     l0->addWidget(inkbtn, 5, 0);
00137     l0->addWidget(identbtn, 6, 0);
00138     l0->addWidget(cleanlab, 2, 1);
00139     l0->addWidget(nozzlelab, 3, 1);
00140     l0->addWidget(alignlab, 4, 1);
00141     l0->addWidget(inklab, 5, 1);
00142     l0->addWidget(identlab, 6, 1);
00143     l0->addMultiCellWidget(m_useraw, 7, 7, 0, 1);
00144     l0->setColStretch(1, 1);
00145 }
00146 
00147 void EscpWidget::startCommand(const QString& arg)
00148 {
00149     bool    useUSB(false);
00150 
00151     if (m_deviceURL.isEmpty())
00152     {
00153         KMessageBox::error(this, i18n("Internal error: no device set."));
00154         return;
00155     }
00156     else
00157     {
00158         QString protocol = m_deviceURL.protocol();
00159         if (protocol == "usb")
00160             useUSB = true;
00161         else if (protocol != "file" && protocol != "parallel" && protocol != "serial" && !protocol.isEmpty())
00162         {
00163             KMessageBox::error(this,
00164                 i18n("Unsupported connection type: %1").arg(protocol));
00165             return;
00166         }
00167     }
00168 
00169     if (m_proc.isRunning())
00170     {
00171         KMessageBox::error(this, i18n("An escputil process is still running. "
00172                                       "You must wait until its completion before continuing."));
00173         return;
00174     }
00175 
00176     QString exestr = KStandardDirs::findExe("escputil");
00177     if (exestr.isEmpty())
00178     {
00179         KMessageBox::error(this, i18n("The executable escputil cannot be found in your "
00180                                       "PATH environment variable. Make sure gimp-print is "
00181                                       "installed and that escputil is in your PATH."));
00182         return;
00183     }
00184 
00185     m_proc.clearArguments();
00186     m_proc << exestr;
00187     if (m_useraw->isChecked() || arg == "-i")
00188         m_proc << "-r" << m_deviceURL.path();
00189     else
00190         m_proc << "-P" << m_printer->text();
00191     if (useUSB)
00192         m_proc << "-u";
00193 
00194     m_proc << arg << "-q";
00195     m_errorbuffer = m_outbuffer = QString::null;
00196     m_hasoutput = ( arg == "-i" || arg == "-d" );
00197     for ( QValueList<QCString>::ConstIterator it=m_proc.args().begin(); it!=m_proc.args().end(); ++it )
00198         kdDebug() << "ARG: " << *it << endl;
00199     if (m_proc.start(KProcess::NotifyOnExit, KProcess::AllOutput))
00200         setEnabled(false);
00201     else
00202     {
00203         KMessageBox::error(this,
00204             i18n("Internal error: unable to start escputil process."));
00205         return;
00206     }
00207 }
00208 
00209 void EscpWidget::slotProcessExited(KProcess*)
00210 {
00211     setEnabled(true);
00212     if (!m_proc.normalExit() || m_proc.exitStatus() != 0)
00213     {
00214         QString msg1 = "<qt>"+i18n("Operation terminated with errors.")+"</qt>";
00215         QString msg2;
00216         if (!m_outbuffer.isEmpty())
00217             msg2 += "<p><b><u>"+i18n("Output")+"</u></b></p><p>"+m_outbuffer+"</p>";
00218         if (!m_errorbuffer.isEmpty())
00219             msg2 += "<p><b><u>"+i18n("Error")+"</u></b></p><p>"+m_errorbuffer+"</p>";
00220         if (!msg2.isEmpty())
00221             KMessageBox::detailedError(this, msg1, msg2);
00222         else
00223             KMessageBox::error(this, msg1);
00224     }
00225     else if ( !m_outbuffer.isEmpty() && m_hasoutput )
00226     {
00227         KMessageBox::information( this, m_outbuffer );
00228     }
00229     m_hasoutput = false;
00230 }
00231 
00232 void EscpWidget::slotReceivedStdout(KProcess*, char *buf, int len)
00233 {
00234     QString bufstr = QCString(buf, len);
00235     m_outbuffer.append(bufstr);
00236 }
00237 
00238 void EscpWidget::slotReceivedStderr(KProcess*, char *buf, int len)
00239 {
00240     QString bufstr = QCString(buf, len);
00241     m_errorbuffer.append(bufstr);
00242 }
00243 
00244 void EscpWidget::slotButtonClicked()
00245 {
00246     QString arg = sender()->name();
00247     startCommand(arg);
00248 }
00249 
00250 void EscpWidget::setPrinterName(const QString& p)
00251 {
00252     m_printer->setText(p);
00253 }
00254 
00255 void EscpWidget::setDevice(const QString& dev)
00256 {
00257     m_deviceURL = dev;
00258     m_device->setText(dev);
00259 }
00260 
00261 #include "escpwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys