~ubuntu-branches/ubuntu/precise/network-manager/precise

« back to all changes in this revision

Viewing changes to examples/C/qt/monitor-nm-running.cpp

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-02-09 16:45:41 UTC
  • mfrom: (1.1.53)
  • Revision ID: package-import@ubuntu.com-20120209164541-4h90zknlsfdb7x35
Tags: 0.9.2.0+git201202091925.c721477-0ubuntu1
* upstream snapshot 2012-02-09 19:25:59 (GMT)
  + c721477d11d4fe144111d6d2eec8f93f2e9186c9
* debian/patches/avoid-periodic-disk-wakeups.patch: refreshed.
* debian/patches/nl3-default-ip6-route.patch: refreshed.
* debian/libnm-glib4.symbols: add symbols:
  + nm_active_connection_get_master@Base
  + nm_client_new_async@Base
  + nm_client_new_finish@Base
  + nm_remote_settings_new_async@Base
  + nm_remote_settings_new_finish@Base
  + nm_device_get_state_reason@Base
* debian/libnm-util2.symbols: add symbols:
  + nm_setting_802_1x_get_pac_file@Base
  + nm_setting_infiniband_get_transport_mode@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: */
 
3
/*
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 * (C) Copyright 2012 Red Hat, Inc.
 
19
 */
 
20
 
 
21
/*
 
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.
 
25
 *
 
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
 
29
 *
 
30
 * You don't need to have NetworkManager devel package installed.
 
31
 */
 
32
 
 
33
#include <iostream>
 
34
#include <QObject>
 
35
#include <QCoreApplication>
 
36
#include <QtDBus/QDBusServiceWatcher>
 
37
#include <QtDBus/QDBusConnection>
 
38
#include <QtCore/QDebug>
 
39
 
 
40
const QString NM_DBUS_SERVICE = "org.freedesktop.NetworkManager";
 
41
 
 
42
// Define a class with slots
 
43
class NMWatcher: public QObject {
 
44
    Q_OBJECT;
 
45
 
 
46
    public slots:
 
47
        void serviceRegistered(const QString& name);
 
48
        void serviceUnregistered(const QString& name);
 
49
};
 
50
 
 
51
 
 
52
void NMWatcher::serviceRegistered(const QString& name)
 
53
{
 
54
    std::cout << "Name '" << name.toStdString() << "' registered"
 
55
              << " => NM is running" << std::endl;
 
56
}
 
57
 
 
58
void NMWatcher::serviceUnregistered(const QString& name)
 
59
{
 
60
    std::cout << "Name '" << name.toStdString() << "' unregistered"
 
61
              << " => NM is not running" << std::endl;
 
62
}
 
63
 
 
64
int main(int argc, char *argv[])
 
65
{
 
66
    QCoreApplication app (argc, argv);
 
67
 
 
68
    qDebug() << "Monitor 'org.freedesktop.NetworkManager' D-Bus name";
 
69
    qDebug() << "===================================================";
 
70
 
 
71
    NMWatcher nm_watcher;
 
72
 
 
73
    // Watch all changes of D-Bus NM_DBUS_SERVICE name
 
74
    QDBusServiceWatcher *watcher = new QDBusServiceWatcher(NM_DBUS_SERVICE,
 
75
                                                           QDBusConnection::systemBus());
 
76
 
 
77
    QObject::connect(watcher, SIGNAL(serviceRegistered(const QString&)),
 
78
                     &nm_watcher, SLOT(serviceRegistered(const QString&)));
 
79
 
 
80
    QObject::connect(watcher, SIGNAL(serviceUnregistered(const QString&)),
 
81
                     &nm_watcher, SLOT(serviceUnregistered(const QString&)));
 
82
 
 
83
    app.exec();
 
84
 
 
85
    delete watcher;
 
86
    return 0;
 
87
}
 
88
 
 
89
#include "monitor-nm-running.moc"