2
* This file is part of the KDE libraries
3
* Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
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.
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.
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.
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 <qfileinfo.h>
41
#include <kiconloader.h>
42
#include <qstringlist.h>
43
#include <qwhatsthis.h>
44
#include <kio/passdlg.h>
49
#include <cups/cups.h>
51
static bool dynamically_loaded = false;
52
static QString pass_string;
56
#include "cups-util.h"
57
KDEPRINT_EXPORT bool restartServer(QString& msg)
59
return CupsdDialog::restartServer(msg);
61
KDEPRINT_EXPORT bool configureServer(QWidget *parent, QString& msg)
63
dynamically_loaded = true;
64
bool result = CupsdDialog::configure(QString::null, parent, &msg);
65
dynamically_loaded = false;
72
QDir dir("/proc",QString::null,QDir::Name,QDir::Dirs);
73
for (uint i=0;i<dir.count();i++)
75
if (dir[i] == "." || dir[i] == ".." || dir[i] == "self") continue;
76
QFile f("/proc/" + dir[i] + "/cmdline");
77
if (f.exists() && f.open(IO_ReadOnly))
83
if (line.right(5) == "cupsd" ||
84
line.right(6).left(5) == "cupsd") // second condition for 2.4.x kernels
85
// which add a null byte at the end
86
return dir[i].toInt();
92
const char* getPassword(const char*)
94
QString user(cupsUser());
97
if (KIO::PasswordDialog::getNameAndPassword(user, pass, NULL) == QDialog::Accepted)
99
cupsSetUser(user.latin1());
101
if (pass_string.isEmpty())
104
return pass_string.latin1();
110
//---------------------------------------------------
112
CupsdDialog::CupsdDialog(QWidget *parent, const char *name)
113
: KDialogBase(IconList, "", Ok|Cancel|User1, Ok, parent, name, true, true, KGuiItem(i18n("Short Help"), "help"))
115
KGlobal::iconLoader()->addAppDir("kdeprint");
116
KGlobal::locale()->insertCatalogue("cupsdconf");
118
setShowIconsInTreeList(true);
119
setRootIsDecorated(false);
121
pagelist_.setAutoDelete(false);
126
setCaption(i18n("CUPS Server Configuration"));
131
CupsdDialog::~CupsdDialog()
136
void CupsdDialog::addConfPage(CupsdPage *page)
138
QPixmap icon = KGlobal::instance()->iconLoader()->loadIcon(
144
QVBox *box = addVBoxPage(page->pageLabel(), page->header(), icon);
145
page->reparent(box, QPoint(0,0));
146
pagelist_.append(page);
149
void CupsdDialog::constructDialog()
151
addConfPage(new CupsdSplash(0));
152
addConfPage(new CupsdServerPage(0));
153
addConfPage(new CupsdNetworkPage(0));
154
addConfPage(new CupsdSecurityPage(0));
155
addConfPage(new CupsdLogPage(0));
156
addConfPage(new CupsdJobsPage(0));
157
addConfPage(new CupsdFilterPage(0));
158
addConfPage(new CupsdDirPage(0));
159
addConfPage(new CupsdBrowsingPage(0));
161
conf_ = new CupsdConf();
162
for (pagelist_.first();pagelist_.current();pagelist_.next())
164
pagelist_.current()->setInfos(conf_);
168
bool CupsdDialog::setConfigFile(const QString& filename)
170
filename_ = filename;
171
if (!conf_->loadFromFile(filename_))
173
KMessageBox::error(this, i18n("Error while loading configuration file!"), i18n("CUPS Configuration Error"));
176
if (conf_->unknown_.count() > 0)
178
// there were some unknown options, warn the user
180
for (QValueList< QPair<QString,QString> >::ConstIterator it=conf_->unknown_.begin(); it!=conf_->unknown_.end(); ++it)
181
msg += ((*it).first + " = " + (*it).second + "<br>");
182
msg.prepend("<p>" + i18n("Some options were not recognized by this configuration tool. "
183
"They will be left untouched and you won't be able to change them.") + "</p>");
184
KMessageBox::sorry(this, msg, i18n("Unrecognized Options"));
188
for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
189
ok = pagelist_.current()->loadConfig(conf_, msg);
192
KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
198
bool CupsdDialog::restartServer(QString& msg)
200
int serverPid = getServerPid();
204
msg = i18n("Unable to find a running CUPS server");
208
bool success = false;
210
success = (::kill(serverPid, SIGHUP) == 0);
214
proc << "kdesu" << "-c" << "kill -SIGHUP " + QString::number( serverPid );
215
success = proc.start( KProcess::Block ) && proc.normalExit();
218
msg = i18n("Unable to restart CUPS server (pid = %1)").arg(serverPid);
220
return (msg.isEmpty());
223
bool CupsdDialog::configure(const QString& filename, QWidget *parent, QString *msg)
225
bool needUpload(false);
229
// init password dialog if needed
230
if (!dynamically_loaded)
231
cupsSetPasswordCB(getPassword);
233
// load config file from server
234
QString fn(filename);
239
errormsg = i18n("Unable to retrieve configuration file from the CUPS server. "
240
"You probably don't have the access permissions to perform this operation.");
241
else needUpload = true;
244
// check read state (only if needed)
248
if (!fi.exists() || !fi.isReadable() || !fi.isWritable())
249
errormsg = i18n("Internal error: file '%1' not readable/writable!").arg(fn);
252
errormsg = i18n("Internal error: empty file '%1'!").arg(fn);
255
if (!errormsg.isEmpty())
257
if ( !dynamically_loaded )
258
KMessageBox::error(parent, errormsg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
263
KGlobal::locale()->insertCatalogue("cupsdconf"); // Must be before dialog is created to translate "Short Help"
264
CupsdDialog dlg(parent);
265
if (dlg.setConfigFile(fn) && dlg.exec())
267
QCString encodedFn = QFile::encodeName(fn);
269
KMessageBox::information(parent,
270
i18n("The config file has not been uploaded to the "
271
"CUPS server. The daemon will not be restarted."));
272
else if (!cupsPutConf(encodedFn.data()))
274
errormsg = i18n("Unable to upload the configuration file to CUPS server. "
275
"You probably don't have the access permissions to perform this operation.");
276
if ( !dynamically_loaded )
277
KMessageBox::error(parent, errormsg, i18n("CUPS configuration error"));
291
void CupsdDialog::slotOk()
293
if (conf_ && !filename_.isEmpty())
294
{ // try to save the file
298
for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
299
ok = pagelist_.current()->saveConfig(&newconf_, msg);
300
// copy unknown options
301
newconf_.unknown_ = conf_->unknown_;
306
else if (!newconf_.saveToFile(filename_))
308
msg = i18n("Unable to write configuration file %1").arg(filename_);
313
KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
316
KDialogBase::slotOk();
320
void CupsdDialog::slotUser1()
322
QWhatsThis::enterWhatsThisMode();
325
int CupsdDialog::serverPid()
327
return getServerPid();
330
int CupsdDialog::serverOwner()
332
int pid = getServerPid();
336
str.sprintf("/proc/%d/status",pid);
338
if (f.exists() && f.open(IO_ReadOnly))
344
if (str.find("Uid:",0,false) == 0)
346
QStringList list = QStringList::split('\t', str, false);
347
if (list.count() >= 2)
350
int u = list[1].toInt(&ok);
360
#include "cupsddialog.moc"