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