1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* vim: set ft=c ts=4 sts=4 sw=4 expandtab smartindent: */
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
* (C) Copyright 2012 Red Hat, Inc.
22
* This example monitors whether NM is running by checking if
23
* "org.freedesktop.NetworkManager" is owned by a process on D-Bus.
24
* It uses QDBusServiceWatcher class.
26
* Standalone compilation:
27
* moc-qt4 monitor-nm-running.cpp -o monitor-nm-running.moc
28
* g++ -Wall `pkg-config --libs --cflags QtCore QtDBus` monitor-nm-running.cpp -o monitor-nm-running
30
* You don't need to have NetworkManager devel package installed.
35
#include <QCoreApplication>
36
#include <QtDBus/QDBusServiceWatcher>
37
#include <QtDBus/QDBusConnection>
38
#include <QtCore/QDebug>
40
const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkManager";
42
// Define a class with slots
43
class NMWatcher: public QObject {
47
void serviceRegistered(const QString& name);
48
void serviceUnregistered(const QString& name);
52
void NMWatcher::serviceRegistered(const QString& name)
54
std::cout << "Name '" << name.toStdString() << "' registered"
55
<< " => NM is running" << std::endl;
58
void NMWatcher::serviceUnregistered(const QString& name)
60
std::cout << "Name '" << name.toStdString() << "' unregistered"
61
<< " => NM is not running" << std::endl;
64
int main(int argc, char *argv[])
66
QCoreApplication app (argc, argv);
68
qDebug() << "Monitor 'org.freedesktop.NetworkManager' D-Bus name";
69
qDebug() << "===================================================";
73
// Watch all changes of D-Bus NM_DBUS_SERVICE name
74
QDBusServiceWatcher *watcher = new QDBusServiceWatcher(NM_DBUS_SERVICE,
75
QDBusConnection::systemBus());
77
QObject::connect(watcher, SIGNAL(serviceRegistered(const QString&)),
78
&nm_watcher, SLOT(serviceRegistered(const QString&)));
80
QObject::connect(watcher, SIGNAL(serviceUnregistered(const QString&)),
81
&nm_watcher, SLOT(serviceUnregistered(const QString&)));
89
#include "monitor-nm-running.moc"