summaryrefslogtreecommitdiffstats
path: root/kdeprint/tools/escputil/escpwidget.cpp
blob: 3000b0788b36d4ce9629a5ebb36afd8e4ade0147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "escpwidget.h"

#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqaccel.h>
#include <kdemacros.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kdialogbase.h>
#include <klibloader.h>
#include <kseparator.h>
#include <kdebug.h>

class EscpFactory : public KLibFactory
{
public:
	EscpFactory(TQObject *parent = 0, const char *name = 0) : KLibFactory(parent, name) {}
protected:
	TQObject* createObject(TQObject *parent = 0, const char *name = 0, const char * className = TQOBJECT_OBJECT_NAME_STRING, const TQStringList& args = TQStringList())
	{
               Q_UNUSED(className);
		KDialogBase	*dlg = new KDialogBase(TQT_TQWIDGET(parent), name, true, i18n("EPSON InkJet Printer Utilities"), KDialogBase::Close);
		EscpWidget	*w = new EscpWidget(dlg);
		if (args.count() > 0)
			w->setDevice(args[0]);
		if (args.count() > 1)
			w->setPrinterName(args[1]);
		dlg->setMainWidget(w);
		return TQT_TQOBJECT(dlg);
	}
};

extern "C"
{
	void* init_tdeprint_tool_escputil() KDE_EXPORT;
	void* init_tdeprint_tool_escputil()
	{
		return new EscpFactory;
	}
}

EscpWidget::EscpWidget(TQWidget *parent, const char *name)
: TQWidget(parent, name)
{
	m_hasoutput = false;

	connect(&m_proc, TQT_SIGNAL(processExited(KProcess*)), TQT_SLOT(slotProcessExited(KProcess*)));
	connect(&m_proc, TQT_SIGNAL(receivedStdout(KProcess*,char*,int)), TQT_SLOT(slotReceivedStdout(KProcess*,char*,int)));
	connect(&m_proc, TQT_SIGNAL(receivedStderr(KProcess*,char*,int)), TQT_SLOT(slotReceivedStderr(KProcess*,char*,int)));

	TQPushButton	*cleanbtn = new TQPushButton(this, "-c");
	cleanbtn->setPixmap(DesktopIcon("exec"));
	TQPushButton	*nozzlebtn = new TQPushButton(this, "-n");
	nozzlebtn->setPixmap(DesktopIcon("exec"));
	TQPushButton	*alignbtn = new TQPushButton(this, "-a");
	alignbtn->setPixmap(DesktopIcon("exec"));
	TQPushButton	*inkbtn = new TQPushButton(this, "-i");
	inkbtn->setPixmap(DesktopIcon("tdeprint_inklevel"));
	TQPushButton	*identbtn = new TQPushButton(this, "-d");
	identbtn->setPixmap(DesktopIcon("exec"));

	TQFont	f(font());
	f.setBold(true);
	m_printer = new TQLabel(this);
	m_printer->setFont(f);
	m_device = new TQLabel(this);
	m_device->setFont(f);
	m_useraw = new TQCheckBox(i18n("&Use direct connection (might need root permissions)"), this);

	connect(cleanbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked()));
	connect(nozzlebtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked()));
	connect(alignbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked()));
	connect(inkbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked()));
	connect(identbtn, TQT_SIGNAL(clicked()), TQT_SLOT(slotButtonClicked()));

	TQLabel	*printerlab = new TQLabel(i18n("Printer:"), this);
	printerlab->tqsetAlignment(AlignRight|AlignVCenter);
	TQLabel	*devicelab = new TQLabel(i18n("Device:"), this);
	devicelab->tqsetAlignment(AlignRight|AlignVCenter);
	TQLabel	*cleanlab = new TQLabel(i18n("Clea&n print head"), this);
	TQLabel	*nozzlelab = new TQLabel(i18n("&Print a nozzle test pattern"), this);
	TQLabel	*alignlab = new TQLabel(i18n("&Align print head"), this);
	TQLabel	*inklab = new TQLabel(i18n("&Ink level"), this);
	TQLabel	*identlab = new TQLabel(i18n("P&rinter identification"), this);

	cleanlab->tqsetAlignment(AlignLeft|AlignVCenter|ShowPrefix);
	nozzlelab->tqsetAlignment(AlignLeft|AlignVCenter|ShowPrefix);
	alignlab->tqsetAlignment(AlignLeft|AlignVCenter|ShowPrefix);
	inklab->tqsetAlignment(AlignLeft|AlignVCenter|ShowPrefix);
	identlab->tqsetAlignment(AlignLeft|AlignVCenter|ShowPrefix);

	cleanbtn->setAccel(TQAccel::shortcutKey(cleanlab->text()));
	nozzlebtn->setAccel(TQAccel::shortcutKey(nozzlelab->text()));
	alignbtn->setAccel(TQAccel::shortcutKey(alignlab->text()));
	inkbtn->setAccel(TQAccel::shortcutKey(inklab->text()));
	identbtn->setAccel(TQAccel::shortcutKey(identlab->text()));

	KSeparator	*sep = new KSeparator(this);
	sep->setFixedHeight(10);

	TQGridLayout	*l0 = new TQGridLayout(this, 8, 2, 10, 10);
	TQGridLayout	*l1 = new TQGridLayout(0, 2, 2, 0, 5);
	l0->addMultiCellLayout(l1, 0, 0, 0, 1);
	l1->addWidget(printerlab, 0, 0);
	l1->addWidget(devicelab, 1, 0);
	l1->addWidget(m_printer, 0, 1);
	l1->addWidget(m_device, 1, 1);
	l1->setColStretch(1, 1);
	l0->addMultiCellWidget(sep, 1, 1, 0, 1);
	l0->addWidget(cleanbtn, 2, 0);
	l0->addWidget(nozzlebtn, 3, 0);
	l0->addWidget(alignbtn, 4, 0);
	l0->addWidget(inkbtn, 5, 0);
	l0->addWidget(identbtn, 6, 0);
	l0->addWidget(cleanlab, 2, 1);
	l0->addWidget(nozzlelab, 3, 1);
	l0->addWidget(alignlab, 4, 1);
	l0->addWidget(inklab, 5, 1);
	l0->addWidget(identlab, 6, 1);
	l0->addMultiCellWidget(m_useraw, 7, 7, 0, 1);
	l0->setColStretch(1, 1);
}

void EscpWidget::startCommand(const TQString& arg)
{
	bool	useUSB(false);

	if (m_deviceURL.isEmpty())
	{
		KMessageBox::error(this, i18n("Internal error: no device set."));
		return;
	}
	else
	{
		TQString	protocol = m_deviceURL.protocol();
		if (protocol == "usb")
			useUSB = true;
		else if (protocol != "file" && protocol != "parallel" && protocol != "serial" && !protocol.isEmpty())
		{
			KMessageBox::error(this,
				i18n("Unsupported connection type: %1").arg(protocol));
			return;
		}
	}

	if (m_proc.isRunning())
	{
		KMessageBox::error(this, i18n("An escputil process is still running. "
		                              "You must wait until its completion before continuing."));
		return;
	}

	TQString	exestr = KStandardDirs::findExe("escputil");
	if (exestr.isEmpty())
	{
		KMessageBox::error(this, i18n("The executable escputil cannot be found in your "
		                              "PATH environment variable. Make sure gimp-print is "
		                              "installed and that escputil is in your PATH."));
		return;
	}

	m_proc.clearArguments();
	m_proc << exestr;
	if (m_useraw->isChecked() || arg == "-i")
		m_proc << "-r" << m_deviceURL.path();
	else
		m_proc << "-P" << m_printer->text();
	if (useUSB)
		m_proc << "-u";

	m_proc << arg << "-q";
	m_errorbuffer = m_outbuffer = TQString::null;
	m_hasoutput = ( arg == "-i" || arg == "-d" );
	for ( TQValueList<TQCString>::ConstIterator it=m_proc.args().begin(); it!=m_proc.args().end(); ++it )
		kdDebug() << "ARG: " << *it << endl;
	if (m_proc.start(KProcess::NotifyOnExit, KProcess::AllOutput))
		setEnabled(false);
	else
	{
		KMessageBox::error(this,
			i18n("Internal error: unable to start escputil process."));
		return;
	}
}

void EscpWidget::slotProcessExited(KProcess*)
{
	setEnabled(true);
	if (!m_proc.normalExit() || m_proc.exitStatus() != 0)
	{
		TQString	msg1 = "<qt>"+i18n("Operation terminated with errors.")+"</qt>";
		TQString	msg2;
		if (!m_outbuffer.isEmpty())
			msg2 += "<p><b><u>"+i18n("Output")+"</u></b></p><p>"+m_outbuffer+"</p>";
		if (!m_errorbuffer.isEmpty())
			msg2 += "<p><b><u>"+i18n("Error")+"</u></b></p><p>"+m_errorbuffer+"</p>";
		if (!msg2.isEmpty())
			KMessageBox::detailedError(this, msg1, msg2);
		else
			KMessageBox::error(this, msg1);
	}
	else if ( !m_outbuffer.isEmpty() && m_hasoutput )
	{
		KMessageBox::information( this, m_outbuffer );
	}
	m_hasoutput = false;
}

void EscpWidget::slotReceivedStdout(KProcess*, char *buf, int len)
{
	TQString	bufstr = TQCString(buf, len);
	m_outbuffer.append(bufstr);
}

void EscpWidget::slotReceivedStderr(KProcess*, char *buf, int len)
{
	TQString	bufstr = TQCString(buf, len);
	m_errorbuffer.append(bufstr);
}

void EscpWidget::slotButtonClicked()
{
	TQString	arg = TQT_TQOBJECT_CONST(sender())->name();
	startCommand(arg);
}

void EscpWidget::setPrinterName(const TQString& p)
{
	m_printer->setText(p);
}

void EscpWidget::setDevice(const TQString& dev)
{
	m_deviceURL = dev;
	m_device->setText(dev);
}

#include "escpwidget.moc"