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

tdeprint

  • tdeprint
  • cups
  • cupsdconf2
cupsddialog.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 "cupsddialog.h"
21 
22 #include "cupsdpage.h"
23 #include "cupsdconf.h"
24 #include "cupsdsplash.h"
25 #include "cupsdserverpage.h"
26 #include "cupsdlogpage.h"
27 #include "cupsdjobspage.h"
28 #include "cupsdfilterpage.h"
29 #include "cupsddirpage.h"
30 #include "cupsdnetworkpage.h"
31 #include "cupsdbrowsingpage.h"
32 #include "cupsdsecuritypage.h"
33 
34 #include <tqdir.h>
35 #include <tqvbox.h>
36 #include <tdemessagebox.h>
37 #include <tdelocale.h>
38 #include <tqfile.h>
39 #include <tqfileinfo.h>
40 #include <tdeglobal.h>
41 #include <kiconloader.h>
42 #include <tqstringlist.h>
43 #include <tqwhatsthis.h>
44 #include <tdeio/passdlg.h>
45 #include <kguiitem.h>
46 #include <kprocess.h>
47 #include <tqprocess.h>
48 
49 #include <stdlib.h>
50 #include <signal.h>
51 #include <cups/cups.h>
52 
53 static bool dynamically_loaded = false;
54 static TQString pass_string;
55 
56 extern "C"
57 {
58 #include "cups-util.h"
59  TDEPRINT_EXPORT bool restartServer(TQString& msg)
60  {
61  return CupsdDialog::restartServer(msg);
62  }
63  TDEPRINT_EXPORT bool configureServer(TQWidget *parent, TQString& msg)
64  {
65  dynamically_loaded = true;
66  bool result = CupsdDialog::configure(TQString::null, parent, &msg);
67  dynamically_loaded = false;
68  return result;
69  }
70 }
71 
72 int getServerPid()
73 {
74 #if defined(__OpenBSD__) || defined(__FreeBSD__)
75  TQProcess *proc = new TQProcess();
76  proc->addArgument("pgrep");
77  proc->addArgument("cupsd");
78  proc->start();
79  while (proc->isRunning()); //Wait for process to exit
80  TQString pidString = proc->readLineStdout();
81  bool ok;
82  int pid = pidString.toInt(&ok);
83  if (ok) return pid;
84  return (-1);
85 #else
86  TQDir dir("/proc",TQString::null,TQDir::Name,TQDir::Dirs);
87  for (uint i=0;i<dir.count();i++)
88  {
89  if (dir[i] == "." || dir[i] == ".." || dir[i] == "self") continue;
90  TQFile f("/proc/" + dir[i] + "/cmdline");
91  if (f.exists() && f.open(IO_ReadOnly))
92  {
93  TQTextStream t(&f);
94  TQString line;
95  t >> line;
96  f.close();
97  if (line.right(5) == "cupsd" ||
98  line.right(6).left(5) == "cupsd") // second condition for 2.4.x kernels
99  // which add a null byte at the end
100  return dir[i].toInt();
101  }
102  }
103  return (-1);
104 #endif
105 }
106 
107 const char* getPassword(const char*)
108 {
109  TQString user(cupsUser());
110  TQString pass;
111 
112  if (TDEIO::PasswordDialog::getNameAndPassword(user, pass, NULL) == TQDialog::Accepted)
113  {
114  cupsSetUser(user.latin1());
115  pass_string = pass;
116  if (pass_string.isEmpty())
117  return "";
118  else
119  return pass_string.latin1();
120  }
121  else
122  return NULL;
123 }
124 
125 //---------------------------------------------------
126 
127 CupsdDialog::CupsdDialog(TQWidget *parent, const char *name)
128  : KDialogBase(IconList, "", Ok|Cancel|User1, Ok, parent, name, true, true, KGuiItem(i18n("Short Help"), "help"))
129 {
130  TDEGlobal::iconLoader()->addAppDir("tdeprint");
131  TDEGlobal::locale()->insertCatalogue("cupsdconf");
132 
133  setShowIconsInTreeList(true);
134  setRootIsDecorated(false);
135 
136  pagelist_.setAutoDelete(false);
137  filename_ = "";
138  conf_ = 0;
139  constructDialog();
140 
141  setCaption(i18n("CUPS Server Configuration"));
142 
143  //resize(500, 400);
144 }
145 
146 CupsdDialog::~CupsdDialog()
147 {
148  delete conf_;
149 }
150 
151 void CupsdDialog::addConfPage(CupsdPage *page)
152 {
153  TQPixmap icon = TDEGlobal::instance()->iconLoader()->loadIcon(
154  page->pixmap(),
155  TDEIcon::NoGroup,
156  TDEIcon::SizeMedium
157  );
158 
159  TQVBox *box = addVBoxPage(page->pageLabel(), page->header(), icon);
160  page->reparent(box, TQPoint(0,0));
161  pagelist_.append(page);
162 }
163 
164 void CupsdDialog::constructDialog()
165 {
166  addConfPage(new CupsdSplash(0));
167  addConfPage(new CupsdServerPage(0));
168  addConfPage(new CupsdNetworkPage(0));
169  addConfPage(new CupsdSecurityPage(0));
170  addConfPage(new CupsdLogPage(0));
171  addConfPage(new CupsdJobsPage(0));
172  addConfPage(new CupsdFilterPage(0));
173  addConfPage(new CupsdDirPage(0));
174  addConfPage(new CupsdBrowsingPage(0));
175 
176  conf_ = new CupsdConf();
177  for (pagelist_.first();pagelist_.current();pagelist_.next())
178  {
179  pagelist_.current()->setInfos(conf_);
180  }
181 }
182 
183 bool CupsdDialog::setConfigFile(const TQString& filename)
184 {
185  filename_ = filename;
186  if (!conf_->loadFromFile(filename_))
187  {
188  KMessageBox::error(this, i18n("Error while loading configuration file!"), i18n("CUPS Configuration Error"));
189  return false;
190  }
191  if (conf_->unknown_.count() > 0)
192  {
193  // there were some unknown options, warn the user
194  TQString msg;
195  for (TQValueList< TQPair<TQString,TQString> >::ConstIterator it=conf_->unknown_.begin(); it!=conf_->unknown_.end(); ++it)
196  msg += ((*it).first + " = " + (*it).second + "<br>");
197  msg.prepend("<p>" + i18n("Some options were not recognized by this configuration tool. "
198  "They will be left untouched and you won't be able to change them.") + "</p>");
199  KMessageBox::sorry(this, msg, i18n("Unrecognized Options"));
200  }
201  bool ok(true);
202  TQString msg;
203  for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
204  ok = pagelist_.current()->loadConfig(conf_, msg);
205  if (!ok)
206  {
207  KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
208  return false;
209  }
210  return true;
211 }
212 
213 bool CupsdDialog::restartServer(TQString& msg)
214 {
215  int serverPid = getServerPid();
216  msg.truncate(0);
217  if (serverPid <= 0)
218  {
219  msg = i18n("Unable to find a running CUPS server");
220  }
221  else
222  {
223  bool success = false;
224  TDEProcess proc;
225  proc << "tdesu" << "-c" << "/etc/init.d/cupsys restart";
226  success = proc.start( TDEProcess::Block ) && proc.normalExit();
227  if( !success )
228  msg = i18n("Unable to restart CUPS server (pid = %1)").arg(serverPid);
229  }
230  return (msg.isEmpty());
231 }
232 
233 bool CupsdDialog::configure(const TQString& filename, TQWidget *parent, TQString *msg)
234 {
235  bool needUpload(false);
236  TQString errormsg;
237  bool result = true;
238 
239  // init password dialog if needed
240  if (!dynamically_loaded)
241  cupsSetPasswordCB(getPassword);
242 
243  // load config file from server
244  TQString fn(filename);
245  if (fn.isEmpty())
246  {
247  fn = cupsGetConf();
248  if (fn.isEmpty())
249  errormsg = i18n("Unable to retrieve configuration file from the CUPS server. "
250  "You probably don't have the access permissions to perform this operation.");
251  else needUpload = true;
252  }
253 
254  // check read state (only if needed)
255  if (!fn.isEmpty())
256  {
257  TQFileInfo fi(fn);
258  if (!fi.exists() || !fi.isReadable() || !fi.isWritable())
259  errormsg = i18n("Internal error: file '%1' not readable/writable!").arg(fn);
260  // check file size
261  if (fi.size() == 0)
262  errormsg = i18n("Internal error: empty file '%1'!").arg(fn);
263  }
264 
265  if (!errormsg.isEmpty())
266  {
267  if ( !dynamically_loaded )
268  KMessageBox::error(parent, errormsg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
269  result = false;
270  }
271  else
272  {
273  TDEGlobal::locale()->insertCatalogue("cupsdconf"); // Must be before dialog is created to translate "Short Help"
274  CupsdDialog dlg(parent);
275  if (dlg.setConfigFile(fn) && dlg.exec())
276  {
277  TQCString encodedFn = TQFile::encodeName(fn);
278  if (!needUpload)
279  KMessageBox::information(parent,
280  i18n("The config file has not been uploaded to the "
281  "CUPS server. The daemon will not be restarted."));
282  else if (!cupsPutConf(encodedFn.data()))
283  {
284  errormsg = i18n("Unable to upload the configuration file to CUPS server. "
285  "You probably don't have the access permissions to perform this operation.");
286  if ( !dynamically_loaded )
287  KMessageBox::error(parent, errormsg, i18n("CUPS configuration error"));
288  result = false;
289  }
290  }
291 
292  }
293  if (needUpload)
294  TQFile::remove(fn);
295 
296  if ( msg )
297  *msg = errormsg;
298  return result;
299 }
300 
301 void CupsdDialog::slotOk()
302 {
303  if (conf_ && !filename_.isEmpty())
304  { // try to save the file
305  bool ok(true);
306  TQString msg;
307  CupsdConf newconf_;
308  for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
309  ok = pagelist_.current()->saveConfig(&newconf_, msg);
310  // copy unknown options
311  newconf_.unknown_ = conf_->unknown_;
312  if (!ok)
313  {
314  ; // do nothing
315  }
316  else if (!newconf_.saveToFile(filename_))
317  {
318  msg = i18n("Unable to write configuration file %1").arg(filename_);
319  ok = false;
320  }
321  if (!ok)
322  {
323  KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
324  }
325  else
326  KDialogBase::slotOk();
327  }
328 }
329 
330 void CupsdDialog::slotUser1()
331 {
332  TQWhatsThis::enterWhatsThisMode();
333 }
334 
335 int CupsdDialog::serverPid()
336 {
337  return getServerPid();
338 }
339 
340 int CupsdDialog::serverOwner()
341 {
342  int pid = getServerPid();
343  if (pid > 0)
344  {
345  TQString str;
346  str.sprintf("/proc/%d/status",pid);
347  TQFile f(str);
348  if (f.exists() && f.open(IO_ReadOnly))
349  {
350  TQTextStream t(&f);
351  while (!t.eof())
352  {
353  str = t.readLine();
354  if (str.find("Uid:",0,false) == 0)
355  {
356  TQStringList list = TQStringList::split('\t', str, false);
357  if (list.count() >= 2)
358  {
359  bool ok;
360  int u = list[1].toInt(&ok);
361  if (ok) return u;
362  }
363  }
364  }
365  }
366  }
367  return (-1);
368 }
369 
370 #include "cupsddialog.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.