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

kdeprint

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

kdeprint

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

kdeprint

Skip menu "kdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeprint by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |