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

« back to all changes in this revision

Viewing changes to solid/solidshell/solid-network.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) 2006 Kevin Ottens <ervin@kde.org>
 
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 "solid-network.h"
 
21
 
 
22
 
 
23
#include <QString>
 
24
#include <QStringList>
 
25
#include <QMetaProperty>
 
26
#include <QMetaEnum>
 
27
#include <QTimer>
 
28
 
 
29
#include <kcomponentdata.h>
 
30
#include <kcmdlineargs.h>
 
31
#include <klocale.h>
 
32
#include <kdebug.h>
 
33
 
 
34
#include <solid/device.h>
 
35
#include <solid/genericinterface.h>
 
36
#include <solid/storageaccess.h>
 
37
#include <solid/opticaldrive.h>
 
38
 
 
39
#include <solid/control/ifaces/authentication.h>
 
40
#include <solid/control/networkmanager.h>
 
41
#include <solid/control/networkinterface.h>
 
42
#include <solid/control/wirednetworkinterface.h>
 
43
#include <solid/control/wirelessnetworkinterface.h>
 
44
#include <solid/control/wirelessaccesspoint.h>
 
45
 
 
46
#include <kjob.h>
 
47
 
 
48
 
 
49
#include <iostream>
 
50
using namespace std;
 
51
 
 
52
static const char appName[] = "solid-network";
 
53
static const char programName[] = I18N_NOOP("solid-network");
 
54
 
 
55
static const char description[] = I18N_NOOP("KDE tool for querying and controlling your network interfaces from the command line");
 
56
 
 
57
static const char version[] = "0.1";
 
58
 
 
59
std::ostream &operator<<(std::ostream &out, const QString &msg)
 
60
{
 
61
    return (out << msg.toLocal8Bit().constData());
 
62
}
 
63
 
 
64
std::ostream &operator<<(std::ostream &out, const QVariant &value)
 
65
{
 
66
    switch (value.type())
 
67
    {
 
68
    case QVariant::StringList:
 
69
    {
 
70
        out << "{";
 
71
 
 
72
        QStringList list = value.toStringList();
 
73
 
 
74
        QStringList::ConstIterator it = list.constBegin();
 
75
        QStringList::ConstIterator end = list.constEnd();
 
76
 
 
77
        for (; it!=end; ++it)
 
78
        {
 
79
            out << "'" << *it << "'";
 
80
 
 
81
            if (it+1!=end)
 
82
            {
 
83
                out << ", ";
 
84
            }
 
85
        }
 
86
 
 
87
        out << "}  (string list)";
 
88
        break;
 
89
    }
 
90
    case QVariant::Bool:
 
91
        out << (value.toBool()?"true":"false") << "  (bool)";
 
92
        break;
 
93
    case QVariant::Int:
 
94
        out << value.toString()
 
95
            << "  (0x" << QString::number(value.toInt(), 16) << ")  (int)";
 
96
        break;
 
97
    default:
 
98
        out << "'" << value.toString() << "'  (string)";
 
99
        break;
 
100
    }
 
101
 
 
102
    return out;
 
103
}
 
104
 
 
105
std::ostream &operator<<(std::ostream &out, const Solid::Device &device)
 
106
{
 
107
    out << "  parent = " << QVariant(device.parentUdi()) << endl;
 
108
    out << "  vendor = " << QVariant(device.vendor()) << endl;
 
109
    out << "  product = " << QVariant(device.product()) << endl;
 
110
 
 
111
    int index = Solid::DeviceInterface::staticMetaObject.indexOfEnumerator("Type");
 
112
    QMetaEnum typeEnum = Solid::DeviceInterface::staticMetaObject.enumerator(index);
 
113
 
 
114
    for (int i=0; i<typeEnum.keyCount(); i++)
 
115
    {
 
116
        Solid::DeviceInterface::Type type = (Solid::DeviceInterface::Type)typeEnum.value(i);
 
117
        const Solid::DeviceInterface *interface = device.asDeviceInterface(type);
 
118
 
 
119
        if (interface)
 
120
        {
 
121
            const QMetaObject *meta = interface->metaObject();
 
122
 
 
123
            for (int i=meta->propertyOffset(); i<meta->propertyCount(); i++)
 
124
            {
 
125
                QMetaProperty property = meta->property(i);
 
126
                out << "  " << QString(meta->className()).mid(7) << "." << property.name()
 
127
                    << " = ";
 
128
 
 
129
                QVariant value = property.read(interface);
 
130
 
 
131
                if (property.isEnumType()) {
 
132
                    QMetaEnum metaEnum = property.enumerator();
 
133
                    out << "'" << metaEnum.valueToKeys(value.toInt()).constData() << "'"
 
134
                        << "  (0x" << QString::number(value.toInt(), 16) << ")  ";
 
135
                    if (metaEnum.isFlag()) {
 
136
                        out << "(flag)";
 
137
                    } else {
 
138
                        out << "(enum)";
 
139
                    }
 
140
                    out << endl;
 
141
                } else {
 
142
                    out << value << endl;
 
143
                }
 
144
            }
 
145
        }
 
146
    }
 
147
 
 
148
    return out;
 
149
}
 
150
 
 
151
std::ostream &operator<<(std::ostream &out, const QMap<QString,QVariant> &properties)
 
152
{
 
153
    QMap<QString, QVariant>::ConstIterator it = properties.constBegin(), itEnd = properties.constEnd();
 
154
    for ( ; it != itEnd; ++it)
 
155
    {
 
156
        out << "  " << it.key() << " = " << it.value() << endl;
 
157
    }
 
158
 
 
159
    return out;
 
160
}
 
161
 
 
162
std::ostream &operator<<(std::ostream &out, const Solid::Control::NetworkInterface &networkdevice)
 
163
{
 
164
    out << "  UNI =                " << QVariant(networkdevice.uni()) << endl;
 
165
    out << "  Type =               " << (networkdevice.type() == Solid::Control::NetworkInterface::Ieee8023 ? "Wired" : "802.11 Wireless") << endl;
 
166
    out << "  Active =             " << (networkdevice.isActive() ? "Yes" : "No") << endl;
 
167
    out << "  Interface Name =     " << networkdevice.interfaceName() << endl;
 
168
    out << "  Driver =             " << networkdevice.driver() << endl;
 
169
    //out << "  HW Address =         " << networkdevice.  // TODO add to solid API.
 
170
    out << "\n  Capabilities:" << endl;
 
171
    out << "    Supported =        " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::IsManageable ? "Yes" : "No") << endl;
 
172
    out << "    Speed =            " << networkdevice.designSpeed() << endl;
 
173
#if 0
 
174
    if (networkdevice.type() == Solid::Control::NetworkInterface::Ieee8023) {
 
175
 
 
176
        out << "    Carrier Detect =   " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::SupportsCarrierDetect ? "Yes" : "No") << endl;
 
177
    }
 
178
    else {
 
179
        out << "    Wireless Scan =    " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::SupportsWirelessScan ? "Yes" : "No") << endl;
 
180
    }
 
181
    out << "    Link Up =          " << (networkdevice.isLinkUp() ? "Yes" : "No") << endl;
 
182
#endif
 
183
 
 
184
    return out;
 
185
}
 
186
 
 
187
std::ostream &operator<<(std::ostream &out, const Solid::Control::AccessPoint &ap)
 
188
{
 
189
    out << "  UNI =                " << QVariant(ap.uni()) << endl;
 
190
    out << "  SSID =               " << QVariant(ap.ssid()) << endl;
 
191
    out << "  MAC Address =        " << QVariant(ap.hardwareAddress()) << endl;
 
192
    out << "  Frequency (MHz) =    " << ap.frequency() << endl;
 
193
    out << "  Max BitRate (Kb/s) = " << ap.maxBitRate() << endl;
 
194
    out << "  Signal Strength =    " << ap.signalStrength() << endl;
 
195
    out << "  Mode =               ";
 
196
    switch (ap.mode())
 
197
    {
 
198
    case Solid::Control::WirelessNetworkInterface::Unassociated:
 
199
        cout << "Unassociated" << endl;
 
200
        break;
 
201
    case Solid::Control::WirelessNetworkInterface::Adhoc:
 
202
        cout << "Ad-hoc" << endl;
 
203
        break;
 
204
    case Solid::Control::WirelessNetworkInterface::Managed:
 
205
        cout << "Infrastructure" << endl;
 
206
        break;
 
207
    case Solid::Control::WirelessNetworkInterface::Master:
 
208
        cout << "Master" << endl;
 
209
        break;
 
210
    case Solid::Control::WirelessNetworkInterface::Repeater:
 
211
        cout << "Repeater" << endl;
 
212
        break;
 
213
    default:
 
214
        cout << "Unknown" << endl;
 
215
        cerr << "Unknown network operation mode: " << ap.mode() << endl;
 
216
        break;
 
217
    }
 
218
    out << "  Capabilities =       ";
 
219
    const Solid::Control::AccessPoint::Capabilities cap = ap.capabilities();
 
220
    if (cap)
 
221
    {
 
222
        if (cap  & Solid::Control::AccessPoint::Privacy)
 
223
            out << "Privacy,";
 
224
        out << endl;
 
225
    }
 
226
    else
 
227
    {
 
228
        out << "(No Capabilities)" << endl;
 
229
    }
 
230
    out << "  WPA Options =        ";
 
231
    const Solid::Control::AccessPoint::WpaFlags wpaFlags = ap.wpaFlags();
 
232
    if (wpaFlags)
 
233
    {
 
234
        if (wpaFlags  & Solid::Control::AccessPoint::PairWep40)
 
235
            out << "PairWep40,";
 
236
        if (wpaFlags  & Solid::Control::AccessPoint::PairWep104)
 
237
            out << "PairWep104,";
 
238
        if (wpaFlags  & Solid::Control::AccessPoint::PairTkip)
 
239
            out << "PairTkip,";
 
240
        if (wpaFlags  & Solid::Control::AccessPoint::PairCcmp)
 
241
            out << "PairCcmp,";
 
242
        if (wpaFlags  & Solid::Control::AccessPoint::GroupWep40)
 
243
            out << "GroupWep40,";
 
244
        if (wpaFlags  & Solid::Control::AccessPoint::GroupWep104)
 
245
            out << "GroupWep104,";
 
246
        if (wpaFlags  & Solid::Control::AccessPoint::GroupTkip)
 
247
            out << "GroupTkip,";
 
248
        if (wpaFlags  & Solid::Control::AccessPoint::GroupCcmp)
 
249
            out << "GroupCcmp,";
 
250
        if (wpaFlags  & Solid::Control::AccessPoint::KeyMgmtPsk)
 
251
            out << "KeyMgmtPsk,";
 
252
        if (wpaFlags  & Solid::Control::AccessPoint::KeyMgmt8021x)
 
253
            out << "KeyMgmt8021x,";
 
254
        out << endl;
 
255
    }
 
256
    else
 
257
    {
 
258
        out << "(No Options)" << endl;
 
259
    }
 
260
    out << "  RSN Options =        ";
 
261
    const Solid::Control::AccessPoint::WpaFlags rsnFlags = ap.rsnFlags();
 
262
    if (rsnFlags)
 
263
    {
 
264
        if (rsnFlags  & Solid::Control::AccessPoint::PairWep40)
 
265
            out << "PairWep40,";
 
266
        if (rsnFlags  & Solid::Control::AccessPoint::PairWep104)
 
267
            out << "PairWep104,";
 
268
        if (rsnFlags  & Solid::Control::AccessPoint::PairTkip)
 
269
            out << "PairTkip,";
 
270
        if (rsnFlags  & Solid::Control::AccessPoint::PairCcmp)
 
271
            out << "PairCcmp,";
 
272
        if (rsnFlags  & Solid::Control::AccessPoint::GroupWep40)
 
273
            out << "GroupWep40,";
 
274
        if (rsnFlags  & Solid::Control::AccessPoint::GroupWep104)
 
275
            out << "GroupWep104,";
 
276
        if (rsnFlags  & Solid::Control::AccessPoint::GroupTkip)
 
277
            out << "GroupTkip,";
 
278
        if (rsnFlags  & Solid::Control::AccessPoint::GroupCcmp)
 
279
            out << "GroupCcmp,";
 
280
        if (rsnFlags  & Solid::Control::AccessPoint::KeyMgmtPsk)
 
281
            out << "KeyMgmtPsk,";
 
282
        if (rsnFlags  & Solid::Control::AccessPoint::KeyMgmt8021x)
 
283
            out << "KeyMgmt8021x,";
 
284
        out << endl;
 
285
    }
 
286
    else
 
287
    {
 
288
        out << "(No Options)" << endl;
 
289
    }
 
290
    return out;
 
291
}
 
292
 
 
293
std::ostream &operator<<(std::ostream &out, const Solid::Control::WirelessNetworkInterface &network)
 
294
{
 
295
    out << static_cast<const Solid::Control::NetworkInterface&>(network);
 
296
    out << endl;
 
297
    out << "  Mode =               ";
 
298
    switch (network.mode())
 
299
    {
 
300
    case Solid::Control::WirelessNetworkInterface::Unassociated:
 
301
        cout << "Unassociated" << endl;
 
302
        break;
 
303
    case Solid::Control::WirelessNetworkInterface::Adhoc:
 
304
        cout << "Ad-hoc" << endl;
 
305
        break;
 
306
    case Solid::Control::WirelessNetworkInterface::Managed:
 
307
        cout << "Infrastructure" << endl;
 
308
        break;
 
309
    case Solid::Control::WirelessNetworkInterface::Master:
 
310
        cout << "Master" << endl;
 
311
        break;
 
312
    case Solid::Control::WirelessNetworkInterface::Repeater:
 
313
        cout << "Repeater" << endl;
 
314
        break;
 
315
    default:
 
316
        cout << "Unknown" << endl;
 
317
        cerr << "Unknown network operation mode: " << network.mode() << endl;
 
318
        break;
 
319
    }
 
320
    out << "  Bit Rate =           " << network.bitRate() << endl;
 
321
    out << "  Hardware Address =   " << network.hardwareAddress() << endl;
 
322
    out << "  Active Access Point= " << qVariantFromValue(network.activeAccessPoint()) << endl;
 
323
    out << "  Capabilities =       ";
 
324
    const Solid::Control::WirelessNetworkInterface::Capabilities cap = network.wirelessCapabilities();
 
325
    if (cap)
 
326
    {
 
327
        if (cap  & Solid::Control::WirelessNetworkInterface::Wpa)
 
328
            out << "WPA,";
 
329
        if (cap  & Solid::Control::WirelessNetworkInterface::Wep40)
 
330
            out << "WEP40,";
 
331
        if (cap  & Solid::Control::WirelessNetworkInterface::Wep104)
 
332
            out << "WEP104,";
 
333
        if (cap  & Solid::Control::WirelessNetworkInterface::Tkip)
 
334
            out << "TKIP,";
 
335
        if (cap  & Solid::Control::WirelessNetworkInterface::Ccmp)
 
336
            out << "CCMP,";
 
337
        if (cap  & Solid::Control::WirelessNetworkInterface::Rsn)
 
338
            out << "RSN,";
 
339
        out << endl;
 
340
    }
 
341
    else
 
342
    {
 
343
        out << "(No Capabilities)" << endl;
 
344
    }
 
345
    return out;
 
346
}
 
347
 
 
348
std::ostream &operator<<(std::ostream &out, const Solid::Control::WiredNetworkInterface &network)
 
349
{
 
350
    out << static_cast<const Solid::Control::NetworkInterface&>(network);
 
351
    out << endl;
 
352
    out << "  Hardware Address =   " << network.hardwareAddress() << endl;
 
353
    out << "  Bit Rate =           " << network.bitRate() << endl;
 
354
    out << "  Carrier =            " << qVariantFromValue(network.carrier()) << endl;
 
355
    return out;
 
356
}
 
357
 
 
358
 
 
359
void checkArgumentCount(int min, int max)
 
360
{
 
361
    int count = KCmdLineArgs::parsedArgs()->count();
 
362
 
 
363
    if (count < min)
 
364
    {
 
365
        cerr << i18n("Syntax Error: Not enough arguments") << endl;
 
366
        ::exit(1);
 
367
    }
 
368
 
 
369
    if ((max > 0) && (count > max))
 
370
    {
 
371
        cerr << i18n("Syntax Error: Too many arguments") << endl;
 
372
        ::exit(1);
 
373
    }
 
374
}
 
375
 
 
376
int main(int argc, char **argv)
 
377
{
 
378
  KCmdLineArgs::init(argc, argv, appName, 0, ki18n(programName), version, ki18n(description), KCmdLineArgs::CmdLineArgNone);
 
379
 
 
380
 
 
381
  KCmdLineOptions options;
 
382
 
 
383
  options.add("commands", ki18n("Show available commands by domains"));
 
384
 
 
385
  options.add("+command", ki18n("Command (see --commands)"));
 
386
 
 
387
  options.add("+[arg(s)]", ki18n("Arguments for command"));
 
388
 
 
389
  KCmdLineArgs::addCmdLineOptions(options);
 
390
 
 
391
  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
392
 
 
393
  KComponentData componentData(appName);
 
394
 
 
395
  if (args->isSet("commands"))
 
396
  {
 
397
      KCmdLineArgs::enable_i18n();
 
398
 
 
399
      cout << endl << i18n("Syntax:") << endl << endl;
 
400
 
 
401
      cout << "  solid-network listdevices" << endl;
 
402
      cout << i18n("             # List the network devices present.\n") << endl;
 
403
 
 
404
      cout << "  solid-network listnetworks 'uni'" << endl;
 
405
      cout << i18n("             # List the networks known to the device specified by 'uni'.\n") << endl;
 
406
 
 
407
      cout << "  solid-network query (status|wireless|wireless-hardware)|(interface 'uni')|(network 'device-uni' 'network-uni')" << endl;
 
408
      cout << i18n("             # Query whether networking features are active or not.\n"
 
409
                    "             # - If the 'status' option is given, return whether\n"
 
410
                    "             # networking is enabled for the system\n"
 
411
                    "             # - If the 'wireless' option is given, return whether\n"
 
412
                    "             # wireless is enabled for the system\n"
 
413
                    "             # - If the 'wireless-hardware' option is given,\n"
 
414
                    "             #  return whether the wireless hardware is enabled\n"
 
415
                    "             # - If the 'interface' option is given, print the\n"
 
416
                    "             # properties of the network interface that 'uni' refers to.\n"
 
417
                    "             # - If the 'network' option is given, print the\n"
 
418
                    "             # properties of the network on 'device-uni' that 'network-uni' refers to.\n") << endl;
 
419
 
 
420
      cout << "  solid-network set wireless (enabled|disabled)" << endl;
 
421
      cout << i18n("             # Enable or disable networking on this system.\n") << endl;
 
422
 
 
423
      cout << "  solid-network set networking (enabled|disabled)" << endl;
 
424
      cout << i18n("             # Enable or disable networking on this system.\n") << endl;
 
425
 
 
426
      cout << "  solid-network set network 'device-uni' 'network-uni' [authentication 'key']" << endl;
 
427
      cout << i18n("             # Activate the network 'network-uni' on 'device-uni'.\n"
 
428
                    "             # Optionally, use WEP128, open-system encryption with hex key 'key'. (Hardcoded)\n"
 
429
                    "             # Where 'authentication' is one of:\n"
 
430
                    "             # wep hex64|ascii64|hex128|ascii128|passphrase64|passphrase128 'key' [open|shared]\n"
 
431
                    "             # wpapsk wpa|wpa2 tkip|ccmp-aes password\n"
 
432
                    "             # wpaeap UNIMPLEMENTED IN SOLIDSHELL\n") << endl;
 
433
 
 
434
      cout << endl;
 
435
 
 
436
      return 0;
 
437
  }
 
438
 
 
439
  return SolidNetwork::doIt() ? 0 : 1;
 
440
}
 
441
 
 
442
bool SolidNetwork::doIt()
 
443
{
 
444
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
445
    checkArgumentCount(1, 0);
 
446
 
 
447
    QString command(args->arg(0));
 
448
 
 
449
    int fake_argc = 0;
 
450
    char **fake_argv = 0;
 
451
    SolidNetwork shell(fake_argc, fake_argv);
 
452
 
 
453
    if (command == "query")
 
454
    {
 
455
        checkArgumentCount(2, 4);
 
456
        QString what(args->arg(1));
 
457
        if (what == "status")
 
458
            return shell.netmgrNetworkingEnabled();
 
459
        else if (what == "wireless")
 
460
            return shell.netmgrWirelessEnabled();
 
461
        else if (what == "wireless-hardware")
 
462
            return shell.netmgrWirelessHardwareEnabled();
 
463
        else if (what == "interface")
 
464
        {
 
465
            checkArgumentCount(3, 3);
 
466
            QString uni(args->arg(2));
 
467
            return shell.netmgrQueryNetworkInterface(uni);
 
468
        }
 
469
        else if (what == "network")
 
470
        {
 
471
            checkArgumentCount(4, 4);
 
472
            QString dev(args->arg(2));
 
473
            QString uni(args->arg(3));
 
474
            return shell.netmgrQueryNetwork(dev, uni);
 
475
        }
 
476
        else
 
477
            cerr << i18n("Syntax Error: Unknown option '%1'", what) << endl;
 
478
    }
 
479
    else if (command == "set")
 
480
    {
 
481
        checkArgumentCount(3, 9);
 
482
        QString what(args->arg(1));
 
483
        QString how(args->arg(2));
 
484
        if (what == "networking")
 
485
        {
 
486
            bool enabled;
 
487
            if (how == "enabled")
 
488
            {
 
489
                enabled = true;
 
490
            }
 
491
            else if (how == "disabled")
 
492
            {
 
493
                enabled = false;
 
494
            }
 
495
            else
 
496
            {
 
497
                cerr << i18n("Syntax Error: Unknown option '%1'", how) << endl;
 
498
                return false;
 
499
            }
 
500
            shell.netmgrChangeNetworkingEnabled(enabled);
 
501
            return true;
 
502
        }
 
503
        else if (what == "wireless")
 
504
        {
 
505
            bool enabled;
 
506
            if (how == "enabled")
 
507
            {
 
508
                enabled = true;
 
509
            }
 
510
            else if (how == "disabled")
 
511
            {
 
512
                enabled = false;
 
513
            }
 
514
            else
 
515
            {
 
516
                cerr << i18n("Syntax Error: Unknown option '%1'", how) << endl;
 
517
                return false;
 
518
            }
 
519
            shell.netmgrChangeWirelessEnabled(enabled);
 
520
            return true;
 
521
        }
 
522
    /*cout << "  solid-network set network 'device-uni' 'network-uni' [authentication 'key']" << endl; */
 
523
        /*wep hex64|ascii64|hex128|ascii128|passphrase 'key' [open|shared] */
 
524
        /* wpaeap UNIMPLEMENTED */
 
525
        else if (what == "network")
 
526
        {
 
527
            cerr << i18n("Not implemented");
 
528
#if 0 // probably won't be reimplemented since solidshell can't provide a persistent settings service...
 
529
            checkArgumentCount(4, 9);
 
530
            QString dev(args->arg(2));
 
531
            QString uni(args->arg(3));
 
532
            Solid::Control::Authentication * auth = 0;
 
533
            QMap<QString,QString> secrets;
 
534
 
 
535
            if (KCmdLineArgs::parsedArgs()->count() > 4)
 
536
            {
 
537
                QString hasAuth = args->arg(4);
 
538
                if (hasAuth == "authentication")
 
539
                {
 
540
                    //encrypted network
 
541
                    QString authScheme = args->arg(5);
 
542
                    if (authScheme == "wep")
 
543
                    {
 
544
                        Solid::Control::AuthenticationWep *wepAuth = new Solid::Control::AuthenticationWep();
 
545
                        QString keyType = args->arg(6);
 
546
                        if (keyType == "hex64")
 
547
                        {
 
548
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepHex);
 
549
                            wepAuth->setKeyLength(64);
 
550
                        }
 
551
                        else if (keyType == "ascii64")
 
552
                        {
 
553
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepAscii);
 
554
                            wepAuth->setKeyLength(64);
 
555
                        }
 
556
                        else if (keyType == "hex128")
 
557
                        {
 
558
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepHex);
 
559
                            wepAuth->setKeyLength(128);
 
560
                        }
 
561
                        else if (keyType == "ascii128")
 
562
                        {
 
563
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepAscii);
 
564
                            wepAuth->setKeyLength(128);
 
565
                        }
 
566
                        else if (keyType == "passphrase64")
 
567
                        {
 
568
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepPassphrase);
 
569
                            wepAuth->setKeyLength(64);
 
570
                        }
 
571
                        else if (keyType == "passphrase128")
 
572
                        {
 
573
                            wepAuth->setType(Solid::Control::AuthenticationWep::WepPassphrase);
 
574
                            wepAuth->setKeyLength(128);
 
575
                        }
 
576
                        else
 
577
                        {
 
578
                            cerr << i18n("Unrecognised WEP type '%1'", keyType) << endl;
 
579
                            delete wepAuth;
 
580
                            return false;
 
581
                        }
 
582
    
 
583
                        QString key = args->arg(7);
 
584
                        secrets.insert("key", key);
 
585
                        wepAuth->setSecrets(secrets);
 
586
    
 
587
                        QString method = args->arg(8);
 
588
                        if (method == "open")
 
589
                            wepAuth->setMethod(Solid::Control::AuthenticationWep::WepOpenSystem);
 
590
                        else if (method == "shared")
 
591
                            wepAuth->setMethod(Solid::Control::AuthenticationWep::WepSharedKey);
 
592
                        else
 
593
                        {
 
594
                            cerr << i18n("Unrecognised WEP method '%1'", method) << endl;
 
595
                            delete wepAuth;
 
596
                            return false;
 
597
                        }
 
598
                        auth = wepAuth;
 
599
                    }
 
600
                    else if (authScheme == "wpapsk")
 
601
                    {
 
602
                        /* wpapsk wpa|wpa2 tkip|ccmp-aes password */
 
603
                        Solid::Control::AuthenticationWpaPersonal *wpapAuth = new Solid::Control::AuthenticationWpaPersonal();
 
604
                        QString version = args->arg(6);
 
605
                        if (version == "wpa")
 
606
                            wpapAuth->setVersion(Solid::Control::AuthenticationWpaPersonal::Wpa1);
 
607
                        else if (version == "wpa2")
 
608
                            wpapAuth->setVersion(Solid::Control::AuthenticationWpaPersonal::Wpa1);
 
609
                        else
 
610
                        {
 
611
                            cerr << i18n("Unrecognised WPA version '%1'", version) << endl;
 
612
                            delete wpapAuth;
 
613
                            return false;
 
614
                        }
 
615
                        QString protocol = args->arg(7);
 
616
                        if (protocol == "tkip")
 
617
                            wpapAuth->setProtocol(Solid::Control::AuthenticationWpaPersonal::WpaTkip);
 
618
                        else if (protocol == "ccmp-aes")
 
619
                            wpapAuth->setProtocol(Solid::Control::AuthenticationWpaPersonal::WpaCcmpAes);
 
620
                        else
 
621
                        {
 
622
                            cerr << i18n("Unrecognised WPA encryption protocol '%1'", protocol) << endl;
 
623
                            delete wpapAuth;
 
624
                            return false;
 
625
                        }
 
626
                        QString key = args->arg(8);
 
627
                        secrets.insert("key", key);
 
628
                        wpapAuth->setSecrets(secrets);
 
629
                        auth = wpapAuth;
 
630
                    }
 
631
                    else
 
632
                    {
 
633
                        cerr << i18n("Unimplemented auth scheme '%1'", args->arg(5)) << endl;
 
634
                        return false;
 
635
                    }
 
636
                }
 
637
            }
 
638
            else
 
639
            {
 
640
                //unencrypted network
 
641
                auth = new Solid::Control::AuthenticationNone;
 
642
            }
 
643
 
 
644
            return shell.netmgrActivateNetwork(dev, uni, auth);
 
645
            delete auth;
 
646
#endif
 
647
        }
 
648
        else
 
649
        {
 
650
            cerr << i18n("Syntax Error: Unknown object '%1'", what) << endl;
 
651
            return false;
 
652
        }
 
653
    }
 
654
    else if (command == "listdevices")
 
655
    {
 
656
        return shell.netmgrList();
 
657
    }
 
658
    else if (command == "listnetworks")
 
659
    {
 
660
        checkArgumentCount(2, 2);
 
661
        QString device(args->arg(1));
 
662
        return shell.netmgrListNetworks(device);
 
663
    }
 
664
    else
 
665
    {
 
666
        cerr << i18n("Syntax Error: Unknown command '%1'" , command) << endl;
 
667
    }
 
668
 
 
669
    return false;
 
670
}
 
671
 
 
672
bool SolidNetwork::netmgrNetworkingEnabled()
 
673
{
 
674
    if (Solid::Control::NetworkManager::isNetworkingEnabled())
 
675
        cout << i18n("networking: is enabled")<< endl;
 
676
    else
 
677
        cout << i18n("networking: is not enabled")<< endl;
 
678
    return Solid::Control::NetworkManager::isNetworkingEnabled();
 
679
}
 
680
 
 
681
bool SolidNetwork::netmgrWirelessEnabled()
 
682
{
 
683
    if (Solid::Control::NetworkManager::isWirelessEnabled())
 
684
        cout << i18n("wireless: is enabled")<< endl;
 
685
    else
 
686
        cout << i18n("wireless: is not enabled")<< endl;
 
687
    return Solid::Control::NetworkManager::isWirelessEnabled();
 
688
}
 
689
 
 
690
bool SolidNetwork::netmgrWirelessHardwareEnabled()
 
691
{
 
692
    if (Solid::Control::NetworkManager::isWirelessHardwareEnabled())
 
693
        cout << i18n("wireless hardware: is enabled")<< endl;
 
694
    else
 
695
        cout << i18n("wireless hardware: is not enabled")<< endl;
 
696
    return Solid::Control::NetworkManager::isWirelessHardwareEnabled();
 
697
}
 
698
 
 
699
bool SolidNetwork::netmgrChangeNetworkingEnabled(bool enabled)
 
700
{
 
701
    Solid::Control::NetworkManager::setNetworkingEnabled(enabled);
 
702
    return true;
 
703
}
 
704
 
 
705
bool SolidNetwork::netmgrChangeWirelessEnabled(bool enabled)
 
706
{
 
707
    Solid::Control::NetworkManager::setWirelessEnabled(enabled);
 
708
    return true;
 
709
}
 
710
 
 
711
bool SolidNetwork::netmgrList()
 
712
{
 
713
    const Solid::Control::NetworkInterfaceList all = Solid::Control::NetworkManager::networkInterfaces();
 
714
 
 
715
    cerr << "debug: network interface list contains: " << all.count() << " entries" << endl;
 
716
    foreach (const Solid::Control::NetworkInterface *device, all)
 
717
    {
 
718
        cout << "UNI = '" << device->uni() << "'" << endl;
 
719
    }
 
720
    return true;
 
721
}
 
722
 
 
723
bool SolidNetwork::netmgrListNetworks(const QString  & deviceUni)
 
724
{
 
725
    Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
 
726
    Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device );
 
727
    if (wifiDev) {
 
728
 
 
729
        Solid::Control::AccessPointList aps = wifiDev->accessPoints();
 
730
        foreach (const QString &apUni, aps)
 
731
        {
 
732
            cout << "NETWORK UNI = '" << apUni << "'" << endl;
 
733
        }
 
734
 
 
735
        return true;
 
736
    }
 
737
    return false;
 
738
}
 
739
 
 
740
bool SolidNetwork::netmgrQueryNetworkInterface(const QString  & deviceUni)
 
741
{
 
742
    cerr << "SolidNetwork::netmgrQueryNetworkInterface()" << endl;
 
743
    Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
 
744
    if (!device) {
 
745
        cerr << "No such interface: " << deviceUni << endl;
 
746
        return false;
 
747
    }
 
748
    Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device);
 
749
    Solid::Control::WiredNetworkInterface * wiredDev =  qobject_cast<Solid::Control::WiredNetworkInterface *>(device);
 
750
    if (wifiDev) {
 
751
        cout << *wifiDev << endl;
 
752
    } else if (wiredDev) {
 
753
        cout << *wiredDev << endl;
 
754
    } else {
 
755
        cout << *device << endl;
 
756
    }
 
757
    return true;
 
758
}
 
759
 
 
760
bool SolidNetwork::netmgrQueryNetwork(const QString  & deviceUni, const QString  & apUni)
 
761
{
 
762
    cerr << "SolidNetwork::netmgrQueryNetwork()" << endl;
 
763
    Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
 
764
    Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device );
 
765
    if (wifiDev) {
 
766
        Solid::Control::AccessPoint * ap = wifiDev->findAccessPoint( apUni );
 
767
        if ( ap ) {
 
768
            cout << *ap << endl;
 
769
            return true;
 
770
        }
 
771
    }
 
772
    return false;
 
773
}
 
774
 
 
775
#if 0
 
776
bool SolidNetwork::netmgrActivateNetwork(const QString  & deviceUni, const QString  & networkUni, Solid::Control::Authentication * auth)
 
777
{
 
778
    Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
 
779
    Solid::Control::Network * network = device.findNetwork(networkUni);
 
780
    Solid::Control::WirelessNetwork * wlan = 0;
 
781
    if (( wlan = qobject_cast<Solid::Control::WirelessNetwork *>(network)))
 
782
    {
 
783
        wlan->setAuthentication(auth);
 
784
        wlan->setActivated(true);
 
785
    }
 
786
    else
 
787
        network->setActivated(true);
 
788
    return true;
 
789
}
 
790
#endif
 
791
 
 
792
void SolidNetwork::connectJob(KJob *job)
 
793
{
 
794
    connect(job, SIGNAL(result(KJob *)),
 
795
             this, SLOT(slotResult(KJob *)));
 
796
    connect(job, SIGNAL(percent(KJob *, unsigned long)),
 
797
             this, SLOT(slotPercent(KJob *, unsigned long)));
 
798
    connect(job, SIGNAL(infoMessage(KJob *, const QString &, const QString &)),
 
799
             this, SLOT(slotInfoMessage(KJob *, const QString &)));
 
800
}
 
801
 
 
802
void SolidNetwork::slotPercent(KJob */*job */, unsigned long percent)
 
803
{
 
804
    cout << i18n("Progress: %1%" , percent) << endl;
 
805
}
 
806
 
 
807
void SolidNetwork::slotInfoMessage(KJob */*job */, const QString &message)
 
808
{
 
809
    cout << i18n("Info: %1" , message) << endl;
 
810
}
 
811
 
 
812
void SolidNetwork::slotResult(KJob *job)
 
813
{
 
814
    m_error = 0;
 
815
 
 
816
    if (job->error())
 
817
    {
 
818
        m_error = job->error();
 
819
        m_errorString = job->errorString();
 
820
    }
 
821
 
 
822
    m_loop.exit();
 
823
}
 
824
 
 
825
void SolidNetwork::slotStorageResult(Solid::ErrorType error, const QVariant &errorData)
 
826
{
 
827
    if (error) {
 
828
        m_error = 1;
 
829
        m_errorString = errorData.toString();
 
830
    }
 
831
    m_loop.exit();
 
832
}
 
833
 
 
834
#include "solid-network.moc"