kio Library API Documentation

kpac_discovery.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (c) 2001 Malte Starostik <malte@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 // $Id: kpac_discovery.cpp,v 1.4.4.2 2003/01/17 05:02:30 adawit Exp $
00020 
00021 #include <unistd.h>
00022 
00023 #include <kapplication.h>
00024 #include <kdebug.h>
00025 #include <kprocess.h>
00026 
00027 #include "kpac_discovery.moc"
00028 
00029 KPACDiscovery::KPACDiscovery()
00030     : QObject(),
00031       m_stage(DHCP)
00032 {
00033     char hostname[256];
00034     if (gethostname(hostname, 255) == 0)
00035         m_hostname = hostname;
00036 }
00037 
00038 bool KPACDiscovery::tryDiscovery()
00039 {
00040     m_curl = KURL();
00041     switch (m_stage)
00042     {
00043         case DHCP:
00044         {
00045             m_stage = DNSAlias;
00046             KProcess proc;
00047             proc << "kpac_dhcp_helper";
00048             connect(&proc, SIGNAL(receivedStdout(KProcess *, char *, int)),
00049                 SLOT(slotDHCPData(KProcess *, char *, int)));
00050             connect(&proc, SIGNAL(processExited(KProcess *)), SLOT(slotDone()));
00051             m_data = 0;
00052             kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DHCP" << endl;
00053             if ((m_working = proc.start(KProcess::NotifyOnExit, KProcess::Stdout)))
00054             {
00055                 loop();
00056                 if (proc.normalExit() && proc.exitStatus() == 0)
00057                 {
00058                     m_curl = m_data.data();
00059                     m_data = 0;
00060                     kdDebug(7025) << "KPACDiscovery::tryDiscovery(): found " << m_curl.prettyURL() << " via DHCP" << endl;
00061                     return true;
00062                 }
00063             }
00064         } // Fallthrough
00065         case DNSAlias:
00066         {
00067             kdDebug(7025) << "KPACDiscovery::tryDiscovery(): trying DNS Alias" << endl;
00068             if (m_hostname.isEmpty())
00069                 return false;
00070             else if (m_data.isEmpty())
00071                 m_data = m_hostname;
00072             int pos = m_data.find('.');
00073             if (pos == -1)
00074                 return false;
00075             m_data.remove(0, pos + 1); // strip till first dot inclusive
00076             if (m_data.find('.') == -1) // only TLD left, bail out
00077                 return false;
00078             m_curl.setProtocol("http");
00079             m_curl.setHost("wpad." + m_data + ".");
00080             m_curl.setPath("/wpad.dat");
00081             kdDebug(7025) << "KPACDiscovery::tryDiscovery(): returning " << m_curl.prettyURL() << endl;
00082             return true;
00083         }    
00084         default:
00085             return false;
00086     }
00087 }
00088 
00089 void KPACDiscovery::slotDHCPData(KProcess *, char *buffer, int len)
00090 {
00091     m_data += QCString(buffer, len);
00092 }
00093 
00094 void KPACDiscovery::slotDone()
00095 {
00096     m_working = false;
00097 }
00098 
00099 void KPACDiscovery::loop()
00100 {
00101     while (m_working)
00102         kapp->processOneEvent();
00103 }
00104 
00105 // vim: ts=4 sw=4 et
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:31 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001