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., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, 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 <kdebug.h> 00043 #include <kextsock.h> 00044 #include <qfile.h> 00045 #include <unistd.h> 00046 00047 static const char* const config_stddirs[] = { 00048 "/etc/", 00049 "/usr/etc/", 00050 "/usr/local/etc/", 00051 "/opt/etc/", 00052 "/opt/local/etc/", 00053 0 00054 }; 00055 00056 bool KdeprintChecker::check(KConfig *conf, const QString& group) 00057 { 00058 if (!group.isEmpty()) 00059 conf->setGroup(group); 00060 QStringList uris = conf->readListEntry("Require"); 00061 return check(uris); 00062 } 00063 00064 bool KdeprintChecker::check(const QStringList& uris) 00065 { 00066 bool state(true); 00067 for (QStringList::ConstIterator it=uris.begin(); it!=uris.end() && state; ++it) 00068 { 00069 state = (state && checkURL(KURL(*it))); 00070 // kdDebug( 500 ) << "auto-detection uri=" << *it << ", state=" << state << endl; 00071 } 00072 return state; 00073 } 00074 00075 bool KdeprintChecker::checkURL(const KURL& url) 00076 { 00077 QString prot(url.protocol()); 00078 if (prot == "config") 00079 return checkConfig(url); 00080 else if (prot == "exec") 00081 return checkExec(url); 00082 else if (prot == "file" || prot == "dir") 00083 return KStandardDirs::exists(url.url()); 00084 else if (prot == "service") 00085 return checkService(url); 00086 return false; 00087 } 00088 00089 bool KdeprintChecker::checkConfig(const KURL& url) 00090 { 00091 // get the config filename (may contain a path) 00092 QString f(url.path().mid(1)); 00093 bool state(false); 00094 00095 // first check for standard KDE config file 00096 if (!locate("config",f).isEmpty()) 00097 state = true; 00098 else 00099 // otherwise check in standard UNIX config directories 00100 { 00101 const char* const *p = config_stddirs; 00102 while (*p) 00103 { 00104 // kdDebug( 500 ) << "checkConfig() with " << QString::fromLatin1( *p ) + f << endl; 00105 if ( QFile::exists( QString::fromLatin1( *p ) + f ) ) 00106 { 00107 state = true; 00108 break; 00109 } 00110 else 00111 p++; 00112 } 00113 } 00114 return state; 00115 } 00116 00117 bool KdeprintChecker::checkExec(const KURL& url) 00118 { 00119 QString execname(url.path().mid(1)); 00120 return !(KStandardDirs::findExe(execname).isEmpty()); 00121 } 00122 00123 bool KdeprintChecker::checkService(const KURL& url) 00124 { 00125 QString serv(url.path().mid(1)); 00126 KExtendedSocket sock; 00127 00128 bool ok; 00129 int port = serv.toInt(&ok); 00130 00131 if (ok) sock.setAddress("localhost", port); 00132 else sock.setAddress("localhost", serv); 00133 return (sock.connect() == 0); 00134 }