glibmm: Gio::NetworkMonitor Class Reference

glibmm 2.66.9

Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Symbols | List of all members
Gio::NetworkMonitor Class Reference

Network status monitor. More...

`#include <giomm/networkmonitor.h>`

Inheritance diagram for Gio::NetworkMonitor:
[legend]

## Public Member Functions
| NetworkMonitor (NetworkMonitor && src) noexcept
NetworkMonitor & | operator= (NetworkMonitor && src) noexcept
| ~NetworkMonitor () noexcept override
GNetworkMonitor * | gobj ()
| Provides access to the underlying C GObject.
const GNetworkMonitor * | gobj () const
| Provides access to the underlying C GObject.
bool | get_network_available () const
| Checks if the network is available.
bool | get_network_metered () const
| Checks if the network is metered.
NetworkConnectivity | get_connectivity () const
| Gets a more detailed networking state than g_network_monitor_get_network_available().
bool | can_reach (const Glib::RefPtr< SocketConnectable > & connectable, const Glib::RefPtr< Cancellable > & cancellable)
| Attempts to determine whether or not the host pointed to by connectable can be reached, without actually trying to connect to it.
bool | can_reach (const Glib::RefPtr< SocketConnectable > & connectable)
| A can_reach() convenience overload.
void | can_reach_async (const Glib::RefPtr< SocketConnectable > & connectable, const SlotAsyncReady & slot, const Glib::RefPtr< Cancellable > & cancellable)
bool | can_reach_finish (const Glib::RefPtr< AsyncResult > & result)
| Finishes an async network connectivity test.
Glib::SignalProxy< void, bool > | signal_network_changed ()
Glib::PropertyProxy_ReadOnly< bool > | property_network_available () const
| Whether the network is considered available.
Glib::PropertyProxy_ReadOnly< NetworkConnectivity > | property_connectivity () const
| More detailed information about the host's network connectivity.
Glib::PropertyProxy_ReadOnly< bool > | property_network_metered () const
| Whether the network is considered metered.
Public Member Functions inherited from Glib::Interface
| Interface ()
| A Default constructor.
| Interface (Interface && src) noexcept
Interface & | operator= (Interface && src) noexcept
| Interface (const Glib::Interface_Class & interface_class)
| Called by constructors of derived classes.
| Interface (GObject * castitem)
| Called by constructors of derived classes.
| ~Interface () noexcept override
| Interface (const Interface &)=delete
Interface & | operator= (const Interface &)=delete
GObject * | gobj ()
const GObject * | gobj () const
Public Member Functions inherited from Glib::ObjectBase
| ObjectBase (const ObjectBase &)=delete
ObjectBase & | operator= (const ObjectBase &)=delete
void | set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value)
| You probably want to use a specific property_*() accessor method instead.
void | get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const
| You probably want to use a specific property_*() accessor method instead.
template<class PropertyType>
void | set_property (const Glib::ustring & property_name, const PropertyType & value)
| You probably want to use a specific property_*() accessor method instead.
template<class PropertyType>
void | get_property (const Glib::ustring & property_name, PropertyType & value) const
| You probably want to use a specific property_*() accessor method instead.
void | connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void > & slot)
| You can use the signal_changed() signal of the property proxy instead.
void | connect_property_changed (const Glib::ustring & property_name, sigc::slot< void > && slot)
| You can use the signal_changed() signal of the property proxy instead.
sigc::connection | connect_property_changed_with_return (const Glib::ustring & property_name, const sigc::slot< void > & slot)
| You can use the signal_changed() signal of the property proxy instead.
sigc::connection | connect_property_changed_with_return (const Glib::ustring & property_name, sigc::slot< void > && slot)
| You can use the signal_changed() signal of the property proxy instead.
void | freeze_notify ()
| Increases the freeze count on object.
void | thaw_notify ()
| Reverts the effect of a previous call to freeze_notify().
virtual void | reference () const
| Increment the reference count for this object.
virtual void | unreference () const
| Decrement the reference count for this object.
GObject * | gobj ()
| Provides access to the underlying C GObject.
const GObject * | gobj () const
| Provides access to the underlying C GObject.
GObject * | gobj_copy () const
| Give a ref-ed copy to someone. Use for direct struct access.

## Static Public Member Functions
static void | add_interface (GType gtype_implementer)
static GType | get_type ()
| Get the GType for this class, for use with the underlying GObject type system.
static Glib::RefPtr< NetworkMonitor > | get_default ()
| Gets the default NetworkMonitor for the system.

## Protected Member Functions
| NetworkMonitor ()
| You should derive from this class to use it.
virtual void | on_network_changed (bool available)
| This is a default handler for the signal signal_network_changed().
Protected Member Functions inherited from Glib::ObjectBase
| ObjectBase ()
| This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor.
| ObjectBase (const char * custom_type_name)
| A derived constructor always overrides this choice.
| ObjectBase (const std::type_info & custom_type_info)
| This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually.
| ObjectBase (ObjectBase && src) noexcept
ObjectBase & | operator= (ObjectBase && src) noexcept
virtual | ~ObjectBase () noexcept=0
void | initialize (GObject * castitem)
void | initialize_move (GObject * castitem, Glib::ObjectBase * previous_wrapper)

## Related Symbols
(Note that these are not member symbols.)
Glib::RefPtr< Gio::NetworkMonitor > | wrap (GNetworkMonitor * object, bool take_copy=false)
| A Glib::wrap() method for this object.

## Detailed Description

Network status monitor.

Gio::NetworkMonitor provides an easy-to-use cross-platform API for monitoring network connectivity. On Linux, the available implementations are based on the kernel's netlink interface and on NetworkManager.

There is also an implementation for use inside Flatpak sandboxes.

Since glibmm 2.44

## Constructor & Destructor Documentation

## ◆ NetworkMonitor() [1/2]

Gio::NetworkMonitor::NetworkMonitor | ( |  | ) |
| protected

You should derive from this class to use it.

## ◆ NetworkMonitor() [2/2]

Gio::NetworkMonitor::NetworkMonitor | ( | NetworkMonitor && | src | ) |
| noexcept

## ◆ ~NetworkMonitor()

Gio::NetworkMonitor::~NetworkMonitor | ( |  | ) |
| overridenoexcept

## Member Function Documentation

## ◆ add_interface()

void Gio::NetworkMonitor::add_interface | ( | GType | gtype_implementer | ) |
| static

## ◆ can_reach() [1/2]

bool Gio::NetworkMonitor::can_reach | ( | const Glib::RefPtr< SocketConnectable > & | connectable | ) |

A can_reach() convenience overload.

## ◆ can_reach() [2/2]

bool Gio::NetworkMonitor::can_reach | ( | const Glib::RefPtr< SocketConnectable > & | connectable,
|  | const Glib::RefPtr< Cancellable > & | cancellable )

Attempts to determine whether or not the host pointed to by connectable can be reached, without actually trying to connect to it.

This may return true even when NetworkMonitor::property_network_available() is false, if, for example, monitor can determine that connectable refers to a host on a local network.

If monitor believes that an attempt to connect to connectable will succeed, it will return true. Otherwise, it will return false and set error to an appropriate error (such as IO_ERROR_HOST_UNREACHABLE).

Note that although this does not attempt to connect to connectable, it may still block for a brief period of time (eg, trying to do multicast DNS on the local network), so if you do not want to block, you should use g_network_monitor_can_reach_async().

Since glibmm 2.44

Parameters

  connectable | A SocketConnectable.
  cancellable | A Cancellable, or nullptr.

Returns
  true if connectable is reachable, false if not.

Exceptions

  Glib::Error |

## ◆ can_reach_async()

void Gio::NetworkMonitor::can_reach_async | ( | const Glib::RefPtr< SocketConnectable > & | connectable,
|  | const SlotAsyncReady & | slot,
|  | const Glib::RefPtr< Cancellable > & | cancellable )

## ◆ can_reach_finish()

bool Gio::NetworkMonitor::can_reach_finish | ( | const Glib::RefPtr< AsyncResult > & | result | ) |

Finishes an async network connectivity test.

See g_network_monitor_can_reach_async().

Since glibmm 2.44

Parameters

  result | A AsyncResult.

Returns
  true if network is reachable, false if not.

Exceptions

  Glib::Error |

## ◆ get_connectivity()

NetworkConnectivity Gio::NetworkMonitor::get_connectivity | ( |  | ) | const

Gets a more detailed networking state than g_network_monitor_get_network_available().

If NetworkMonitor::property_network_available() is false, then the connectivity state will be NETWORK_CONNECTIVITY_LOCAL.

If NetworkMonitor::property_network_available() is true, then the connectivity state will be NETWORK_CONNECTIVITY_FULL (if there is full Internet connectivity), NETWORK_CONNECTIVITY_LIMITED (if the host has a default route, but appears to be unable to actually reach the full Internet), or NETWORK_CONNECTIVITY_PORTAL (if the host is trapped behind a "captive portal" that requires some sort of login or acknowledgement before allowing full Internet access).

Note that in the case of NETWORK_CONNECTIVITY_LIMITED and NETWORK_CONNECTIVITY_PORTAL, it is possible that some sites are reachable but others are not. In this case, applications can attempt to connect to remote servers, but should gracefully fall back to their "offline" behavior if the connection attempt fails.

Since glibmm 2.44

Returns
  The network connectivity state.

## ◆ get_default()

Glib::RefPtr< NetworkMonitor > Gio::NetworkMonitor::get_default | ( |  | ) |
| static

Gets the default NetworkMonitor for the system.

Since glibmm 2.44

Returns
  A NetworkMonitor.

## ◆ get_network_available()

bool Gio::NetworkMonitor::get_network_available | ( |  | ) | const

Checks if the network is available.

"Available" here means that the system has a default route available for at least one of IPv4 or IPv6. It does not necessarily imply that the public Internet is reachable. See NetworkMonitor::property_network_available() for more details.

Since glibmm 2.44

Returns
  Whether the network is available.

## ◆ get_network_metered()

bool Gio::NetworkMonitor::get_network_metered | ( |  | ) | const

Checks if the network is metered.

See NetworkMonitor::property_network_metered() for more details.

Since glibmm 2.46

Returns
  Whether the connection is metered.

## ◆ get_type()

GType Gio::NetworkMonitor::get_type | ( |  | ) |
| static

Get the GType for this class, for use with the underlying GObject type system.

## ◆ gobj() [1/2]

GNetworkMonitor * Gio::NetworkMonitor::gobj | ( |  | ) |
| inline

Provides access to the underlying C GObject.

## ◆ gobj() [2/2]

const GNetworkMonitor * Gio::NetworkMonitor::gobj | ( |  | ) | const
| inline

Provides access to the underlying C GObject.

## ◆ on_network_changed()

virtual void Gio::NetworkMonitor::on_network_changed | ( | bool | available | ) |
| protectedvirtual

This is a default handler for the signal signal_network_changed().

## ◆ operator=()

NetworkMonitor & Gio::NetworkMonitor::operator= | ( | NetworkMonitor && | src | ) |
| noexcept

## ◆ property_connectivity()

Glib::PropertyProxy_ReadOnly< NetworkConnectivity > Gio::NetworkMonitor::property_connectivity | ( |  | ) | const

More detailed information about the host's network connectivity.

See g_network_monitor_get_connectivity() and NetworkConnectivity for more details.

Since glibmm 2.44

Default value: NETWORK_CONNECTIVITY_FULL

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_network_available()

Glib::PropertyProxy_ReadOnly< bool > Gio::NetworkMonitor::property_network_available | ( |  | ) | const

Whether the network is considered available.

That is, whether the system has a default route for at least one of IPv4 or IPv6.

Real-world networks are of course much more complicated than this; the machine may be connected to a wifi hotspot that requires payment before allowing traffic through, or may be connected to a functioning router that has lost its own upstream connectivity. Some hosts might only be accessible when a VPN is active. Other hosts might only be accessible when the VPN is not active. Thus, it is best to use g_network_monitor_can_reach() or g_network_monitor_can_reach_async() to test for reachability on a host-by-host basis. (On the other hand, when the property is false, the application can reasonably expect that no remote hosts at all are reachable, and should indicate this to the user in its UI.)

See also NetworkMonitor::signal_network_changed().

Since glibmm 2.44

Default value: false

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_network_metered()

Glib::PropertyProxy_ReadOnly< bool > Gio::NetworkMonitor::property_network_metered | ( |  | ) | const

Whether the network is considered metered.

That is, whether the system has traffic flowing through the default connection that is subject to limitations set by service providers. For example, traffic might be billed by the amount of data transmitted, or there might be a quota on the amount of traffic per month. This is typical with tethered connections (3G and 4G) and in such situations, bandwidth intensive applications may wish to avoid network activity where possible if it will cost the user money or use up their limited quota.

If more information is required about specific devices then the system network management API should be used instead (for example, NetworkManager or ConnMan).

If this information is not available then no networks will be marked as metered.

See also NetworkMonitor::property_network_available().

Since glibmm 2.46

Default value: false

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ signal_network_changed()

Glib::SignalProxy< void, bool > Gio::NetworkMonitor::signal_network_changed | ( |  | ) |

Slot Prototype:
  void on_my_network_changed(bool available)

Flags: Run Last

Emitted when the network configuration changes.

Since glibmm 2.44

Parameters

  available | The current value of NetworkMonitor::property_network_available().

## Friends And Related Symbol Documentation

## ◆ wrap()

Glib::RefPtr< Gio::NetworkMonitor > wrap | ( | GNetworkMonitor * | object,
|  | bool | take_copy = false )
| related

A Glib::wrap() method for this object.

Parameters

  object | The C instance.
  take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref.

Returns
  A C++ instance that wraps this C instance.