summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/ppp/pppmodule.cpp
blob: 2462fa435379112808c80d6474d002f701f87125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <errno.h>
#include <signal.h>


#include <qpe/config.h>

#include "modem.h"
#include "pppconfig.h"
#include "pppmodule.h"
#include "pppdata.h"
#include "interfaceinformationppp.h"
#include "interfaceppp.h"

// don't polute global namespace
namespace {
    /*
     * If network settings is qutting and we've ppp
     * devices open we need to save the pid_t the PPData
     * and the interface number
     */
    struct Connection {
        pid_t pid;
        QString device;
        QString name;
    };
    class InterfaceKeeper {
    public:
        InterfaceKeeper();
        ~InterfaceKeeper();

        void addInterface( pid_t, const QString& pppDev, const QString& name );
        QMap<QString, Connection> interfaces()const; // will check if still available
    private:
        bool isAvailable( pid_t )const;
        QMap<QString, Connection> m_interfaces;
    };
}


/**
 * Constructor, find all of the possible interfaces
 * We also need to restore the state.. it could be that
 * an interface was up while closing the application
 * we need to be able to shut it down...
 */
PPPModule::PPPModule() : Module()
{
    InterfaceKeeper inFace;
    QMap<QString,Connection> running = inFace.interfaces();
    QStringList handledInterfaceNames;

    QMap<QString,QString> ifaces = PPPData::getConfiguredInterfaces();
    QMap<QString,QString>::Iterator it;
    InterfacePPP *iface;
    qDebug("getting interfaces");
    for( it = ifaces.begin(); it != ifaces.end(); ++it ){
        qDebug("ifaces %s %s", it.key().latin1(), it.data().latin1() );
        iface = new InterfacePPP( 0, it.key() );
        iface->setHardwareName( it.data() );
        list.append( (Interface*)iface );

        // check if (*it) is one of the running ifaces
        if ( running.contains( it.data() ) ) {
            qDebug("iface is running %s", it.key().latin1() );
            handledInterfaceNames << running[it.data()].device;
            iface->setStatus( true );
            iface->setPPPDpid( running[it.data()].pid );
            iface->modem()->setPPPDevice( running[it.data()].device );
            iface->refresh();
        }
    }

    setHandledInterfaceNames( handledInterfaceNames );
}

/**
 * Delete any interfaces that we own.
 */
PPPModule::~PPPModule(){
    qDebug("PPPModule::~PPPModule() " );
    QMap<QString,QString> ifaces;
    InterfaceKeeper keeper;
    Interface *i;
    for ( i=list.first(); i != 0; i=list.next() ){
        /* if online save the state */
        if ( i->getStatus() ) {
            qDebug("Iface %s is still up", i->getHardwareName().latin1() );
            InterfacePPP* ppp = static_cast<InterfacePPP*>(i);
            keeper.addInterface( ppp->pppPID(), ppp->pppDev(), ppp->getHardwareName() );
        }
        ifaces.insert( i->getInterfaceName(), i->getHardwareName() );
        delete i;
    }
    PPPData::setConfiguredInterfaces( ifaces );
}

/**
 * Change the current profile
 */
void PPPModule::setProfile(const QString &newProfile){
  profile = newProfile;
}

/**
 * get the icon name for this device.
 * @param Interface* can be used in determining the icon.
 * @return QString the icon name (minus .png, .gif etc)
 */
QString PPPModule::getPixmapName(Interface* ){
  return "ppp";
}

/**
 * Check to see if the interface i is owned by this module.
 * @param Interface* interface to check against
 * @return bool true if i is owned by this module, false otherwise.
 */
bool PPPModule::isOwner(Interface *i){
    return list.find( i ) != -1;
}

/**
 * Create, and return the WLANConfigure Module
 * @return QWidget* pointer to this modules configure.
 */
QWidget *PPPModule::configure(Interface *i){
    qDebug("return ModemWidget");
    PPPConfigWidget *pppconfig = new PPPConfigWidget( (InterfacePPP*)i,
                                                      0, "PPPConfig", false,
                                                      Qt::WDestructiveClose | Qt::WStyle_ContextHelp);
    return pppconfig;
}

/**
 * Create, and return the Information Module
 * @return QWidget* pointer to this modules info.
 */
QWidget *PPPModule::information(Interface *i){
  // We don't have any advanced pppd information widget yet :-D
  // TODO ^

   return new InterfaceInformationPPP( 0, "InterfaceInformationPPP", i );
}

/**
 * Get all active (up or down) interfaces
 * @return QList<Interface> A list of interfaces that exsist that havn't
 * been called by isOwner()
 */
QList<Interface> PPPModule::getInterfaces(){
  // List all of the files in the peer directory
    qDebug("PPPModule::getInterfaces");
  return list;
}

/**
 * Attempt to add a new interface as defined by name
 * @param name the name of the type of interface that should be created given
 *  by possibleNewInterfaces();
 * @return Interface* NULL if it was unable to be created.
 */
Interface *PPPModule::addNewInterface(const QString &newInterface){

  InterfacePPP *ifaceppp;
  Interface *iface;
  ifaceppp = new InterfacePPP();
  PPPConfigWidget imp(ifaceppp, 0, "PPPConfigImp", true);
  imp.showMaximized();
  if(imp.exec() == QDialog::Accepted ){
      iface = (InterfacePPP*) ifaceppp;
      iface->setModuleOwner( this );
      list.append( iface );
      return iface;
  }else {
      delete ifaceppp;
      iface = NULL;
  }
  return iface;
}

/**
 * Attempts to remove the interface, doesn't delete i
 * @return bool true if successful, false otherwise.
 */
bool PPPModule::remove(Interface *i){
    return list.remove(i);
}

void PPPModule::possibleNewInterfaces(QMap<QString, QString> &newIfaces)
{
    newIfaces.insert(QObject::tr("PPP") ,
                     QObject::tr("generic ppp device"));
}



namespace {
    InterfaceKeeper::InterfaceKeeper( ) {
    }
    InterfaceKeeper::~InterfaceKeeper() {
        Config cfg("ppp_plugin_keeper");
        QStringList lst = cfg.groupList();
        for (QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
            Connection con;
            cfg.setGroup( (*it) );
            cfg.clearGroup();
        }

        for (QMap<QString, Connection>::Iterator it = m_interfaces.begin(); it != m_interfaces.end(); ++it ) {
            Connection con = it.data();
            cfg.setGroup( con.name );
            cfg.writeEntry( "pid", con.pid );
            cfg.writeEntry( "device", con.device );
        }
    }
    void InterfaceKeeper::addInterface(pid_t pid, const QString& dev, const QString& name ) {
        Connection con;
        con.pid = pid;
        con.device = dev;
        con.name = name;
        m_interfaces.insert( name, con );
    }
    QMap<QString, Connection> InterfaceKeeper::interfaces()const {
        Config cfg("ppp_plugin_keeper");
        QMap<QString, Connection> ifaces;
        QStringList lst = cfg.groupList();
        for (QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
            Connection con;
            cfg.setGroup( (*it) );
            con.name = (*it);
            con.pid = cfg.readNumEntry("pid");
            con.device = cfg.readEntry("device");
            qDebug(" %s %s %d", con.name.latin1(), con.device.latin1(),  con.pid );

            if ( con.pid != -1 && isAvailable( con.pid ) )
                ifaces.insert( con.name, con );
        }
        return ifaces;
    }
    bool InterfaceKeeper::isAvailable( pid_t p)const {
        if (::kill(p, 0 ) == 0 || errno != ESRCH ) {
            qDebug("isAvailable %d",  p);
            return true;
        }

        qDebug("notAvailable %d", p);
        return false;
    }

}