~ubuntu-branches/debian/squeeze/kdelibs/squeeze

« back to all changes in this revision

Viewing changes to .pc/34_cups12_init_script.diff/kdeprint/cups/cupsdconf2/cupsddialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2010-08-07 23:20:21 UTC
  • Revision ID: james.westby@ubuntu.com-20100807232021-owvkgp5wpc076s33
Tags: 4:3.5.10.dfsg.1-5
* Change by email address to @debian.org.
* Drop common HTML docs from kdelibs-data package. Instead suggest
  kdelibs5-data which ships them (Closes: #591609). What's more, whoever
  wants to view docs, will have to install khelpcenter4 which pulls in
  kdelibs5-data anyway.
* Switch to dpkg-source format 3.0 (quilt):
  - drop simple-patchsys.mk from debian/rules;
  - add debian/patches/series file.
* Fix corruption of zip files caused by wrong encoding of umlauts in kzip
  (patch 67_kio_zip_file_encoding.diff). (Closes: #563942) Thanks to Bjoern
  Ricks for the patch.
* Support opening of KDE 4 khelpcenter in Help -> Handbook. (Closes: #525621)
  Thanks to Ben Burton for the patch.
* Do not recurse into .pc subdirectory with doxygen 
  (patch debian/patches/02_exclude_pc_from_dox.diff).
* Urgency=medium due to multiple RC bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <qdir.h>
 
35
#include <qvbox.h>
 
36
#include <kmessagebox.h>
 
37
#include <klocale.h>
 
38
#include <qfile.h>
 
39
#include <qfileinfo.h>
 
40
#include <kglobal.h>
 
41
#include <kiconloader.h>
 
42
#include <qstringlist.h>
 
43
#include <qwhatsthis.h>
 
44
#include <kio/passdlg.h>
 
45
#include <kguiitem.h>
 
46
#include <kprocess.h>
 
47
 
 
48
#include <signal.h>
 
49
#include <cups/cups.h>
 
50
 
 
51
static bool     dynamically_loaded = false;
 
52
static QString  pass_string;
 
53
 
 
54
extern "C"
 
55
{
 
56
#include "cups-util.h"
 
57
        KDEPRINT_EXPORT bool restartServer(QString& msg)
 
58
        {
 
59
                return CupsdDialog::restartServer(msg);
 
60
        }
 
61
        KDEPRINT_EXPORT bool configureServer(QWidget *parent, QString& msg)
 
62
        {
 
63
                dynamically_loaded = true;
 
64
                bool result = CupsdDialog::configure(QString::null, parent, &msg);
 
65
                dynamically_loaded = false;
 
66
                return result;
 
67
        }
 
68
}
 
69
 
 
70
int getServerPid()
 
71
{
 
72
        QDir    dir("/proc",QString::null,QDir::Name,QDir::Dirs);
 
73
        for (uint i=0;i<dir.count();i++)
 
74
        {
 
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))
 
78
                {
 
79
                        QTextStream     t(&f);
 
80
                        QString line;
 
81
                        t >> line;
 
82
                        f.close();
 
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();
 
87
                }
 
88
        }
 
89
        return (-1);
 
90
}
 
91
 
 
92
const char* getPassword(const char*)
 
93
{
 
94
        QString user(cupsUser());
 
95
        QString pass;
 
96
 
 
97
        if (KIO::PasswordDialog::getNameAndPassword(user, pass, NULL) == QDialog::Accepted)
 
98
        {
 
99
                cupsSetUser(user.latin1());
 
100
                pass_string = pass;
 
101
                if (pass_string.isEmpty())
 
102
                        return "";
 
103
                else
 
104
                        return pass_string.latin1();
 
105
        }
 
106
        else
 
107
                return NULL;
 
108
}
 
109
 
 
110
//---------------------------------------------------
 
111
 
 
112
CupsdDialog::CupsdDialog(QWidget *parent, const char *name)
 
113
        : KDialogBase(IconList, "", Ok|Cancel|User1, Ok, parent, name, true, true, KGuiItem(i18n("Short Help"), "help"))
 
114
{
 
115
        KGlobal::iconLoader()->addAppDir("kdeprint");
 
116
        KGlobal::locale()->insertCatalogue("cupsdconf");
 
117
 
 
118
        setShowIconsInTreeList(true);
 
119
        setRootIsDecorated(false);
 
120
 
 
121
        pagelist_.setAutoDelete(false);
 
122
        filename_ = "";
 
123
        conf_ = 0;
 
124
        constructDialog();
 
125
 
 
126
        setCaption(i18n("CUPS Server Configuration"));
 
127
 
 
128
        //resize(500, 400);
 
129
}
 
130
 
 
131
CupsdDialog::~CupsdDialog()
 
132
{
 
133
        delete conf_;
 
134
}
 
135
 
 
136
void CupsdDialog::addConfPage(CupsdPage *page)
 
137
{
 
138
        QPixmap icon = KGlobal::instance()->iconLoader()->loadIcon(
 
139
                                                                   page->pixmap(),
 
140
                                                                   KIcon::NoGroup,
 
141
                                                                   KIcon::SizeMedium
 
142
                                                                  );
 
143
 
 
144
        QVBox   *box = addVBoxPage(page->pageLabel(), page->header(), icon);
 
145
        page->reparent(box, QPoint(0,0));
 
146
        pagelist_.append(page);
 
147
}
 
148
 
 
149
void CupsdDialog::constructDialog()
 
150
{
 
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));
 
160
 
 
161
        conf_ = new CupsdConf();
 
162
        for (pagelist_.first();pagelist_.current();pagelist_.next())
 
163
        {
 
164
                pagelist_.current()->setInfos(conf_);
 
165
        }
 
166
}
 
167
 
 
168
bool CupsdDialog::setConfigFile(const QString& filename)
 
169
{
 
170
        filename_ = filename;
 
171
        if (!conf_->loadFromFile(filename_))
 
172
        {
 
173
                KMessageBox::error(this, i18n("Error while loading configuration file!"), i18n("CUPS Configuration Error"));
 
174
                return false;
 
175
        }
 
176
        if (conf_->unknown_.count() > 0)
 
177
        {
 
178
                // there were some unknown options, warn the user
 
179
                QString msg;
 
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"));
 
185
        }
 
186
        bool    ok(true);
 
187
        QString msg;
 
188
        for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
 
189
                ok = pagelist_.current()->loadConfig(conf_, msg);
 
190
        if (!ok)
 
191
        {
 
192
                KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
 
193
                return false;
 
194
        }
 
195
        return true;
 
196
}
 
197
 
 
198
bool CupsdDialog::restartServer(QString& msg)
 
199
{
 
200
        int     serverPid = getServerPid();
 
201
        msg.truncate(0);
 
202
        if (serverPid <= 0)
 
203
        {
 
204
                msg = i18n("Unable to find a running CUPS server");
 
205
        }
 
206
        else
 
207
        {
 
208
                bool success = false;
 
209
                if (getuid() == 0 )
 
210
                        success = (::kill(serverPid, SIGHUP) == 0);
 
211
                else
 
212
                {
 
213
                        KProcess proc;
 
214
                        proc << "kdesu" << "-c" << "kill -SIGHUP " + QString::number( serverPid );
 
215
                        success = proc.start( KProcess::Block ) && proc.normalExit();
 
216
                }
 
217
                if( !success )    
 
218
                        msg = i18n("Unable to restart CUPS server (pid = %1)").arg(serverPid);
 
219
        }
 
220
        return (msg.isEmpty());
 
221
}
 
222
 
 
223
bool CupsdDialog::configure(const QString& filename, QWidget *parent, QString *msg)
 
224
{
 
225
        bool needUpload(false);
 
226
        QString errormsg;
 
227
        bool result = true;
 
228
 
 
229
        // init password dialog if needed
 
230
        if (!dynamically_loaded)
 
231
                cupsSetPasswordCB(getPassword);
 
232
 
 
233
        // load config file from server
 
234
        QString fn(filename);
 
235
        if (fn.isEmpty())
 
236
        {
 
237
                fn = cupsGetConf();
 
238
                if (fn.isEmpty())
 
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;
 
242
        }
 
243
 
 
244
        // check read state (only if needed)
 
245
        if (!fn.isEmpty())
 
246
        {
 
247
                QFileInfo       fi(fn);
 
248
                if (!fi.exists() || !fi.isReadable() || !fi.isWritable())
 
249
                        errormsg = i18n("Internal error: file '%1' not readable/writable!").arg(fn);
 
250
                // check file size
 
251
                if (fi.size() == 0)
 
252
                        errormsg = i18n("Internal error: empty file '%1'!").arg(fn);
 
253
        }
 
254
 
 
255
        if (!errormsg.isEmpty())
 
256
        {
 
257
                if ( !dynamically_loaded )
 
258
                        KMessageBox::error(parent, errormsg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
 
259
                result = false;
 
260
        }
 
261
        else
 
262
        {
 
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())
 
266
                {
 
267
                        QCString        encodedFn = QFile::encodeName(fn);
 
268
                        if (!needUpload)
 
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()))
 
273
                        {
 
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"));
 
278
                                result = false;
 
279
                        }
 
280
                }
 
281
 
 
282
        }
 
283
        if (needUpload)
 
284
                QFile::remove(fn);
 
285
 
 
286
        if ( msg )
 
287
                *msg = errormsg;
 
288
        return result;
 
289
}
 
290
 
 
291
void CupsdDialog::slotOk()
 
292
{
 
293
        if (conf_ && !filename_.isEmpty())
 
294
        { // try to save the file
 
295
                bool    ok(true);
 
296
                QString msg;
 
297
                CupsdConf       newconf_;
 
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_;
 
302
                if (!ok)
 
303
                {
 
304
                        ; // do nothing
 
305
                }
 
306
                else if (!newconf_.saveToFile(filename_))
 
307
                {
 
308
                        msg = i18n("Unable to write configuration file %1").arg(filename_);
 
309
                                ok = false;
 
310
                }
 
311
                if (!ok)
 
312
                {
 
313
                        KMessageBox::error(this, msg.prepend("<qt>").append("</qt>"), i18n("CUPS Configuration Error"));
 
314
                }
 
315
                else
 
316
                        KDialogBase::slotOk();
 
317
        }
 
318
}
 
319
 
 
320
void CupsdDialog::slotUser1()
 
321
{
 
322
        QWhatsThis::enterWhatsThisMode();
 
323
}
 
324
 
 
325
int CupsdDialog::serverPid()
 
326
{
 
327
        return getServerPid();
 
328
}
 
329
 
 
330
int CupsdDialog::serverOwner()
 
331
{
 
332
        int     pid = getServerPid();
 
333
        if (pid > 0)
 
334
        {
 
335
                QString str;
 
336
                str.sprintf("/proc/%d/status",pid);
 
337
                QFile   f(str);
 
338
                if (f.exists() && f.open(IO_ReadOnly))
 
339
                {
 
340
                        QTextStream     t(&f);
 
341
                        while (!t.eof())
 
342
                        {
 
343
                                str = t.readLine();
 
344
                                if (str.find("Uid:",0,false) == 0)
 
345
                                {
 
346
                                        QStringList     list = QStringList::split('\t', str, false);
 
347
                                        if (list.count() >= 2)
 
348
                                        {
 
349
                                                bool    ok;
 
350
                                                int     u = list[1].toInt(&ok);
 
351
                                                if (ok) return u;
 
352
                                        }
 
353
                                }
 
354
                        }
 
355
                }
 
356
        }
 
357
        return (-1);
 
358
}
 
359
 
 
360
#include "cupsddialog.moc"