kdeprint Library API Documentation

cupsdconf.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 "cupsdconf.h"
00021 
00022 #include <qfile.h>
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 #include <kconfig.h>
00026 
00027 #include <stdlib.h>
00028 #include <cups/cups.h>
00029 #include <cups/ipp.h>
00030 #include <cups/language.h>
00031 
00032 CupsdConf::CupsdConf()
00033 {
00034     servername_ = QString::null;
00035     serveradmin_ = QString::null;
00036     user_ = QString::null;
00037     group_ = QString::null;
00038     remroot_ = QString::null;
00039     accesslog_ = QString::null;
00040     errorlog_ = QString::null;
00041     pagelog_ = QString::null;
00042     loglevel_ = -1;
00043     maxlogsize_ = -1;
00044     datadir_ = QString::null;
00045     requestroot_ = QString::null;
00046     serverbin_ = QString::null;
00047     serverroot_ = QString::null;
00048     tempdir_ = QString::null;
00049     fontpath_ = QString::null;
00050     documentroot_ = QString::null;
00051     defaultcharset_ = QString::null;
00052     defaultlanguage_ = QString::null;
00053     preservejobhistory_ = -1;
00054     preservejobfiles_ = -1;
00055     autopurgejobs_ = -1;
00056     maxjobs_ = -1;
00057     filterlimit_ = -1;
00058     classification_ = QString::null;
00059     classifyoverride_ = -1;
00060     printcap_ = QString::null;
00061     ripcache_ = QString::null;
00062     hostnamelookups_ = -1;
00063     keepalive_ = -1;
00064     keepalivetimeout_ = -1;
00065     maxclients_ = -1;
00066     maxrequestsize_ = -1;
00067     timeout_ = -1;
00068     browsing_ = -1;
00069     browseshortnames_ = -1;
00070     implicitclasses_ = -1;
00071     implicitanyclasses_ = -1;
00072     hideimplicitmembers_ = -1;
00073     browseprotocols_ = -1;
00074     // browseaddress_
00075     browseport_ = -1;
00076     // browserelay
00077     // browsepoll
00078     // browseallow_
00079     // browsedeny_
00080     browseorder_ = -1;
00081     browseinterval_ = -1;
00082     browsetimeout_ = -1;
00083     systemgroup_ = QString::null;
00084     locations_.setAutoDelete(true);
00085     resources_.setAutoDelete(true);
00086         servercertificate_ = QString::null;
00087         serverkey_ = QString::null;
00088 
00089     loadAvailableResources();
00090 }
00091 
00092 CupsdConf::~CupsdConf()
00093 {
00094 }
00095 
00096 bool CupsdConf::loadFromFile(const QString& filename)
00097 {
00098     QFile   f(filename);
00099     if (!f.exists() || !f.open(IO_ReadOnly)) return false;
00100     else
00101     {
00102         QTextStream t(&f);
00103         QString line;
00104         bool    done(false), value(true);
00105         while (!done && value)
00106         {
00107             line = t.readLine().simplifyWhiteSpace();
00108             if (line.isEmpty())
00109             {
00110                 if (t.atEnd()) done = true;
00111                 else continue;
00112             }
00113             else if (line[0] == '#') continue;
00114             else if (line.left(9).lower() == "<location")
00115             {
00116                 CupsLocation    *location = new CupsLocation();
00117                 locations_.append(location);
00118                 if (!location->parseResource(line) || !parseLocation(location, t))
00119                     value = false;
00120                 // search corresponding resource
00121                 for (resources_.first();resources_.current();resources_.next())
00122                     if (resources_.current()->path_ == location->resourcename_)
00123                         location->resource_ = resources_.current();
00124             }
00125             else value = parseOption(line);
00126         }
00127         f.close();
00128         return value;
00129     }
00130 }
00131 
00132 bool CupsdConf::saveToFile(const QString& filename)
00133 {
00134     QFile   f(filename);
00135     if (!f.open(IO_WriteOnly)) return false;
00136     else
00137     {
00138         QTextStream t(&f);
00139         t << comments_[HEADER_COMM] << endl;
00140         t << comments_[SERVERNAME_COMM] << endl;
00141         if (!servername_.isNull()) t << "ServerName " << servername_ << endl;
00142         t << endl << comments_[SERVERADMIN_COMM] << endl;
00143         if (!serveradmin_.isNull()) t << "ServerAdmin " << serveradmin_ << endl;
00144         t << endl << comments_[USER_COMM] << endl;
00145         if (!user_.isNull()) t << "User " << user_ << endl;
00146         t << endl << comments_[GROUP_COMM] << endl;
00147         if (!group_.isNull()) t << "Group " << group_ << endl;
00148         t << endl << comments_[REMOTEROOT_COMM] << endl;
00149         if (!remroot_.isNull()) t << "RemoteRoot " << remroot_ << endl;
00150         t << endl << comments_[ACCESSLOG_COMM] << endl;
00151         if (!accesslog_.isNull()) t << "AccessLog " << accesslog_ << endl;
00152         t << endl << comments_[ERRORLOG_COMM] << endl;
00153         if (!errorlog_.isNull()) t << "ErrorLog " << errorlog_ << endl;
00154         t << endl << comments_[PAGELOG_COMM] << endl;
00155         if (!pagelog_.isNull()) t << "PageLog " << pagelog_ << endl;
00156         t << endl << comments_[LOGLEVEL_COMM] << endl;
00157         if (loglevel_ != -1)
00158         {
00159             t << "LogLevel ";
00160             switch (loglevel_)
00161             {
00162                case LOGLEVEL_DEBUG2: t << "debug2" << endl; break;
00163                case LOGLEVEL_DEBUG: t << "debug" << endl; break;
00164                case LOGLEVEL_INFO: t << "info" << endl; break;
00165                case LOGLEVEL_WARN: t << "warn" << endl; break;
00166                case LOGLEVEL_ERROR: t << "error" << endl; break;
00167                case LOGLEVEL_NONE: t << "none" << endl; break;
00168                default: t << "info" << endl; break;
00169             }
00170         }
00171         t << endl << comments_[MAXLOGSIZE_COMM] << endl;
00172         if (maxlogsize_ != -1) t << "MaxLogSize " << maxlogsize_ << endl;
00173         t << endl << comments_[DATADIR_COMM] << endl;
00174         if (!datadir_.isNull()) t << "DataDir " << datadir_ << endl;
00175         t << endl << comments_[REQUESTROOT_COMM] << endl;
00176         if (!requestroot_.isNull()) t << "RequestRoot " << requestroot_ << endl;
00177         t << endl << comments_[SERVERBIN_COMM] << endl;
00178         if (!serverbin_.isNull()) t << "ServerBin " << serverbin_ << endl;
00179         t << endl << comments_[SERVERROOT_COMM] << endl;
00180         if (!serverroot_.isNull()) t << "ServerRoot " << serverroot_ << endl;
00181         t << endl << comments_[SERVERCERTIFICATE_COMM] << endl;
00182         if (!servercertificate_.isNull()) t << "ServerCertificate " << servercertificate_ << endl;
00183         t << endl << comments_[SERVERKEY_COMM] << endl;
00184         if (!serverkey_.isNull()) t << "ServerKey " << serverkey_ << endl;
00185         t << endl << comments_[TEMPDIR_COMM] << endl;
00186         if (!tempdir_.isNull()) t << "TempDir " << tempdir_ << endl;
00187         t << endl << comments_[FONTPATH_COMM] << endl;
00188         if (!fontpath_.isNull()) t << "FontPath " << fontpath_ << endl;
00189         t << endl << comments_[DOCUMENTROOT_COMM] << endl;
00190         if (!documentroot_.isNull()) t << "DocumentRoot " << documentroot_ << endl;
00191         t << endl << comments_[DEFAULTCHARSET_COMM] << endl;
00192         if (!defaultcharset_.isNull()) t << "DefaultCharset " << defaultcharset_ << endl;
00193         t << endl << comments_[DEFAULTLANG_COMM] << endl;
00194         if (!defaultlanguage_.isNull()) t << "DefaultLanguage " << defaultlanguage_ << endl;
00195         t << endl << comments_[PRESERVEJOBHIST_COMM] << endl;
00196         if (preservejobhistory_ != -1) t << "PreserveJobHistory " << (preservejobhistory_ == 1 ? "Yes" : "No") << endl;
00197         t << endl << comments_[PRESERVEJOBFILE_COMM] << endl;
00198         if (preservejobfiles_ != -1) t << "PreserveJobFiles " << (preservejobfiles_ == 1 ? "Yes" : "No") << endl;
00199         t << endl << comments_[AUTOPURGEJOBS_COMM] << endl;
00200         if (autopurgejobs_ != -1) t << "AutoPurgeJobs " << (autopurgejobs_ == 1 ? "Yes" : "No") << endl;
00201         t << endl << comments_[MAXJOBS_COMM] << endl;
00202         if (maxjobs_ != -1) t << "MaxJobs " << maxjobs_ << endl;
00203         t << endl << comments_[FILTERLIMIT_COMM] << endl;
00204         if (filterlimit_ != -1) t << "FilterLimit " << filterlimit_ << endl;
00205         t << endl << comments_[CLASSIFICATION_COMM] << endl;
00206         if (!classification_.isNull()) t << "Classification " << classification_ << endl;
00207         t << endl << comments_[CLASSIFYOVERRIDE_COMM] << endl;
00208         if (classifyoverride_ != -1) t << "ClassifyOverride " << (classifyoverride_ == 1 ? "On" : "Off") << endl;
00209         t << endl << comments_[PRINTCAP_COMM] << endl;
00210         if (!printcap_.isNull()) t << "Printcap " << printcap_ << endl;
00211         t << endl << comments_[RIPCACHE_COMM] << endl;
00212         if (!ripcache_.isNull()) t << "RIPCache " << ripcache_ << endl;
00213         t << endl << comments_[PORT_COMM] << endl;
00214         QValueList<int>::Iterator   it;
00215         for (it=port_.begin();it!=port_.end();++it)
00216             t << "Port " << (*it) << endl;
00217         t << endl << comments_[HOSTNAMELOOKUPS_COMM] << endl;
00218         if (hostnamelookups_ != -1) t << "HostNameLookups " << (hostnamelookups_ == 1 ? "On" : "Off") << endl;
00219         t << endl << comments_[KEEPALIVE_COMM] << endl;
00220         if (keepalive_ != -1) t << "KeepAlive " << (keepalive_ == 1 ? "On" : "Off") << endl;
00221         t << endl << comments_[KEEPALIVETIMEOUT_COMM] << endl;
00222         if (keepalivetimeout_ != -1) t << "KeepAliveTimeout " << keepalivetimeout_ << endl;
00223         t << endl << comments_[MAXCLIENTS_COMM] << endl;
00224         if (maxclients_ != -1) t << "MaxClients " << maxclients_ << endl;
00225         t << endl << comments_[MAXREQUESTSIZE_COMM] << endl;
00226         if (maxrequestsize_ != -1) t << "MaxRequestSize " << maxrequestsize_ << endl;
00227         t << endl << comments_[TIMEOUT_COMM] << endl;
00228         if (timeout_ != -1) t << "Timeout " << timeout_ << endl;
00229         t << endl << comments_[BROWSING_COMM] << endl;
00230         if (browsing_ != -1) t << "Browsing " << (browsing_ == 1 ? "On" : "Off") << endl;
00231         t << endl << comments_[BROWSESHORTNAMES_COMM] << endl;
00232         if (browseshortnames_ != -1) t << "BrowseShortNames " << (browseshortnames_ == 1 ? "Yes" : "No") << endl;
00233         t << endl << comments_[IMPLICITCLASSES_COMM] << endl;
00234         if (implicitclasses_ != -1) t << "ImplicitClasses " << (implicitclasses_ == 1 ? "On" : "Off") << endl;
00235         t << endl << comments_[IMPLICITANYCLASSES_COMM] << endl;
00236         if (implicitanyclasses_ != -1) t << "ImplicitAnyClasses " << (implicitanyclasses_ == 1 ? "On" : "Off") << endl;
00237         t << endl << comments_[HIDEIMPLICITMEMBERS_COMM] << endl;
00238         if (hideimplicitmembers_ != -1) t << "HideImplicitMembers " << (hideimplicitmembers_ == 1 ? "On" : "Off") << endl;
00239         t << endl << comments_[BROWSEPROTOCOLS_COMM] << endl;
00240         switch (browseprotocols_)
00241         {
00242             case BROWSE_ALL: t << "BrowseProtocols all" << endl; break;
00243             case BROWSE_CUPS: t << "BrowseProtocols cups" << endl; break;
00244             case BROWSE_SLP: t << "BrowseProtocols slp" << endl; break;
00245             default: break;
00246         }
00247         t << endl << comments_[BROWSEADDRESS_COMM] << endl;
00248         QStringList::Iterator   sit;
00249         for (sit=browseaddress_.begin();sit!=browseaddress_.end();++sit)
00250             t << "BrowseAddress " << (*sit) << endl;
00251         t << endl << comments_[BROWSEPORT_COMM] << endl;
00252         if (browseport_ != -1) t << "BrowsePort " << browseport_ << endl;
00253         t << endl << comments_[BROWSERELAY_COMM] << endl;
00254         for (sit=browserelay_.begin();sit!=browserelay_.end();++sit)
00255         {
00256             t << "BrowseRelay " << (*sit);
00257             ++sit;
00258             t << " " << (*sit) << endl;
00259         }
00260         t << endl << comments_[BROWSEPOLL_COMM] << endl;
00261         for (sit=browsepoll_.begin();sit!=browsepoll_.end();++sit)
00262             t << "BrowsePoll " << (*sit) << endl;
00263         t << endl << comments_[BROWSEALLOWDENY_COMM] << endl;
00264         for (sit=browseallow_.begin();sit!=browseallow_.end();++sit)
00265             t << "BrowseAllow " << (*sit) << endl;
00266         for (sit=browsedeny_.begin();sit!=browsedeny_.end();++sit)
00267             t << "BrowseDeny " << (*sit) << endl;
00268         t << endl << comments_[BROWSEORDER_COMM] << endl;
00269         if (browseorder_ != -1) t << "BrowseOrder " << (browseorder_ == ORDER_ALLOW_DENY ? "allow,deny" : "deny,allow") << endl;
00270         t << endl << comments_[BROWSEINTERVAL_COMM] << endl;
00271         if (browseinterval_ != -1) t << "BrowseInterval " << browseinterval_ << endl;
00272         t << endl << comments_[BROWSETIMEOUT_COMM] << endl;
00273         if (browsetimeout_ != -1) t << "BrowseTimeout " << browsetimeout_ << endl;
00274         t << endl << comments_[SYSTEMGROUP_COMM] << endl;
00275         if (!systemgroup_.isNull()) t << "SystemGroup " << systemgroup_ << endl;
00276         t << endl << comments_[LOCATIONSCOMPLETE_COMM] << endl;
00277         for (locations_.first();locations_.current();locations_.next())
00278         {
00279             CupsLocation    *loc = locations_.current();
00280             t << "<Location " << loc->resourcename_ << ">" << endl;
00281             if (loc->authtype_ != -1)
00282             {
00283                 t << "AuthType ";
00284                 switch (loc->authtype_)
00285                 {
00286                    case AUTHTYPE_NONE: t << "None" << endl; break;
00287                    case AUTHTYPE_BASIC: t << "Basic" << endl; break;
00288                    case AUTHTYPE_DIGEST: t << "Digest" << endl; break;
00289                    default: t << "None" << endl; break;
00290                 }
00291             }
00292                         if (loc->authtype_ != AUTHTYPE_NONE)
00293                         { // print other settings only if type is not "None"
00294                     if (loc->authclass_ != -1)
00295                     {
00296                     t << "AuthClass ";
00297                     switch (loc->authclass_)
00298                     {
00299                        case AUTHCLASS_ANONYMOUS: t << "Anonymous" << endl; break;
00300                        case AUTHCLASS_USER: t << "User" << endl; break;
00301                        case AUTHCLASS_SYSTEM: t << "System" << endl; break;
00302                        case AUTHCLASS_GROUP: t << "Group" << endl; break;
00303                        default: t << "User" << endl; break;
00304                     }
00305                 }
00306                 if (!loc->authgroupname_.isEmpty()) t << "AuthGroupName " << loc->authgroupname_ << endl;
00307                 if (loc->order_ != -1) t << "Order " << (loc->order_ == ORDER_ALLOW_DENY ? "Allow,Deny" : "Deny,Allow") << endl;
00308                 for (sit=loc->allow_.begin();sit!=loc->allow_.end();++sit)
00309                     t << "Allow From " << (*sit) << endl;
00310                 for (sit=loc->deny_.begin();sit!=loc->deny_.end();++sit)
00311                     t << "Deny From " << (*sit) << endl;
00312             }
00313             if (loc->encryption_ != -1)
00314                         {
00315                             t << "Encryption ";
00316                                 switch (loc->encryption_)
00317                                 {
00318                                    case ENCRYPT_ALWAYS: t << "Always" << endl; break;
00319                                    case ENCRYPT_NEVER: t << "Never" << endl; break;
00320                                    case ENCRYPT_REQUIRED: t << "Required" << endl; break;
00321                                    case ENCRYPT_IFREQUESTED: t << "IfRequested" << endl; break;
00322                                    default: t << "IfRequested" << endl; break;
00323                                 }
00324                         }
00325             t << "</Location>" << endl;
00326         }
00327         return true;
00328     }
00329 }
00330 
00331 bool CupsdConf::parseLocation(CupsLocation *location, QTextStream& file)
00332 {
00333     QString line;
00334     bool    done(false);
00335     bool    value(true);
00336     while (!done && value)
00337     {
00338         line = file.readLine().simplifyWhiteSpace();
00339         if (line.isEmpty())
00340         {
00341             if (file.atEnd())
00342             {
00343                 value = false;
00344                 done = true;
00345             }
00346             else continue;
00347         }
00348         else if (line[0] == '#') continue;
00349         else if (line.lower() == "</location>") done = true;
00350         else value = location->parseOption(line);
00351     }
00352     return value;
00353 }
00354 
00355 bool CupsdConf::parseOption(const QString& line)
00356 {
00357     QStringList wordlist_ = QStringList::split(' ', line.simplifyWhiteSpace().append(' '), false);
00358     if (wordlist_.count() == 0)
00359         return false;
00360     if (wordlist_.count() == 1)
00361     {
00362 kdDebug() << "warning: empty option \"" << (*(wordlist_.at(0))) << "\", adding a pseudo empty argument" << endl;
00363         wordlist_.append(QString(""));
00364     }
00365     QString opt = *(wordlist_.at(0));
00366     if (opt.lower() == "servername") servername_ = *(wordlist_.at(1));
00367     else if (opt.lower() == "serveradmin") serveradmin_ = *(wordlist_.at(1));
00368     else if (opt.lower() == "user") user_ = *(wordlist_.at(1));
00369     else if (opt.lower() == "group") group_ = *(wordlist_.at(1));
00370     else if (opt.lower() == "remoteroot") remroot_ = *(wordlist_.at(1));
00371     else if (opt.lower() == "accesslog") accesslog_ = *(wordlist_.at(1));
00372     else if (opt.lower() == "errorlog") errorlog_ = *(wordlist_.at(1));
00373     else if (opt.lower() == "pagelog") pagelog_ = *(wordlist_.at(1));
00374     else if (opt.lower() == "loglevel")
00375     {
00376         QString log = *(wordlist_.at(1));
00377         if (log.lower() == "debug2") loglevel_ = LOGLEVEL_DEBUG2;
00378         else if (log.lower() == "debug") loglevel_ = LOGLEVEL_DEBUG;
00379         else if (log.lower() == "info") loglevel_ = LOGLEVEL_INFO;
00380         else if (log.lower() == "warn") loglevel_ = LOGLEVEL_WARN;
00381         else if (log.lower() == "error") loglevel_ = LOGLEVEL_ERROR;
00382         else if (log.lower() == "none") loglevel_ = LOGLEVEL_NONE;
00383         else return false;
00384     }
00385     else if (opt.lower() == "maxlogsize") maxlogsize_ = (*(wordlist_.at(1))).toInt();
00386     else if (opt.lower() == "datadir") datadir_ = *(wordlist_.at(1));
00387     else if (opt.lower() == "requestroot") requestroot_ = *(wordlist_.at(1));
00388     else if (opt.lower() == "serverbin") serverbin_ = *(wordlist_.at(1));
00389     else if (opt.lower() == "serverroot") serverroot_ = *(wordlist_.at(1));
00390     else if (opt.lower() == "servercertificate") servercertificate_ = *(wordlist_.at(1));
00391     else if (opt.lower() == "serverkey") serverkey_ = *(wordlist_.at(1));
00392     else if (opt.lower() == "tempdir") tempdir_ = *(wordlist_.at(1));
00393     else if (opt.lower() == "fontpath") fontpath_ = *(wordlist_.at(1));
00394     else if (opt.lower() == "documentroot") documentroot_ = *(wordlist_.at(1));
00395     else if (opt.lower() == "defaultcharset") defaultcharset_ = *(wordlist_.at(1));
00396     else if (opt.lower() == "defaultlanguage") defaultlanguage_ = *(wordlist_.at(1));
00397     else if (opt.lower() == "preservejobhistory") preservejobhistory_ = (*(wordlist_.at(1)) == "Yes" ? 1 : 0);
00398     else if (opt.lower() == "preservejobfiles") preservejobfiles_ = (*(wordlist_.at(1)) == "Yes" ? 1 : 0);
00399     else if (opt.lower() == "autopurgejobs") autopurgejobs_ = (*(wordlist_.at(1)) == "Yes" ? 1 : 0);
00400     else if (opt.lower() == "maxjobs") maxjobs_ = (*(wordlist_.at(1))).toInt();
00401     else if (opt.lower() == "filterlimit") filterlimit_ = (*(wordlist_.at(1))).toInt();
00402     else if (opt.lower() == "classification") classification_ = (*(wordlist_.at(1)));
00403     else if (opt.lower() == "classifyoverride") classifyoverride_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00404     else if (opt.lower() == "printcap") printcap_ = *(wordlist_.at(1));
00405     else if (opt.lower() == "ripcache") ripcache_ = *(wordlist_.at(1));
00406     else if (opt.lower() == "port") port_.append((*(wordlist_.at(1))).toInt());
00407     else if (opt.lower() == "hostnamelookups") hostnamelookups_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00408     else if (opt.lower() == "keepalive") keepalive_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00409     else if (opt.lower() == "keepalivetimeout") keepalivetimeout_ = (*(wordlist_.at(1))).toInt();
00410     else if (opt.lower() == "maxclients") maxclients_ = (*(wordlist_.at(1))).toInt();
00411     else if (opt.lower() == "maxrequestsize") maxrequestsize_ = (*(wordlist_.at(1))).toInt();
00412     else if (opt.lower() == "timeout") timeout_ = (*(wordlist_.at(1))).toInt();
00413     else if (opt.lower() == "browsing") browsing_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00414     else if (opt.lower() == "browseshortnames") browseshortnames_ = (*(wordlist_.at(1)) == "Yes" ? 1 : 0);
00415     else if (opt.lower() == "implicitclasses") implicitclasses_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00416     else if (opt.lower() == "implicitanyclasses") implicitanyclasses_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00417     else if (opt.lower() == "hideimplicitmembers") hideimplicitmembers_ = (*(wordlist_.at(1)) == "On" ? 1 : 0);
00418     else if (opt.lower() == "browseprotocols")
00419     {
00420         QString value = *(wordlist_.at(1));
00421         if (value.lower() == "all") browseprotocols_ = 0;
00422         else if (value.lower() == "cups") browseprotocols_ = 1;
00423         else if (value.lower() == "slp") browseprotocols_ = 2;
00424         else browseprotocols_ = -1;
00425     }
00426     else if (opt.lower() == "browseaddress") browseaddress_.append(*(wordlist_.at(1)));
00427     else if (opt.lower() == "browseport") browseport_ = (*(wordlist_.at(1))).toInt();
00428     else if (opt.lower() == "browserelay")
00429     {
00430         if (wordlist_.count() < 3) return false;
00431         browserelay_.append(*(wordlist_.at(1)));
00432         browserelay_.append(*(wordlist_.at(2)));
00433     }
00434     else if (opt.lower() == "browsepoll") browsepoll_.append(*(wordlist_.at(1)));
00435     else if (opt.lower() == "browseallow") browseallow_.append(*(wordlist_.at(1)));
00436     else if (opt.lower() == "browsedeny") browsedeny_.append(*(wordlist_.at(1)));
00437     else if (opt.lower() == "browseorder")
00438     {
00439         QString value = *(wordlist_.at(1));
00440         if (value.lower() == "allow,deny") browseorder_ = ORDER_ALLOW_DENY;
00441         else if (value.lower() == "deny,allow") browseorder_ = ORDER_DENY_ALLOW;
00442         else return false;
00443     }
00444     else if (opt.lower() == "browseinterval") browseinterval_ = (*(wordlist_.at(1))).toInt();
00445     else if (opt.lower() == "browsetimeout") browsetimeout_ = (*(wordlist_.at(1))).toInt();
00446     else if (opt.lower() == "systemgroup") systemgroup_ = *(wordlist_.at(1));
00447     else return false;
00448     return true;
00449 }
00450 
00451 bool CupsdConf::loadAvailableResources()
00452 {
00453     KConfig conf("kdeprintrc");
00454     conf.setGroup("CUPS");
00455     QString host = conf.readEntry("Host",cupsServer());
00456     int     port = conf.readNumEntry("Port",ippPort());
00457     http_t  *http_ = httpConnect(host.local8Bit(),port);
00458 
00459     resources_.clear();
00460     // standard resources
00461     resources_.append(new CupsResource("/"));
00462     resources_.append(new CupsResource("/admin"));
00463     resources_.append(new CupsResource("/printers"));
00464     resources_.append(new CupsResource("/classes"));
00465 
00466     if (!http_)
00467         return false;
00468 
00469     // printer resources
00470     ipp_t   *request_ = ippNew();
00471     cups_lang_t*    lang = cupsLangDefault();
00472     ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, cupsLangEncoding(lang));
00473     ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, lang->language);
00474     request_->request.op.operation_id = CUPS_GET_PRINTERS;
00475     request_ = cupsDoRequest(http_, request_, "/printers/");
00476     if (request_)
00477     {
00478         QString name;
00479         int type(0);
00480         ipp_attribute_t *attr = request_->attrs;
00481         while (attr)
00482         {
00483             // check new printer (keep only local non-implicit printers)
00484             if (!attr->name)
00485             {
00486                 if (!(type & CUPS_PRINTER_REMOTE) && !(type & CUPS_PRINTER_IMPLICIT) && !name.isEmpty())
00487                     resources_.append(new CupsResource("/printers/"+name));
00488                 name = "";
00489                 type = 0;
00490             }
00491             else if (strcmp(attr->name, "printer-name") == 0) name = attr->values[0].string.text;
00492             else if (strcmp(attr->name, "printer-type") == 0) type = attr->values[0].integer;
00493             attr = attr->next;
00494         }
00495         if (!(type & CUPS_PRINTER_REMOTE) && !(type & CUPS_PRINTER_IMPLICIT) && !name.isEmpty())
00496             resources_.append(new CupsResource("/printers/"+name));
00497         ippDelete(request_);
00498     }
00499     // class resources
00500     request_ = ippNew();
00501     ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_CHARSET, "attributes-charset", NULL, cupsLangEncoding(lang));
00502     ippAddString(request_, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "attributes-natural-language", NULL, lang->language);
00503     request_->request.op.operation_id = CUPS_GET_CLASSES;
00504     request_ = cupsDoRequest(http_, request_, "/classes/");
00505     if (request_)
00506     {
00507         QString name;
00508         int type(0);
00509         ipp_attribute_t *attr = request_->attrs;
00510         while (attr)
00511         {
00512             // check new class (keep only local classes)
00513             if (!attr->name)
00514             {
00515                 if (!(type & CUPS_PRINTER_REMOTE) && !name.isEmpty())
00516                     resources_.append(new CupsResource("/classes/"+name));
00517                 name = "";
00518                 type = 0;
00519             }
00520             else if (strcmp(attr->name, "printer-name") == 0) name = attr->values[0].string.text;
00521             else if (strcmp(attr->name, "printer-type") == 0) type = attr->values[0].integer;
00522             attr = attr->next;
00523         }
00524         if (!(type & CUPS_PRINTER_REMOTE) && !name.isEmpty())
00525             resources_.append(new CupsResource("/classes/"+name));
00526         ippDelete(request_);
00527     }
00528     httpClose(http_);
00529     return true;
00530 }
00531 
00532 //------------------------------------------------------------------------------------------------
00533 
00534 CupsLocation::CupsLocation()
00535 {
00536     resource_ = 0;
00537     resourcename_ = "";
00538     authtype_ = -1;
00539     authclass_ = -1;
00540     authgroupname_ = QString::null;
00541     order_ = -1;
00542     // allow_
00543     // deny_
00544         encryption_ = -1;
00545 }
00546 
00547 CupsLocation::CupsLocation(const CupsLocation& loc)
00548 : resource_(loc.resource_),
00549   resourcename_(loc.resourcename_),
00550   authtype_(loc.authtype_),
00551   authclass_(loc.authclass_),
00552   authgroupname_(loc.authgroupname_),
00553   order_(loc.order_),
00554   allow_(loc.allow_),
00555   deny_(loc.deny_),
00556   encryption_(loc.encryption_)
00557 {
00558 }
00559 
00560 bool CupsLocation::parseResource(const QString& line)
00561 {
00562     QString str = line.simplifyWhiteSpace();
00563     int p1 = line.find(' '), p2 = line.find('>');
00564     if (p1 != -1 && p2 != -1)
00565     {
00566         resourcename_ = str.mid(p1+1,p2-p1-1);
00567         return true;
00568     }
00569     else return false;
00570 }
00571 
00572 bool CupsLocation::parseOption(const QString& line)
00573 {
00574     QStringList wordlist_ = QStringList::split(' ', line.simplifyWhiteSpace().append(' '), false);
00575     if (wordlist_.count() == 0) return false;
00576     if (wordlist_.count() == 1)
00577     {
00578 kdDebug() << "warning: empty option \"" << (*(wordlist_.at(0))) << "\", adding a pseudo empty argument" << endl;
00579         wordlist_.append(QString(""));
00580     }
00581     QString opt = *(wordlist_.at(0)), value = *(wordlist_.at(1));
00582     if (opt.lower() == "authtype")
00583     {
00584         if (value.lower() == "none") authtype_ = AUTHTYPE_NONE;
00585         else if (value.lower() == "basic") authtype_ = AUTHTYPE_BASIC;
00586         else if (value.lower() == "digest") authtype_ = AUTHTYPE_DIGEST;
00587         else return false;
00588     }
00589     else if (opt.lower() == "authclass")
00590     {
00591         if (value.lower() == "anonymous") authclass_ = AUTHCLASS_ANONYMOUS;
00592         else if (value.lower() == "user") authclass_ = AUTHCLASS_USER;
00593         else if (value.lower() == "system") authclass_ = AUTHCLASS_SYSTEM;
00594         else if (value.lower() == "group") authclass_ = AUTHCLASS_GROUP;
00595         else return false;
00596     }
00597     else if (opt.lower() == "authgroupname") authgroupname_ = value;
00598     else if (opt.lower() == "allow") allow_.append(*(wordlist_.at(2)));
00599     else if (opt.lower() == "deny") deny_.append(*(wordlist_.at(2)));
00600     else if (opt.lower() == "order")
00601     {
00602         if (value.lower() == "allow,deny") order_ = ORDER_ALLOW_DENY;
00603         else if (value.lower() == "deny,allow") order_ = ORDER_DENY_ALLOW;
00604         else return false;
00605     }
00606         else if (opt.lower() == "encryption")
00607         {
00608             if (value.lower() == "always") encryption_ = ENCRYPT_ALWAYS;
00609             else if (value.lower() == "never") encryption_ = ENCRYPT_NEVER;
00610             else if (value.lower() == "required") encryption_ = ENCRYPT_REQUIRED;
00611             else if (value.lower() == "ifrequested") encryption_ = ENCRYPT_IFREQUESTED;
00612                 else return false;
00613         }
00614     return true;
00615 }
00616 
00617 //------------------------------------------------------------------------------------------------
00618 
00619 CupsResource::CupsResource()
00620 {
00621     type_ = RESOURCE_GLOBAL;
00622 }
00623 
00624 CupsResource::CupsResource(const QString& path)
00625 {
00626     setPath(path);
00627 }
00628 
00629 void CupsResource::setPath(const QString& path)
00630 {
00631     path_ = path;
00632     type_ = typeFromPath(path_);
00633     text_ = pathToText(path_);
00634 }
00635 
00636 int CupsResource::typeFromText(const QString& text)
00637 {
00638     if (text == i18n("Base", "Root") || text == i18n("All printers") || text == i18n("All classes")) return RESOURCE_GLOBAL;
00639     else if (text == i18n("Administration")) return RESOURCE_ADMIN;
00640     else if (text.find(i18n("Class")) == 0) return RESOURCE_CLASS;
00641     else if (text.find(i18n("Printer")) == 0) return RESOURCE_PRINTER;
00642     else return RESOURCE_PRINTER;
00643 }
00644 
00645 int CupsResource::typeFromPath(const QString& path)
00646 {
00647     if (path == "/admin") return RESOURCE_ADMIN;
00648     else if (path == "/printers" || path == "/classes" || path == "/") return RESOURCE_GLOBAL;
00649     else if (path.left(9) == "/printers") return RESOURCE_PRINTER;
00650     else if (path.left(8) == "/classes") return RESOURCE_CLASS;
00651     else return RESOURCE_GLOBAL;
00652 }
00653 
00654 QString CupsResource::textToPath(const QString& text)
00655 {
00656     QString path("/");
00657     if (text == i18n("Administration")) path = "/admin";
00658     else if (text == i18n("All printers")) path = "/printers";
00659     else if (text == i18n("All classes")) path = "/classes";
00660     else if (text == i18n("Base", "Root")) path = "/";
00661     else if (text.find(i18n("Printer")) == 0)
00662     {
00663         path = "/printers/";
00664         path.append(text.right(text.length()-i18n("Printer").length()-1));
00665     }
00666     else if (text.find(i18n("Class")) == 0)
00667     {
00668         path = "/classes/";
00669         path.append(text.right(text.length()-i18n("Class").length()-1));
00670     }
00671     return path;
00672 }
00673 
00674 QString CupsResource::pathToText(const QString& path)
00675 {
00676     QString text(i18n("Base", "Root"));
00677     if (path == "/admin") text = i18n("Administration");
00678     else if (path == "/printers") text = i18n("All printers");
00679     else if (path == "/classes") text = i18n("All classes");
00680     else if (path == "/") text = i18n("Root");
00681     else if (path.find("/printers/") == 0)
00682     {
00683         text = i18n("Printer");
00684         text.append(" ");
00685         text.append(path.right(path.length()-10));
00686     }
00687     else if (path.find("/classes/") == 0)
00688     {
00689         text = i18n("Class");
00690         text.append(" ");
00691         text.append(path.right(path.length()-9));
00692     }
00693     return text;
00694 }
00695 
00696 QString CupsResource::typeToIconName(int type)
00697 {
00698     switch (type)
00699     {
00700        case RESOURCE_ADMIN:
00701        case RESOURCE_GLOBAL:
00702         return QString("folder");
00703        case RESOURCE_PRINTER:
00704         return QString("kdeprint_printer");
00705        case RESOURCE_CLASS:
00706         return QString("kdeprint_printer_class");
00707     }
00708     return QString("folder");
00709 }
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jan 22 16:49:47 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003