• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

SolidModules

solid-network.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2006 Kevin Ottens <ervin@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., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 
00018 */
00019 
00020 #include "solid-network.h"
00021 
00022 
00023 #include <QString>
00024 #include <QStringList>
00025 #include <QMetaProperty>
00026 #include <QMetaEnum>
00027 #include <QTimer>
00028 
00029 #include <kcomponentdata.h>
00030 #include <kcmdlineargs.h>
00031 #include <klocale.h>
00032 #include <k3socketaddress.h>
00033 #include <kdebug.h>
00034 
00035 #include <solid/device.h>
00036 #include <solid/genericinterface.h>
00037 #include <solid/storageaccess.h>
00038 #include <solid/opticaldrive.h>
00039 
00040 #include <solid/control/ifaces/authentication.h>
00041 #include <solid/control/networkmanager.h>
00042 #include <solid/control/networkinterface.h>
00043 #include <solid/control/wirednetworkinterface.h>
00044 #include <solid/control/wirelessnetworkinterface.h>
00045 #include <solid/control/wirelessaccesspoint.h>
00046 
00047 #include <kjob.h>
00048 
00049 
00050 #include <iostream>
00051 using namespace std;
00052 
00053 static const char appName[] = "solid-network";
00054 static const char programName[] = I18N_NOOP("solid-network");
00055 
00056 static const char description[] = I18N_NOOP("KDE tool for querying and controlling your network interfaces from the command line");
00057 
00058 static const char version[] = "0.1";
00059 
00060 std::ostream &operator<<(std::ostream &out, const QString &msg)
00061 {
00062     return (out << msg.toLocal8Bit().constData());
00063 }
00064 
00065 std::ostream &operator<<(std::ostream &out, const QVariant &value)
00066 {
00067     switch (value.type())
00068     {
00069     case QVariant::StringList:
00070     {
00071         out << "{";
00072 
00073         QStringList list = value.toStringList();
00074 
00075         QStringList::ConstIterator it = list.constBegin();
00076         QStringList::ConstIterator end = list.constEnd();
00077 
00078         for (; it!=end; ++it)
00079         {
00080             out << "'" << *it << "'";
00081 
00082             if (it+1!=end)
00083             {
00084                 out << ", ";
00085             }
00086         }
00087 
00088         out << "}  (string list)";
00089         break;
00090     }
00091     case QVariant::Bool:
00092         out << (value.toBool()?"true":"false") << "  (bool)";
00093         break;
00094     case QVariant::Int:
00095         out << value.toString()
00096             << "  (0x" << QString::number(value.toInt(), 16) << ")  (int)";
00097         break;
00098     default:
00099         out << "'" << value.toString() << "'  (string)";
00100         break;
00101     }
00102 
00103     return out;
00104 }
00105 
00106 std::ostream &operator<<(std::ostream &out, const Solid::Device &device)
00107 {
00108     out << "  parent = " << QVariant(device.parentUdi()) << endl;
00109     out << "  vendor = " << QVariant(device.vendor()) << endl;
00110     out << "  product = " << QVariant(device.product()) << endl;
00111 
00112     int index = Solid::DeviceInterface::staticMetaObject.indexOfEnumerator("Type");
00113     QMetaEnum typeEnum = Solid::DeviceInterface::staticMetaObject.enumerator(index);
00114 
00115     for (int i=0; i<typeEnum.keyCount(); i++)
00116     {
00117         Solid::DeviceInterface::Type type = (Solid::DeviceInterface::Type)typeEnum.value(i);
00118         const Solid::DeviceInterface *interface = device.asDeviceInterface(type);
00119 
00120         if (interface)
00121         {
00122             const QMetaObject *meta = interface->metaObject();
00123 
00124             for (int i=meta->propertyOffset(); i<meta->propertyCount(); i++)
00125             {
00126                 QMetaProperty property = meta->property(i);
00127                 out << "  " << QString(meta->className()).mid(7) << "." << property.name()
00128                     << " = ";
00129 
00130                 QVariant value = property.read(interface);
00131 
00132                 if (property.isEnumType()) {
00133                     QMetaEnum metaEnum = property.enumerator();
00134                     out << "'" << metaEnum.valueToKeys(value.toInt()).constData() << "'"
00135                         << "  (0x" << QString::number(value.toInt(), 16) << ")  ";
00136                     if (metaEnum.isFlag()) {
00137                         out << "(flag)";
00138                     } else {
00139                         out << "(enum)";
00140                     }
00141                     out << endl;
00142                 } else {
00143                     out << value << endl;
00144                 }
00145             }
00146         }
00147     }
00148 
00149     return out;
00150 }
00151 
00152 std::ostream &operator<<(std::ostream &out, const QMap<QString,QVariant> &properties)
00153 {
00154     QMap<QString, QVariant>::ConstIterator it = properties.constBegin(), itEnd = properties.constEnd();
00155     for ( ; it != itEnd; ++it)
00156     {
00157         out << "  " << it.key() << " = " << it.value() << endl;
00158     }
00159 
00160     return out;
00161 }
00162 
00163 std::ostream &operator<<(std::ostream &out, const Solid::Control::NetworkInterface &networkdevice)
00164 {
00165     out << "  UNI =                " << QVariant(networkdevice.uni()) << endl;
00166     out << "  Type =               " << (networkdevice.type() == Solid::Control::NetworkInterface::Ieee8023 ? "Wired" : "802.11 Wireless") << endl;
00167     out << "  Active =             " << (networkdevice.isActive() ? "Yes" : "No") << endl;
00168     out << "  Interface Name =     " << networkdevice.interfaceName() << endl;
00169     out << "  Driver =             " << networkdevice.driver() << endl;
00170     //out << "  HW Address =         " << networkdevice.  // TODO add to solid API.
00171     out << "\n  Capabilities:" << endl;
00172     out << "    Supported =        " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::IsManageable ? "Yes" : "No") << endl;
00173     out << "    Speed =            " << networkdevice.designSpeed() << endl;
00174 #if 0
00175     if (networkdevice.type() == Solid::Control::NetworkInterface::Ieee8023) {
00176 
00177         out << "    Carrier Detect =   " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::SupportsCarrierDetect ? "Yes" : "No") << endl;
00178     }
00179     else {
00180         out << "    Wireless Scan =    " << (networkdevice.capabilities()  & Solid::Control::NetworkInterface::SupportsWirelessScan ? "Yes" : "No") << endl;
00181     }
00182     out << "    Link Up =          " << (networkdevice.isLinkUp() ? "Yes" : "No") << endl;
00183 #endif
00184 
00185     return out;
00186 }
00187 
00188 std::ostream &operator<<(std::ostream &out, const Solid::Control::AccessPoint &ap)
00189 {
00190     out << "  UNI =                " << QVariant(ap.uni()) << endl;
00191     out << "  SSID =               " << QVariant(ap.ssid()) << endl;
00192     out << "  MAC Address =        " << QVariant(ap.hardwareAddress()) << endl;
00193     out << "  Frequency (MHz) =    " << ap.frequency() << endl;
00194     out << "  Max BitRate (Kb/s) = " << ap.maxBitRate() << endl;
00195     out << "  Signal Strength =    " << ap.signalStrength() << endl;
00196     out << "  Mode =               ";
00197     switch (ap.mode())
00198     {
00199     case Solid::Control::WirelessNetworkInterface::Unassociated:
00200         cout << "Unassociated" << endl;
00201         break;
00202     case Solid::Control::WirelessNetworkInterface::Adhoc:
00203         cout << "Ad-hoc" << endl;
00204         break;
00205     case Solid::Control::WirelessNetworkInterface::Managed:
00206         cout << "Infrastructure" << endl;
00207         break;
00208     case Solid::Control::WirelessNetworkInterface::Master:
00209         cout << "Master" << endl;
00210         break;
00211     case Solid::Control::WirelessNetworkInterface::Repeater:
00212         cout << "Repeater" << endl;
00213         break;
00214     default:
00215         cout << "Unknown" << endl;
00216         cerr << "Unknown network operation mode: " << ap.mode() << endl;
00217         break;
00218     }
00219     out << "  Capabilities =       ";
00220     const Solid::Control::AccessPoint::Capabilities cap = ap.capabilities();
00221     if (cap)
00222     {
00223         if (cap  & Solid::Control::AccessPoint::Privacy)
00224             out << "Privacy,";
00225         out << endl;
00226     }
00227     else
00228     {
00229         out << "(No Capabilities)" << endl;
00230     }
00231     out << "  WPA Options =        ";
00232     const Solid::Control::AccessPoint::WpaFlags wpaFlags = ap.wpaFlags();
00233     if (wpaFlags)
00234     {
00235         if (wpaFlags  & Solid::Control::AccessPoint::PairWep40)
00236             out << "PairWep40,";
00237         if (wpaFlags  & Solid::Control::AccessPoint::PairWep104)
00238             out << "PairWep104,";
00239         if (wpaFlags  & Solid::Control::AccessPoint::PairTkip)
00240             out << "PairTkip,";
00241         if (wpaFlags  & Solid::Control::AccessPoint::PairCcmp)
00242             out << "PairCcmp,";
00243         if (wpaFlags  & Solid::Control::AccessPoint::GroupWep40)
00244             out << "GroupWep40,";
00245         if (wpaFlags  & Solid::Control::AccessPoint::GroupWep104)
00246             out << "GroupWep104,";
00247         if (wpaFlags  & Solid::Control::AccessPoint::GroupTkip)
00248             out << "GroupTkip,";
00249         if (wpaFlags  & Solid::Control::AccessPoint::GroupCcmp)
00250             out << "GroupCcmp,";
00251         if (wpaFlags  & Solid::Control::AccessPoint::KeyMgmtPsk)
00252             out << "KeyMgmtPsk,";
00253         if (wpaFlags  & Solid::Control::AccessPoint::KeyMgmt8021x)
00254             out << "KeyMgmt8021x,";
00255         out << endl;
00256     }
00257     else
00258     {
00259         out << "(No Options)" << endl;
00260     }
00261     out << "  RSN Options =        ";
00262     const Solid::Control::AccessPoint::WpaFlags rsnFlags = ap.rsnFlags();
00263     if (rsnFlags)
00264     {
00265         if (rsnFlags  & Solid::Control::AccessPoint::PairWep40)
00266             out << "PairWep40,";
00267         if (rsnFlags  & Solid::Control::AccessPoint::PairWep104)
00268             out << "PairWep104,";
00269         if (rsnFlags  & Solid::Control::AccessPoint::PairTkip)
00270             out << "PairTkip,";
00271         if (rsnFlags  & Solid::Control::AccessPoint::PairCcmp)
00272             out << "PairCcmp,";
00273         if (rsnFlags  & Solid::Control::AccessPoint::GroupWep40)
00274             out << "GroupWep40,";
00275         if (rsnFlags  & Solid::Control::AccessPoint::GroupWep104)
00276             out << "GroupWep104,";
00277         if (rsnFlags  & Solid::Control::AccessPoint::GroupTkip)
00278             out << "GroupTkip,";
00279         if (rsnFlags  & Solid::Control::AccessPoint::GroupCcmp)
00280             out << "GroupCcmp,";
00281         if (rsnFlags  & Solid::Control::AccessPoint::KeyMgmtPsk)
00282             out << "KeyMgmtPsk,";
00283         if (rsnFlags  & Solid::Control::AccessPoint::KeyMgmt8021x)
00284             out << "KeyMgmt8021x,";
00285         out << endl;
00286     }
00287     else
00288     {
00289         out << "(No Options)" << endl;
00290     }
00291     return out;
00292 }
00293 
00294 std::ostream &operator<<(std::ostream &out, const Solid::Control::WirelessNetworkInterface &network)
00295 {
00296     out << static_cast<const Solid::Control::NetworkInterface&>(network);
00297     out << endl;
00298     out << "  Mode =               ";
00299     switch (network.mode())
00300     {
00301     case Solid::Control::WirelessNetworkInterface::Unassociated:
00302         cout << "Unassociated" << endl;
00303         break;
00304     case Solid::Control::WirelessNetworkInterface::Adhoc:
00305         cout << "Ad-hoc" << endl;
00306         break;
00307     case Solid::Control::WirelessNetworkInterface::Managed:
00308         cout << "Infrastructure" << endl;
00309         break;
00310     case Solid::Control::WirelessNetworkInterface::Master:
00311         cout << "Master" << endl;
00312         break;
00313     case Solid::Control::WirelessNetworkInterface::Repeater:
00314         cout << "Repeater" << endl;
00315         break;
00316     default:
00317         cout << "Unknown" << endl;
00318         cerr << "Unknown network operation mode: " << network.mode() << endl;
00319         break;
00320     }
00321     out << "  Bit Rate =           " << network.bitRate() << endl;
00322     out << "  Hardware Address =   " << network.hardwareAddress() << endl;
00323     out << "  Active Access Point= " << qVariantFromValue(network.activeAccessPoint()) << endl;
00324     out << "  Capabilities =       ";
00325     const Solid::Control::WirelessNetworkInterface::Capabilities cap = network.wirelessCapabilities();
00326     if (cap)
00327     {
00328         if (cap  & Solid::Control::WirelessNetworkInterface::Wpa)
00329             out << "WPA,";
00330         if (cap  & Solid::Control::WirelessNetworkInterface::Wep40)
00331             out << "WEP40,";
00332         if (cap  & Solid::Control::WirelessNetworkInterface::Wep104)
00333             out << "WEP104,";
00334         if (cap  & Solid::Control::WirelessNetworkInterface::Tkip)
00335             out << "TKIP,";
00336         if (cap  & Solid::Control::WirelessNetworkInterface::Ccmp)
00337             out << "CCMP,";
00338         if (cap  & Solid::Control::WirelessNetworkInterface::Rsn)
00339             out << "RSN,";
00340         out << endl;
00341     }
00342     else
00343     {
00344         out << "(No Capabilities)" << endl;
00345     }
00346     return out;
00347 }
00348 
00349 std::ostream &operator<<(std::ostream &out, const Solid::Control::WiredNetworkInterface &network)
00350 {
00351     out << static_cast<const Solid::Control::NetworkInterface&>(network);
00352     out << endl;
00353     out << "  Hardware Address =   " << network.hardwareAddress() << endl;
00354     out << "  Bit Rate =           " << network.bitRate() << endl;
00355     out << "  Carrier =            " << qVariantFromValue(network.carrier()) << endl;
00356     return out;
00357 }
00358 
00359 
00360 void checkArgumentCount(int min, int max)
00361 {
00362     int count = KCmdLineArgs::parsedArgs()->count();
00363 
00364     if (count < min)
00365     {
00366         cerr << i18n("Syntax Error: Not enough arguments") << endl;
00367         ::exit(1);
00368     }
00369 
00370     if ((max > 0) && (count > max))
00371     {
00372         cerr << i18n("Syntax Error: Too many arguments") << endl;
00373         ::exit(1);
00374     }
00375 }
00376 
00377 int main(int argc, char **argv)
00378 {
00379   KCmdLineArgs::init(argc, argv, appName, 0, ki18n(programName), version, ki18n(description), false);
00380 
00381 
00382   KCmdLineOptions options;
00383 
00384   options.add("commands", ki18n("Show available commands by domains"));
00385 
00386   options.add("+command", ki18n("Command (see --commands)"));
00387 
00388   options.add("+[arg(s)]", ki18n("Arguments for command"));
00389 
00390   KCmdLineArgs::addCmdLineOptions(options);
00391 
00392   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00393 
00394   KComponentData componentData(appName);
00395 
00396   if (args->isSet("commands"))
00397   {
00398       KCmdLineArgs::enable_i18n();
00399 
00400       cout << endl << i18n("Syntax:") << endl << endl;
00401 
00402       cout << "  solid-network listdevices" << endl;
00403       cout << i18n("             # List the network devices present.\n") << endl;
00404 
00405       cout << "  solid-network listnetworks 'uni'" << endl;
00406       cout << i18n("             # List the networks known to the device specified by 'uni'.\n") << endl;
00407 
00408       cout << "  solid-network query (status|wireless|wireless-hardware)|(interface 'uni')|(network 'device-uni' 'network-uni')" << endl;
00409       cout << i18n("             # Query whether networking features are active or not.\n"
00410                     "             # - If the 'status' option is given, return whether\n"
00411                     "             # networking is enabled for the system\n"
00412                     "             # - If the 'wireless' option is given, return whether\n"
00413                     "             # wireless is enabled for the system\n"
00414                     "             # - If the 'wireless-hardware' option is given,\n"
00415                     "             #  return whether the wireless hardware is enabled\n"
00416                     "             # - If the 'interface' option is given, print the\n"
00417                     "             # properties of the network interface that 'uni' refers to.\n"
00418                     "             # - If the 'network' option is given, print the\n"
00419                     "             # properties of the network on 'device-uni' that 'network-uni' refers to.\n") << endl;
00420 
00421       cout << "  solid-network set wireless (enabled|disabled)" << endl;
00422       cout << i18n("             # Enable or disable networking on this system.\n") << endl;
00423 
00424       cout << "  solid-network set networking (enabled|disabled)" << endl;
00425       cout << i18n("             # Enable or disable networking on this system.\n") << endl;
00426 
00427       cout << "  solid-network set network 'device-uni' 'network-uni' [authentication 'key']" << endl;
00428       cout << i18n("             # Activate the network 'network-uni' on 'device-uni'.\n"
00429                     "             # Optionally, use WEP128, open-system encryption with hex key 'key'. (Hardcoded)\n"
00430                     "             # Where 'authentication' is one of:\n"
00431                     "             # wep hex64|ascii64|hex128|ascii128|passphrase64|passphrase128 'key' [open|shared]\n"
00432                     "             # wpapsk wpa|wpa2 tkip|ccmp-aes password\n"
00433                     "             # wpaeap UNIMPLEMENTED IN SOLIDSHELL\n") << endl;
00434 
00435       cout << endl;
00436 
00437       return 0;
00438   }
00439 
00440   return SolidNetwork::doIt() ? 0 : 1;
00441 }
00442 
00443 bool SolidNetwork::doIt()
00444 {
00445     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00446     checkArgumentCount(1, 0);
00447 
00448     QString command(args->arg(0));
00449 
00450     int fake_argc = 0;
00451     char **fake_argv = 0;
00452     SolidNetwork shell(fake_argc, fake_argv);
00453 
00454     if (command == "query")
00455     {
00456         checkArgumentCount(2, 4);
00457         QString what(args->arg(1));
00458         if (what == "status")
00459             return shell.netmgrNetworkingEnabled();
00460         else if (what == "wireless")
00461             return shell.netmgrWirelessEnabled();
00462         else if (what == "wireless-hardware")
00463             return shell.netmgrWirelessHardwareEnabled();
00464         else if (what == "interface")
00465         {
00466             checkArgumentCount(3, 3);
00467             QString uni(args->arg(2));
00468             return shell.netmgrQueryNetworkInterface(uni);
00469         }
00470         else if (what == "network")
00471         {
00472             checkArgumentCount(4, 4);
00473             QString dev(args->arg(2));
00474             QString uni(args->arg(3));
00475             return shell.netmgrQueryNetwork(dev, uni);
00476         }
00477         else
00478             cerr << i18n("Syntax Error: Unknown option '%1'", what) << endl;
00479     }
00480     else if (command == "set")
00481     {
00482         checkArgumentCount(3, 9);
00483         QString what(args->arg(1));
00484         QString how(args->arg(2));
00485         if (what == "networking")
00486         {
00487             bool enabled;
00488             if (how == "enabled")
00489             {
00490                 enabled = true;
00491             }
00492             else if (how == "disabled")
00493             {
00494                 enabled = false;
00495             }
00496             else
00497             {
00498                 cerr << i18n("Syntax Error: Unknown option '%1'", how) << endl;
00499                 return false;
00500             }
00501             shell.netmgrChangeNetworkingEnabled(enabled);
00502             return true;
00503         }
00504         else if (what == "wireless")
00505         {
00506             bool enabled;
00507             if (how == "enabled")
00508             {
00509                 enabled = true;
00510             }
00511             else if (how == "disabled")
00512             {
00513                 enabled = false;
00514             }
00515             else
00516             {
00517                 cerr << i18n("Syntax Error: Unknown option '%1'", how) << endl;
00518                 return false;
00519             }
00520             shell.netmgrChangeWirelessEnabled(enabled);
00521             return true;
00522         }
00523     /*cout << "  solid-network set network 'device-uni' 'network-uni' [authentication 'key']" << endl; */
00524         /*wep hex64|ascii64|hex128|ascii128|passphrase 'key' [open|shared] */
00525         /* wpaeap UNIMPLEMENTED */
00526         else if (what == "network")
00527         {
00528             cerr << i18n("Not implemented");
00529 #if 0 // probably won't be reimplemented since solidshell can't provide a persistent settings service...
00530             checkArgumentCount(4, 9);
00531             QString dev(args->arg(2));
00532             QString uni(args->arg(3));
00533             Solid::Control::Authentication * auth = 0;
00534             QMap<QString,QString> secrets;
00535 
00536             if (KCmdLineArgs::parsedArgs()->count() > 4)
00537             {
00538                 QString hasAuth = args->arg(4);
00539                 if (hasAuth == "authentication")
00540                 {
00541                     //encrypted network
00542                     QString authScheme = args->arg(5);
00543                     if (authScheme == "wep")
00544                     {
00545                         Solid::Control::AuthenticationWep *wepAuth = new Solid::Control::AuthenticationWep();
00546                         QString keyType = args->arg(6);
00547                         if (keyType == "hex64")
00548                         {
00549                             wepAuth->setType(Solid::Control::AuthenticationWep::WepHex);
00550                             wepAuth->setKeyLength(64);
00551                         }
00552                         else if (keyType == "ascii64")
00553                         {
00554                             wepAuth->setType(Solid::Control::AuthenticationWep::WepAscii);
00555                             wepAuth->setKeyLength(64);
00556                         }
00557                         else if (keyType == "hex128")
00558                         {
00559                             wepAuth->setType(Solid::Control::AuthenticationWep::WepHex);
00560                             wepAuth->setKeyLength(128);
00561                         }
00562                         else if (keyType == "ascii128")
00563                         {
00564                             wepAuth->setType(Solid::Control::AuthenticationWep::WepAscii);
00565                             wepAuth->setKeyLength(128);
00566                         }
00567                         else if (keyType == "passphrase64")
00568                         {
00569                             wepAuth->setType(Solid::Control::AuthenticationWep::WepPassphrase);
00570                             wepAuth->setKeyLength(64);
00571                         }
00572                         else if (keyType == "passphrase128")
00573                         {
00574                             wepAuth->setType(Solid::Control::AuthenticationWep::WepPassphrase);
00575                             wepAuth->setKeyLength(128);
00576                         }
00577                         else
00578                         {
00579                             cerr << i18n("Unrecognised WEP type '%1'", keyType) << endl;
00580                             delete wepAuth;
00581                             return false;
00582                         }
00583     
00584                         QString key = args->arg(7);
00585                         secrets.insert("key", key);
00586                         wepAuth->setSecrets(secrets);
00587     
00588                         QString method = args->arg(8);
00589                         if (method == "open")
00590                             wepAuth->setMethod(Solid::Control::AuthenticationWep::WepOpenSystem);
00591                         else if (method == "shared")
00592                             wepAuth->setMethod(Solid::Control::AuthenticationWep::WepSharedKey);
00593                         else
00594                         {
00595                             cerr << i18n("Unrecognised WEP method '%1'", method) << endl;
00596                             delete wepAuth;
00597                             return false;
00598                         }
00599                         auth = wepAuth;
00600                     }
00601                     else if (authScheme == "wpapsk")
00602                     {
00603                         /* wpapsk wpa|wpa2 tkip|ccmp-aes password */
00604                         Solid::Control::AuthenticationWpaPersonal *wpapAuth = new Solid::Control::AuthenticationWpaPersonal();
00605                         QString version = args->arg(6);
00606                         if (version == "wpa")
00607                             wpapAuth->setVersion(Solid::Control::AuthenticationWpaPersonal::Wpa1);
00608                         else if (version == "wpa2")
00609                             wpapAuth->setVersion(Solid::Control::AuthenticationWpaPersonal::Wpa1);
00610                         else
00611                         {
00612                             cerr << i18n("Unrecognised WPA version '%1'", version) << endl;
00613                             delete wpapAuth;
00614                             return false;
00615                         }
00616                         QString protocol = args->arg(7);
00617                         if (protocol == "tkip")
00618                             wpapAuth->setProtocol(Solid::Control::AuthenticationWpaPersonal::WpaTkip);
00619                         else if (protocol == "ccmp-aes")
00620                             wpapAuth->setProtocol(Solid::Control::AuthenticationWpaPersonal::WpaCcmpAes);
00621                         else
00622                         {
00623                             cerr << i18n("Unrecognised WPA encryption protocol '%1'", protocol) << endl;
00624                             delete wpapAuth;
00625                             return false;
00626                         }
00627                         QString key = args->arg(8);
00628                         secrets.insert("key", key);
00629                         wpapAuth->setSecrets(secrets);
00630                         auth = wpapAuth;
00631                     }
00632                     else
00633                     {
00634                         cerr << i18n("Unimplemented auth scheme '%1'", args->arg(5)) << endl;
00635                         return false;
00636                     }
00637                 }
00638             }
00639             else
00640             {
00641                 //unencrypted network
00642                 auth = new Solid::Control::AuthenticationNone;
00643             }
00644 
00645             return shell.netmgrActivateNetwork(dev, uni, auth);
00646             delete auth;
00647 #endif
00648         }
00649         else
00650         {
00651             cerr << i18n("Syntax Error: Unknown object '%1'", what) << endl;
00652             return false;
00653         }
00654     }
00655     else if (command == "listdevices")
00656     {
00657         return shell.netmgrList();
00658     }
00659     else if (command == "listnetworks")
00660     {
00661         checkArgumentCount(2, 2);
00662         QString device(args->arg(1));
00663         return shell.netmgrListNetworks(device);
00664     }
00665     else
00666     {
00667         cerr << i18n("Syntax Error: Unknown command '%1'" , command) << endl;
00668     }
00669 
00670     return false;
00671 }
00672 
00673 bool SolidNetwork::netmgrNetworkingEnabled()
00674 {
00675     if (Solid::Control::NetworkManager::isNetworkingEnabled())
00676         cout << i18n("networking: is enabled")<< endl;
00677     else
00678         cout << i18n("networking: is not enabled")<< endl;
00679     return Solid::Control::NetworkManager::isNetworkingEnabled();
00680 }
00681 
00682 bool SolidNetwork::netmgrWirelessEnabled()
00683 {
00684     if (Solid::Control::NetworkManager::isWirelessEnabled())
00685         cout << i18n("wireless: is enabled")<< endl;
00686     else
00687         cout << i18n("wireless: is not enabled")<< endl;
00688     return Solid::Control::NetworkManager::isWirelessEnabled();
00689 }
00690 
00691 bool SolidNetwork::netmgrWirelessHardwareEnabled()
00692 {
00693     if (Solid::Control::NetworkManager::isWirelessHardwareEnabled())
00694         cout << i18n("wireless hardware: is enabled")<< endl;
00695     else
00696         cout << i18n("wireless hardware: is not enabled")<< endl;
00697     return Solid::Control::NetworkManager::isWirelessHardwareEnabled();
00698 }
00699 
00700 bool SolidNetwork::netmgrChangeNetworkingEnabled(bool enabled)
00701 {
00702     Solid::Control::NetworkManager::setNetworkingEnabled(enabled);
00703     return true;
00704 }
00705 
00706 bool SolidNetwork::netmgrChangeWirelessEnabled(bool enabled)
00707 {
00708     Solid::Control::NetworkManager::setWirelessEnabled(enabled);
00709     return true;
00710 }
00711 
00712 bool SolidNetwork::netmgrList()
00713 {
00714     const Solid::Control::NetworkInterfaceList all = Solid::Control::NetworkManager::networkInterfaces();
00715 
00716     cerr << "debug: network interface list contains: " << all.count() << " entries" << endl;
00717     foreach (const Solid::Control::NetworkInterface *device, all)
00718     {
00719         cout << "UNI = '" << device->uni() << "'" << endl;
00720     }
00721     return true;
00722 }
00723 
00724 bool SolidNetwork::netmgrListNetworks(const QString  & deviceUni)
00725 {
00726     Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
00727     Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device );
00728     if (wifiDev) {
00729 
00730         Solid::Control::AccessPointList aps = wifiDev->accessPoints();
00731         foreach (const QString &apUni, aps)
00732         {
00733             cout << "NETWORK UNI = '" << apUni << "'" << endl;
00734         }
00735 
00736         return true;
00737     }
00738     return false;
00739 }
00740 
00741 bool SolidNetwork::netmgrQueryNetworkInterface(const QString  & deviceUni)
00742 {
00743     cerr << "SolidNetwork::netmgrQueryNetworkInterface()" << endl;
00744     Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
00745     if (!device) {
00746         cerr << "No such interface: " << deviceUni << endl;
00747         return false;
00748     }
00749     Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device);
00750     Solid::Control::WiredNetworkInterface * wiredDev =  qobject_cast<Solid::Control::WiredNetworkInterface *>(device);
00751     if (wifiDev) {
00752         cout << *wifiDev << endl;
00753     } else if (wiredDev) {
00754         cout << *wiredDev << endl;
00755     } else {
00756         cout << *device << endl;
00757     }
00758     return true;
00759 }
00760 
00761 bool SolidNetwork::netmgrQueryNetwork(const QString  & deviceUni, const QString  & apUni)
00762 {
00763     cerr << "SolidNetwork::netmgrQueryNetwork()" << endl;
00764     Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
00765     Solid::Control::WirelessNetworkInterface * wifiDev =  qobject_cast<Solid::Control::WirelessNetworkInterface *>(device );
00766     if (wifiDev) {
00767         Solid::Control::AccessPoint * ap = wifiDev->findAccessPoint( apUni );
00768         if ( ap ) {
00769             cout << *ap << endl;
00770             return true;
00771         }
00772     }
00773     return false;
00774 }
00775 
00776 #if 0
00777 bool SolidNetwork::netmgrActivateNetwork(const QString  & deviceUni, const QString  & networkUni, Solid::Control::Authentication * auth)
00778 {
00779     Solid::Control::NetworkInterface * device = Solid::Control::NetworkManager::findNetworkInterface(deviceUni);
00780     Solid::Control::Network * network = device.findNetwork(networkUni);
00781     Solid::Control::WirelessNetwork * wlan = 0;
00782     if (( wlan = qobject_cast<Solid::Control::WirelessNetwork *>(network)))
00783     {
00784         wlan->setAuthentication(auth);
00785         wlan->setActivated(true);
00786     }
00787     else
00788         network->setActivated(true);
00789     return true;
00790 }
00791 #endif
00792 
00793 void SolidNetwork::connectJob(KJob *job)
00794 {
00795     connect(job, SIGNAL(result(KJob *)),
00796              this, SLOT(slotResult(KJob *)));
00797     connect(job, SIGNAL(percent(KJob *, unsigned long)),
00798              this, SLOT(slotPercent(KJob *, unsigned long)));
00799     connect(job, SIGNAL(infoMessage(KJob *, const QString &, const QString &)),
00800              this, SLOT(slotInfoMessage(KJob *, const QString &)));
00801 }
00802 
00803 void SolidNetwork::slotPercent(KJob */*job */, unsigned long percent)
00804 {
00805     cout << i18n("Progress: %1%" , percent) << endl;
00806 }
00807 
00808 void SolidNetwork::slotInfoMessage(KJob */*job */, const QString &message)
00809 {
00810     cout << i18n("Info: %1" , message) << endl;
00811 }
00812 
00813 void SolidNetwork::slotResult(KJob *job)
00814 {
00815     m_error = 0;
00816 
00817     if (job->error())
00818     {
00819         m_error = job->error();
00820         m_errorString = job->errorString();
00821     }
00822 
00823     m_loop.exit();
00824 }
00825 
00826 void SolidNetwork::slotStorageResult(Solid::ErrorType error, const QVariant &errorData)
00827 {
00828     if (error) {
00829         m_error = 1;
00830         m_errorString = errorData.toString();
00831     }
00832     m_loop.exit();
00833 }
00834 
00835 #include "solid-network.moc"

SolidModules

Skip menu "SolidModules"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal