• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
tdeprintd.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "tdeprintd.h"
21 #include "kprintprocess.h"
22 
23 #include <tqfile.h>
24 #include <tdelocale.h>
25 #include <knotifyclient.h>
26 #include <tdemessagebox.h>
27 #include <kdebug.h>
28 #include <dcopclient.h>
29 #include <tdeio/passdlg.h>
30 #include <tdeio/authinfo.h>
31 #include <tqlabel.h>
32 #include <kpushbutton.h>
33 #include <kiconloader.h>
34 #include <kstandarddirs.h>
35 #include <twin.h>
36 #include <tdeapplication.h>
37 #include <tqlayout.h>
38 #include <tqtimer.h>
39 #include <tqregexp.h>
40 
41 #include <unistd.h>
42 
43 extern "C"
44 {
45  KDE_EXPORT KDEDModule *create_tdeprintd(const TQCString& name)
46  {
47  return new KDEPrintd(name);
48  }
49 }
50 
51 class StatusWindow : public TQWidget
52 {
53 public:
54  StatusWindow(int pid = -1);
55  void setMessage(const TQString&);
56  int pid() const { return m_pid; }
57 
58 private:
59  TQLabel *m_label;
60  TQPushButton *m_button;
61  int m_pid;
62  TQLabel *m_icon;
63 };
64 
65 StatusWindow::StatusWindow(int pid)
66 : TQWidget(NULL, "StatusWindow", (WFlags)(WType_TopLevel|WStyle_DialogBorder|WStyle_StaysOnTop|WDestructiveClose)), m_pid(pid)
67 {
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, TQT_SIGNAL(clicked()), TQT_SLOT(hide()));
82  resize(200, 50);
83 }
84 
85 void StatusWindow::setMessage(const TQString& msg)
86 {
87  //QSize oldSz = size();
88  m_label->setText(msg);
89  //QSize sz = m_label->sizeHint();
90  //sz += TQSize(layout()->margin()*2, layout()->margin()*2+layout()->spacing()+m_button->sizeHint().height());
91  // dialog will never be smaller
92  //sz = sz.expandedTo(oldSz);
93  //resize(sz);
94  //setFixedSize(sz);
95  //layout()->activate();
96 }
97 
98 //*****************************************************************************************************
99 
100 KDEPrintd::KDEPrintd(const TQCString& obj)
101 : KDEDModule(obj)
102 {
103  m_processpool.setAutoDelete(true);
104  m_windows.setAutoDelete(false);
105  m_requestsPending.setAutoDelete( true );
106 }
107 
108 KDEPrintd::~KDEPrintd()
109 {
110 }
111 
112 int KDEPrintd::print(const TQString& cmd, const TQStringList& files, bool remflag)
113 {
114  KPrintProcess *proc = new KPrintProcess;
115  TQString command(cmd);
116  TQRegExp re( "\\$out\\{([^}]*)\\}" );
117 
118  connect(proc,TQT_SIGNAL(printTerminated(KPrintProcess*)),TQT_SLOT(slotPrintTerminated(KPrintProcess*)));
119  connect(proc,TQT_SIGNAL(printError(KPrintProcess*,const TQString&)),TQT_SLOT(slotPrintError(KPrintProcess*,const TQString&)));
120  proc->setCommand( command );
121  if ( re.search( command ) != -1 )
122  {
123  KURL url( re.cap( 1 ) );
124  if ( !url.isLocalFile() )
125  {
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 );
130  }
131  else
132  command.replace( re, TDEProcess::quote( re.cap( 1 ) ) );
133  }
134 
135  if ( checkFiles( command, files ) )
136  {
137  *proc << command;
138  if ( remflag )
139  proc->setTempFiles( files );
140  if ( proc->print() )
141  {
142  m_processpool.append( proc );
143  return ( int )proc->pid();
144  }
145  }
146 
147  delete proc;
148  return -1;
149 }
150 
151 void KDEPrintd::slotPrintTerminated( KPrintProcess *proc )
152 {
153  m_processpool.removeRef( proc );
154 }
155 
156 void KDEPrintd::slotPrintError( KPrintProcess *proc, const TQString& msg )
157 {
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 );
160 }
161 
162 TQString KDEPrintd::openPassDlg(const TQString& user)
163 {
164  TQString user_(user), pass_, result;
165  if (TDEIO::PasswordDialog::getNameAndPassword(user_, pass_, NULL) == KDialog::Accepted)
166  result.append(user_).append(":").append(pass_);
167  return result;
168 }
169 
170 bool KDEPrintd::checkFiles(TQString& cmd, const TQStringList& files)
171 {
172  for (TQStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
173  if (::access(TQFile::encodeName(*it).data(), R_OK) != 0)
174  {
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."),
180  TQString::null,
181  i18n("Provide root's Password"),
182  "provideRootsPassword") == KMessageBox::Continue)
183  {
184  cmd = ("tdesu -c " + TDEProcess::quote(cmd));
185  break;
186  }
187  else
188  return false;
189  }
190  return true;
191 }
192 
193 void KDEPrintd::statusMessage(const TQString& msg, int pid, const TQString& appName)
194 {
195  StatusWindow *w = m_windows.find(pid);
196  if (!w && !msg.isEmpty())
197  {
198  w = new StatusWindow(pid);
199  if (appName.isEmpty())
200  w->setCaption(i18n("Printing Status - %1").arg("(pid="+TQString::number(pid)+")"));
201  else
202  w->setCaption(i18n("Printing Status - %1").arg(appName));
203  connect(w, TQT_SIGNAL(destroyed()), TQT_SLOT(slotClosed()));
204  w->show();
205  m_windows.insert(pid, w);
206  }
207  if (w)
208  {
209  if (!msg.isEmpty())
210  w->setMessage(msg);
211  else
212  w->close();
213  }
214 }
215 
216 void KDEPrintd::slotClosed()
217 {
218  const StatusWindow *w = static_cast<const StatusWindow*>(sender());
219  if (w)
220  {
221  m_windows.remove(w->pid());
222  }
223 }
224 
225 //******************************************************************************************
226 
227 class KDEPrintd::Request
228 {
229 public:
230  DCOPClientTransaction *transaction;
231  TQString user;
232  TQString uri;
233  int seqNbr;
234 };
235 
236 TQString KDEPrintd::requestPassword( const TQString& user, const TQString& host, int port, int seqNbr )
237 {
238  Request *req = new Request;
239  req->user = user;
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, TQT_SLOT( processRequest() ) );
246  return "::";
247 }
248 
249 void KDEPrintd::processRequest()
250 {
251  if ( m_requestsPending.count() == 0 )
252  return;
253 
254  Request *req = m_requestsPending.first();
255  TDEIO::AuthInfo info;
256  TQByteArray params, reply;
257  TQCString replyType;
258  TQString authString( "::" );
259 
260  info.username = req->user;
261  info.keepPassword = true;
262  info.url = req->uri;
263  info.comment = i18n( "Printing system" );
264 
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 ) )
269  {
270  if ( replyType == "TDEIO::AuthInfo" )
271  {
272  TQDataStream output( reply, IO_ReadOnly );
273  TDEIO::AuthInfo result;
274  int seqNbr;
275  output >> result >> seqNbr;
276 
277  if ( result.isModified() )
278  authString = result.username + ":" + result.password + ":" + TQString::number( seqNbr );
279  }
280  else
281  kdWarning( 500 ) << "DCOP returned type error, expected TDEIO::AuthInfo, received " << replyType << endl;
282  }
283  else
284  kdWarning( 500 ) << "Cannot communicate with kded_kpasswdserver" << endl;
285 
286  TQByteArray outputData;
287  TQDataStream output( outputData, IO_WriteOnly );
288  output << authString;
289  replyType = "TQString";
290  callingDcopClient()->endTransaction( req->transaction, replyType, outputData );
291 
292  m_requestsPending.remove( ( unsigned int )0 );
293  if ( m_requestsPending.count() > 0 )
294  TQTimer::singleShot( 0, this, TQT_SLOT( processRequest() ) );
295 }
296 
297 void KDEPrintd::initPassword( const TQString& user, const TQString& passwd, const TQString& host, int port )
298 {
299  TQByteArray params, reply;
300  TQCString replyType;
301  TDEIO::AuthInfo info;
302 
303  info.username = user;
304  info.password = passwd;
305  info.url = "print://" + user + "@" + host + ":" + TQString::number(port);
306 
307  TQDataStream input( params, IO_WriteOnly );
308  input << info << ( long int )0;
309 
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;
313 }
314 
315 #include "tdeprintd.moc"

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.8.8
This website is maintained by Timothy Pearson.