~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to solid/wicd/networkinterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the KDE project
 
2
    Copyright (C) 2008 Dario Freddi <drf54321@gmail.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License version 2 as published by the Free Software Foundation.
 
7
 
 
8
    This library is distributed in the hope that it will be useful,
 
9
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
    Library General Public License for more details.
 
12
 
 
13
    You should have received a copy of the GNU Library General Public License
 
14
    along with this library; see the file COPYING.LIB.  If not, write to
 
15
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
16
    Boston, MA 02110-1301, USA.
 
17
 
 
18
*/
 
19
 
 
20
#include "networkinterface.h"
 
21
 
 
22
#include <QProcess>
 
23
 
 
24
class WicdNetworkInterfacePrivate
 
25
{
 
26
public:
 
27
    WicdNetworkInterfacePrivate(const QString &name);
 
28
 
 
29
    quint32 parseIPv4Address(const QString & addressString);
 
30
 
 
31
    QString name;
 
32
 
 
33
};
 
34
 
 
35
WicdNetworkInterfacePrivate::WicdNetworkInterfacePrivate(const QString &n)
 
36
        : name(n)
 
37
{
 
38
}
 
39
 
 
40
quint32 WicdNetworkInterfacePrivate::parseIPv4Address(const QString & addressString)
 
41
{
 
42
    const QStringList parts = addressString.split(QChar::fromLatin1('.'), QString::SkipEmptyParts);
 
43
    if (parts.count() != 4)
 
44
        return 0;
 
45
 
 
46
    quint32 address = 0;
 
47
    for (int i = 0; i < 4; ++i) {
 
48
        bool ok = false;
 
49
        const short value = parts.at(i).toShort(&ok);
 
50
        if (value < 0 || value > 255)
 
51
            return 0;
 
52
        address |= (value << ((3 - i) * 8));
 
53
    }
 
54
    return address;
 
55
}
 
56
 
 
57
WicdNetworkInterface::WicdNetworkInterface(const QString &name)
 
58
        : NetworkInterface(), d(new WicdNetworkInterfacePrivate(name))
 
59
{
 
60
 
 
61
}
 
62
 
 
63
WicdNetworkInterface::~WicdNetworkInterface()
 
64
{
 
65
    delete d;
 
66
}
 
67
 
 
68
QString WicdNetworkInterface::interfaceName() const
 
69
{
 
70
    return d->name;
 
71
}
 
72
 
 
73
QString WicdNetworkInterface::ipInterfaceName() const
 
74
{
 
75
    return d->name;
 
76
}
 
77
 
 
78
bool WicdNetworkInterface::firmwareMissing() const
 
79
{
 
80
    // TODO: check if wicd can detect firmware missing.
 
81
    return false;
 
82
}
 
83
 
 
84
Solid::Control::IPv4Config WicdNetworkInterface::ipV4Config() const
 
85
{
 
86
    // Let's parse ifconfig here
 
87
 
 
88
    QProcess ifconfig;
 
89
 
 
90
    ifconfig.setEnvironment(QStringList() << QProcess::systemEnvironment() << "LANG=C");
 
91
    ifconfig.start(QString("ifconfig %1").arg(d->name));
 
92
    ifconfig.waitForFinished();
 
93
 
 
94
    QString result = ifconfig.readAllStandardOutput();
 
95
 
 
96
    QStringList lines = result.split('\n');
 
97
 
 
98
    if (!result.contains("inet addr:")) {
 
99
        return Solid::Control::IPv4Config(
 
100
                   QList<Solid::Control::IPv4Address>(),
 
101
                   QList<quint32>() /*nameservers*/,
 
102
                   QStringList() /* domains */,
 
103
                   QList<Solid::Control::IPv4Route>() /* routes*/);
 
104
    }
 
105
    QString inetadd = lines.at(1).split("inet addr:").at(1).split(' ').at(0);
 
106
    QString bcast = lines.at(1).split("Bcast:").at(1).split(' ').at(0);
 
107
    QString mask = lines.at(1).split("Mask:").at(1);
 
108
 
 
109
    Solid::Control::IPv4Address address(
 
110
        d->parseIPv4Address(inetadd),
 
111
        d->parseIPv4Address(mask),
 
112
        d->parseIPv4Address(bcast));
 
113
 
 
114
    QList<quint32> dnsServers;
 
115
    dnsServers.append(d->parseIPv4Address(bcast));
 
116
 
 
117
    return Solid::Control::IPv4Config(
 
118
               QList<Solid::Control::IPv4Address>() << address,
 
119
               dnsServers /*nameservers*/,
 
120
               QStringList() /* domains */,
 
121
               QList<Solid::Control::IPv4Route>() /* routes*/);
 
122
}
 
123
 
 
124
QString WicdNetworkInterface::uni() const
 
125
{
 
126
    return d->name;
 
127
}
 
128
 
 
129
QString WicdNetworkInterface::udi() const
 
130
{
 
131
    return d->name;
 
132
}
 
133
 
 
134
int WicdNetworkInterface::designSpeed() const
 
135
{
 
136
    return 0;
 
137
}
 
138
 
 
139
bool WicdNetworkInterface::activateConnection(const QString &, const QVariantMap &)
 
140
{
 
141
    return false;
 
142
}
 
143
 
 
144
bool WicdNetworkInterface::deactivateConnection()
 
145
{
 
146
    return false;
 
147
}
 
148
 
 
149
void WicdNetworkInterface::disconnectInterface()
 
150
{
 
151
}
 
152
 
 
153
#include "networkinterface.moc"