20 #include "cupsddialog.h"
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"
36 #include <tdemessagebox.h>
37 #include <tdelocale.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>
46 #include <tdeprocess.h>
47 #include <tqprocess.h>
51 #include <cups/cups.h>
53 static bool dynamically_loaded =
false;
54 static TQString pass_string;
58 #include "cups-util.h"
59 TDEPRINT_EXPORT
bool restartServer(TQString& msg)
61 return CupsdDialog::restartServer(msg);
63 TDEPRINT_EXPORT
bool configureServer(TQWidget *parent, TQString& msg)
65 dynamically_loaded =
true;
66 bool result = CupsdDialog::configure(TQString::null, parent, &msg);
67 dynamically_loaded =
false;
74 #if defined(__OpenBSD__) || defined(__FreeBSD__)
75 TQProcess *proc =
new TQProcess();
76 proc->addArgument(
"pgrep");
77 proc->addArgument(
"cupsd");
79 while (proc->isRunning());
80 TQString pidString = proc->readLineStdout();
82 int pid = pidString.toInt(&ok);
86 TQDir dir(
"/proc",TQString::null,TQDir::Name,TQDir::Dirs);
87 for (uint i=0;i<dir.count();i++)
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))
97 if (line.right(5) ==
"cupsd" ||
98 line.right(6).left(5) ==
"cupsd")
100 return dir[i].toInt();
107 const char* getPassword(
const char*)
109 TQString user(cupsUser());
112 if (TDEIO::PasswordDialog::getNameAndPassword(user, pass, NULL) == TQDialog::Accepted)
114 cupsSetUser(user.latin1());
116 if (pass_string.isEmpty())
119 return pass_string.latin1();
127 CupsdDialog::CupsdDialog(TQWidget *parent,
const char *name)
128 : KDialogBase(IconList,
"", Ok|Cancel|User1, Ok, parent, name, true, true, KGuiItem(i18n(
"Short Help"),
"help"))
130 TDEGlobal::iconLoader()->addAppDir(
"tdeprint");
131 TDEGlobal::locale()->insertCatalogue(
"cupsdconf");
133 setShowIconsInTreeList(
true);
134 setRootIsDecorated(
false);
136 pagelist_.setAutoDelete(
false);
141 setCaption(i18n(
"CUPS Server Configuration"));
146 CupsdDialog::~CupsdDialog()
151 void CupsdDialog::addConfPage(CupsdPage *page)
153 TQPixmap icon = TDEGlobal::instance()->iconLoader()->loadIcon(
159 TQVBox *box = addVBoxPage(page->pageLabel(), page->header(), icon);
160 page->reparent(box, TQPoint(0,0));
161 pagelist_.append(page);
164 void CupsdDialog::constructDialog()
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));
176 conf_ =
new CupsdConf();
177 for (pagelist_.first();pagelist_.current();pagelist_.next())
179 pagelist_.current()->setInfos(conf_);
183 bool CupsdDialog::setConfigFile(
const TQString& filename)
185 filename_ = filename;
186 if (!conf_->loadFromFile(filename_))
188 KMessageBox::error(
this, i18n(
"Error while loading configuration file!"), i18n(
"CUPS Configuration Error"));
191 if (conf_->unknown_.count() > 0)
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"));
203 for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
204 ok = pagelist_.current()->loadConfig(conf_, msg);
207 KMessageBox::error(
this, msg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
213 bool CupsdDialog::restartServer(TQString& msg)
215 int serverPid = getServerPid();
219 msg = i18n(
"Unable to find a running CUPS server");
223 bool success =
false;
225 proc <<
"tdesu" <<
"-c" <<
"/etc/init.d/cupsys restart";
226 success = proc.start( TDEProcess::Block ) && proc.normalExit();
228 msg = i18n(
"Unable to restart CUPS server (pid = %1)").arg(serverPid);
230 return (msg.isEmpty());
233 bool CupsdDialog::configure(
const TQString& filename, TQWidget *parent, TQString *msg)
235 bool needUpload(
false);
240 if (!dynamically_loaded)
241 cupsSetPasswordCB(getPassword);
244 TQString fn(filename);
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;
258 if (!fi.exists() || !fi.isReadable() || !fi.isWritable())
259 errormsg = i18n(
"Internal error: file '%1' not readable/writable!").arg(fn);
262 errormsg = i18n(
"Internal error: empty file '%1'!").arg(fn);
265 if (!errormsg.isEmpty())
267 if ( !dynamically_loaded )
268 KMessageBox::error(parent, errormsg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
273 TDEGlobal::locale()->insertCatalogue(
"cupsdconf");
274 CupsdDialog dlg(parent);
275 if (dlg.setConfigFile(fn) && dlg.exec())
277 TQCString encodedFn = TQFile::encodeName(fn);
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()))
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"));
301 void CupsdDialog::slotOk()
303 if (conf_ && !filename_.isEmpty())
308 for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
309 ok = pagelist_.current()->saveConfig(&newconf_, msg);
311 newconf_.unknown_ = conf_->unknown_;
316 else if (!newconf_.saveToFile(filename_))
318 msg = i18n(
"Unable to write configuration file %1").arg(filename_);
323 KMessageBox::error(
this, msg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
326 KDialogBase::slotOk();
330 void CupsdDialog::slotUser1()
332 TQWhatsThis::enterWhatsThisMode();
335 int CupsdDialog::serverPid()
337 return getServerPid();
340 int CupsdDialog::serverOwner()
342 int pid = getServerPid();
346 str.sprintf(
"/proc/%d/status",pid);
348 if (f.exists() && f.open(IO_ReadOnly))
354 if (str.find(
"Uid:",0,
false) == 0)
356 TQStringList list = TQStringList::split(
'\t', str,
false);
357 if (list.count() >= 2)
360 int u = list[1].toInt(&ok);
370 #include "cupsddialog.moc"