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 <kmessagebox.h>
39 #include <tqfileinfo.h>
41 #include <kiconloader.h>
42 #include <tqstringlist.h>
43 #include <tqwhatsthis.h>
44 #include <kio/passdlg.h>
50 #include <cups/cups.h>
52 static bool dynamically_loaded =
false;
53 static TQString pass_string;
57 #include "cups-util.h"
58 KDEPRINT_EXPORT
bool restartServer(TQString& msg)
60 return CupsdDialog::restartServer(msg);
62 KDEPRINT_EXPORT
bool configureServer(TQWidget *parent, TQString& msg)
64 dynamically_loaded =
true;
65 bool result = CupsdDialog::configure(TQString::null, parent, &msg);
66 dynamically_loaded =
false;
73 TQDir dir(
"/proc",TQString::null,TQDir::Name,TQDir::Dirs);
74 for (uint i=0;i<dir.count();i++)
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))
84 if (line.right(5) ==
"cupsd" ||
85 line.right(6).left(5) ==
"cupsd")
87 return dir[i].toInt();
93 const char* getPassword(
const char*)
95 TQString user(cupsUser());
98 if (KIO::PasswordDialog::getNameAndPassword(user, pass, NULL) == TQDialog::Accepted)
100 cupsSetUser(user.latin1());
102 if (pass_string.isEmpty())
105 return pass_string.latin1();
113 CupsdDialog::CupsdDialog(TQWidget *parent,
const char *name)
114 : KDialogBase(IconList,
"", Ok|Cancel|User1, Ok, parent, name, true, true, KGuiItem(i18n(
"Short Help"),
"help"))
116 KGlobal::iconLoader()->addAppDir(
"kdeprint");
117 KGlobal::locale()->insertCatalogue(
"cupsdconf");
119 setShowIconsInTreeList(
true);
120 setRootIsDecorated(
false);
122 pagelist_.setAutoDelete(
false);
127 setCaption(i18n(
"CUPS Server Configuration"));
132 CupsdDialog::~CupsdDialog()
137 void CupsdDialog::addConfPage(CupsdPage *page)
139 TQPixmap icon = KGlobal::instance()->iconLoader()->loadIcon(
145 TQVBox *box = addVBoxPage(page->pageLabel(), page->header(), icon);
146 page->reparent(box, TQPoint(0,0));
147 pagelist_.append(page);
150 void CupsdDialog::constructDialog()
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));
162 conf_ =
new CupsdConf();
163 for (pagelist_.first();pagelist_.current();pagelist_.next())
165 pagelist_.current()->setInfos(conf_);
169 bool CupsdDialog::setConfigFile(
const TQString& filename)
171 filename_ = filename;
172 if (!conf_->loadFromFile(filename_))
174 KMessageBox::error(
this, i18n(
"Error while loading configuration file!"), i18n(
"CUPS Configuration Error"));
177 if (conf_->unknown_.count() > 0)
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"));
189 for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
190 ok = pagelist_.current()->loadConfig(conf_, msg);
193 KMessageBox::error(
this, msg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
199 bool CupsdDialog::restartServer(TQString& msg)
201 int serverPid = getServerPid();
205 msg = i18n(
"Unable to find a running CUPS server");
209 bool success =
false;
211 proc <<
"kdesu" <<
"-c" <<
"/etc/init.d/cupsys restart";
212 success = proc.start( KProcess::Block ) && proc.normalExit();
214 msg = i18n(
"Unable to restart CUPS server (pid = %1)").arg(serverPid);
216 return (msg.isEmpty());
219 bool CupsdDialog::configure(
const TQString& filename, TQWidget *parent, TQString *msg)
221 bool needUpload(
false);
226 if (!dynamically_loaded)
227 cupsSetPasswordCB(getPassword);
230 TQString fn(filename);
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;
244 if (!fi.exists() || !fi.isReadable() || !fi.isWritable())
245 errormsg = i18n(
"Internal error: file '%1' not readable/writable!").arg(fn);
248 errormsg = i18n(
"Internal error: empty file '%1'!").arg(fn);
251 if (!errormsg.isEmpty())
253 if ( !dynamically_loaded )
254 KMessageBox::error(parent, errormsg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
259 KGlobal::locale()->insertCatalogue(
"cupsdconf");
260 CupsdDialog dlg(parent);
261 if (dlg.setConfigFile(fn) && dlg.exec())
263 TQCString encodedFn = TQFile::encodeName(fn);
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()))
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"));
287 void CupsdDialog::slotOk()
289 if (conf_ && !filename_.isEmpty())
294 for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
295 ok = pagelist_.current()->saveConfig(&newconf_, msg);
297 newconf_.unknown_ = conf_->unknown_;
302 else if (!newconf_.saveToFile(filename_))
304 msg = i18n(
"Unable to write configuration file %1").arg(filename_);
309 KMessageBox::error(
this, msg.prepend(
"<qt>").append(
"</qt>"), i18n(
"CUPS Configuration Error"));
312 KDialogBase::slotOk();
316 void CupsdDialog::slotUser1()
318 TQWhatsThis::enterWhatsThisMode();
321 int CupsdDialog::serverPid()
323 return getServerPid();
326 int CupsdDialog::serverOwner()
328 int pid = getServerPid();
332 str.sprintf(
"/proc/%d/status",pid);
334 if (f.exists() && f.open(IO_ReadOnly))
340 if (str.find(
"Uid:",0,
false) == 0)
342 TQStringList list = TQStringList::split(
'\t', str,
false);
343 if (list.count() >= 2)
346 int u = list[1].toInt(&ok);
356 #include "cupsddialog.moc"