20 #include "lpchelper.h"
21 #include "kpipeprocess.h"
23 #include "lprsettings.h"
25 #include <kstandarddirs.h>
26 #include <tqtextstream.h>
29 #include <tdelocale.h>
33 static TQString execute(
const TQString& cmd)
39 TQTextStream t(&proc);
41 output.append(t.readLine()).append(
"\n");
47 LpcHelper::LpcHelper(TQObject *parent,
const char *name)
48 : TQObject(parent, name)
52 TQString PATH = getenv(
"PATH");
53 PATH.append(
":/usr/sbin:/usr/local/sbin:/sbin:/opt/sbin:/opt/local/sbin");
54 m_exepath = TDEStandardDirs::findExe(
"lpc", PATH);
55 m_checkpcpath = TDEStandardDirs::findExe(
"checkpc", PATH);
56 m_lprmpath = TDEStandardDirs::findExe(
"lprm");
59 LpcHelper::~LpcHelper()
63 KMPrinter::PrinterState LpcHelper::state(
const TQString& prname)
const
65 if (m_state.contains(prname))
66 return m_state[prname];
67 return KMPrinter::Unknown;
70 KMPrinter::PrinterState LpcHelper::state(KMPrinter *prt)
const
72 return state(prt->printerName());
75 void LpcHelper::parsetStatusLPR(TQTextStream &t)
77 TQString printer, line;
85 else if (!line[0].isSpace() && (p = line.find(
':')) != -1)
87 printer = line.left(p);
88 m_state[printer] = KMPrinter::Idle;
90 else if (line.find(
"printing is disabled") != -1)
92 if (!printer.isEmpty())
93 m_state[printer] = KMPrinter::PrinterState((KMPrinter::Stopped) | (m_state[printer] & ~KMPrinter::StateMask));
95 else if (line.find(
"queuing is disabled") != -1)
97 if (!printer.isEmpty())
98 m_state[printer] = KMPrinter::PrinterState((KMPrinter::Rejecting) | (m_state[printer] & KMPrinter::StateMask));
100 else if (line.find(
"entries") != -1)
102 if (!printer.isEmpty() &&
103 (m_state[printer] & KMPrinter::StateMask) != KMPrinter::Stopped &&
104 line.find(
"no entries") == -1)
105 m_state[printer] = KMPrinter::PrinterState((m_state[printer] & ~KMPrinter::StateMask) | KMPrinter::Processing);
110 void LpcHelper::parsetStatusLPRng(TQTextStream& t)
117 if (t.readLine().stripWhiteSpace().startsWith(
"Printer"))
121 l = TQStringList::split(TQRegExp(
"\\s"), t.readLine(),
false);
128 printer = l[0].left(p);
130 if (l[1] ==
"disabled")
131 st = KMPrinter::Stopped;
132 else if (l[3] !=
"0")
133 st = KMPrinter::Processing;
135 st = KMPrinter::Idle;
136 if (l[2] ==
"disabled")
137 st |= KMPrinter::Rejecting;
138 m_state[printer] = KMPrinter::PrinterState(st);
142 void LpcHelper::updateStates()
147 if (!m_exepath.isEmpty() && proc.open(m_exepath +
" status all"))
149 TQTextStream t(&proc);
151 switch (LprSettings::self()->mode())
154 case LprSettings::LPR:
157 case LprSettings::LPRng:
158 parsetStatusLPRng(t);
166 bool LpcHelper::enable(KMPrinter *prt,
bool state, TQString& msg)
168 int st = m_state[prt->printerName()] & KMPrinter::StateMask;
169 if (changeState(prt->printerName(), (state ?
"enable" :
"disable"), msg))
171 m_state[prt->printerName()] = KMPrinter::PrinterState((state ? KMPrinter::Rejecting : 0) | st);
177 bool LpcHelper::start(KMPrinter *prt,
bool state, TQString& msg)
179 int rej = m_state[prt->printerName()] & ~KMPrinter::StateMask;
180 if (changeState(prt->printerName(), (state ?
"start" :
"stop"), msg))
182 m_state[prt->printerName()] = KMPrinter::PrinterState((state ? KMPrinter::Idle : KMPrinter::Stopped) | rej);
193 int LpcHelper::parseStateChangeLPR(
const TQString& result,
const TQString& printer)
195 if (result.startsWith(printer +
":"))
197 else if (result.startsWith(
"?Privileged"))
199 else if (result.startsWith(
"unknown"))
205 static TQString lprngAnswer(
const TQString& result,
const TQString& printer)
209 p = result.find(
"\n" + printer);
212 q = result.find(
':', p)+2;
213 p = result.find(
'\n', q);
214 TQString answer = result.mid(q, p-q).stripWhiteSpace();
217 return TQString::null;
220 int LpcHelper::parseStateChangeLPRng(
const TQString& result,
const TQString& printer)
222 TQString answer = lprngAnswer(result, printer);
225 else if (answer ==
"disabled" || answer ==
"enabled" || answer ==
"started" || answer ==
"stopped")
231 bool LpcHelper::changeState(
const TQString& printer,
const TQString& op, TQString& msg)
233 if (m_exepath.isEmpty())
235 msg = i18n(
"The executable %1 couldn't be found in your PATH.").arg(
"lpc");
238 TQString result = execute(m_exepath +
" " + op +
" " + TDEProcess::quote(printer));
241 switch (LprSettings::self()->mode())
244 case LprSettings::LPR:
245 status = parseStateChangeLPR(result, printer);
247 case LprSettings::LPRng:
248 status = parseStateChangeLPRng(result, printer);
256 msg = i18n(
"Permission denied.");
259 msg = i18n(
"Printer %1 does not exist.").arg(printer);
263 msg = i18n(
"Unknown error: %1").arg(result.replace(TQRegExp(
"\\n"),
" "));
266 return (status == 0);
269 bool LpcHelper::removeJob(KMJob *job, TQString& msg)
271 if (m_lprmpath.isEmpty())
273 msg = i18n(
"The executable %1 couldn't be found in your PATH.").arg(
"lprm");
276 TQString result = execute(m_lprmpath +
" -P " + TDEProcess::quote(job->printer()) +
" " + TQString::number(job->id()));
277 if (result.find(
"dequeued") != -1)
279 else if (result.find(
"Permission denied") != -1 || result.find(
"no permissions") != -1)
280 msg = i18n(
"Permission denied.");
282 msg = i18n(
"Execution of lprm failed: %1").arg(result);
287 bool LpcHelper::changeJobState(KMJob *job,
int state, TQString& msg)
289 if (m_lprmpath.isEmpty())
291 msg = i18n(
"The executable %1 couldn't be found in your PATH.").arg(
"lpc");
294 TQString result = execute(m_exepath + (state == KMJob::Held ?
" hold " :
" release ") + TDEProcess::quote(job->printer()) +
" " + TQString::number(job->id()));
295 TQString answer = lprngAnswer(result, job->printer());
298 msg = i18n(
"Permission denied.");
305 bool LpcHelper::restart(TQString& msg)
308 if (m_exepath.isEmpty())
310 else if (m_checkpcpath.isEmpty())
314 msg = i18n(
"The executable %1 couldn't be found in your PATH.").arg(s);
317 ::system(TQFile::encodeName(m_exepath +
" reread"));
318 ::system(TQFile::encodeName(m_checkpcpath +
" -f"));