00001
00002 #include "modem.h"
00003 #include "pppconfig.h"
00004 #include "pppmodule.h"
00005 #include "pppdata.h"
00006 #include "interfaceinformationppp.h"
00007 #include "interfaceppp.h"
00008
00009
00010 #include <opie2/odebug.h>
00011 #include <qpe/config.h>
00012 #include <qpe/qpeapplication.h>
00013 using namespace Opie::Core;
00014
00015
00016 #include <qt.h>
00017
00018
00019 #include <errno.h>
00020 #include <signal.h>
00021
00022
00023 namespace
00024 {
00025
00026
00027
00028
00029
00030 struct Connection
00031 {
00032 pid_t pid;
00033 QString device;
00034 QString name;
00035 };
00036 class InterfaceKeeper
00037 {
00038 public:
00039 InterfaceKeeper();
00040 ~InterfaceKeeper();
00041
00042 void addInterface( pid_t, const QString& pppDev, const QString& name );
00043 QMap<QString, Connection> interfaces()const;
00044 private:
00045 bool isAvailable( pid_t )const;
00046 QMap<QString, Connection> m_interfaces;
00047 };
00048 }
00049
00050
00057 PPPModule::PPPModule() : Module()
00058 {
00059 InterfaceKeeper inFace;
00060 QMap<QString,Connection> running = inFace.interfaces();
00061 QStringList handledInterfaceNames;
00062
00063 QMap<QString,QString> ifaces = PPPData::getConfiguredInterfaces();
00064 QMap<QString,QString>::Iterator it;
00065 InterfacePPP *iface;
00066 odebug << "getting interfaces" << oendl;
00067 for( it = ifaces.begin(); it != ifaces.end(); ++it )
00068 {
00069 odebug << "ifaces " << it.key().latin1() << " " << it.data().latin1() << "" << oendl;
00070 iface = new InterfacePPP( 0, it.key() );
00071 iface->setHardwareName( it.data() );
00072 list.append( (Interface*)iface );
00073
00074
00075 if ( running.contains( it.data() ) )
00076 {
00077 odebug << "iface is running " << it.key().latin1() << "" << oendl;
00078 handledInterfaceNames << running[it.data()].device;
00079 iface->setStatus( true );
00080 iface->setPPPDpid( running[it.data()].pid );
00081 iface->modem()->setPPPDevice( running[it.data()].device );
00082 iface->refresh();
00083 }
00084 }
00085
00086 setHandledInterfaceNames( handledInterfaceNames );
00087 }
00088
00092 PPPModule::~PPPModule()
00093 {
00094 odebug << "PPPModule::~PPPModule() " << oendl;
00095 QMap<QString,QString> ifaces;
00096 InterfaceKeeper keeper;
00097 Interface *i;
00098 for ( i=list.first(); i != 0; i=list.next() )
00099 {
00100
00101 if ( i->getStatus() )
00102 {
00103 odebug << "Iface " << i->getHardwareName().latin1() << " is still up" << oendl;
00104 InterfacePPP* ppp = static_cast<InterfacePPP*>(i);
00105 keeper.addInterface( ppp->pppPID(), ppp->pppDev(), ppp->getHardwareName() );
00106 }
00107 ifaces.insert( i->getInterfaceName(), i->getHardwareName() );
00108 delete i;
00109 }
00110 PPPData::setConfiguredInterfaces( ifaces );
00111 }
00112
00116 void PPPModule::setProfile(const QString &newProfile)
00117 {
00118 profile = newProfile;
00119 }
00120
00126 QString PPPModule::getPixmapName(Interface* )
00127 {
00128 return "ppp";
00129 }
00130
00136 bool PPPModule::isOwner(Interface *i)
00137 {
00138 return list.find( i ) != -1;
00139 }
00140
00145 QWidget *PPPModule::configure(Interface *i)
00146 {
00147 odebug << "return ModemWidget" << oendl;
00148 PPPConfigWidget *pppconfig = new PPPConfigWidget( (InterfacePPP*)i,
00149 0, "PPPConfig", false,
00150 ::Qt::WDestructiveClose | ::Qt::WStyle_ContextHelp);
00151 return pppconfig;
00152 }
00153
00158 QWidget *PPPModule::information(Interface *i)
00159 {
00160
00161
00162
00163 return new InterfaceInformationPPP( 0, "InterfaceInformationPPP", i );
00164 }
00165
00171 QList<Interface> PPPModule::getInterfaces()
00172 {
00173
00174 odebug << "PPPModule::getInterfaces" << oendl;
00175 return list;
00176 }
00177
00184 Interface *PPPModule::addNewInterface(const QString &newInterface)
00185 {
00186 Q_CONST_UNUSED( newInterface )
00187
00188 InterfacePPP *ifaceppp;
00189 Interface *iface;
00190 ifaceppp = new InterfacePPP();
00191 PPPConfigWidget imp(ifaceppp, 0, "PPPConfigImp", true);
00192
00193 if( QPEApplication::execDialog( &imp ) == QDialog::Accepted )
00194 {
00195 iface = (InterfacePPP*) ifaceppp;
00196 iface->setModuleOwner( this );
00197 list.append( iface );
00198 return iface;
00199 }
00200 else
00201 {
00202 delete ifaceppp;
00203 iface = NULL;
00204 }
00205 return iface;
00206 }
00207
00212 bool PPPModule::remove(Interface *i)
00213 {
00214 return list.remove(i);
00215 }
00216
00217 void PPPModule::possibleNewInterfaces(QMap<QString, QString> &newIfaces)
00218 {
00219 newIfaces.insert(::QObject::tr("PPP") ,
00220 ::QObject::tr("generic ppp device"));
00221 }
00222
00223
00224
00225 namespace
00226 {
00227 InterfaceKeeper::InterfaceKeeper( )
00228 {}
00229 InterfaceKeeper::~InterfaceKeeper()
00230 {
00231 Config cfg("ppp_plugin_keeper");
00232 QStringList lst = cfg.groupList();
00233 for (QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00234 {
00235 Connection con;
00236 cfg.setGroup( (*it) );
00237 cfg.clearGroup();
00238 }
00239
00240 for (QMap<QString, Connection>::Iterator it = m_interfaces.begin(); it != m_interfaces.end(); ++it )
00241 {
00242 Connection con = it.data();
00243 cfg.setGroup( con.name );
00244 cfg.writeEntry( "pid", con.pid );
00245 cfg.writeEntry( "device", con.device );
00246 }
00247 }
00248 void InterfaceKeeper::addInterface(pid_t pid, const QString& dev, const QString& name )
00249 {
00250 Connection con;
00251 con.pid = pid;
00252 con.device = dev;
00253 con.name = name;
00254 m_interfaces.insert( name, con );
00255 }
00256 QMap<QString, Connection> InterfaceKeeper::interfaces()const
00257 {
00258 Config cfg("ppp_plugin_keeper");
00259 QMap<QString, Connection> ifaces;
00260 QStringList lst = cfg.groupList();
00261 for (QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00262 {
00263 Connection con;
00264 cfg.setGroup( (*it) );
00265 con.name = (*it);
00266 con.pid = cfg.readNumEntry("pid");
00267 con.device = cfg.readEntry("device");
00268 odebug << " " << con.name.latin1() << " " << con.device.latin1() << " " << con.pid << "" << oendl;
00269
00270 if ( con.pid != -1 && isAvailable( con.pid ) )
00271 ifaces.insert( con.name, con );
00272 }
00273 return ifaces;
00274 }
00275 bool InterfaceKeeper::isAvailable( pid_t p)const
00276 {
00277 if (::kill(p, 0 ) == 0 || errno != ESRCH )
00278 {
00279 odebug << "isAvailable " << p << "" << oendl;
00280 return true;
00281 }
00282
00283 odebug << "notAvailable " << p << "" << oendl;
00284 return false;
00285 }
00286
00287 }