kdeprintcheck.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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
00092 QString f(url.path().mid(1));
00093 bool state(false);
00094
00095
00096 if (!locate("config",f).isEmpty())
00097 state = true;
00098 else
00099
00100 {
00101 const char* const *p = config_stddirs;
00102 while (*p)
00103 {
00104
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 }
|