kdeprint Library API Documentation

cupsddialog.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 #include "cupsddialog.h"
00021 
00022 #include "cupsdpage.h"
00023 #include "cupsdconf.h"
00024 #include "cupsdserveridentitypage.h"
00025 #include "cupsdserverlogpage.h"
00026 #include "cupsdserverdirpage.h"
00027 #include "cupsdserverhttppage.h"
00028 #include "cupsdserverjobpage.h"
00029 #include "cupsdservermiscpage.h"
00030 #include "cupsdbrowsinggeneralpage.h"
00031 #include "cupsdbrowsingtimeoutpage.h"
00032 #include "cupsdsplash.h"
00033 #include "cupsdbrowsingmaskspage.h"
00034 #include "cupsdbrowsingconnpage.h"
00035 #include "cupsdnetworkgeneralpage.h"
00036 #include "cupsdbrowsingrelaypage.h"
00037 #include "cupsdnetworkclientspage.h"
00038 #include "cupsdserversecuritypage.h"
00039 #include "cupsdserverencryptpage.h"
00040 
00041 #include <qdir.h>
00042 #include <qvbox.h>
00043 #include <kmessagebox.h>
00044 #include <klocale.h>
00045 #include <qfile.h>
00046 #include <qfileinfo.h>
00047 #include <kglobal.h>
00048 #include <kiconloader.h>
00049 #include <qstringlist.h>
00050 
00051 #include <signal.h>
00052 
00053 extern "C"
00054 {
00055     bool restartServer(QString& msg)
00056     {
00057         return CupsdDialog::restartServer(msg);
00058     }
00059     bool configureServer(const QString& configfile, QWidget *parent)
00060     {
00061         CupsdDialog::configure(configfile,parent);
00062         return true;
00063     }
00064 }
00065 
00066 int getServerPid()
00067 {
00068     QDir    dir("/proc",QString::null,QDir::Name,QDir::Dirs);
00069     for (uint i=0;i<dir.count();i++)
00070     {
00071         if (dir[i] == "." || dir[i] == ".." || dir[i] == "self") continue;
00072         QFile   f("/proc/" + dir[i] + "/cmdline");
00073         if (f.exists() && f.open(IO_ReadOnly))
00074         {
00075             QTextStream t(&f);
00076             QString line;
00077             t >> line;
00078             f.close();
00079             if (line.right(5) == "cupsd" ||
00080                 line.right(6).left(5) == "cupsd")   // second condition for 2.4.x kernels
00081                                 // which add a null byte at the end
00082                 return dir[i].toInt();
00083         }
00084     }
00085     return (-1);
00086 }
00087 
00088 //---------------------------------------------------
00089 
00090 CupsdDialog::CupsdDialog(QWidget *parent, const char *name)
00091     : MyDialogBase(parent, name)
00092 {
00093     KGlobal::iconLoader()->addAppDir("kdeprint");
00094     KGlobal::locale()->insertCatalogue("cupsdconf");
00095 
00096     pagelist_.setAutoDelete(false);
00097     filename_ = "";
00098     conf_ = 0;
00099     constructDialog();
00100 
00101         setCaption(i18n("CUPS Server Configuration"));
00102 
00103         resize(500, 400);
00104 }
00105 
00106 CupsdDialog::~CupsdDialog()
00107 {
00108         delete conf_;
00109 }
00110 
00111 void CupsdDialog::constructDialog()
00112 {
00113     CupsdPage   *cpage = new CupsdSplash(0);
00114     pagelist_.append(cpage);
00115         addPage(cpage->pagePath(), cpage->header(), SmallIcon("fileprint"), cpage);
00116 
00117     cpage = new CupsdServerSecurityPage(0);
00118     pagelist_.append(cpage);
00119         addPage(cpage->pagePath(), cpage->header(), SmallIcon("folder_locked"), cpage);
00120 
00121     cpage = new CupsdBrowsingGeneralPage(0);
00122     pagelist_.append(cpage);
00123         addPage(cpage->pagePath(), cpage->header(), SmallIcon("kdeprint_printer_remote"), cpage);
00124 
00125     cpage = new CupsdBrowsingRelayPage(0);
00126     pagelist_.append(cpage);
00127         addPage(cpage->pagePath(), cpage->header(), SmallIcon("connect_established"), cpage);
00128 
00129     cpage = new CupsdBrowsingTimeoutPage(0);
00130     pagelist_.append(cpage);
00131         addPage(cpage->pagePath(), cpage->header(), SmallIcon("date"), cpage);
00132 
00133         cpage = new CupsdBrowsingMasksPage(0);
00134     pagelist_.append(cpage);
00135         addPage(cpage->pagePath(), cpage->header(), SmallIcon("encrypted"), cpage);
00136 
00137     cpage = new CupsdBrowsingConnPage(0);
00138     pagelist_.append(cpage);
00139         addPage(cpage->pagePath(), cpage->header(), SmallIcon("connect_no"), cpage);
00140 
00141     cpage = new CupsdNetworkGeneralPage(0);
00142     pagelist_.append(cpage);
00143         addPage(cpage->pagePath(), cpage->header(), SmallIcon("network"), cpage);
00144 
00145     cpage = new CupsdNetworkClientsPage(0);
00146     pagelist_.append(cpage);
00147         addPage(cpage->pagePath(), cpage->header(), SmallIcon("openterm"), cpage);
00148 
00149     cpage = new CupsdServerIdentityPage(0);
00150     pagelist_.append(cpage);
00151         addPage(cpage->pagePath(), cpage->header(), SmallIcon("gear"), cpage);
00152 
00153     cpage = new CupsdServerMiscPage(0);
00154     pagelist_.append(cpage);
00155         addPage(cpage->pagePath(), cpage->header(), SmallIcon("package_utilities"), cpage);
00156 
00157     cpage = new CupsdServerJobPage(0);
00158     pagelist_.append(cpage);
00159         addPage(cpage->pagePath(), cpage->header(), SmallIcon("kdeprint_job"), cpage);
00160 
00161     cpage = new CupsdServerEncryptPage(0);
00162     pagelist_.append(cpage);
00163         addPage(cpage->pagePath(), cpage->header(), SmallIcon("password"), cpage);
00164 
00165     cpage = new CupsdServerHTTPPage(0);
00166     pagelist_.append(cpage);
00167         addPage(cpage->pagePath(), cpage->header(), SmallIcon("konqueror"), cpage);
00168 
00169     cpage = new CupsdServerDirPage(0);
00170     pagelist_.append(cpage);
00171         addPage(cpage->pagePath(), cpage->header(), SmallIcon("folder"), cpage);
00172 
00173     cpage = new CupsdServerLogPage(0);
00174     pagelist_.append(cpage);
00175         addPage(cpage->pagePath(), cpage->header(), SmallIcon("contents"), cpage);
00176 
00177     conf_ = new CupsdConf();
00178     for (pagelist_.first();pagelist_.current();pagelist_.next())
00179         {
00180         pagelist_.current()->setDefaults();
00181                 pagelist_.current()->setInfos(conf_);
00182         }
00183 }
00184 
00185 bool CupsdDialog::setConfigFile(const QString& filename)
00186 {
00187     filename_ = filename;
00188     if (!conf_->loadFromFile(filename_))
00189     {
00190         KMessageBox::error(this, i18n("Error while loading configuration file!"), i18n("CUPS Configuration Error"));
00191         return false;
00192     }
00193     bool    ok(true);
00194     QString msg;
00195     for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
00196         ok = pagelist_.current()->loadConfig(conf_, msg);
00197     if (!ok)
00198     {
00199         KMessageBox::error(this, msg, i18n("CUPS Configuration Error"));
00200         return false;
00201     }
00202     return true;
00203 }
00204 
00205 void CupsdDialog::slotOk()
00206 {
00207         slotApply();
00208         MyDialogBase::slotOk();
00209 }
00210 
00211 bool CupsdDialog::restartServer(QString& msg)
00212 {
00213     int serverPid = getServerPid();
00214         msg.truncate(0);
00215     if (serverPid <= 0)
00216     {
00217         msg = i18n("Unable to find a running CUPS server");
00218     }
00219     else
00220     {
00221         if (::kill(serverPid, SIGHUP) != 0)
00222             msg = i18n("Unable to restart CUPS server (pid = %1)").arg(serverPid);
00223     }
00224         return (msg.isEmpty());
00225 }
00226 
00227 #define DEFAULT_CONFIGFILE  "/etc/cups/cupsd.conf"
00228 void CupsdDialog::configure(const QString& filename, QWidget *parent)
00229 {
00230     QString fn = (filename.isEmpty() ? QString(DEFAULT_CONFIGFILE) : filename);
00231     QFileInfo   fi(fn);
00232     QString     errormsg;
00233     // check existence
00234     if (!fi.exists()) errormsg = i18n("File \"%1\" doesn't exist!").arg(fn);
00235     // check read state
00236     else if (!fi.isReadable()) errormsg = i18n("Can't open file \"%1\" !\nCheck file permissions.").arg(fn);
00237     // check write state
00238     else if (!fi.isWritable()) errormsg = i18n("You are not allowed to modify file \"%1\" !\nCheck file permissions or contact system administrator.").arg(fn);
00239 
00240     if (!errormsg.isEmpty())
00241     {
00242         KMessageBox::error(parent, errormsg, i18n("CUPS Configuration Error"));
00243     }
00244     else
00245     {
00246         CupsdDialog dlg(parent);
00247         if (dlg.setConfigFile(fn))
00248             dlg.exec();
00249     }
00250 }
00251 
00252 void CupsdDialog::slotApply()
00253 {
00254         if (conf_ && !filename_.isEmpty())
00255     { // try to save the file
00256         bool    ok(true);
00257         QString msg;
00258         CupsdConf   newconf_;
00259         for (pagelist_.first();pagelist_.current() && ok;pagelist_.next())
00260             ok = pagelist_.current()->saveConfig(&newconf_, msg);
00261         if (!ok)
00262         {
00263             ; // do nothing
00264         }
00265         else if (!newconf_.saveToFile(filename_))
00266         {
00267             msg = i18n("Unable to write configuration file %1").arg(filename_);
00268                         ok = false;
00269         }
00270         else ok = restartServer(msg);
00271                 if (!ok)
00272                 {
00273             KMessageBox::error(this, msg, i18n("CUPS Configuration Error"));
00274                 }
00275         }
00276 }
00277 
00278 int CupsdDialog::serverPid()
00279 {
00280     return getServerPid();
00281 }
00282 
00283 int CupsdDialog::serverOwner()
00284 {
00285     int pid = getServerPid();
00286     if (pid > 0)
00287     {
00288         QString str;
00289         str.sprintf("/proc/%d/status",pid);
00290         QFile   f(str);
00291         if (f.exists() && f.open(IO_ReadOnly))
00292         {
00293             QTextStream t(&f);
00294             while (!t.eof())
00295             {
00296                 str = t.readLine();
00297                 if (str.find("Uid:",0,false) == 0)
00298                 {
00299                     QStringList list = QStringList::split('\t', str, false);
00300                     if (list.count() >= 2)
00301                     {
00302                         bool    ok;
00303                         int u = list[1].toInt(&ok);
00304                         if (ok) return u;
00305                     }
00306                 }
00307             }
00308         }
00309     }
00310     return (-1);
00311 }
00312 #include "cupsddialog.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 5 07:17:46 2004 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003