Development/Tutorials/Solid/Network Management: Difference between revisions

From KDE TechBase
m (Fix link)
(Mark for updating)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Review|
* Port to KF5
* Add more content
}}
== Prerequisites ==
== Prerequisites ==
This tutorial assumes that you have read [[Development/Tutorials/Solid_Tutorials|Introduction to Solid]] and are familiar with the Solid Hardware framework.  If you want to use any backends other than the fake backends provided in kdelibs, you'll need to compile and install kdebase.
This tutorial assumes that you have read [[Development/Tutorials/Solid_Tutorials|Introduction to Solid]] and are familiar with the Solid hardware framework.  If you want to use any backends other than the fake backends provided in kdelibs, you will need to compile and install kdebase.


== Features of the Solid Network Manager ==
== Features of the Solid Network Manager ==
Line 13: Line 18:
Our first program is going to be the most common use of the network manager, checking the network status of the system.  This will be useful for programs like KMail which will be able to see if the computer has network connectivity.  It would then be able to judge whether or not it should check for mail.
Our first program is going to be the most common use of the network manager, checking the network status of the system.  This will be useful for programs like KMail which will be able to see if the computer has network connectivity.  It would then be able to judge whether or not it should check for mail.


<code cppqt>
<syntaxhighlight lang="cpp-qt">
//get a reference to the network manager
Solid::NetworkManager &netmanager = Solid::NetworkManager::self();
 
//test to see if networking is enabled on the system
//test to see if networking is enabled on the system
if(netmanager.isNetworkingEnabled() )
if(Solid::Networking::status() == Solid::Networking::Connected)
{
{
     kDebug() << "Networking is enabled. Feel free to go online!"
     kDebug() << "Networking is enabled. Feel free to go online!";
            << endl;
}
}
else
else
{
{
     kDebug() << "Network not available." << endl;
     kDebug() << "Network not available.";
}
}
</code>
</syntaxhighlight>


As of 28DEC2006 there are no signals that report a change in the network status so polling is required.
To get informed about changes in network connectivity you'll have to connect to the events of Solid::Networking::notifier().


== Cross-referencing devices to interfaces ==
== Cross-referencing devices to interfaces ==
It is no longer required for the user to figure out which device corresponds to a given interface as the NetworkManager has the ability to cross-reference a device to it's interface name. This can be done with the following code:
It is no longer required for the user to figure out which device corresponds to a given interface as the NetworkManager has the ability to cross-reference a device to its interface name. This can be done with the following code:
<code cppqt>
<syntaxhighlight lang="cpp-qt">
//get a reference to the device manager
//get a reference to the device manager
Solid::DeviceManager &manager = Solid::DeviceManager::self();
Solid::DeviceManager &manager = Solid::DeviceManager::self();
 
   
//get a network device
//get a network device
Solid::DeviceList netlist =
Solid::DeviceList netlist = manager.findDevicesFromQuery(Solid::Capability::NetworkHw, QString());
manager.findDevicesFromQuery("",Solid::Capability::NetworkHw);
      
      
//check to see if no network devices were found
//check to see if no network devices were found
if(netlist.empty() )
if(netlist.empty() )
{
{
     kDebug() << "No network devices found!" << endl;
     kDebug() << "No network devices found!";
    return 0;
}
}
 
   
Solid::Device device = netlist[0];
Solid::Device device = netlist[0];
Solid::NetworkHw *netdev = device.as<Solid::NetworkHw>();
Solid::NetworkHw *netdev = device.as<Solid::NetworkHw>();
//keep the program from crashing in the event that there's
//keep the program from crashing in the event that there's a bug in solid
//a bug in solid
if(!netdev)
if(!netdev)
{
{
     kDebug() << "Device could not be converted. There is a bug."
     kDebug() << "Device could not be converted. There is a bug.";
            << endl;
     return 0;
     return 0;
}
}
   
kDebug() << "The iface of" << device.udi() << "is" << netdev->ifaceName();
</syntaxhighlight>


kDebug() << "The iface of " << device.udi() << " is "
        << netdev->ifaceName() << endl;
</code>
== Initializing devices ==
== Initializing devices ==


== Encryption ==
== Encryption ==
== Putting it all together ==
== Putting it all together ==
[[Category:Tutorial]]
[[Category:C++]]
[[Category:KDE4]]
[[Category:Solid]]

Latest revision as of 11:21, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

  • Port to KF5
  • Add more content

Prerequisites

This tutorial assumes that you have read Introduction to Solid and are familiar with the Solid hardware framework. If you want to use any backends other than the fake backends provided in kdelibs, you will need to compile and install kdebase.

Features of the Solid Network Manager

The Solid Network Manager provides many useful features that have, up until now, been missing from KDE. These include:

  • Notification of connection state. This allows applications to know when there is no internet connection, allowing them to not bother the user with "Connection Error" messages
  • Notification of wireless connection state.
  • Access to network devices and interfaces.
  • Device <-> interface cross-referencing.

Note that there is a difference between the devices and the interfaces. The device is a representation of the device itself and all the things that normally are associated with a PCI/USB device such as: bus address, IRQ, etc. The interface is the name of the device e.g. 'eth0' which is used for commands like 'ifconfig.'

Let's begin

Our first program is going to be the most common use of the network manager, checking the network status of the system. This will be useful for programs like KMail which will be able to see if the computer has network connectivity. It would then be able to judge whether or not it should check for mail.

//test to see if networking is enabled on the system
if(Solid::Networking::status() == Solid::Networking::Connected)
{
    kDebug() << "Networking is enabled. Feel free to go online!";
}
else
{
    kDebug() << "Network not available.";
}

To get informed about changes in network connectivity you'll have to connect to the events of Solid::Networking::notifier().

Cross-referencing devices to interfaces

It is no longer required for the user to figure out which device corresponds to a given interface as the NetworkManager has the ability to cross-reference a device to its interface name. This can be done with the following code:

//get a reference to the device manager
Solid::DeviceManager &manager = Solid::DeviceManager::self();
    
//get a network device
Solid::DeviceList netlist = manager.findDevicesFromQuery(Solid::Capability::NetworkHw, QString());
    
//check to see if no network devices were found
if(netlist.empty() )
{
    kDebug() << "No network devices found!";
}
    
Solid::Device device = netlist[0];
Solid::NetworkHw *netdev = device.as<Solid::NetworkHw>();
//keep the program from crashing in the event that there's a bug in solid
if(!netdev)
{
    kDebug() << "Device could not be converted. There is a bug.";
    return 0;
}
    
kDebug() << "The iface of" << device.udi() << "is" << netdev->ifaceName();

Initializing devices

Encryption

Putting it all together