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 <tqpushbutton.h> 00023 #include <tqlayout.h> 00024 #include <tqlabel.h> 00025 #include <tqcheckbox.h> 00026 #include <tqaccel.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(TQObject *parent = 0, const char *name = 0) : KLibFactory(parent, name) {} 00041 protected: 00042 TQObject* createObject(TQObject *parent = 0, const char *name = 0, const char * className = TQOBJECT_OBJECT_NAME_STRING, const TQStringList& args = TQStringList()) 00043 { 00044 Q_UNUSED(className); 00045 KDialogBase *dlg = new KDialogBase(TQT_TQWIDGET(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 TQT_TQOBJECT(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(TQWidget *parent, const char *name) 00066 : TQWidget(parent, name) 00067 { 00068 m_hasoutput = false; 00069 00070 connect(&m_proc, TQT_SIGNAL(processExited(KProcess*)), TQT_SLOT(slotProcessExited(KProcess*))); 00071 connect(&m_proc, TQT_SIGNAL(receivedStdout(KProcess*,char*,int)), TQT_SLOT(slotReceivedStdout(KProcess*,char*,int))); 00072 connect(&m_proc, TQT_SIGNAL(receivedStderr(KProcess*,char*,int)), TQT_SLOT(slotReceivedStderr(KProcess*,char*,int))); 00073 00074 TQPushButton *cleanbtn = new TQPushButton(this, "-c"); 00075 cleanbtn->setPixmap(DesktopIcon("exec")); 00076 TQPushButton *nozzlebtn = new TQPushButton(this, "-n"); 00077 nozzlebtn->setPixmap(DesktopIcon("exec")); 00078 TQPushButton *alignbtn = new TQPushButton(this, "-a"); 00079 alignbtn->setPixmap(DesktopIcon("exec")); 00080 TQPushButton *inkbtn = new TQPushButton(this, "-i"); 00081 inkbtn->setPixmap(DesktopIcon("kdeprint_inklevel")); 00082 TQPushButton *identbtn = new TQPushButton(this, "-d"); 00083 identbtn->setPixmap(DesktopIcon("exec")); 00084 00085 TQFont f(font()); 00086 f.setBold(true); 00087 m_printer = new TQLabel(this); 00088 m_printer->setFont(f); 00089 m_device = new TQLabel(this); 00090 m_device->setFont(f); 00091 m_useraw = new TQCheckBox(i18n("&Use direct connection (might need root permissions)"), this); 00092 00093 connect(cleanbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked())); 00094 connect(nozzlebtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked())); 00095 connect(alignbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked())); 00096 connect(inkbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked())); 00097 connect(identbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked())); 00098 00099 TQLabel *printerlab = new TQLabel(i18n("Printer:"), this); 00100 printerlab->setAlignment(AlignRight|AlignVCenter); 00101 TQLabel *devicelab = new TQLabel(i18n("Device:"), this); 00102 devicelab->setAlignment(AlignRight|AlignVCenter); 00103 TQLabel *cleanlab = new TQLabel(i18n("Clea&n print head"), this); 00104 TQLabel *nozzlelab = new TQLabel(i18n("&Print a nozzle test pattern"), this); 00105 TQLabel *alignlab = new TQLabel(i18n("&Align print head"), this); 00106 TQLabel *inklab = new TQLabel(i18n("&Ink level"), this); 00107 TQLabel *identlab = new TQLabel(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(TQAccel::shortcutKey(cleanlab->text())); 00116 nozzlebtn->setAccel(TQAccel::shortcutKey(nozzlelab->text())); 00117 alignbtn->setAccel(TQAccel::shortcutKey(alignlab->text())); 00118 inkbtn->setAccel(TQAccel::shortcutKey(inklab->text())); 00119 identbtn->setAccel(TQAccel::shortcutKey(identlab->text())); 00120 00121 KSeparator *sep = new KSeparator(this); 00122 sep->setFixedHeight(10); 00123 00124 TQGridLayout *l0 = new TQGridLayout(this, 8, 2, 10, 10); 00125 TQGridLayout *l1 = new TQGridLayout(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 TQString& 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 TQString 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 TQString 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 = TQString::null; 00196 m_hasoutput = ( arg == "-i" || arg == "-d" ); 00197 for ( TQValueList<TQCString>::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 TQString msg1 = "<qt>"+i18n("Operation terminated with errors.")+"</qt>"; 00215 TQString 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 TQString bufstr = TQCString(buf, len); 00235 m_outbuffer.append(bufstr); 00236 } 00237 00238 void EscpWidget::slotReceivedStderr(KProcess*, char *buf, int len) 00239 { 00240 TQString bufstr = TQCString(buf, len); 00241 m_errorbuffer.append(bufstr); 00242 } 00243 00244 void EscpWidget::slotButtonClicked() 00245 { 00246 TQString arg = TQT_TQOBJECT_CONST(sender())->name(); 00247 startCommand(arg); 00248 } 00249 00250 void EscpWidget::setPrinterName(const TQString& p) 00251 { 00252 m_printer->setText(p); 00253 } 00254 00255 void EscpWidget::setDevice(const TQString& dev) 00256 { 00257 m_deviceURL = dev; 00258 m_device->setText(dev); 00259 } 00260 00261 #include "escpwidget.moc"