kdeprint Library API Documentation

kdeprintcheck.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 /*
00021  * Implementation of simple checking mechanism. Rules are defined in
00022  * the form of an URI. Available syntax is:
00023  *  - exec:/<execname>  ->  check for an executable in
00024  *                  $PATH variable.
00025  *  - config:/path/to/file  ->      check for the existence of a file
00026  *                  or directory in KDE or standard
00027  *                  UNIX config locations
00028  *  - file:/path/to/file
00029  *  - dir:/path/to/dir  ->  simply check the existence of the
00030  *                  a file or directory
00031  *  - service:/serv     ->  try to connect to a port on the
00032  *                  specified host (usually localhost)
00033  *                  "serv" can be a port value or service name
00034  *
00035  * TO BE IMPLEMENTED:
00036  *  - run:/<execname>   ->  check for a running executable
00037  */
00038 
00039 #include "kdeprintcheck.h"
00040 
00041 #include <kstandarddirs.h>
00042 #include <kextsock.h>
00043 
00044 static const char* const config_stddirs[] = {
00045     "/etc/",
00046     "/usr/etc/",
00047     "/usr/local/etc/",
00048     "/opt/etc/",
00049     "/opt/local/etc/",
00050     0
00051 };
00052 
00053 bool KdeprintChecker::check(KConfig *conf, const QString& group)
00054 {
00055     if (!group.isEmpty())
00056         conf->setGroup(group);
00057     QStringList uris = conf->readListEntry("Require");
00058     return check(uris);
00059 }
00060 
00061 bool KdeprintChecker::check(const QStringList& uris)
00062 {
00063     bool    state(true);
00064     for (QStringList::ConstIterator it=uris.begin(); it!=uris.end() && state; ++it)
00065         state = (state && checkURL(KURL(*it)));
00066     return state;
00067 }
00068 
00069 bool KdeprintChecker::checkURL(const KURL& url)
00070 {
00071     QString prot(url.protocol());
00072     if (prot == "config")
00073         return checkConfig(url);
00074     else if (prot == "exec")
00075         return checkExec(url);
00076     else if (prot == "file" || prot == "dir")
00077         return KStandardDirs::exists(url.url());
00078     else if (prot == "service")
00079         return checkService(url);
00080     return false;
00081 }
00082 
00083 bool KdeprintChecker::checkConfig(const KURL& url)
00084 {
00085     // get the config filename (may contain a path)
00086     QString f(url.path().mid(1));
00087     bool    state(false);
00088 
00089     // first check for standard KDE config file
00090     if (!locate("config",f).isEmpty())
00091         state = true;
00092     else
00093     // otherwise check in standard UNIX config directories
00094     {
00095         const char* const *p = config_stddirs;
00096         while (*p)
00097         {
00098             if (KStandardDirs::exists(QString::fromLatin1(*p)+f))
00099             {
00100                 state = true;
00101                 break;
00102             }
00103             else
00104                 p++;
00105         }
00106     }
00107     return state;
00108 }
00109 
00110 bool KdeprintChecker::checkExec(const KURL& url)
00111 {
00112     QString execname(url.path().mid(1));
00113     return !(KStandardDirs::findExe(execname).isEmpty());
00114 }
00115 
00116 bool KdeprintChecker::checkService(const KURL& url)
00117 {
00118     QString serv(url.path().mid(1));
00119     KExtendedSocket sock;
00120 
00121     bool    ok;
00122     int port = serv.toInt(&ok);
00123 
00124     if (ok) sock.setAddress("localhost", port);
00125     else sock.setAddress("localhost", serv);
00126     return (sock.connect() == 0);
00127 }
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 Apr 21 18:44:34 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003