20 #include "tdeprintd.h"
21 #include "kprintprocess.h"
24 #include <tdelocale.h>
25 #include <knotifyclient.h>
26 #include <tdemessagebox.h>
28 #include <dcopclient.h>
29 #include <tdeio/passdlg.h>
30 #include <tdeio/authinfo.h>
32 #include <kpushbutton.h>
33 #include <kiconloader.h>
34 #include <kstandarddirs.h>
36 #include <tdeapplication.h>
45 TDE_EXPORT KDEDModule *create_tdeprintd(
const TQCString& name)
47 return new KDEPrintd(name);
51 class StatusWindow :
public TQWidget
54 StatusWindow(
int pid = -1);
55 void setMessage(
const TQString&);
56 int pid()
const {
return m_pid; }
60 TQPushButton *m_button;
65 StatusWindow::StatusWindow(
int pid)
66 : TQWidget(NULL,
"StatusWindow", (WFlags)(WType_TopLevel|WStyle_DialogBorder|WStyle_StaysOnTop|WDestructiveClose)), m_pid(pid)
68 m_label =
new TQLabel(
this);
69 m_label->setAlignment(AlignCenter);
70 m_button =
new KPushButton(KStdGuiItem::close(),
this);
71 m_icon =
new TQLabel(
this);
72 m_icon->setPixmap(DesktopIcon(
"document-print"));
73 m_icon->setAlignment(AlignCenter);
74 KWin::setIcons(winId(), *(m_icon->pixmap()), SmallIcon(
"document-print"));
75 TQGridLayout *l0 =
new TQGridLayout(
this, 2, 3, 10, 10);
76 l0->setRowStretch(0, 1);
77 l0->setColStretch(1, 1);
78 l0->addMultiCellWidget(m_label, 0, 0, 1, 2);
79 l0->addWidget(m_button, 1, 2);
80 l0->addMultiCellWidget(m_icon, 0, 1, 0, 0);
81 connect(m_button, TQ_SIGNAL(clicked()), TQ_SLOT(hide()));
85 void StatusWindow::setMessage(
const TQString& msg)
88 m_label->setText(msg);
100 KDEPrintd::KDEPrintd(
const TQCString& obj)
103 m_processpool.setAutoDelete(
true);
104 m_windows.setAutoDelete(
false);
105 m_requestsPending.setAutoDelete(
true );
108 KDEPrintd::~KDEPrintd()
112 int KDEPrintd::print(
const TQString& cmd,
const TQStringList& files,
bool remflag)
114 KPrintProcess *proc =
new KPrintProcess;
115 TQString command(cmd);
116 TQRegExp re(
"\\$out\\{([^}]*)\\}" );
118 connect(proc,TQ_SIGNAL(printTerminated(KPrintProcess*)),TQ_SLOT(slotPrintTerminated(KPrintProcess*)));
119 connect(proc,TQ_SIGNAL(printError(KPrintProcess*,
const TQString&)),TQ_SLOT(slotPrintError(KPrintProcess*,
const TQString&)));
120 proc->setCommand( command );
121 if ( re.search( command ) != -1 )
123 KURL url( re.cap( 1 ) );
124 if ( !url.isLocalFile() )
126 TQString tmpFilename = locateLocal(
"tmp",
"tdeprint_" + kapp->randomString( 8 ) );
127 command.replace( re, TDEProcess::quote( tmpFilename ) );
128 proc->setOutput( re.cap( 1 ) );
129 proc->setTempOutput( tmpFilename );
132 command.replace( re, TDEProcess::quote( re.cap( 1 ) ) );
135 if ( checkFiles( command, files ) )
139 proc->setTempFiles( files );
142 m_processpool.append( proc );
143 return (
int )proc->pid();
151 void KDEPrintd::slotPrintTerminated( KPrintProcess *proc )
153 m_processpool.removeRef( proc );
156 void KDEPrintd::slotPrintError( KPrintProcess *proc,
const TQString& msg )
158 KNotifyClient::event(
"printerror",i18n(
"<p><nobr>A print error occurred. Error message received from system:</nobr></p><br>%1").arg(msg));
159 m_processpool.removeRef( proc );
162 TQString KDEPrintd::openPassDlg(
const TQString& user)
164 TQString user_(user), pass_, result;
165 if (TDEIO::PasswordDialog::getNameAndPassword(user_, pass_, NULL) == KDialog::Accepted)
166 result.append(user_).append(
":").append(pass_);
170 bool KDEPrintd::checkFiles(TQString& cmd,
const TQStringList& files)
172 for (TQStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
173 if (::access(TQFile::encodeName(*it).data(), R_OK) != 0)
175 if (KMessageBox::warningContinueCancel(0,
176 i18n(
"Some of the files to print are not readable by the TDE "
177 "print daemon. This may happen if you are trying to print "
178 "as a different user to the one currently logged in. To continue "
179 "printing, you need to provide root's password."),
181 i18n(
"Provide root's Password"),
182 "provideRootsPassword") == KMessageBox::Continue)
184 cmd = (
"tdesu -c " + TDEProcess::quote(cmd));
193 void KDEPrintd::statusMessage(
const TQString& msg,
int pid,
const TQString& appName)
195 StatusWindow *w = m_windows.find(pid);
196 if (!w && !msg.isEmpty())
198 w =
new StatusWindow(pid);
199 if (appName.isEmpty())
200 w->setCaption(i18n(
"Printing Status - %1").arg(
"(pid="+TQString::number(pid)+
")"));
202 w->setCaption(i18n(
"Printing Status - %1").arg(appName));
203 connect(w, TQ_SIGNAL(destroyed()), TQ_SLOT(slotClosed()));
205 m_windows.insert(pid, w);
216 void KDEPrintd::slotClosed()
218 const StatusWindow *w =
static_cast<const StatusWindow*
>(sender());
221 m_windows.remove(w->pid());
227 class KDEPrintd::Request
230 DCOPClientTransaction *transaction;
236 TQString KDEPrintd::requestPassword(
const TQString& user,
const TQString& host,
int port,
int seqNbr )
238 Request *req =
new Request;
240 req->uri =
"print://" + user +
"@" + host +
":" + TQString::number(port);
241 req->seqNbr = seqNbr;
242 req->transaction = callingDcopClient()->beginTransaction();
243 m_requestsPending.append( req );
244 if ( m_requestsPending.count() == 1 )
245 TQTimer::singleShot( 0,
this, TQ_SLOT( processRequest() ) );
249 void KDEPrintd::processRequest()
251 if ( m_requestsPending.count() == 0 )
254 Request *req = m_requestsPending.first();
255 TDEIO::AuthInfo info;
256 TQByteArray params, reply;
258 TQString authString(
"::" );
260 info.username = req->user;
261 info.keepPassword =
true;
263 info.comment = i18n(
"Printing system" );
265 TQDataStream input( params, IO_WriteOnly );
266 input << info << TQString(i18n(
"Authentication failed (user name=%1)" ).arg( info.username )) << 0L << (
long int) req->seqNbr;
267 if ( callingDcopClient()->call(
"kded",
"kpasswdserver",
"queryAuthInfo(TDEIO::AuthInfo,TQString,long int,long int)",
268 params, replyType, reply ) )
270 if ( replyType ==
"TDEIO::AuthInfo" )
272 TQDataStream output( reply, IO_ReadOnly );
273 TDEIO::AuthInfo result;
275 output >> result >> seqNbr;
277 if ( result.isModified() )
278 authString = result.username +
":" + result.password +
":" + TQString::number( seqNbr );
281 kdWarning( 500 ) <<
"DCOP returned type error, expected TDEIO::AuthInfo, received " << replyType << endl;
284 kdWarning( 500 ) <<
"Cannot communicate with kded_kpasswdserver" << endl;
286 TQByteArray outputData;
287 TQDataStream output( outputData, IO_WriteOnly );
288 output << authString;
289 replyType =
"TQString";
290 callingDcopClient()->endTransaction( req->transaction, replyType, outputData );
292 m_requestsPending.remove( (
unsigned int )0 );
293 if ( m_requestsPending.count() > 0 )
294 TQTimer::singleShot( 0,
this, TQ_SLOT( processRequest() ) );
297 void KDEPrintd::initPassword(
const TQString& user,
const TQString& passwd,
const TQString& host,
int port )
299 TQByteArray params, reply;
301 TDEIO::AuthInfo info;
303 info.username = user;
304 info.password = passwd;
305 info.url =
"print://" + user +
"@" + host +
":" + TQString::number(port);
307 TQDataStream input( params, IO_WriteOnly );
308 input << info << (
long int )0;
310 if ( !callingDcopClient()->call(
"kded",
"kpasswdserver",
"addAuthInfo(TDEIO::AuthInfo,long int)",
311 params, replyType, reply ) )
312 kdWarning( 500 ) <<
"Unable to initialize password, cannot communicate with kded_kpasswdserver" << endl;
315 #include "tdeprintd.moc"