diff options
Diffstat (limited to 'examples/C/qt')
| -rw-r--r-- | examples/C/qt/Makefile.am | 11 | ||||
| -rw-r--r-- | examples/C/qt/Makefile.in | 23 | ||||
| -rw-r--r-- | examples/C/qt/change-ipv4-addresses.cpp | 146 |
3 files changed, 174 insertions, 6 deletions
diff --git a/examples/C/qt/Makefile.am b/examples/C/qt/Makefile.am index e1e17b38..5f0c6a1e 100644 --- a/examples/C/qt/Makefile.am +++ b/examples/C/qt/Makefile.am @@ -6,7 +6,8 @@ AM_CPPFLAGS = \ noinst_PROGRAMS = \ add-connection-wired \ - list-connections + list-connections \ + change-ipv4-addresses add_connection_wired_SOURCES = add-connection-wired.cpp add_connection_wired_LDADD = \ @@ -18,7 +19,13 @@ list_connections_LDADD = \ $(DBUS_LIBS) \ $(QT_LIBS) +change_ipv4_addresses_SOURCES = change-ipv4-addresses.cpp +change_ipv4_addresses_LDADD = \ + $(DBUS_LIBS) \ + $(QT_LIBS) + EXTRA_DIST = \ add-connection-wired.cpp \ - list-connections.cpp + list-connections.cpp \ + change-ipv4-addresses.cpp diff --git a/examples/C/qt/Makefile.in b/examples/C/qt/Makefile.in index c70f4855..a07fe015 100644 --- a/examples/C/qt/Makefile.in +++ b/examples/C/qt/Makefile.in @@ -35,7 +35,7 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ noinst_PROGRAMS = add-connection-wired$(EXEEXT) \ - list-connections$(EXEEXT) + list-connections$(EXEEXT) change-ipv4-addresses$(EXEEXT) subdir = examples/C/qt DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -65,6 +65,10 @@ add_connection_wired_DEPENDENCIES = $(am__DEPENDENCIES_1) \ AM_V_lt = $(am__v_lt_$(V)) am__v_lt_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY)) am__v_lt_0 = --silent +am_change_ipv4_addresses_OBJECTS = change-ipv4-addresses.$(OBJEXT) +change_ipv4_addresses_OBJECTS = $(am_change_ipv4_addresses_OBJECTS) +change_ipv4_addresses_DEPENDENCIES = $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) am_list_connections_OBJECTS = list-connections.$(OBJEXT) list_connections_OBJECTS = $(am_list_connections_OBJECTS) list_connections_DEPENDENCIES = $(am__DEPENDENCIES_1) \ @@ -95,9 +99,10 @@ am__v_CXXLD_0 = @echo " CXXLD " $@; AM_V_GEN = $(am__v_GEN_$(V)) am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY)) am__v_GEN_0 = @echo " GEN " $@; -SOURCES = $(add_connection_wired_SOURCES) $(list_connections_SOURCES) +SOURCES = $(add_connection_wired_SOURCES) \ + $(change_ipv4_addresses_SOURCES) $(list_connections_SOURCES) DIST_SOURCES = $(add_connection_wired_SOURCES) \ - $(list_connections_SOURCES) + $(change_ipv4_addresses_SOURCES) $(list_connections_SOURCES) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -322,9 +327,15 @@ list_connections_LDADD = \ $(DBUS_LIBS) \ $(QT_LIBS) +change_ipv4_addresses_SOURCES = change-ipv4-addresses.cpp +change_ipv4_addresses_LDADD = \ + $(DBUS_LIBS) \ + $(QT_LIBS) + EXTRA_DIST = \ add-connection-wired.cpp \ - list-connections.cpp + list-connections.cpp \ + change-ipv4-addresses.cpp all: all-am @@ -372,6 +383,9 @@ clean-noinstPROGRAMS: add-connection-wired$(EXEEXT): $(add_connection_wired_OBJECTS) $(add_connection_wired_DEPENDENCIES) @rm -f add-connection-wired$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(add_connection_wired_OBJECTS) $(add_connection_wired_LDADD) $(LIBS) +change-ipv4-addresses$(EXEEXT): $(change_ipv4_addresses_OBJECTS) $(change_ipv4_addresses_DEPENDENCIES) + @rm -f change-ipv4-addresses$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(change_ipv4_addresses_OBJECTS) $(change_ipv4_addresses_LDADD) $(LIBS) list-connections$(EXEEXT): $(list_connections_OBJECTS) $(list_connections_DEPENDENCIES) @rm -f list-connections$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(list_connections_OBJECTS) $(list_connections_LDADD) $(LIBS) @@ -383,6 +397,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add-connection-wired.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/change-ipv4-addresses.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list-connections.Po@am__quote@ .cpp.o: diff --git a/examples/C/qt/change-ipv4-addresses.cpp b/examples/C/qt/change-ipv4-addresses.cpp new file mode 100644 index 00000000..ca340f7f --- /dev/null +++ b/examples/C/qt/change-ipv4-addresses.cpp @@ -0,0 +1,146 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2011 Red Hat, Inc. + */ + +/* + * This example shows how to set manual IPv4 addresses to a connection. + * It uses Qt and D-Bus libraries to do that. + * + * Standalone compilation: + * g++ -Wall `pkg-config --libs --cflags NetworkManager QtCore QtDBus QtNetwork` change-ipv4-addresses.cpp -o change-ipv4-addresses + * + * You don't need to have NetworkManager devel package installed; you can just + * grab NetworkManager.h and put it in the path + */ + +#include <QtDBus/QDBusConnection> +#include <QtDBus/QDBusInterface> +#include <QtDBus/QDBusMetaType> +#include <QtDBus/QDBusReply> +#include <QtCore/QList> +#include <QtCore/QMap> +#include <QtCore/QString> +#include <QtCore/QDebug> +#include <QtNetwork/QHostAddress> + +#include "arpa/inet.h" + +#include "NetworkManager.h" + +typedef QMap<QString, QMap<QString, QVariant> > Connection; +Q_DECLARE_METATYPE(Connection) +Q_DECLARE_METATYPE(QList<uint>); +Q_DECLARE_METATYPE(QList<QList<uint> >); + +const QString NM_SETTING_CONNECTION_SETTING_NAME = "connection"; +const QString NM_SETTING_CONNECTION_ID = "id"; +const QString NM_SETTING_CONNECTION_UUID = "uuid"; + + +const QString getConnection(const QString& connectionUuid, Connection *found_connection) +{ + Connection settings; + QDBusInterface *ifaceForSettings; + + // Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h + QDBusInterface interface( + NM_DBUS_SERVICE, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_IFACE_SETTINGS, + QDBusConnection::systemBus()); + + // Get connection list and find the connection with 'connectionUuid' + QDBusReply<QList<QDBusObjectPath> > result1 = interface.call("ListConnections"); + + foreach (const QDBusObjectPath& connection, result1.value()) { + ifaceForSettings = new QDBusInterface( + NM_DBUS_SERVICE, + connection.path(), + NM_DBUS_IFACE_SETTINGS_CONNECTION, + QDBusConnection::systemBus()); + QDBusReply<Connection> result2 = ifaceForSettings->call("GetSettings"); + delete ifaceForSettings; + + settings = result2.value(); + QVariantMap connectionSettings = settings.value(NM_SETTING_CONNECTION_SETTING_NAME); + QString uuid = connectionSettings.value(NM_SETTING_CONNECTION_UUID).toString(); + + if (uuid == connectionUuid) { + // Connection found; set the settings to found_connection + // connection object path + *found_connection = settings; + return connection.path(); + } + } + + return QString(); +} + +void changeConnection(const QString& uuid) +{ + // Register types with D-Bus + qDBusRegisterMetaType<Connection>(); + qDBusRegisterMetaType<QList<uint> >(); + qDBusRegisterMetaType<QList<QList<uint> > >(); + + Connection connection; + QString conPath; + + // Find connection by provided UUID + conPath = getConnection(uuid, &connection); + + if (!conPath.isEmpty()) { + QList<QList<uint> > addresses; + QList<uint> addr1, addr2; + + // Add some addresses + addr1 << htonl(QHostAddress("192.168.100.4").toIPv4Address()) << 24 << htonl(QHostAddress("192.168.100.1").toIPv4Address()); + addr2 << htonl(QHostAddress("10.0.1.222").toIPv4Address()) << 8 << htonl(QHostAddress("10.0.1.254").toIPv4Address()); + addresses << addr1 << addr2; + + // Set method to "Manual" and put addresses to the connection map + connection["ipv4"]["method"] = "manual"; + connection["ipv4"]["addresses"] = QVariant::fromValue(addresses); + + QDBusInterface interface( + NM_DBUS_SERVICE, + conPath, + NM_DBUS_IFACE_SETTINGS_CONNECTION, + QDBusConnection::systemBus()); + + // Call Update() D-Bus method to update connection + QDBusReply<void> result = interface.call("Update", QVariant::fromValue(connection)); + if (result.isValid()) { + qDebug() << QString("Connection successfully updated (path %1)").arg(conPath); + } else { + qDebug() << QString("Error: could not update connection: %1 %2").arg(result.error().name()).arg(result.error().message()); + } + } else { + qDebug() << QString("Error: connection with UUID '%1' not found").arg(uuid); + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + qDebug() << QString("Usage: %1 <UUID>").arg(argv[0]); + return -1; + } + + changeConnection(argv[1]); +} |